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