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