xref: /aosp_15_r20/external/bazel-skylib/tests/write_file/BUILD (revision bcb5dc7965af6ee42bf2f21341a2ec00233a8c8a)
1*bcb5dc79SHONG Yifan# This package aids testing the 'write_file' rule.
2*bcb5dc79SHONG Yifan#
3*bcb5dc79SHONG Yifan# The package contains 4 write_file rules:
4*bcb5dc79SHONG Yifan# - 'write_empty_text' and 'write_empty_bin' write an empty text and an empty
5*bcb5dc79SHONG Yifan#   executable file respectively
6*bcb5dc79SHONG Yifan# - 'write_nonempty_text' and 'write_nonempty_bin' write a non-empty text and
7*bcb5dc79SHONG Yifan#   a non-empty executable file (a shell script) respectively
8*bcb5dc79SHONG Yifan#
9*bcb5dc79SHONG Yifan# The 'bin_empty' and 'bin_nonempty' rules are sh_binary rules. They use
10*bcb5dc79SHONG Yifan# the 'write_empty_bin' and 'write_nonempty_bin' rules respectively. The
11*bcb5dc79SHONG Yifan# sh_binary rule requires its source to be executable, so building these two
12*bcb5dc79SHONG Yifan# rules successfully means that 'write_file' managed to make its output
13*bcb5dc79SHONG Yifan# executable.
14*bcb5dc79SHONG Yifan#
15*bcb5dc79SHONG Yifan# The 'run_executables' genrule runs the 'bin_empty' and 'bin_nonempty'
16*bcb5dc79SHONG Yifan# binaries, partly to ensure they can be run, and partly so we can observe their
17*bcb5dc79SHONG Yifan# output and assert the contents in the 'write_file_tests' test.
18*bcb5dc79SHONG Yifan#
19*bcb5dc79SHONG Yifan# The 'file_deps' filegroup depends on 'write_empty_text'. The filegroup rule
20*bcb5dc79SHONG Yifan# uses the DefaultInfo.files field from its dependencies. When we data-depend on
21*bcb5dc79SHONG Yifan# the filegroup from 'write_file_tests', we transitively data-depend on the
22*bcb5dc79SHONG Yifan# DefaultInfo.files of the 'write_empty_text' rule.
23*bcb5dc79SHONG Yifan#
24*bcb5dc79SHONG Yifan# The 'write_file_tests' test is the actual integration test. It data-depends
25*bcb5dc79SHONG Yifan# on:
26*bcb5dc79SHONG Yifan# - the 'run_executables' rule, to get the outputs of 'bin_empty' and
27*bcb5dc79SHONG Yifan#   'bin_nonempty'
28*bcb5dc79SHONG Yifan# - the 'file_deps' rule, and by nature of using a filegroup, we get the files
29*bcb5dc79SHONG Yifan#   from the DefaultInfo.files of the 'write_file' rule, and thereby assert that
30*bcb5dc79SHONG Yifan#   that field contains the output file of the rule
31*bcb5dc79SHONG Yifan# - the 'write_nonempty_text' rule, and thereby on the DefaultInfo.runfiles
32*bcb5dc79SHONG Yifan#   field of it, so we assert that that field contains the output file of the
33*bcb5dc79SHONG Yifan#   rule
34*bcb5dc79SHONG Yifan
35*bcb5dc79SHONG Yifanload("//rules:diff_test.bzl", "diff_test")
36*bcb5dc79SHONG Yifanload("//rules:write_file.bzl", "write_file")
37*bcb5dc79SHONG Yifan
38*bcb5dc79SHONG Yifanpackage(
39*bcb5dc79SHONG Yifan    default_applicable_licenses = ["//:license"],
40*bcb5dc79SHONG Yifan    default_testonly = 1,
41*bcb5dc79SHONG Yifan)
42*bcb5dc79SHONG Yifan
43*bcb5dc79SHONG Yifanlicenses(["notice"])
44*bcb5dc79SHONG Yifan
45*bcb5dc79SHONG Yifansh_test(
46*bcb5dc79SHONG Yifan    name = "write_file_tests",
47*bcb5dc79SHONG Yifan    srcs = ["write_file_tests.sh"],
48*bcb5dc79SHONG Yifan    data = [
49*bcb5dc79SHONG Yifan        ":run_executables",
50*bcb5dc79SHONG Yifan        # Use DefaultInfo.files from 'write_empty_text' (via 'file_deps').
51*bcb5dc79SHONG Yifan        ":file_deps",
52*bcb5dc79SHONG Yifan        # Use DefaultInfo.runfiles from 'write_nonempty_text'.
53*bcb5dc79SHONG Yifan        ":write_nonempty_text",
54*bcb5dc79SHONG Yifan        "//tests:unittest.bash",
55*bcb5dc79SHONG Yifan    ],
56*bcb5dc79SHONG Yifan    deps = ["@bazel_tools//tools/bash/runfiles"],
57*bcb5dc79SHONG Yifan)
58*bcb5dc79SHONG Yifan
59*bcb5dc79SHONG Yifanfilegroup(
60*bcb5dc79SHONG Yifan    name = "file_deps",
61*bcb5dc79SHONG Yifan    # Use DefaultInfo.files from 'write_empty_text'.
62*bcb5dc79SHONG Yifan    srcs = [":write_empty_text"],
63*bcb5dc79SHONG Yifan)
64*bcb5dc79SHONG Yifan
65*bcb5dc79SHONG Yifan# If 'run_executables' is built, then 'bin_nonempty' and 'bin_empty' are
66*bcb5dc79SHONG Yifan# executable, asserting that write_file makes the output executable.
67*bcb5dc79SHONG Yifangenrule(
68*bcb5dc79SHONG Yifan    name = "run_executables",
69*bcb5dc79SHONG Yifan    outs = [
70*bcb5dc79SHONG Yifan        "empty-bin-out.txt",
71*bcb5dc79SHONG Yifan        "nonempty-bin-out.txt",
72*bcb5dc79SHONG Yifan    ],
73*bcb5dc79SHONG Yifan    cmd = ("$(location :bin_empty) > $(location empty-bin-out.txt) && " +
74*bcb5dc79SHONG Yifan           "$(location :bin_nonempty) > $(location nonempty-bin-out.txt)"),
75*bcb5dc79SHONG Yifan    output_to_bindir = 1,
76*bcb5dc79SHONG Yifan    tools = [
77*bcb5dc79SHONG Yifan        ":bin_empty",
78*bcb5dc79SHONG Yifan        ":bin_nonempty",
79*bcb5dc79SHONG Yifan    ],
80*bcb5dc79SHONG Yifan)
81*bcb5dc79SHONG Yifan
82*bcb5dc79SHONG Yifan# If 'bin_empty' is built, then 'write_empty_bin' made its output executable.
83*bcb5dc79SHONG Yifansh_binary(
84*bcb5dc79SHONG Yifan    name = "bin_empty",
85*bcb5dc79SHONG Yifan    srcs = [":write_empty_bin"],
86*bcb5dc79SHONG Yifan)
87*bcb5dc79SHONG Yifan
88*bcb5dc79SHONG Yifan# If 'bin_nonempty' is built, then 'write_nonempty_bin' made its output
89*bcb5dc79SHONG Yifan# executable.
90*bcb5dc79SHONG Yifansh_binary(
91*bcb5dc79SHONG Yifan    name = "bin_nonempty",
92*bcb5dc79SHONG Yifan    srcs = [":write_nonempty_bin"],
93*bcb5dc79SHONG Yifan)
94*bcb5dc79SHONG Yifan
95*bcb5dc79SHONG Yifanwrite_file(
96*bcb5dc79SHONG Yifan    name = "write_empty_text",
97*bcb5dc79SHONG Yifan    out = "out/empty.txt",
98*bcb5dc79SHONG Yifan)
99*bcb5dc79SHONG Yifan
100*bcb5dc79SHONG Yifanwrite_file(
101*bcb5dc79SHONG Yifan    name = "write_nonempty_text",
102*bcb5dc79SHONG Yifan    out = "out/nonempty.txt",
103*bcb5dc79SHONG Yifan    content = [
104*bcb5dc79SHONG Yifan        "aaa",
105*bcb5dc79SHONG Yifan        "bbb",
106*bcb5dc79SHONG Yifan    ],
107*bcb5dc79SHONG Yifan)
108*bcb5dc79SHONG Yifan
109*bcb5dc79SHONG Yifanwrite_file(
110*bcb5dc79SHONG Yifan    name = "write_empty_bin",
111*bcb5dc79SHONG Yifan    out = "out/empty.sh",
112*bcb5dc79SHONG Yifan    is_executable = True,
113*bcb5dc79SHONG Yifan)
114*bcb5dc79SHONG Yifan
115*bcb5dc79SHONG Yifanwrite_file(
116*bcb5dc79SHONG Yifan    name = "write_nonempty_bin",
117*bcb5dc79SHONG Yifan    out = "out/nonempty.sh",
118*bcb5dc79SHONG Yifan    content = [
119*bcb5dc79SHONG Yifan        "#!/usr/bin/env bash",
120*bcb5dc79SHONG Yifan        "echo potato",
121*bcb5dc79SHONG Yifan    ],
122*bcb5dc79SHONG Yifan    is_executable = True,
123*bcb5dc79SHONG Yifan)
124*bcb5dc79SHONG Yifan
125*bcb5dc79SHONG Yifanwrite_file(
126*bcb5dc79SHONG Yifan    name = "newline_unix_actual",
127*bcb5dc79SHONG Yifan    out = "out/newline_unix_actual.txt",
128*bcb5dc79SHONG Yifan    content = [
129*bcb5dc79SHONG Yifan        "ab",
130*bcb5dc79SHONG Yifan        "cd",
131*bcb5dc79SHONG Yifan        "ef",
132*bcb5dc79SHONG Yifan    ],
133*bcb5dc79SHONG Yifan    newline = "unix",
134*bcb5dc79SHONG Yifan)
135*bcb5dc79SHONG Yifan
136*bcb5dc79SHONG Yifanwrite_file(
137*bcb5dc79SHONG Yifan    name = "newline_unix_exp",
138*bcb5dc79SHONG Yifan    out = "out/newline_unix_exp.txt",
139*bcb5dc79SHONG Yifan    content = ["ab\ncd\nef"],
140*bcb5dc79SHONG Yifan)
141*bcb5dc79SHONG Yifan
142*bcb5dc79SHONG Yifandiff_test(
143*bcb5dc79SHONG Yifan    name = "unix_line_ending_test",
144*bcb5dc79SHONG Yifan    file1 = ":newline_unix_actual",
145*bcb5dc79SHONG Yifan    file2 = ":newline_unix_exp",
146*bcb5dc79SHONG Yifan)
147*bcb5dc79SHONG Yifan
148*bcb5dc79SHONG Yifanwrite_file(
149*bcb5dc79SHONG Yifan    name = "newline_win_actual",
150*bcb5dc79SHONG Yifan    out = "out/newline_win_actual.txt",
151*bcb5dc79SHONG Yifan    content = [
152*bcb5dc79SHONG Yifan        "ab",
153*bcb5dc79SHONG Yifan        "cd",
154*bcb5dc79SHONG Yifan        "ef",
155*bcb5dc79SHONG Yifan    ],
156*bcb5dc79SHONG Yifan    newline = "windows",
157*bcb5dc79SHONG Yifan)
158*bcb5dc79SHONG Yifan
159*bcb5dc79SHONG Yifanwrite_file(
160*bcb5dc79SHONG Yifan    name = "newline_win_exp",
161*bcb5dc79SHONG Yifan    out = "out/newline_win_exp.txt",
162*bcb5dc79SHONG Yifan    content = ["ab\r\ncd\r\nef"],
163*bcb5dc79SHONG Yifan)
164*bcb5dc79SHONG Yifan
165*bcb5dc79SHONG Yifandiff_test(
166*bcb5dc79SHONG Yifan    name = "win_line_ending_test",
167*bcb5dc79SHONG Yifan    file1 = ":newline_win_actual",
168*bcb5dc79SHONG Yifan    file2 = ":newline_win_exp",
169*bcb5dc79SHONG Yifan)
170