• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
38licenses(["notice"])
39
40package(default_testonly = 1)
41
42sh_test(
43    name = "write_file_tests",
44    srcs = ["write_file_tests.sh"],
45    data = [
46        ":run_executables",
47        # Use DefaultInfo.files from 'write_empty_text' (via 'file_deps').
48        ":file_deps",
49        # Use DefaultInfo.runfiles from 'write_nonempty_text'.
50        ":write_nonempty_text",
51        "//tests:unittest.bash",
52    ],
53    deps = ["@bazel_tools//tools/bash/runfiles"],
54)
55
56filegroup(
57    name = "file_deps",
58    # Use DefaultInfo.files from 'write_empty_text'.
59    srcs = [":write_empty_text"],
60)
61
62# If 'run_executables' is built, then 'bin_nonempty' and 'bin_empty' are
63# executable, asserting that write_file makes the output executable.
64genrule(
65    name = "run_executables",
66    outs = [
67        "empty-bin-out.txt",
68        "nonempty-bin-out.txt",
69    ],
70    cmd = ("$(location :bin_empty) > $(location empty-bin-out.txt) && " +
71           "$(location :bin_nonempty) > $(location nonempty-bin-out.txt)"),
72    output_to_bindir = 1,
73    tools = [
74        ":bin_empty",
75        ":bin_nonempty",
76    ],
77)
78
79# If 'bin_empty' is built, then 'write_empty_bin' made its output executable.
80sh_binary(
81    name = "bin_empty",
82    srcs = [":write_empty_bin"],
83)
84
85# If 'bin_nonempty' is built, then 'write_nonempty_bin' made its output
86# executable.
87sh_binary(
88    name = "bin_nonempty",
89    srcs = [":write_nonempty_bin"],
90)
91
92write_file(
93    name = "write_empty_text",
94    out = "out/empty.txt",
95)
96
97write_file(
98    name = "write_nonempty_text",
99    out = "out/nonempty.txt",
100    content = [
101        "aaa",
102        "bbb",
103    ],
104)
105
106write_file(
107    name = "write_empty_bin",
108    out = "out/empty.sh",
109    is_executable = True,
110)
111
112write_file(
113    name = "write_nonempty_bin",
114    out = "out/nonempty.sh",
115    content = [
116        "#!/bin/bash",
117        "echo potato",
118    ],
119    is_executable = True,
120)
121
122write_file(
123    name = "newline_unix_actual",
124    out = "out/newline_unix_actual.txt",
125    content = [
126        "ab",
127        "cd",
128        "ef",
129    ],
130    newline = "unix",
131)
132
133write_file(
134    name = "newline_unix_exp",
135    out = "out/newline_unix_exp.txt",
136    content = ["ab\ncd\nef"],
137)
138
139diff_test(
140    name = "unix_line_ending_test",
141    file1 = ":newline_unix_actual",
142    file2 = ":newline_unix_exp",
143)
144
145write_file(
146    name = "newline_win_actual",
147    out = "out/newline_win_actual.txt",
148    content = [
149        "ab",
150        "cd",
151        "ef",
152    ],
153    newline = "windows",
154)
155
156write_file(
157    name = "newline_win_exp",
158    out = "out/newline_win_exp.txt",
159    content = ["ab\r\ncd\r\nef"],
160)
161
162diff_test(
163    name = "win_line_ending_test",
164    file1 = ":newline_win_actual",
165    file2 = ":newline_win_exp",
166)
167