aboutsummaryrefslogtreecommitdiff
path: root/tests/write_file/BUILD
blob: 321bcc08d38fbf3f9d5f525e3028e1cbadea9fd8 (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
# This package aids testing the 'write_file' rule.
#
# The package contains 4 write_file rules:
# - 'write_empty_text' and 'write_empty_bin' write an empty text and an empty
#   executable file respectively
# - 'write_nonempty_text' and 'write_nonempty_bin' write a non-empty text and
#   a non-empty executable file (a shell script) respectively
#
# The 'bin_empty' and 'bin_nonempty' rules are sh_binary rules. They use
# the 'write_empty_bin' and 'write_nonempty_bin' rules respectively. The
# sh_binary rule requires its source to be executable, so building these two
# rules successfully means that 'write_file' managed to make its output
# executable.
#
# The 'run_executables' genrule runs the 'bin_empty' and 'bin_nonempty'
# binaries, partly to ensure they can be run, and partly so we can observe their
# output and assert the contents in the 'write_file_tests' test.
#
# The 'file_deps' filegroup depends on 'write_empty_text'. The filegroup rule
# uses the DefaultInfo.files field from its dependencies. When we data-depend on
# the filegroup from 'write_file_tests', we transitively data-depend on the
# DefaultInfo.files of the 'write_empty_text' rule.
#
# The 'write_file_tests' test is the actual integration test. It data-depends
# on:
# - the 'run_executables' rule, to get the outputs of 'bin_empty' and
#   'bin_nonempty'
# - the 'file_deps' rule, and by nature of using a filegroup, we get the files
#   from the DefaultInfo.files of the 'write_file' rule, and thereby assert that
#   that field contains the output file of the rule
# - the 'write_nonempty_text' rule, and thereby on the DefaultInfo.runfiles
#   field of it, so we assert that that field contains the output file of the
#   rule

load("//rules:write_file.bzl", "write_file")

licenses(["notice"])

package(default_testonly = 1)

sh_test(
    name = "write_file_tests",
    srcs = ["write_file_tests.sh"],
    data = [
        ":run_executables",
        # Use DefaultInfo.files from 'write_empty_text' (via 'file_deps').
        ":file_deps",
        # Use DefaultInfo.runfiles from 'write_nonempty_text'.
        ":write_nonempty_text",
        "//tests:unittest.bash",
    ],
    deps = ["@bazel_tools//tools/bash/runfiles"],
)

filegroup(
    name = "file_deps",
    # Use DefaultInfo.files from 'write_empty_text'.
    srcs = [":write_empty_text"],
)

# If 'run_executables' is built, then 'bin_nonempty' and 'bin_empty' are
# executable, asserting that write_file makes the output executable.
genrule(
    name = "run_executables",
    outs = [
        "empty-bin-out.txt",
        "nonempty-bin-out.txt",
    ],
    cmd = ("$(location :bin_empty) > $(location empty-bin-out.txt) && " +
           "$(location :bin_nonempty) > $(location nonempty-bin-out.txt)"),
    output_to_bindir = 1,
    tools = [
        ":bin_empty",
        ":bin_nonempty",
    ],
)

# If 'bin_empty' is built, then 'write_empty_bin' made its output executable.
sh_binary(
    name = "bin_empty",
    srcs = [":write_empty_bin"],
)

# If 'bin_nonempty' is built, then 'write_nonempty_bin' made its output
# executable.
sh_binary(
    name = "bin_nonempty",
    srcs = [":write_nonempty_bin"],
)

write_file(
    name = "write_empty_text",
    out = "out/empty.txt",
)

write_file(
    name = "write_nonempty_text",
    out = "out/nonempty.txt",
    content = [
        "aaa",
        "bbb",
    ],
)

write_file(
    name = "write_empty_bin",
    out = "out/empty.sh",
    is_executable = True,
)

write_file(
    name = "write_nonempty_bin",
    out = "out/nonempty.sh",
    content = [
        "#!/bin/bash",
        "echo potato",
    ],
    is_executable = True,
)