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 36licenses(["notice"]) 37 38package(default_testonly = 1) 39 40sh_test( 41 name = "copy_file_tests", 42 srcs = ["copy_file_tests.sh"], 43 data = [ 44 ":run_executables", 45 # Use DefaultInfo.files from 'copy_src' (via 'file_deps'). 46 ":file_deps", 47 # Use DefaultInfo.runfiles from 'copy_gen'. 48 ":copy_gen", 49 ":copy_gen_symlink", 50 "//tests:unittest.bash", 51 ], 52 deps = ["@bazel_tools//tools/bash/runfiles"], 53) 54 55filegroup( 56 name = "file_deps", 57 # Use DefaultInfo.files from 'copy_src'. 58 srcs = [ 59 ":copy_src", 60 ":copy_src_symlink", 61 ], 62) 63 64# If 'run_executables' is built, then 'bin_gen' and 'bin_src' are 65# executable, asserting that copy_file makes the output executable. 66genrule( 67 name = "run_executables", 68 outs = [ 69 "xsrc-out-symlink.txt", 70 "xgen-out-symlink.txt", 71 "xsrc-out.txt", 72 "xgen-out.txt", 73 ], 74 cmd = " && ".join([ 75 "$(location :bin_src_symlink) > $(location xsrc-out-symlink.txt)", 76 "$(location :bin_gen_symlink) > $(location xgen-out-symlink.txt)", 77 "$(location :bin_src) > $(location xsrc-out.txt)", 78 "$(location :bin_gen) > $(location xgen-out.txt)", 79 ]), 80 output_to_bindir = 1, 81 tools = [ 82 ":bin_gen", 83 ":bin_gen_symlink", 84 ":bin_src", 85 ":bin_src_symlink", 86 ], 87) 88 89# If 'bin_src' is built, then 'copy_xsrc' made its output executable. 90sh_binary( 91 name = "bin_src", 92 srcs = [":copy_xsrc"], 93) 94 95# If 'bin_src' is built, then 'copy_xsrc' made its output executable. 96sh_binary( 97 name = "bin_src_symlink", 98 srcs = [":copy_xsrc_symlink"], 99) 100 101# If 'bin_gen' is built, then 'copy_xgen' made its output executable. 102sh_binary( 103 name = "bin_gen", 104 srcs = [":copy_xgen"], 105) 106 107# If 'bin_gen' is built, then 'copy_xgen' made its output executable. 108sh_binary( 109 name = "bin_gen_symlink", 110 srcs = [":copy_xgen_symlink"], 111) 112 113copy_file( 114 name = "copy_src", 115 src = "a.txt", 116 out = "out/a-out.txt", 117) 118 119copy_file( 120 name = "copy_src_symlink", 121 src = "a.txt", 122 out = "out/a-out-symlink.txt", 123 allow_symlink = True, 124) 125 126copy_file( 127 name = "copy_gen", 128 src = ":gen", 129 out = "out/gen-out.txt", 130 allow_symlink = True, 131) 132 133copy_file( 134 name = "copy_gen_symlink", 135 src = ":gen", 136 out = "out/gen-out-symlink.txt", 137) 138 139copy_file( 140 name = "copy_xsrc", 141 src = "a.txt", 142 out = "xout/a-out.sh", 143 is_executable = True, 144) 145 146copy_file( 147 name = "copy_xsrc_symlink", 148 src = "a_with_exec_bit.txt", 149 out = "xout/a-out-symlink.sh", 150 allow_symlink = True, 151 is_executable = True, 152) 153 154copy_file( 155 name = "copy_xgen", 156 src = ":gen", 157 out = "xout/gen-out.sh", 158 is_executable = True, 159) 160 161copy_file( 162 name = "copy_xgen_symlink", 163 src = ":gen", 164 out = "xout/gen-out-symlink.sh", 165 allow_symlink = True, 166 is_executable = True, 167) 168 169genrule( 170 name = "gen", 171 outs = ["b.txt"], 172 cmd = "echo -e '#!/usr/bin/env bash\necho potato' > $@", 173) 174