blob: 67f34bb1573ba17bddea8f66a143cdb1ac7749a0 [file] [log] [blame]
twertha1a57a52018-07-18 05:34:13 -07001#!/bin/bash
2#
3# Copyright 2018 The Bazel Authors. All rights reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Laszlo Csomor3e0ede42018-08-09 07:52:41 -070017# --- begin runfiles.bash initialization ---
18# Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash).
19set -euo pipefail
20if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
21 if [[ -f "$0.runfiles_manifest" ]]; then
22 export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
23 elif [[ -f "$0.runfiles/MANIFEST" ]]; then
24 export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
25 elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
26 export RUNFILES_DIR="$0.runfiles"
27 fi
28fi
29if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
30 source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
31elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
32 source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
33 "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
34else
35 echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
36 exit 1
37fi
38# --- end runfiles.bash initialization ---
39
40source "$(rlocation "io_bazel/src/test/shell/integration_test_setup.sh")" \
twertha1a57a52018-07-18 05:34:13 -070041 || { echo "integration_test_setup.sh not found!" >&2; exit 1; }
42
Laszlo Csomor3e0ede42018-08-09 07:52:41 -070043case "$(uname -s | tr [:upper:] [:lower:])" in
44msys*|mingw*|cygwin*)
45 declare -r is_windows=true
46 ;;
47*)
48 declare -r is_windows=false
49 ;;
50esac
51
52if "$is_windows"; then
53 export MSYS_NO_PATHCONV=1
54 export MSYS2_ARG_CONV_EXCL="*"
55fi
56
twertha1a57a52018-07-18 05:34:13 -070057add_to_bazelrc "build --package_path=%workspace%"
58
59function test_basic_aquery() {
60 local pkg="${FUNCNAME[0]}"
61 mkdir -p "$pkg" || fail "mkdir -p $pkg"
62 cat > "$pkg/BUILD" <<'EOF'
63genrule(
64 name = "foo",
twerth66a20632018-07-31 08:29:27 -070065 srcs = [":bar"],
twertha1a57a52018-07-18 05:34:13 -070066 outs = ["foo_out.txt"],
67 cmd = "cat $(SRCS) > $(OUTS)",
68)
69
70genrule(
71 name = "bar",
72 srcs = ["dummy.txt"],
73 outs = ["bar_out.txt"],
74 cmd = "echo unused > $(OUTS)",
75)
76EOF
77 echo "hello aquery" > "$pkg/in.txt"
78
79 bazel aquery "//$pkg:foo" > output 2> "$TEST_log" || fail "Expected success"
twerth66a20632018-07-31 08:29:27 -070080 assert_contains "//$pkg:foo" output
81 assert_not_contains "//$pkg:bar" output
twertha1a57a52018-07-18 05:34:13 -070082
twerthf621bbe2018-08-02 07:30:30 -070083 bazel aquery "deps(//$pkg:foo)" > output 2> "$TEST_log" \
84 || fail "Expected success"
twerth66a20632018-07-31 08:29:27 -070085 assert_contains "//$pkg:foo" output
86 assert_contains "//$pkg:bar" output
twertha1a57a52018-07-18 05:34:13 -070087}
twerth66a20632018-07-31 08:29:27 -070088
twerthf621bbe2018-08-02 07:30:30 -070089function test_aquery_text() {
90 local pkg="${FUNCNAME[0]}"
91 mkdir -p "$pkg" || fail "mkdir -p $pkg"
92 cat > "$pkg/BUILD" <<'EOF'
93genrule(
94 name = "bar",
95 srcs = ["dummy.txt"],
96 outs = ["bar_out.txt"],
97 cmd = "echo unused > $(OUTS)",
98)
99EOF
100 echo "hello aquery" > "$pkg/in.txt"
101
102 bazel aquery --output=text "//$pkg:bar" > output 2> "$TEST_log" \
103 || fail "Expected success"
104 cat output >> "$TEST_log"
105 assert_contains "action 'Executing genrule //$pkg:bar'" output
106 assert_contains "Mnemonic: Genrule" output
107 assert_contains "Owner: //$pkg:bar" output
108 assert_contains "Configuration: .*-fastbuild" output
109 # Only check that the inputs/outputs/command line/environment exist, but not
110 # their actual contents since that would be too much.
111 assert_contains "Inputs: \[" output
112 assert_contains "Outputs: \[" output
Laszlo Csomor3e0ede42018-08-09 07:52:41 -0700113 if is_windows; then
114 assert_contains "Command Line: .*bash\.exe" output
115 else
116 assert_contains "Command Line: (" output
117 fi
twerth50ea2dd2018-09-06 04:58:19 -0700118}
twerthf621bbe2018-08-02 07:30:30 -0700119
twerth50ea2dd2018-09-06 04:58:19 -0700120function test_aquery_skylark_env() {
121 local pkg="${FUNCNAME[0]}"
122 mkdir -p "$pkg" || fail "mkdir -p $pkg"
123 cat > "$pkg/rule.bzl" <<'EOF'
124def _impl(ctx):
125 output = ctx.outputs.out
126 input = ctx.file.source
127 env = {}
128 env["foo"] = "bar"
129
130 ctx.actions.run_shell(
131 inputs = [input],
132 outputs = [output],
133 command = "cat '%s' > '%s'" % (input.path, output.path),
134 env = env,
135 )
136
137copy = rule(
138 implementation = _impl,
139 attrs = {"source": attr.label(mandatory = True, allow_single_file = True)},
140 outputs = {"out": "%{name}.copy"},
141)
142EOF
143
144 cat > "$pkg/BUILD" <<'EOF'
145load(":rule.bzl", "copy")
146copy(
147 name = "goo",
148 source = "dummy.txt",
149)
150EOF
151 echo "hello aquery" > "$pkg/dummy.txt"
152
153 bazel aquery --output=text "//$pkg:goo" > output 2> "$TEST_log" \
154 || fail "Expected success"
155 cat output >> "$TEST_log"
156 assert_contains "Mnemonic: SkylarkAction" output
157 assert_contains "Owner: //$pkg:goo" output
158 assert_contains "Environment: \[.*foo=bar" output
twerthf621bbe2018-08-02 07:30:30 -0700159}
160
twerth66a20632018-07-31 08:29:27 -0700161run_suite "${PRODUCT_NAME} action graph query tests"