diff options
Diffstat (limited to 'tests/copy_directory')
-rw-r--r-- | tests/copy_directory/BUILD.bazel | 43 | ||||
-rwxr-xr-x | tests/copy_directory/copy_directory_tests.sh | 81 | ||||
-rw-r--r-- | tests/copy_directory/dir_with_subdir/a | 1 | ||||
-rw-r--r-- | tests/copy_directory/dir_with_subdir/b | 1 | ||||
-rw-r--r-- | tests/copy_directory/dir_with_subdir/subdir/c | 1 | ||||
-rw-r--r-- | tests/copy_directory/dir_with_symlink/file | 1 | ||||
l--------- | tests/copy_directory/dir_with_symlink/symlink | 1 | ||||
-rw-r--r-- | tests/copy_directory/empty_directory.bzl | 34 |
8 files changed, 163 insertions, 0 deletions
diff --git a/tests/copy_directory/BUILD.bazel b/tests/copy_directory/BUILD.bazel new file mode 100644 index 0000000..f766e99 --- /dev/null +++ b/tests/copy_directory/BUILD.bazel @@ -0,0 +1,43 @@ +# This package aids testing the 'copy_directory' rule. + +load("//rules:copy_directory.bzl", "copy_directory") +load(":empty_directory.bzl", "empty_directory") + +licenses(["notice"]) + +package(default_testonly = 1) + +# Copy of directory containing files a and b, and a subdir containing c +copy_directory( + name = "copy_of_dir_with_subdir", + src = "dir_with_subdir", + out = "dir_copy", +) + +empty_directory( + name = "empty_dir", +) + +copy_directory( + name = "copy_of_empty_dir", + src = "empty_dir", + out = "empty_dir_copy", +) + +copy_directory( + name = "copy_of_dir_with_symlink", + src = "dir_with_symlink", + out = "dir_with_symlink_copy", +) + +sh_test( + name = "copy_directory_tests", + srcs = ["copy_directory_tests.sh"], + data = [ + ":copy_of_dir_with_subdir", + ":copy_of_dir_with_symlink", + ":copy_of_empty_dir", + "//tests:unittest.bash", + ], + deps = ["@bazel_tools//tools/bash/runfiles"], +) diff --git a/tests/copy_directory/copy_directory_tests.sh b/tests/copy_directory/copy_directory_tests.sh new file mode 100755 index 0000000..0448b51 --- /dev/null +++ b/tests/copy_directory/copy_directory_tests.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bash + +# Copyright 2019 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. + +# --- begin runfiles.bash initialization --- +# Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash). +set -euo pipefail +if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then + if [[ -f "$0.runfiles_manifest" ]]; then + export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest" + elif [[ -f "$0.runfiles/MANIFEST" ]]; then + export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST" + elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then + export RUNFILES_DIR="$0.runfiles" + fi +fi +if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then + source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash" +elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then + source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \ + "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)" +else + echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash" + exit 1 +fi +# --- end runfiles.bash initialization --- + +source "$(rlocation $TEST_WORKSPACE/tests/unittest.bash)" \ + || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } + +function test_copy_dir_with_subdir__copies_a() { + cat "$(rlocation $TEST_WORKSPACE/tests/copy_directory/dir_copy)/a" >"$TEST_log" + expect_log '^foo$' +} + +function test_copy_dir_with_subdir__copies_b() { + cat "$(rlocation $TEST_WORKSPACE/tests/copy_directory/dir_copy)/b" >"$TEST_log" + expect_log '^bar$' +} + +function test_copy_dir_with_subdir__copies_c() { + cat "$(rlocation $TEST_WORKSPACE/tests/copy_directory/dir_copy)/subdir/c" >"$TEST_log" + expect_log '^moocow$' +} + +function test_copy_dir_with_subdir__correct_filecounts() { + local -r dir_filecount=$(ls "$(rlocation $TEST_WORKSPACE/tests/copy_directory/dir_copy)" | wc -l) + assert_equals $dir_filecount 3 + local -r subdir_filecount=$(ls "$(rlocation $TEST_WORKSPACE/tests/copy_directory/dir_copy)/subdir" | wc -l) + assert_equals $subdir_filecount 1 +} + +function test_copy_empty_dir() { + local -r filecount=$(ls "$(rlocation $TEST_WORKSPACE/tests/copy_directory/empty_dir_copy)" | wc -l) + assert_equals $filecount 0 +} + +function test_copy_dir_with_symlink__copies_file() { + cat "$(rlocation $TEST_WORKSPACE/tests/copy_directory/dir_with_symlink_copy)/file" >"$TEST_log" + expect_log '^foo$' +} + +function test_copy_dir_with_symlink__copies_symlink() { + cat "$(rlocation $TEST_WORKSPACE/tests/copy_directory/dir_with_symlink_copy)/symlink" >"$TEST_log" + expect_log '^foo$' +} + + +run_suite "copy_directory test suite" diff --git a/tests/copy_directory/dir_with_subdir/a b/tests/copy_directory/dir_with_subdir/a new file mode 100644 index 0000000..1910281 --- /dev/null +++ b/tests/copy_directory/dir_with_subdir/a @@ -0,0 +1 @@ +foo
\ No newline at end of file diff --git a/tests/copy_directory/dir_with_subdir/b b/tests/copy_directory/dir_with_subdir/b new file mode 100644 index 0000000..ba0e162 --- /dev/null +++ b/tests/copy_directory/dir_with_subdir/b @@ -0,0 +1 @@ +bar
\ No newline at end of file diff --git a/tests/copy_directory/dir_with_subdir/subdir/c b/tests/copy_directory/dir_with_subdir/subdir/c new file mode 100644 index 0000000..062f177 --- /dev/null +++ b/tests/copy_directory/dir_with_subdir/subdir/c @@ -0,0 +1 @@ +moocow
\ No newline at end of file diff --git a/tests/copy_directory/dir_with_symlink/file b/tests/copy_directory/dir_with_symlink/file new file mode 100644 index 0000000..1910281 --- /dev/null +++ b/tests/copy_directory/dir_with_symlink/file @@ -0,0 +1 @@ +foo
\ No newline at end of file diff --git a/tests/copy_directory/dir_with_symlink/symlink b/tests/copy_directory/dir_with_symlink/symlink new file mode 120000 index 0000000..7b22242 --- /dev/null +++ b/tests/copy_directory/dir_with_symlink/symlink @@ -0,0 +1 @@ +./file
\ No newline at end of file diff --git a/tests/copy_directory/empty_directory.bzl b/tests/copy_directory/empty_directory.bzl new file mode 100644 index 0000000..92c5a5f --- /dev/null +++ b/tests/copy_directory/empty_directory.bzl @@ -0,0 +1,34 @@ +# Copyright 2022 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. + +"""Creates an empty directory.""" + +def _empty_directory_impl(ctx): + out = ctx.actions.declare_directory(ctx.attr.name) + ctx.actions.run_shell( + outputs = [out], + command = "mkdir -p $@", + arguments = [out.path], + mnemonic = "EmptyDirectory", + progress_message = "Creating empty directory %s" % out.path, + use_default_shell_env = True, + execution_requirements = {"no-remote": "1"}, # see rules/private/copy_directory_private.bzl + ) + return [DefaultInfo(files = depset(direct = [out]))] + +empty_directory = rule( + implementation = _empty_directory_impl, + provides = [DefaultInfo], + doc = "Creates an empty directory with the same name as the target", +) |