1# This package aids testing the 'write_file' rule. 2# 3# The package contains 4 write_file rules: 4# - 'write_empty_text' and 'write_empty_bin' write an empty text and an empty 5# executable file respectively 6# - 'write_nonempty_text' and 'write_nonempty_bin' write a non-empty text and 7# a non-empty executable file (a shell script) respectively 8# 9# The 'bin_empty' and 'bin_nonempty' rules are sh_binary rules. They use 10# the 'write_empty_bin' and 'write_nonempty_bin' rules respectively. The 11# sh_binary rule requires its source to be executable, so building these two 12# rules successfully means that 'write_file' managed to make its output 13# executable. 14# 15# The 'run_executables' genrule runs the 'bin_empty' and 'bin_nonempty' 16# binaries, partly to ensure they can be run, and partly so we can observe their 17# output and assert the contents in the 'write_file_tests' test. 18# 19# The 'file_deps' filegroup depends on 'write_empty_text'. The filegroup rule 20# uses the DefaultInfo.files field from its dependencies. When we data-depend on 21# the filegroup from 'write_file_tests', we transitively data-depend on the 22# DefaultInfo.files of the 'write_empty_text' rule. 23# 24# The 'write_file_tests' test is the actual integration test. It data-depends 25# on: 26# - the 'run_executables' rule, to get the outputs of 'bin_empty' and 27# 'bin_nonempty' 28# - the 'file_deps' rule, and by nature of using a filegroup, we get the files 29# from the DefaultInfo.files of the 'write_file' rule, and thereby assert that 30# that field contains the output file of the rule 31# - the 'write_nonempty_text' rule, and thereby on the DefaultInfo.runfiles 32# field of it, so we assert that that field contains the output file of the 33# rule 34 35load("//rules:diff_test.bzl", "diff_test") 36load("//rules:write_file.bzl", "write_file") 37 38package( 39 default_applicable_licenses = ["//:license"], 40 default_testonly = 1, 41) 42 43licenses(["notice"]) 44 45sh_test( 46 name = "write_file_tests", 47 srcs = ["write_file_tests.sh"], 48 data = [ 49 ":run_executables", 50 # Use DefaultInfo.files from 'write_empty_text' (via 'file_deps'). 51 ":file_deps", 52 # Use DefaultInfo.runfiles from 'write_nonempty_text'. 53 ":write_nonempty_text", 54 "//tests:unittest.bash", 55 ], 56 deps = ["@bazel_tools//tools/bash/runfiles"], 57) 58 59filegroup( 60 name = "file_deps", 61 # Use DefaultInfo.files from 'write_empty_text'. 62 srcs = [":write_empty_text"], 63) 64 65# If 'run_executables' is built, then 'bin_nonempty' and 'bin_empty' are 66# executable, asserting that write_file makes the output executable. 67genrule( 68 name = "run_executables", 69 outs = [ 70 "empty-bin-out.txt", 71 "nonempty-bin-out.txt", 72 ], 73 cmd = ("$(location :bin_empty) > $(location empty-bin-out.txt) && " + 74 "$(location :bin_nonempty) > $(location nonempty-bin-out.txt)"), 75 output_to_bindir = 1, 76 tools = [ 77 ":bin_empty", 78 ":bin_nonempty", 79 ], 80) 81 82# If 'bin_empty' is built, then 'write_empty_bin' made its output executable. 83sh_binary( 84 name = "bin_empty", 85 srcs = [":write_empty_bin"], 86) 87 88# If 'bin_nonempty' is built, then 'write_nonempty_bin' made its output 89# executable. 90sh_binary( 91 name = "bin_nonempty", 92 srcs = [":write_nonempty_bin"], 93) 94 95write_file( 96 name = "write_empty_text", 97 out = "out/empty.txt", 98) 99 100write_file( 101 name = "write_nonempty_text", 102 out = "out/nonempty.txt", 103 content = [ 104 "aaa", 105 "bbb", 106 ], 107) 108 109write_file( 110 name = "write_empty_bin", 111 out = "out/empty.sh", 112 is_executable = True, 113) 114 115write_file( 116 name = "write_nonempty_bin", 117 out = "out/nonempty.sh", 118 content = [ 119 "#!/usr/bin/env bash", 120 "echo potato", 121 ], 122 is_executable = True, 123) 124 125write_file( 126 name = "newline_unix_actual", 127 out = "out/newline_unix_actual.txt", 128 content = [ 129 "ab", 130 "cd", 131 "ef", 132 ], 133 newline = "unix", 134) 135 136write_file( 137 name = "newline_unix_exp", 138 out = "out/newline_unix_exp.txt", 139 content = ["ab\ncd\nef"], 140) 141 142diff_test( 143 name = "unix_line_ending_test", 144 file1 = ":newline_unix_actual", 145 file2 = ":newline_unix_exp", 146) 147 148write_file( 149 name = "newline_win_actual", 150 out = "out/newline_win_actual.txt", 151 content = [ 152 "ab", 153 "cd", 154 "ef", 155 ], 156 newline = "windows", 157) 158 159write_file( 160 name = "newline_win_exp", 161 out = "out/newline_win_exp.txt", 162 content = ["ab\r\ncd\r\nef"], 163) 164 165diff_test( 166 name = "win_line_ending_test", 167 file1 = ":newline_win_actual", 168 file2 = ":newline_win_exp", 169) 170