aboutsummaryrefslogtreecommitdiff
path: root/tests/run_binary/BUILD
blob: 259c84a3fc113f329080b46d2829d8dab620ebf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
load("@rules_cc//cc:defs.bzl", "cc_binary")
load("//rules:diff_test.bzl", "diff_test")
load("//rules:run_binary.bzl", "run_binary")
load("//rules:write_file.bzl", "write_file")

package(
    default_testonly = 1,
    default_visibility = ["//visibility:private"],
)

diff_test(
    name = "run_script_test",
    file1 = ":run_script.out",
    file2 = ":run_script_expected",
)

# Generate this file with write_file instead of checking it in to the source
# tree. This ensures line endings are consistent across "run_script.expected"
# and "run_script.out".
write_file(
    name = "run_script_expected",
    out = "run_script.expected",
    content = [
        "arg1=(foo)",
        "arg2=(bar)",
        "ENV_LOCATION=(a tests/run_binary/BUILD)",
        "ENV_LOCATIONS=(b\\ tests/run_binary/BUILD tests/run_binary/printargs.cc)",
        "ENV_COMPLEX=(xx/yy \\\"zz)",
        "ENV_PATH_BASH=($PATH)",
        "ENV_PATH_CMD=(%PATH%)",
        # Can't prevent "echo" from adding a newline on Windows, so let's add
        # one to the expected output too.
        "",
    ],
)

run_binary(
    name = "run_script",
    srcs = [
        "BUILD",
        ":dummy_srcs",
    ],
    outs = ["run_script.out"],
    # Not testing any complex arguments here, because Windows .bat file argument
    # escaping is different from most MSVC-built Windows binaries. We test
    # argument escaping in "run_bin".
    args = [
        "foo",
        "bar",
    ],
    # Test complex environment variables. They are location-expanded but not
    # Bash-tokenized, and should appear the same for Windows .bat files and Bash
    # .sh scripts.
    env = {
        # Testing $(location) expansion only on source files so the result is
        # predictable. The path of generated files depends on the target
        # platform.
        "ENV_LOCATION": "a $(location BUILD)",
        "ENV_LOCATIONS": "b\\ $(locations :dummy_srcs)",
        "ENV_COMPLEX": "xx/yy \\\"zz",
        "ENV_PATH_BASH": "$PATH",
        "ENV_PATH_CMD": "%PATH%",
        "OUT": "$(location run_script.out)",
    },
    tool = ":script",
)

write_file(
    name = "script",
    # On Windows we need the ".bat" extension.
    # On other platforms the extension doesn't matter.
    # Therefore we can use ".bat" on every platform.
    out = "script.bat",
    content = select({
        "@bazel_tools//src/conditions:host_windows": [
            "@echo>%OUT% arg1=(%1)",
            "@echo>>%OUT% arg2=(%2)",
            "@echo>>%OUT% ENV_LOCATION=(%ENV_LOCATION%)",
            "@echo>>%OUT% ENV_LOCATIONS=(%ENV_LOCATIONS%)",
            "@echo>>%OUT% ENV_COMPLEX=(%ENV_COMPLEX%)",
            "@echo>>%OUT% ENV_PATH_BASH=(%ENV_PATH_BASH%)",
            "@echo>>%OUT% ENV_PATH_CMD=(%ENV_PATH_CMD%)",
        ],
        "//conditions:default": [
            "#!/usr/bin/env bash",
            "echo > \"$OUT\" \"arg1=($1)\"",
            "echo >> \"$OUT\" \"arg2=($2)\"",
            "echo >> \"$OUT\" \"ENV_LOCATION=($ENV_LOCATION)\"",
            "echo >> \"$OUT\" \"ENV_LOCATIONS=($ENV_LOCATIONS)\"",
            "echo >> \"$OUT\" \"ENV_COMPLEX=($ENV_COMPLEX)\"",
            "echo >> \"$OUT\" \"ENV_PATH_BASH=($ENV_PATH_BASH)\"",
            "echo >> \"$OUT\" \"ENV_PATH_CMD=($ENV_PATH_CMD)\"",
        ],
    }),
    is_executable = True,
)

diff_test(
    name = "run_bin_test",
    file1 = ":run_bin.out",
    file2 = ":run_bin_expected",
)

# Generate this file with write_file instead of checking it in to the source
# tree. This ensures line endings are consistent across "run_bin.expected"
# and "run_bin.out".
write_file(
    name = "run_bin_expected",
    out = "run_bin.expected",
    content = [
        "arg1=(a b)",
        "arg2=(\"c d\")",
        "arg3=(e\\ f)",
        "arg4=(xx/yy\\ \\\"zz)",
        "arg5=(tests/run_binary/BUILD)",
        "arg6=(tests/run_binary/BUILD tests/run_binary/printargs.cc)",
        "arg7=('tests/run_binary/BUILD $tests/run_binary/BUILD')",
        "arg8=($PATH)",
        "arg9=($$PATH)",
        "arg10=(${PATH})",
        # Add trailing newline, as printed by printargs.
        "",
    ],
)

run_binary(
    name = "run_bin",
    srcs = [
        "BUILD",
        ":dummy_srcs",
    ],
    outs = ["run_bin.out"],
    # Test complex arguments here. They are location-expanded but not
    # Bash-tokenized, and should appear the same on every platform.
    args = [
        "a b",
        "\"c d\"",
        "e\\ f",
        "xx/yy\\ \\\"zz",
        # Testing $(location) expansion only on source files so the result is
        # predictable. The path of generated files depends on the target
        # platform.
        "$(location BUILD)",
        "$(locations :dummy_srcs)",
        "'$(location BUILD) $$(location BUILD)'",
        "$PATH",
        "$$PATH",
        "${PATH}",
    ],
    # Not testing any complex envvars here, because we already did in
    # "run_script".
    env = {"OUT": "$(location run_bin.out)"},
    tool = ":printargs",
)

filegroup(
    name = "dummy_srcs",
    srcs = [
        "BUILD",
        "printargs.cc",
    ],
)

cc_binary(
    name = "printargs",
    srcs = ["printargs.cc"],
)