aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/shell/bazel/external_skylark_load_test.sh
blob: f3a07269db03f4eefa027a658ad14c6af15a0437 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/bin/bash
#
# Copyright 2015 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Test handling of Skylark loads from and in external repositories.
#

# Load test environment
source $(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/test-setup.sh \
  || { echo "test-setup.sh not found!" >&2; exit 1; }

# The following tests build an instance of a Skylark macro loaded from a
# local_repository, which in turns loads another Skylark file either from
# the external repo or the main repo, depending on the test parameters.
# The tests cover all the valid syntactic variants of the second load. The
# package structure used for the tests is as follows:
#
# ${WORKSPACE_DIR}/
#   WORKSPACE
#   local_pkg/
#     BUILD
#   another_local_pkg/
#     BUILD
#     local_constants.bzl
# ${external_repo}/
#   external_pkg/
#     BUILD
#     macro_def.bzl
#     external_constants.bzl
function run_external_skylark_load_test() {
  load_target_to_test=$1
  expected_test_output=$2

  create_new_workspace
  external_repo=${new_workspace_dir}

  # Since we're creating an artifical WORKSPACE file in this test, we must
  # first remove the symlink to the actual bazel/WORKSPACE file.
  rm ${WORKSPACE_DIR}/WORKSPACE
  touch ${WORKSPACE_DIR}/WORKSPACE

  cat > ${WORKSPACE_DIR}/WORKSPACE <<EOF
local_repository(name = "external_repo", path = "${external_repo}")
EOF

  mkdir ${WORKSPACE_DIR}/local_pkg
  cat > ${WORKSPACE_DIR}/local_pkg/BUILD <<EOF
load("@external_repo//external_pkg:macro_def.bzl", "macro")
macro(name="macro_instance")
EOF

  mkdir ${WORKSPACE_DIR}/another_local_pkg
  touch ${WORKSPACE_DIR}/another_local_pkg/BUILD
  cat > ${WORKSPACE_DIR}/another_local_pkg/local_constants.bzl <<EOF
OUTPUT_STRING = "LOCAL!"
EOF

  mkdir ${external_repo}/external_pkg
  touch ${external_repo}/external_pkg/BUILD
  cat > ${external_repo}/external_pkg/macro_def.bzl <<EOF
load("${load_target_to_test}", "OUTPUT_STRING")
def macro(name):
  native.genrule(
      name = name,
      outs = [name + ".txt"],
      cmd = "echo " + OUTPUT_STRING + " > \$@",
  )
EOF

  cat > ${external_repo}/external_pkg/external_constants.bzl <<EOF
OUTPUT_STRING = "EXTERNAL!"
EOF

  cd ${WORKSPACE_DIR}
  bazel build local_pkg:macro_instance >& $TEST_log || \
    fail "Expected build to succeed"
  assert_contains "${expected_test_output}" \
    bazel-genfiles/local_pkg/macro_instance.txt
}

# A label with an explicit external repo reference should be resolved relative
# to the external repo.
function test_load_skylark_from_external_repo_with_pkg_relative_label_load() {
  run_external_skylark_load_test \
    "@external_repo//external_pkg:external_constants.bzl" "EXTERNAL!"
}

# A relative label should be resolved relative to the external package.
function test_load_skylark_from_external_repo_with_pkg_relative_label_load() {
  run_external_skylark_load_test ":external_constants.bzl" "EXTERNAL!"
}

# A relative path should be resolved relative to the current (external)
# directory.
function test_load_skylark_from_external_repo_with_pkg_relative_path_load() {
  run_external_skylark_load_test "external_constants" "EXTERNAL!"
}

# An absolute label with no repo prefix should be resolved relative to the
# current (external) repo.
function test_load_skylark_from_external_repo_with_pkg_relative_path_load() {
  run_external_skylark_load_test "//external_pkg:external_constants.bzl" \
    "EXTERNAL!"
}

# An absolute label with the special "@" prefix should cause be resolved
# relative to the default repo.
function test_load_skylark_from_external_repo_with_repo_relative_label_load() {
  run_external_skylark_load_test "@//another_local_pkg:local_constants.bzl" \
    "LOCAL!"
}

# An absolute path should always be resolved in the default repo.
function test_load_skylark_from_external_repo_with_repo_relative_label_load() {
  run_external_skylark_load_test "/another_local_pkg/local_constants" \
    "LOCAL!"
}

function test_skylark_repository_relative_label() {
  repo2=$TEST_TMPDIR/repo2
  mkdir -p $repo2
  touch $repo2/WORKSPACE $repo2/BUILD
  cat > $repo2/remote.bzl <<EOF
def _impl(ctx):
  print(Label("//foo:bar"))

remote_rule = rule(
    implementation = _impl,
)
EOF

  cat > WORKSPACE <<EOF
local_repository(
    name = "r",
    path = "$repo2",
)
EOF
  cat > BUILD <<EOF
load('@r//:remote.bzl', 'remote_rule')

remote_rule(name = 'local')
EOF

  bazel build //:local &> $TEST_log || fail "Building local failed"
  expect_log "@r//foo:bar"

  cat > $repo2/remote.bzl <<EOF
def _impl(ctx):
  print(Label("//foo:bar", relative_to_caller_repository = True))

remote_rule = rule(
    implementation = _impl,
)
EOF
  bazel build //:local &> $TEST_log || fail "Building local failed"
  expect_log "//foo:bar"
}

# Going one level deeper: if we have:
# local/
#   BUILD
# r1/
#   BUILD
# r2/
#   BUILD
#   remote.bzl
# If //foo in local depends on //bar in r1, which is a Skylark rule
# defined in r2/remote.bzl, then a Label in remote.bzl should either
# resolve to @r2//whatever or @r1//whatever.
function test_skylark_repository_nested_relative_label() {
  repo1=$TEST_TMPDIR/repo1
  repo2=$TEST_TMPDIR/repo2
  mkdir -p $repo1 $repo2

  # local
  cat > WORKSPACE <<EOF
local_repository(
    name = "r1",
    path = "$repo1",
)
local_repository(
    name = "r2",
    path = "$repo2",
)
EOF
  cat > BUILD <<'EOF'
genrule(
    name = "foo",
    srcs = ["@r1//:bar"],
    outs = ["foo.out"],
    cmd = "echo '$(SRCS)' > $@",
)
EOF

  # r1
  touch $repo1/WORKSPACE
  cat > $repo1/BUILD <<EOF
load('@r2//:remote.bzl', 'remote_rule')

remote_rule(
    name = 'bar',
    visibility = ["//visibility:public"]
)
EOF

  # r2
  touch $repo2/WORKSPACE $repo2/BUILD
  cat > $repo2/remote.bzl <<EOF
def _impl(ctx):
  print(Label("//foo:bar"))

remote_rule = rule(
    implementation = _impl,
)
EOF

  bazel build //:foo &> $TEST_log || fail "Building local failed"
  expect_log "@r2//foo:bar"

  cat > $repo2/remote.bzl <<EOF
def _impl(ctx):
  print(Label("//foo:bar", relative_to_caller_repository = True))

remote_rule = rule(
    implementation = _impl,
)
EOF
  bazel build //:foo &> $TEST_log || fail "Building local failed"
  expect_log "@r1//foo:bar"
}

run_suite "Test Skylark loads from/in external repositories"