1load("//rules:diff_test.bzl", "diff_test") 2load("//rules:run_binary.bzl", "run_binary") 3load("//rules:write_file.bzl", "write_file") 4 5package( 6 default_applicable_licenses = ["//:license"], 7 default_testonly = 1, 8 default_visibility = ["//visibility:private"], 9) 10 11diff_test( 12 name = "run_script_test", 13 file1 = ":run_script.out", 14 file2 = ":run_script_expected", 15) 16 17# Generate this file with write_file instead of checking it in to the source 18# tree. This ensures line endings are consistent across "run_script.expected" 19# and "run_script.out". 20write_file( 21 name = "run_script_expected", 22 out = "run_script.expected", 23 content = [ 24 "arg1=(foo)", 25 "arg2=(bar)", 26 "ENV_LOCATION=(a tests/run_binary/BUILD)", 27 "ENV_LOCATIONS=(b\\ tests/run_binary/BUILD tests/run_binary/printargs.cc)", 28 "ENV_EXECPATH=(a tests/run_binary/BUILD)", 29 "ENV_EXECPATHS=(b\\ tests/run_binary/BUILD tests/run_binary/printargs.cc)", 30 "ENV_COMPLEX=(xx/yy \\\"zz)", 31 "ENV_PATH_BASH=($PATH)", 32 "ENV_PATH_CMD=(%PATH%)", 33 # Can't prevent "echo" from adding a newline on Windows, so let's add 34 # one to the expected output too. 35 "", 36 ], 37) 38 39run_binary( 40 name = "run_script", 41 srcs = [ 42 "BUILD", 43 ":dummy_srcs", 44 ], 45 outs = ["run_script.out"], 46 # Not testing any complex arguments here, because Windows .bat file argument 47 # escaping is different from most MSVC-built Windows binaries. We test 48 # argument escaping in "run_bin". 49 args = [ 50 "foo", 51 "bar", 52 ], 53 # Test complex environment variables. They are location-expanded but not 54 # Bash-tokenized, and should appear the same for Windows .bat files and Bash 55 # .sh scripts. 56 env = { 57 "ENV_COMPLEX": "xx/yy \\\"zz", 58 "ENV_EXECPATH": "a $(execpath BUILD)", 59 "ENV_EXECPATHS": "b\\ $(execpaths :dummy_srcs)", 60 # Testing $(location) expansion only on source files so the result is 61 # predictable. The path of generated files depends on the target 62 # platform. 63 "ENV_LOCATION": "a $(location BUILD)", 64 "ENV_LOCATIONS": "b\\ $(locations :dummy_srcs)", 65 "ENV_PATH_BASH": "$PATH", 66 "ENV_PATH_CMD": "%PATH%", 67 "OUT": "$(location run_script.out)", 68 }, 69 tool = ":script", 70) 71 72write_file( 73 name = "script", 74 # On Windows we need the ".bat" extension. 75 # On other platforms the extension doesn't matter. 76 # Therefore we can use ".bat" on every platform. 77 out = "script.bat", 78 content = select({ 79 "@bazel_tools//src/conditions:host_windows": [ 80 "@echo>%OUT% arg1=(%1)", 81 "@echo>>%OUT% arg2=(%2)", 82 "@echo>>%OUT% ENV_LOCATION=(%ENV_LOCATION%)", 83 "@echo>>%OUT% ENV_LOCATIONS=(%ENV_LOCATIONS%)", 84 "@echo>>%OUT% ENV_EXECPATH=(%ENV_EXECPATH%)", 85 "@echo>>%OUT% ENV_EXECPATHS=(%ENV_EXECPATHS%)", 86 "@echo>>%OUT% ENV_COMPLEX=(%ENV_COMPLEX%)", 87 "@echo>>%OUT% ENV_PATH_BASH=(%ENV_PATH_BASH%)", 88 "@echo>>%OUT% ENV_PATH_CMD=(%ENV_PATH_CMD%)", 89 ], 90 "//conditions:default": [ 91 "#!/usr/bin/env bash", 92 "echo > \"$OUT\" \"arg1=($1)\"", 93 "echo >> \"$OUT\" \"arg2=($2)\"", 94 "echo >> \"$OUT\" \"ENV_LOCATION=($ENV_LOCATION)\"", 95 "echo >> \"$OUT\" \"ENV_LOCATIONS=($ENV_LOCATIONS)\"", 96 "echo >> \"$OUT\" \"ENV_EXECPATH=($ENV_EXECPATH)\"", 97 "echo >> \"$OUT\" \"ENV_EXECPATHS=($ENV_EXECPATHS)\"", 98 "echo >> \"$OUT\" \"ENV_COMPLEX=($ENV_COMPLEX)\"", 99 "echo >> \"$OUT\" \"ENV_PATH_BASH=($ENV_PATH_BASH)\"", 100 "echo >> \"$OUT\" \"ENV_PATH_CMD=($ENV_PATH_CMD)\"", 101 ], 102 }), 103 is_executable = True, 104) 105 106diff_test( 107 name = "run_bin_test", 108 file1 = ":run_bin.out", 109 file2 = ":run_bin_expected", 110) 111 112# Generate this file with write_file instead of checking it in to the source 113# tree. This ensures line endings are consistent across "run_bin.expected" 114# and "run_bin.out". 115write_file( 116 name = "run_bin_expected", 117 out = "run_bin.expected", 118 content = [ 119 "arg1=(a b)", 120 "arg2=(\"c d\")", 121 "arg3=(e\\ f)", 122 "arg4=(xx/yy\\ \\\"zz)", 123 "arg5=(tests/run_binary/BUILD)", 124 "arg6=(tests/run_binary/BUILD tests/run_binary/printargs.cc)", 125 "arg7=('tests/run_binary/BUILD $tests/run_binary/BUILD')", 126 "arg8=(tests/run_binary/BUILD)", 127 "arg9=(tests/run_binary/BUILD tests/run_binary/printargs.cc)", 128 "arg10=('tests/run_binary/BUILD $tests/run_binary/BUILD')", 129 "arg11=($PATH)", 130 "arg12=($$PATH)", 131 "arg13=(${PATH})", 132 "arg14=($(echo hello))", 133 # Add trailing newline, as printed by printargs. 134 "", 135 ], 136) 137 138run_binary( 139 name = "run_bin", 140 srcs = [ 141 "BUILD", 142 ":dummy_srcs", 143 ], 144 outs = ["run_bin.out"], 145 # Test complex arguments here. They are location-expanded but not 146 # Bash-tokenized, and should appear the same on every platform. 147 args = [ 148 "a b", 149 "\"c d\"", 150 "e\\ f", 151 "xx/yy\\ \\\"zz", 152 # Testing $(execpath) expansion only on source files so the result is 153 # predictable. The path of generated files depends on the target 154 # platform. 155 "$(execpath BUILD)", 156 "$(execpaths :dummy_srcs)", 157 "'$(execpath BUILD) $$(execpath BUILD)'", 158 # Test the legacy 'location' expansions 159 "$(location BUILD)", 160 "$(locations :dummy_srcs)", 161 "'$(location BUILD) $$(location BUILD)'", 162 "$PATH", 163 "$$PATH", 164 "${PATH}", 165 "$(echo hello)", 166 ], 167 # Not testing any complex envvars here, because we already did in 168 # "run_script". 169 env = {"OUT": "$(location run_bin.out)"}, 170 tool = ":printargs", 171) 172 173filegroup( 174 name = "dummy_srcs", 175 srcs = [ 176 "BUILD", 177 "printargs.cc", 178 ], 179) 180 181cc_binary( 182 name = "printargs", 183 srcs = ["printargs.cc"], 184) 185