aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/shell/bazel/cross_repository_test.sh
blob: 5c6a97f2ca12460fef0be15c683255d988526b18 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/bin/bash
#
# Copyright 2016 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.
#
# Tests for package labels that cross repository boundaries
# Includes regression tests for #1592.
#

# Load the test setup defined in the parent directory
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${CURRENT_DIR}/../integration_test_setup.sh" \
  || { echo "integration_test_setup.sh not found!" >&2; exit 1; }

function write_rule {
  name=$1
  shift
  srcs=""
  for src in "$@"; do
    srcs="\"$src\", $srcs"
  done

  cat <<EOF
genrule(
  name = "$name",
  srcs = [
    $srcs
  ],
  outs = ["$name.out"],
  cmd = "echo $name > \$@",
  visibility = ["//visibility:public"],
)
EOF
}

# Basic use case: Define a repository nested in the main repository, check
# correct targets are visible
function test_basic_cross_repo_targets {
  cat > WORKSPACE <<EOF
local_repository(
    name = "bar",
    path = "bar",
)
EOF

  write_rule depend_via_repo "@bar//:bar" >> BUILD
  write_rule depend_via_repo_subdir "@bar//subbar:subbar" >> BUILD
  write_rule depend_directly "//bar:bar" >> BUILD
  write_rule depend_directly_subdir "//bar/subbar:subbar" >> BUILD

  mkdir -p bar
  touch bar/WORKSPACE
  write_rule bar >> bar/BUILD
  mkdir -p bar/subbar
  write_rule subbar >> bar/subbar/BUILD

  # These should succeed, they use the correct label.
  bazel build //:depend_via_repo || fail "build should succeed"
  bazel build //:depend_via_repo_subdir || fail "build should succeed"

  # These should fail, they is using the wrong label that crosses the
  # repository boundary.
  bazel build //:depend_directly >& $TEST_log && fail "build should fail"
  expect_log "no such package 'bar': Invalid package reference bar crosses into repository @bar"
  bazel build //:depend_directly_subdir >& $TEST_log && fail "build should fail"
  expect_log "no such package 'bar/subbar': Invalid package reference bar/subbar crosses into repository @bar"
}

function test_top_level_local_repository {
  cat > WORKSPACE <<EOF
local_repository(
    name = "loc",
    path = ".",
)
EOF

  write_rule foo >> BUILD

  # These should succeed, they use the correct label.
  bazel build //:foo || fail "build should succeed"
  bazel build @loc//:foo || fail "build should succeed"
}

# Loading rules.
function test_workspace_loads_rules {
  cat > WORKSPACE <<EOF
load("//baz:rules.bzl", "baz_rule")
local_repository(
    name = "bar",
    path = "bar",
)
EOF

  write_rule depend_via_repo "@bar//:bar" >> BUILD
  write_rule depend_directly "//bar:bar" >> BUILD

  mkdir -p bar
  touch bar/WORKSPACE
  write_rule bar >> bar/BUILD

  mkdir -p baz
  touch baz/BUILD
  cat > baz/rules.bzl <<EOF
baz_rule = 'baz_rule'
EOF

  # This should succeed, it uses the correct label.
  bazel build //:depend_via_repo || fail "build should succeed"

  # This should fail, it is using the wrong label that crosses the repository
  # boundary.
  bazel build //:depend_directly >& $TEST_log && fail "build should fail"
  expect_log "no such package 'bar': Invalid package reference bar crosses into repository @bar"
}

# Load rules via an invalid label.
function test_workspace_loads_rules_failure {
  cat > WORKSPACE <<EOF
load("//bar:rules.bzl", "bar_rule")
local_repository(
    name = "bar",
    path = "bar",
)
EOF

  write_rule depend_via_repo "@bar//:bar" >> BUILD

  mkdir -p bar
  touch bar/WORKSPACE
  write_rule bar >> bar/BUILD
  cat > bar/rules.bzl <<EOF
bar_rule = 'bar_rule'
EOF

  # This should fail in workspace parsing.
  bazel build //:depend_via_repo >& $TEST_log && fail "build should fail"
  # TODO(jcater): Show a better error when this occurs
  #expect_log "no such package 'bar': Package crosses into repository @bar"
  # This is the current error shown.
  expect_log "cycles detected"
}

function test_top_level_local_repository {
  cat > WORKSPACE <<EOF
local_repository(
    name = "loc",
    path = ".",
)
EOF

  write_rule foo >> BUILD

  # These should succeed, they use the correct label.
  bazel build //:foo || fail "build should succeed"
  bazel build @loc//:foo || fail "build should succeed"
}

function test_incremental_add_repository {
  # Empty workspace, defines no local_repository.
  cat > WORKSPACE

  mkdir -p bar
  write_rule bar >> bar/BUILD
  mkdir -p bar/subbar
  write_rule subbar >> bar/subbar/BUILD

  bazel query //bar/... >& $TEST_log || fail "query should succeed"

  # Now add the local_repository and WORKSPACE.
  cat > WORKSPACE <<EOF
local_repository(
    name = "bar",
    path = "bar",
)
EOF
  touch bar/WORKSPACE

  # These should now fail, using the incorrect label.
  bazel query //bar:bar >& $TEST_log && fail "build should fail"
  expect_log "no such package 'bar': Invalid package reference bar crosses into repository @bar"
  bazel query //bar/subbar:subbar >& $TEST_log && fail "build should fail"
  expect_log "no such package 'bar/subbar': Invalid package reference bar/subbar crosses into repository @bar"

  # These should succeed.
  echo "about to test @bar//"
  bazel query @bar//:bar || fail "query should succeed"
  bazel query @bar//subbar:subbar || fail "query should succeed"
}

function test_incremental_remove_repository {
  # Add the local_repository and WORKSPACE.
  cat > WORKSPACE <<EOF
local_repository(
    name = "bar",
    path = "bar",
)
EOF
  mkdir -p bar
  touch bar/WORKSPACE
  write_rule bar >> bar/BUILD
  mkdir -p bar/subbar
  write_rule subbar >> bar/subbar/BUILD

  # These should succeed.
  echo "about to test @bar//"
  bazel query @bar//:bar || fail "query should succeed"
  bazel query @bar//subbar:subbar || fail "query should succeed"

  # Now remove the workspace and the local_repository.
  cat > WORKSPACE
  rm bar/WORKSPACE

  # These should now succeed.
  bazel query //bar:bar || fail "query should succeed"
  bazel query //bar/subbar:subbar || fail "query should succeed"


  # These should now fail.
  bazel query @bar//:bar >& $TEST_log && fail "build should fail"
  expect_log "no such package '@bar//': The repository could not be resolved"
  bazel query @bar//subbar:subbar >& $TEST_log && fail "build should fail"
  expect_log "no such package '@bar//subbar': The repository could not be resolved"
}

# Test for https://github.com/bazelbuild/bazel/issues/2580
# This issue does not involve a local repository but it is triggered by the
# local repository cross-reference check.
function test_workspace_directory {
  cat > WORKSPACE <<EOF
load('//pkg/WORKSPACE:ext.bzl', 'VALUE')
EOF

  mkdir -p pkg/WORKSPACE

  cat > pkg/WORKSPACE/BUILD <<EOF
exports_files(['ext.bzl'])
EOF

  cat > pkg/WORKSPACE/ext.bzl <<EOF
VALUE = 'a value'
EOF

  # These should succeed, they use the correct label.
  bazel build //pkg/WORKSPACE:all || fail "build should succeed"
}

# TODO(katre): Add tests to verify incremental package reloads are necessary and correct.
# - /WORKSPACE edited, rule not changed - no reload
# - /WORKSPACE not edited, /dir/WORKSPACE added or removed - only packages in
#   /dir invalidated and re-loaded

run_suite "cross-repository tests"