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