xref: /aosp_15_r20/external/bazel-skylib/tests/copy_file/BUILD (revision bcb5dc7965af6ee42bf2f21341a2ec00233a8c8a)
1# This package aids testing the 'copy_file' rule.
2#
3# The package contains 4 copy_file rules:
4# - 'copy_src' and 'copy_gen' copy a source file and a generated file
5#   respectively
6# - 'copy_xsrc' and 'copy_xgen' copy a source file and a generated file
7#   respectively (both are shell scripts), and mark their output as executable
8#
9# The generated file is the output of the 'gen' genrule.
10#
11# The 'bin_src' and 'bin_gen' rules are sh_binary rules. They use the
12# 'copy_xsrc' and 'copy_xgen' rules respectively. The sh_binary rule requires
13# its source to be executable, so building these two rules successfully means
14# that 'copy_file' managed to make its output executable.
15#
16# The 'run_executables' genrule runs the 'bin_src' and 'bin_gen' binaries,
17# partly to ensure they can be run, and partly so we can observe their output
18# and assert the contents in the 'copy_file_tests' test.
19#
20# The 'file_deps' filegroup depends on 'copy_src'. The filegroup rule uses the
21# DefaultInfo.files field from its dependencies. When we data-depend on the
22# filegroup from 'copy_file_tests', we transitively data-depend on the
23# DefaultInfo.files of the 'copy_src' rule.
24#
25# The 'copy_file_tests' test is the actual integration test. It data-depends
26# on:
27# - the 'run_executables' rule, to get the outputs of 'bin_src' and 'bin_gen'
28# - the 'file_deps' rule, and by nature of using a filegroup, we get the files
29#   from the DefaultInfo.files of the 'copy_file' rule, and thereby assert that
30#   that field contains the output file of the rule
31# - the 'copy_nonempty_text' rule, and thereby on the DefaultInfo.runfiles field
32#   of it, so we assert that that field contains the output file of the rule
33
34load("//rules:copy_file.bzl", "copy_file")
35
36package(
37    default_applicable_licenses = ["//:license"],
38    default_testonly = 1,
39)
40
41licenses(["notice"])
42
43sh_test(
44    name = "copy_file_tests",
45    srcs = ["copy_file_tests.sh"],
46    data = [
47        ":run_executables",
48        # Use DefaultInfo.files from 'copy_src' (via 'file_deps').
49        ":file_deps",
50        # Use DefaultInfo.runfiles from 'copy_gen'.
51        ":copy_gen",
52        ":copy_gen_symlink",
53        "//tests:unittest.bash",
54    ],
55    deps = ["@bazel_tools//tools/bash/runfiles"],
56)
57
58filegroup(
59    name = "file_deps",
60    # Use DefaultInfo.files from 'copy_src'.
61    srcs = [
62        ":copy_src",
63        ":copy_src_symlink",
64    ],
65)
66
67# If 'run_executables' is built, then 'bin_gen' and 'bin_src' are
68# executable, asserting that copy_file makes the output executable.
69genrule(
70    name = "run_executables",
71    outs = [
72        "xsrc-out-symlink.txt",
73        "xgen-out-symlink.txt",
74        "xsrc-out.txt",
75        "xgen-out.txt",
76    ],
77    cmd = " && ".join([
78        "$(location :bin_src_symlink) > $(location xsrc-out-symlink.txt)",
79        "$(location :bin_gen_symlink) > $(location xgen-out-symlink.txt)",
80        "$(location :bin_src) > $(location xsrc-out.txt)",
81        "$(location :bin_gen) > $(location xgen-out.txt)",
82    ]),
83    output_to_bindir = 1,
84    tools = [
85        ":bin_gen",
86        ":bin_gen_symlink",
87        ":bin_src",
88        ":bin_src_symlink",
89    ],
90)
91
92# If 'bin_src' is built, then 'copy_xsrc' made its output executable.
93sh_binary(
94    name = "bin_src",
95    srcs = [":copy_xsrc"],
96)
97
98# If 'bin_src' is built, then 'copy_xsrc' made its output executable.
99sh_binary(
100    name = "bin_src_symlink",
101    srcs = [":copy_xsrc_symlink"],
102)
103
104# If 'bin_gen' is built, then 'copy_xgen' made its output executable.
105sh_binary(
106    name = "bin_gen",
107    srcs = [":copy_xgen"],
108)
109
110# If 'bin_gen' is built, then 'copy_xgen' made its output executable.
111sh_binary(
112    name = "bin_gen_symlink",
113    srcs = [":copy_xgen_symlink"],
114)
115
116copy_file(
117    name = "copy_src",
118    src = "a.txt",
119    out = "out/a-out.txt",
120)
121
122copy_file(
123    name = "copy_src_symlink",
124    src = "a.txt",
125    out = "out/a-out-symlink.txt",
126    allow_symlink = True,
127)
128
129copy_file(
130    name = "copy_gen",
131    src = ":gen",
132    out = "out/gen-out.txt",
133    allow_symlink = True,
134)
135
136copy_file(
137    name = "copy_gen_symlink",
138    src = ":gen",
139    out = "out/gen-out-symlink.txt",
140)
141
142copy_file(
143    name = "copy_xsrc",
144    src = "a.txt",
145    out = "xout/a-out.sh",
146    is_executable = True,
147)
148
149copy_file(
150    name = "copy_xsrc_symlink",
151    src = "a_with_exec_bit.txt",
152    out = "xout/a-out-symlink.sh",
153    allow_symlink = True,
154    is_executable = True,
155)
156
157copy_file(
158    name = "copy_xgen",
159    src = ":gen",
160    out = "xout/gen-out.sh",
161    is_executable = True,
162)
163
164copy_file(
165    name = "copy_xgen_symlink",
166    src = ":gen",
167    out = "xout/gen-out-symlink.sh",
168    allow_symlink = True,
169    is_executable = True,
170)
171
172genrule(
173    name = "gen",
174    outs = ["b.txt"],
175    cmd = "echo -e '#!/usr/bin/env bash\necho potato' > $@",
176)
177