diff options
author | 2017-10-10 18:15:56 +0200 | |
---|---|---|
committer | 2017-10-11 10:56:55 +0200 | |
commit | 61c97c0c6619fbeccbeabdc48f25d035cfa7d1f7 (patch) | |
tree | 3fe530e4cdacc539132de84c41d714ba5dba3f55 | |
parent | 00d128c6fd6d4f1f1e976c7d6f3b3c3f717431f0 (diff) |
Add tests for the Skylark-based implementation of git repository rules.
Fixes #3825.
Change-Id: I59cb0edb50af30991153a1a684e613853df22dd2
PiperOrigin-RevId: 171687620
-rw-r--r-- | src/test/shell/bazel/BUILD | 10 | ||||
-rwxr-xr-x | src/test/shell/bazel/git_repository_test.sh | 30 | ||||
-rw-r--r-- | src/test/shell/bazel/skylark_git_repository_test.sh | 453 | ||||
-rw-r--r-- | tools/build_defs/repo/git.bzl | 5 |
4 files changed, 488 insertions, 10 deletions
diff --git a/src/test/shell/bazel/BUILD b/src/test/shell/bazel/BUILD index d78befae85..b0b963b629 100644 --- a/src/test/shell/bazel/BUILD +++ b/src/test/shell/bazel/BUILD @@ -241,6 +241,16 @@ sh_test( ) sh_test( + name = "skylark_git_repository_test", + size = "large", + srcs = ["skylark_git_repository_test.sh"], + data = [ + ":test-deps", + "//src/test/shell/bazel/testdata:git-repos", + ], +) + +sh_test( name = "local_repository_test", size = "large", srcs = ["local_repository_test.sh"], diff --git a/src/test/shell/bazel/git_repository_test.sh b/src/test/shell/bazel/git_repository_test.sh index 639d21698f..b6465531f6 100755 --- a/src/test/shell/bazel/git_repository_test.sh +++ b/src/test/shell/bazel/git_repository_test.sh @@ -134,10 +134,13 @@ new_git_repository( name = "pluto", remote = "$pluto_repo_dir", tag = "0-initial", - build_file = "pluto.BUILD", + build_file = "//:pluto.BUILD", ) EOF + cat > BUILD <<EOF +exports_files(['pluto.BUILD']) +EOF cat > pluto.BUILD <<EOF filegroup( name = "pluto", @@ -215,10 +218,13 @@ new_git_repository( remote = "$outer_planets_repo_dir", tag = "1-submodule", init_submodules = 1, - build_file = "outer_planets.BUILD", + build_file = "//:outer_planets.BUILD", ) EOF + cat > BUILD <<EOF +exports_files(['outer_planets.BUILD']) +EOF cat > outer_planets.BUILD <<EOF filegroup( name = "neptune", @@ -266,30 +272,35 @@ function test_git_repository_not_refetched_on_server_restart() { git_repository(name='g', remote='$repo_dir', commit='f0b79ff0') EOF - bazel --batch build @g//:g >& $TEST_log || fail "Build failed" + # Use batch to force server restarts. + bazel --batch build --noexperimental_ui @g//:g >& $TEST_log || fail "Build failed" expect_log "Cloning" assert_contains "GIT 1" bazel-genfiles/external/g/go - bazel --batch build @g//:g >& $TEST_log || fail "Build failed" + + # Without changing anything, restart the server, which should not cause the checkout to be re-cloned. + bazel --batch build --noexperimental_ui @g//:g >& $TEST_log || fail "Build failed" expect_not_log "Cloning" assert_contains "GIT 1" bazel-genfiles/external/g/go + + # Change the commit id, which should cause the checkout to be re-cloned. cat > WORKSPACE <<EOF git_repository(name='g', remote='$repo_dir', commit='62777acc') EOF - bazel --batch build @g//:g >& $TEST_log || fail "Build failed" + bazel --batch build --noexperimental_ui @g//:g >& $TEST_log || fail "Build failed" expect_log "Cloning" assert_contains "GIT 2" bazel-genfiles/external/g/go + # Change the WORKSPACE but not the commit id, which should not cause the checkout to be re-cloned. cat > WORKSPACE <<EOF # This comment line is to change the line numbers, which should not cause Bazel # to refetch the repository git_repository(name='g', remote='$repo_dir', commit='62777acc') EOF - bazel --batch build @g//:g >& $TEST_log || fail "Build failed" + bazel --batch build --noexperimental_ui @g//:g >& $TEST_log || fail "Build failed" expect_not_log "Cloning" assert_contains "GIT 2" bazel-genfiles/external/g/go - } @@ -301,16 +312,17 @@ function test_git_repository_refetched_when_commit_changes() { git_repository(name='g', remote='$repo_dir', commit='f0b79ff0') EOF - bazel build @g//:g >& $TEST_log || fail "Build failed" + bazel build --noexperimental_ui @g//:g >& $TEST_log || fail "Build failed" expect_log "Cloning" assert_contains "GIT 1" bazel-genfiles/external/g/go + # Change the commit id, which should cause the checkout to be re-cloned. cat > WORKSPACE <<EOF git_repository(name='g', remote='$repo_dir', commit='62777acc') EOF - bazel build @g//:g >& $TEST_log || fail "Build failed" + bazel build --noexperimental_ui @g//:g >& $TEST_log || fail "Build failed" expect_log "Cloning" assert_contains "GIT 2" bazel-genfiles/external/g/go } diff --git a/src/test/shell/bazel/skylark_git_repository_test.sh b/src/test/shell/bazel/skylark_git_repository_test.sh new file mode 100644 index 0000000000..1c7231f8ef --- /dev/null +++ b/src/test/shell/bazel/skylark_git_repository_test.sh @@ -0,0 +1,453 @@ +#!/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 git_repository and new_git_repository workspace rules. +# + +# 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; } + +# Global test setup. +# +# Unpacks the test Git repositories in the test temporary directory. +function set_up() { + bazel clean --expunge + local repos_dir=$TEST_TMPDIR/repos + if [ -e "$repos_dir" ]; then + rm -rf $repos_dir + fi + + mkdir -p $repos_dir + cp $testdata_path/pluto-repo.tar.gz $repos_dir + cp $testdata_path/outer-planets-repo.tar.gz $repos_dir + cp $testdata_path/refetch-repo.tar.gz $repos_dir + cd $repos_dir + tar zxf pluto-repo.tar.gz + tar zxf outer-planets-repo.tar.gz + tar zxf refetch-repo.tar.gz + + # Fix environment variables for a hermetic use of git. + export GIT_CONFIG_NOSYSTEM=1 + export GIT_CONFIG_NOGLOBAL=1 + export HOME= + export XDG_CONFIG_HOME= +} + +# Test cloning a Git repository using the git_repository rule. +# +# This test uses the pluto Git repository at tag 1-build, which contains the +# following files: +# +# pluto/ +# WORKSPACE +# BUILD +# info +# +# Then, set up workspace with the following files: +# +# $WORKSPACE_DIR/ +# WORKSPACE +# planets/ +# BUILD +# planet_info.sh +# +# //planets has a dependency on a target in the pluto Git repository. +function test_git_repository() { + local pluto_repo_dir=$TEST_TMPDIR/repos/pluto + # Commit 85b8224 corresponds to tag 1-build. See testdata/pluto.git_log. + local commit_hash="b87de93" + + # Create a workspace that clones the repository at the first commit. + cd $WORKSPACE_DIR + cat > WORKSPACE <<EOF +load('@bazel_tools//tools/build_defs/repo:git.bzl', 'git_repository') +git_repository( + name = "pluto", + remote = "$pluto_repo_dir", + commit = "$commit_hash", +) +EOF + mkdir -p planets + cat > planets/BUILD <<EOF +sh_binary( + name = "planet-info", + srcs = ["planet_info.sh"], + data = ["@pluto//:pluto"], +) +EOF + + cat > planets/planet_info.sh <<EOF +#!/bin/sh +cat ../pluto/info +EOF + chmod +x planets/planet_info.sh + + bazel run //planets:planet-info >& $TEST_log \ + || echo "Expected build/run to succeed" + expect_log "Pluto is a dwarf planet" +} + +function test_new_git_repository_with_build_file() { + do_new_git_repository_test "build_file" +} + +function test_new_git_repository_with_build_file_content() { + do_new_git_repository_test "build_file_content" +} + +# Test cloning a Git repository using the new_git_repository rule. +# +# This test uses the pluto Git repository at tag 0-initial, which contains the +# following files: +# +# pluto/ +# info +# +# Set up workspace with the following files: +# +# $WORKSPACE_DIR/ +# WORKSPACE +# pluto.BUILD +# planets/ +# BUILD +# planet_info.sh +# +# //planets has a dependency on a target in the $TEST_TMPDIR/pluto Git +# repository. +function do_new_git_repository_test() { + local pluto_repo_dir=$TEST_TMPDIR/repos/pluto + + # Create a workspace that clones the repository at the first commit. + cd $WORKSPACE_DIR + + if [ "$1" == "build_file" ] ; then + cat > WORKSPACE <<EOF +load('@bazel_tools//tools/build_defs/repo:git.bzl', 'new_git_repository') +new_git_repository( + name = "pluto", + remote = "$pluto_repo_dir", + tag = "0-initial", + build_file = "//:pluto.BUILD", +) +EOF + + cat > BUILD <<EOF +exports_files(['pluto.BUILD']) +EOF + cat > pluto.BUILD <<EOF +filegroup( + name = "pluto", + srcs = ["info"], + visibility = ["//visibility:public"], +) +EOF + else + cat > WORKSPACE <<EOF +load('@bazel_tools//tools/build_defs/repo:git.bzl', 'new_git_repository') +new_git_repository( + name = "pluto", + remote = "$pluto_repo_dir", + tag = "0-initial", + build_file_content = """ +filegroup( + name = "pluto", + srcs = ["info"], + visibility = ["//visibility:public"], +)""" +) +EOF + fi + + mkdir -p planets + cat > planets/BUILD <<EOF +sh_binary( + name = "planet-info", + srcs = ["planet_info.sh"], + data = ["@pluto//:pluto"], +) +EOF + + cat > planets/planet_info.sh <<EOF +#!/bin/sh +cat ../pluto/info +EOF + chmod +x planets/planet_info.sh + + bazel run //planets:planet-info >& $TEST_log \ + || echo "Expected build/run to succeed" + expect_log "Pluto is a planet" +} + +# Test cloning a Git repository that has a submodule using the +# new_git_repository rule. +# +# This test uses the outer-planets Git repository at revision 1-submodule, which +# contains the following files: +# +# outer_planets/ +# neptune/ +# info +# pluto/ --> submodule ../pluto +# info +# +# Set up workspace with the following files: +# +# $WORKSPACE_DIR/ +# WORKSPACE +# outer_planets.BUILD +# planets/ +# BUILD +# planet_info.sh +# +# planets has a dependency on targets in the $TEST_TMPDIR/outer_planets Git +# repository. +function test_new_git_repository_submodules() { + local outer_planets_repo_dir=$TEST_TMPDIR/repos/outer-planets + + # Create a workspace that clones the outer_planets repository. + cd $WORKSPACE_DIR + cat > WORKSPACE <<EOF +load('@bazel_tools//tools/build_defs/repo:git.bzl', 'new_git_repository') +new_git_repository( + name = "outer_planets", + remote = "$outer_planets_repo_dir", + tag = "1-submodule", + init_submodules = 1, + build_file = "//:outer_planets.BUILD", +) +EOF + + cat > BUILD <<EOF +exports_files(['outer_planets.BUILD']) +EOF + cat > outer_planets.BUILD <<EOF +filegroup( + name = "neptune", + srcs = ["neptune/info"], + visibility = ["//visibility:public"], +) + +filegroup( + name = "pluto", + srcs = ["pluto/info"], + visibility = ["//visibility:public"], +) +EOF + + mkdir -p planets + cat > planets/BUILD <<EOF +sh_binary( + name = "planet-info", + srcs = ["planet_info.sh"], + data = [ + "@outer_planets//:neptune", + "@outer_planets//:pluto", + ], +) +EOF + + cat > planets/planet_info.sh <<EOF +#!/bin/sh +cat ../outer_planets/neptune/info +cat ../outer_planets/pluto/info +EOF + chmod +x planets/planet_info.sh + + bazel run //planets:planet-info >& $TEST_log \ + || echo "Expected build/run to succeed" + expect_log "Neptune is a planet" + expect_log "Pluto is a planet" +} + +function test_git_repository_not_refetched_on_server_restart() { + local repo_dir=$TEST_TMPDIR/repos/refetch + + cd $WORKSPACE_DIR + cat > WORKSPACE <<EOF +load('@bazel_tools//tools/build_defs/repo:git.bzl', 'git_repository') +git_repository(name='g', remote='$repo_dir', commit='f0b79ff0', verbose=True) +EOF + + # Use batch to force server restarts. + bazel --batch build @g//:g >& $TEST_log || fail "Build failed" + expect_log "Cloning" + assert_contains "GIT 1" bazel-genfiles/external/g/go + + # Without changing anything, restart the server, which should not cause the checkout to be re-cloned. + bazel --batch build @g//:g >& $TEST_log || fail "Build failed" + expect_not_log "Cloning" + assert_contains "GIT 1" bazel-genfiles/external/g/go + + # Change the commit id, which should cause the checkout to be re-cloned. + cat > WORKSPACE <<EOF +load('@bazel_tools//tools/build_defs/repo:git.bzl', 'git_repository') +git_repository(name='g', remote='$repo_dir', commit='62777acc', verbose=True) +EOF + + bazel --batch build @g//:g >& $TEST_log || fail "Build failed" + expect_log "Cloning" + assert_contains "GIT 2" bazel-genfiles/external/g/go + + # Change the WORKSPACE but not the commit id, which should not cause the checkout to be re-cloned. + cat > WORKSPACE <<EOF +# This comment line is to change the line numbers, which should not cause Bazel +# to refetch the repository +load('@bazel_tools//tools/build_defs/repo:git.bzl', 'git_repository') +git_repository(name='g', remote='$repo_dir', commit='62777acc', verbose=True) +EOF + + bazel --batch build @g//:g >& $TEST_log || fail "Build failed" + expect_not_log "Cloning" + assert_contains "GIT 2" bazel-genfiles/external/g/go +} + + +function test_git_repository_refetched_when_commit_changes() { + local repo_dir=$TEST_TMPDIR/repos/refetch + + cd $WORKSPACE_DIR + cat > WORKSPACE <<EOF +load('@bazel_tools//tools/build_defs/repo:git.bzl', 'git_repository') +git_repository(name='g', remote='$repo_dir', commit='f0b79ff0', verbose=True) +EOF + + bazel build @g//:g >& $TEST_log || fail "Build failed" + expect_log "Cloning" + assert_contains "GIT 1" bazel-genfiles/external/g/go + + # Change the commit id, which should cause the checkout to be re-cloned. + cat > WORKSPACE <<EOF +load('@bazel_tools//tools/build_defs/repo:git.bzl', 'git_repository') +git_repository(name='g', remote='$repo_dir', commit='62777acc', verbose=True) +EOF + + bazel build @g//:g >& $TEST_log || fail "Build failed" + expect_log "Cloning" + assert_contains "GIT 2" bazel-genfiles/external/g/go +} + +function test_git_repository_and_nofetch() { + local repo_dir=$TEST_TMPDIR/repos/refetch + + cd $WORKSPACE_DIR + cat > WORKSPACE <<EOF +load('@bazel_tools//tools/build_defs/repo:git.bzl', 'git_repository') +git_repository(name='g', remote='$repo_dir', commit='f0b79ff0') +EOF + + bazel build --nofetch @g//:g >& $TEST_log && fail "Build succeeded" + expect_log "fetching repositories is disabled" + bazel build @g//:g >& $TEST_log || fail "Build failed" + assert_contains "GIT 1" bazel-genfiles/external/g/go + + cat > WORKSPACE <<EOF +load('@bazel_tools//tools/build_defs/repo:git.bzl', 'git_repository') +git_repository(name='g', remote='$repo_dir', commit='62777acc') +EOF + + + bazel build --nofetch @g//:g >& $TEST_log || fail "Build failed" + expect_log "External repository 'g' is not up-to-date" + assert_contains "GIT 1" bazel-genfiles/external/g/go + bazel build @g//:g >& $TEST_log || fail "Build failed" + assert_contains "GIT 2" bazel-genfiles/external/g/go +} + +# Helper function for setting up the workspace as follows +# +# $WORKSPACE_DIR/ +# WORKSPACE +# planets/ +# planet_info.sh +# BUILD +function setup_error_test() { + cd $WORKSPACE_DIR + mkdir -p planets + cat > planets/planet_info.sh <<EOF +#!/bin/sh +cat external/pluto/info +EOF + + cat > planets/BUILD <<EOF +sh_binary( + name = "planet-info", + srcs = ["planet_info.sh"], + data = ["@pluto//:pluto"], +) +EOF +} + +# Verifies that rule fails if both tag and commit are set. +# +# This test uses the pluto Git repository at tag 1-build, which contains the +# following files: +# +# pluto/ +# WORKSPACE +# BUILD +# info +function test_git_repository_both_commit_tag_error() { + setup_error_test + local pluto_repo_dir=$TEST_TMPDIR/pluto + # Commit 85b8224 corresponds to tag 1-build. See testdata/pluto.git_log. + local commit_hash="b87de93" + + cd $WORKSPACE_DIR + cat > WORKSPACE <<EOF +load('@bazel_tools//tools/build_defs/repo:git.bzl', 'git_repository') +git_repository( + name = "pluto", + remote = "$pluto_repo_dir", + tag = "1-build", + commit = "$commit_hash", +) +EOF + + bazel fetch //planets:planet-info >& $TEST_log \ + || echo "Expect run to fail." + expect_log "Exactly one of commit and tag must be provided" +} + +# Verifies that rule fails if neither tag or commit are set. +# +# This test uses the pluto Git repository at tag 1-build, which contains the +# following files: +# +# pluto/ +# WORKSPACE +# BUILD +# info +function test_git_repository_no_commit_tag_error() { + setup_error_test + local pluto_repo_dir=$TEST_TMPDIR/pluto + + cd $WORKSPACE_DIR + cat > WORKSPACE <<EOF +load('@bazel_tools//tools/build_defs/repo:git.bzl', 'git_repository') +git_repository( + name = "pluto", + remote = "$pluto_repo_dir", +) +EOF + + bazel fetch //planets:planet-info >& $TEST_log \ + || echo "Expect run to fail." + expect_log "Exactly one of commit and tag must be provided" +} + +run_suite "skylark git_repository tests" diff --git a/tools/build_defs/repo/git.bzl b/tools/build_defs/repo/git.bzl index b4eacfd2af..7f49a16817 100644 --- a/tools/build_defs/repo/git.bzl +++ b/tools/build_defs/repo/git.bzl @@ -14,6 +14,8 @@ """Rules for cloning external git repositories.""" def _clone_or_update(ctx): + if (ctx.attr.verbose): + print('git.bzl: Cloning or updating repository %s' % ctx.name) if ((not ctx.attr.tag and not ctx.attr.commit) or (ctx.attr.tag and ctx.attr.commit)): fail('Exactly one of commit and tag must be provided') @@ -72,13 +74,14 @@ _common_attrs = { 'commit': attr.string(default=''), 'tag': attr.string(default=''), 'init_submodules': attr.bool(default=False), + 'verbose': attr.bool(default=False), } new_git_repository = repository_rule( implementation=_new_git_repository_implementation, attrs=_common_attrs + { - 'build_file': attr.label(), + 'build_file': attr.label(allow_single_file=True), 'build_file_content': attr.string(), } ) |