#!/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 < WORKSPACE <> 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 <> 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 <> 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 <& $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 <> BUILD mkdir -p bar touch bar/WORKSPACE write_rule bar >> bar/BUILD cat > bar/rules.bzl <& $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 <> 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 <& $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 <> 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 < pkg/WORKSPACE/BUILD < pkg/WORKSPACE/ext.bzl <