aboutsummaryrefslogtreecommitdiff
path: root/tests/write_file/BUILD
blob: 9ea3609a290a026313fc95d7dd74e9174fae1b35 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# 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:diff_test.bzl", "diff_test")
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,
)

write_file(
    name = "newline_unix_actual",
    out = "out/newline_unix_actual.txt",
    content = [
        "ab",
        "cd",
        "ef",
    ],
    newline = "unix",
)

write_file(
    name = "newline_unix_exp",
    out = "out/newline_unix_exp.txt",
    content = ["ab\ncd\nef"],
)

diff_test(
    name = "unix_line_ending_test",
    file1 = ":newline_unix_actual",
    file2 = ":newline_unix_exp",
)

write_file(
    name = "newline_win_actual",
    out = "out/newline_win_actual.txt",
    content = [
        "ab",
        "cd",
        "ef",
    ],
    newline = "windows",
)

write_file(
    name = "newline_win_exp",
    out = "out/newline_win_exp.txt",
    content = ["ab\r\ncd\r\nef"],
)

diff_test(
    name = "win_line_ending_test",
    file1 = ":newline_win_actual",
    file2 = ":newline_win_exp",
)