diff options
Diffstat (limited to 'tests')
35 files changed, 824 insertions, 74 deletions
diff --git a/tests/BUILD b/tests/BUILD index c2a4223..7f056d2 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -1,6 +1,7 @@ load("//:bzl_library.bzl", "bzl_library") load(":build_test_tests.bzl", "build_test_test_suite") load(":collections_tests.bzl", "collections_test_suite") +load(":common_settings_tests.bzl", "common_settings_test_suite") load(":dicts_tests.bzl", "dicts_test_suite") load(":new_sets_tests.bzl", "new_sets_test_suite") load(":partial_tests.bzl", "partial_test_suite") @@ -8,6 +9,7 @@ load(":paths_tests.bzl", "paths_test_suite") load(":selects_tests.bzl", "selects_test_suite") load(":shell_tests.bzl", "shell_args_test_gen", "shell_test_suite") load(":structs_tests.bzl", "structs_test_suite") +load(":subpackages_tests.bzl", "subpackages_test_suite") load(":types_tests.bzl", "types_test_suite") load(":unittest_tests.bzl", "unittest_passing_tests_suite") load(":versions_tests.bzl", "versions_test_suite") @@ -23,6 +25,8 @@ build_test_test_suite() collections_test_suite() +common_settings_test_suite() + dicts_test_suite() new_sets_test_suite() @@ -37,6 +41,8 @@ shell_test_suite() structs_test_suite() +subpackages_test_suite() + types_test_suite() unittest_passing_tests_suite() diff --git a/tests/analysis_test_test.sh b/tests/analysis_test_test.sh index 2edae15..5d5d7de 100755 --- a/tests/analysis_test_test.sh +++ b/tests/analysis_test_test.sh @@ -41,7 +41,7 @@ else fi # --- end runfiles.bash initialization --- -source "$(rlocation bazel_skylib/tests/unittest.bash)" \ +source "$(rlocation $TEST_WORKSPACE/tests/unittest.bash)" \ || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } function create_pkg() { @@ -58,7 +58,22 @@ EOF exports_files(["*.bzl"]) EOF - ln -sf "$(rlocation bazel_skylib/rules/analysis_test.bzl)" rules/analysis_test.bzl + mkdir -p lib + cat > lib/BUILD <<EOF +exports_files(["*.bzl"]) +EOF + cat > lib/types.bzl <<EOF +_a_tuple_type = type(()) + +def _is_tuple(v): + return type(v) == _a_tuple_type + +types = struct( + is_tuple = _is_tuple, +) +EOF + + ln -sf "$(rlocation $TEST_WORKSPACE/rules/analysis_test.bzl)" rules/analysis_test.bzl mkdir -p fakerules cat > fakerules/rules.bzl <<EOF diff --git a/tests/build_test_tests.bzl b/tests/build_test_tests.bzl index 5bcbbd3..4d3dfb6 100644 --- a/tests/build_test_tests.bzl +++ b/tests/build_test_tests.bzl @@ -14,8 +14,8 @@ """Unit tests for build_test.bzl.""" -load("//rules:build_test.bzl", "build_test") load("@rules_cc//cc:defs.bzl", "cc_library") +load("//rules:build_test.bzl", "build_test") # buildifier: disable=unnamed-macro def build_test_test_suite(): diff --git a/tests/common_settings/BUILD b/tests/common_settings/BUILD new file mode 100644 index 0000000..bbf32d5 --- /dev/null +++ b/tests/common_settings/BUILD @@ -0,0 +1,25 @@ +load("//rules:common_settings.bzl", "int_flag", "string_flag") + +int_flag( + name = "my_int_flag", + build_setting_default = 42, + make_variable = "MY_INT_FLAG", +) + +string_flag( + name = "my_string_flag", + build_setting_default = "foo", + make_variable = "MY_STRING_FLAG", +) + +sh_test( + name = "make_variable_test", + srcs = ["make_variable_test.sh"], + env = { + "MESSAGE": "Hello, $(MY_STRING_FLAG)! My name is $(MY_INT_FLAG).", + }, + toolchains = [ + ":my_int_flag", + ":my_string_flag", + ], +) diff --git a/tests/common_settings/make_variable_test.sh b/tests/common_settings/make_variable_test.sh new file mode 100755 index 0000000..546c53d --- /dev/null +++ b/tests/common_settings/make_variable_test.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +# Copyright 2023 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. + +[[ "$MESSAGE" == "Hello, foo! My name is 42." ]] diff --git a/tests/common_settings_test.sh b/tests/common_settings_test.sh index 1e3d03d..51eb38e 100755 --- a/tests/common_settings_test.sh +++ b/tests/common_settings_test.sh @@ -38,7 +38,7 @@ else fi # --- end runfiles.bash initialization --- -source "$(rlocation bazel_skylib/tests/unittest.bash)" \ +source "$(rlocation $TEST_WORKSPACE/tests/unittest.bash)" \ || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } function create_volcano_pkg() { @@ -55,7 +55,7 @@ EOF exports_files(["*.bzl"]) EOF - ln -sf "$(rlocation bazel_skylib/rules/common_settings.bzl)" rules/common_settings.bzl + ln -sf "$(rlocation $TEST_WORKSPACE/rules/common_settings.bzl)" rules/common_settings.bzl mkdir -p volcano cat > volcano/rules.bzl <<EOF diff --git a/tests/common_settings_tests.bzl b/tests/common_settings_tests.bzl new file mode 100644 index 0000000..efbe0e0 --- /dev/null +++ b/tests/common_settings_tests.bzl @@ -0,0 +1,167 @@ +# Copyright 2023 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. + +"""Analysis tests for common_settings.bzl.""" + +load("//lib:unittest.bzl", "analysistest", "asserts") +load("//rules:common_settings.bzl", "int_flag", "int_setting", "string_flag", "string_setting") + +def _template_variable_info_contents_test_impl(ctx): + env = analysistest.begin(ctx) + + target_under_test = analysistest.target_under_test(env) + if ctx.attr.expected: + asserts.equals( + env, + expected = ctx.attr.expected, + actual = target_under_test[platform_common.TemplateVariableInfo].variables, + ) + else: + asserts.false(env, platform_common.TemplateVariableInfo in target_under_test) + + return analysistest.end(env) + +_template_variable_info_contents_test = analysistest.make( + _template_variable_info_contents_test_impl, + attrs = { + "expected": attr.string_dict(), + }, +) + +def _test_template_variable_info_contents(): + int_flag( + name = "my_int_flag", + build_setting_default = 42, + make_variable = "MY_INT_1", + ) + + _template_variable_info_contents_test( + name = "my_int_flag_test", + target_under_test = ":my_int_flag", + expected = { + "MY_INT_1": "42", + }, + ) + + int_setting( + name = "my_int_setting", + build_setting_default = 21, + make_variable = "MY_INT_2", + ) + + _template_variable_info_contents_test( + name = "my_int_setting_test", + target_under_test = ":my_int_setting", + expected = { + "MY_INT_2": "21", + }, + ) + + string_flag( + name = "my_string_flag", + build_setting_default = "foo", + make_variable = "MY_STRING_1", + ) + + _template_variable_info_contents_test( + name = "my_string_flag_test", + target_under_test = ":my_string_flag", + expected = { + "MY_STRING_1": "foo", + }, + ) + + string_setting( + name = "my_string_setting", + build_setting_default = "bar", + make_variable = "MY_STRING_2", + ) + + _template_variable_info_contents_test( + name = "my_string_setting_test", + target_under_test = ":my_string_setting", + expected = { + "MY_STRING_2": "bar", + }, + ) + + string_flag( + name = "my_string_flag_without_make_variable", + build_setting_default = "foo", + ) + + _template_variable_info_contents_test( + name = "my_string_flag_without_make_variable_test", + target_under_test = ":my_string_flag_without_make_variable", + expected = {}, + ) + +def _failure_test_impl(ctx): + env = analysistest.begin(ctx) + + asserts.expect_failure(env, ctx.attr.expected_failure) + + return analysistest.end(env) + +_failure_test = analysistest.make( + _failure_test_impl, + attrs = { + "expected_failure": attr.string(), + }, + expect_failure = True, +) + +def _test_make_variable_name_failures(): + int_flag( + name = "my_failing_int_flag", + build_setting_default = 42, + make_variable = "my_int_1", + tags = ["manual"], + ) + + _failure_test( + name = "my_failing_int_flag_test", + target_under_test = ":my_failing_int_flag", + expected_failure = "Error setting //tests:my_failing_int_flag: invalid make variable name 'my_int_1'. Make variable names may only contain uppercase letters, digits, and underscores.", + ) + + string_flag( + name = "my_failing_string_flag", + build_setting_default = "foo", + make_variable = "MY STRING", + tags = ["manual"], + ) + + _failure_test( + name = "my_failing_string_flag_test", + target_under_test = ":my_failing_string_flag", + expected_failure = "Error setting //tests:my_failing_string_flag: invalid make variable name 'MY STRING'. Make variable names may only contain uppercase letters, digits, and underscores.", + ) + +def common_settings_test_suite(name = "common_settings_test_suite"): + _test_template_variable_info_contents() + _test_make_variable_name_failures() + + native.test_suite( + name = "common_settings_test_suite", + tests = [ + "my_int_flag_test", + "my_int_setting_test", + "my_string_flag_test", + "my_string_setting_test", + "my_string_flag_without_make_variable_test", + "my_failing_int_flag_test", + "my_failing_string_flag_test", + ], + ) 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", +) diff --git a/tests/copy_file/copy_file_tests.sh b/tests/copy_file/copy_file_tests.sh index 737afd7..f7f14ac 100755 --- a/tests/copy_file/copy_file_tests.sh +++ b/tests/copy_file/copy_file_tests.sh @@ -37,50 +37,50 @@ else fi # --- end runfiles.bash initialization --- -source "$(rlocation bazel_skylib/tests/unittest.bash)" \ +source "$(rlocation $TEST_WORKSPACE/tests/unittest.bash)" \ || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } function test_copy_src() { - cat "$(rlocation bazel_skylib/tests/copy_file/out/a-out.txt)" >"$TEST_log" + cat "$(rlocation $TEST_WORKSPACE/tests/copy_file/out/a-out.txt)" >"$TEST_log" expect_log '^#!/usr/bin/env bash$' expect_log '^echo aaa$' } function test_copy_src_symlink() { - cat "$(rlocation bazel_skylib/tests/copy_file/out/a-out-symlink.txt)" >"$TEST_log" + cat "$(rlocation $TEST_WORKSPACE/tests/copy_file/out/a-out-symlink.txt)" >"$TEST_log" expect_log '^#!/usr/bin/env bash$' expect_log '^echo aaa$' } function test_copy_gen() { - cat "$(rlocation bazel_skylib/tests/copy_file/out/gen-out.txt)" >"$TEST_log" + cat "$(rlocation $TEST_WORKSPACE/tests/copy_file/out/gen-out.txt)" >"$TEST_log" expect_log '^#!/usr/bin/env bash$' expect_log '^echo potato$' } function test_copy_gen_symlink() { - cat "$(rlocation bazel_skylib/tests/copy_file/out/gen-out-symlink.txt)" >"$TEST_log" + cat "$(rlocation $TEST_WORKSPACE/tests/copy_file/out/gen-out-symlink.txt)" >"$TEST_log" expect_log '^#!/usr/bin/env bash$' expect_log '^echo potato$' } function test_copy_xsrc() { - cat "$(rlocation bazel_skylib/tests/copy_file/xsrc-out.txt)" >"$TEST_log" + cat "$(rlocation $TEST_WORKSPACE/tests/copy_file/xsrc-out.txt)" >"$TEST_log" expect_log '^aaa$' } function test_copy_xsrc_symlink() { - cat "$(rlocation bazel_skylib/tests/copy_file/xsrc-out-symlink.txt)" >"$TEST_log" + cat "$(rlocation $TEST_WORKSPACE/tests/copy_file/xsrc-out-symlink.txt)" >"$TEST_log" expect_log '^aaa$' } function test_copy_xgen() { - cat "$(rlocation bazel_skylib/tests/copy_file/xgen-out.txt)" >"$TEST_log" + cat "$(rlocation $TEST_WORKSPACE/tests/copy_file/xgen-out.txt)" >"$TEST_log" expect_log '^potato$' } function test_copy_xgen_symlink() { - cat "$(rlocation bazel_skylib/tests/copy_file/xgen-out-symlink.txt)" >"$TEST_log" + cat "$(rlocation $TEST_WORKSPACE/tests/copy_file/xgen-out-symlink.txt)" >"$TEST_log" expect_log '^potato$' } diff --git a/tests/dicts_tests.bzl b/tests/dicts_tests.bzl index 2fc407d..816fcea 100644 --- a/tests/dicts_tests.bzl +++ b/tests/dicts_tests.bzl @@ -83,9 +83,77 @@ def _add_test(ctx): add_test = unittest.make(_add_test) +def _omit_test(ctx): + """Unit tests for dicts.omit.""" + env = unittest.begin(ctx) + + # Test empty dict, empty list. + asserts.equals(env, {}, dicts.omit({}, [])) + + # Test empty dict, nonempty list. + asserts.equals(env, {}, dicts.omit({}, ["a"])) + + # Test nonempty dict, empty list. + asserts.equals(env, {"a": 1}, dicts.omit({"a": 1}, [])) + + # Test key in dict. + asserts.equals(env, {}, dicts.omit({"a": 1}, ["a"])) + + # Test key not in dict. + asserts.equals(env, {"a": 1}, dicts.omit({"a": 1}, ["b"])) + + # Since dictionaries are passed around by reference, make sure that the + # result of dicts.omit is always a *copy* by modifying it afterwards and + # ensuring that the original argument doesn't also reflect the change. We do + # this to protect against someone who might attempt to optimize the function + # by returning the argument itself in the empty list case. + original = {"a": 1} + result = dicts.omit(original, []) + result["a"] = 2 + asserts.equals(env, 1, original["a"]) + + return unittest.end(env) + +omit_test = unittest.make(_omit_test) + +def _pick_test(ctx): + """Unit tests for dicts.pick.""" + env = unittest.begin(ctx) + + # Test empty dict, empty list. + asserts.equals(env, {}, dicts.pick({}, [])) + + # Test empty dict, nonempty list. + asserts.equals(env, {}, dicts.pick({}, ["a"])) + + # Test nonempty dict, empty list. + asserts.equals(env, {}, dicts.pick({"a": 1}, [])) + + # Test key in dict. + asserts.equals(env, {"a": 1}, dicts.pick({"a": 1}, ["a"])) + + # Test key not in dict. + asserts.equals(env, {}, dicts.pick({"a": 1}, ["b"])) + + # Since dictionaries are passed around by reference, make sure that the + # result of dicts.pick is always a *copy* by modifying it afterwards and + # ensuring that the original argument doesn't also reflect the change. We do + # this to protect against someone who might attempt to optimize the function + # by returning the argument itself. + original = {"a": 1} + result = dicts.pick(original, ["a"]) + result["a"] = 2 + asserts.equals(env, 1, original["a"]) + + return unittest.end(env) + +pick_test = unittest.make(_pick_test) + def dicts_test_suite(): """Creates the test targets and test suite for dicts.bzl tests.""" unittest.suite( "dicts_tests", add_test, + omit_test, + pick_test, ) diff --git a/tests/diff_test/diff_test_tests.sh b/tests/diff_test/diff_test_tests.sh index 4b58e6c..9fffcc6 100755 --- a/tests/diff_test/diff_test_tests.sh +++ b/tests/diff_test/diff_test_tests.sh @@ -37,20 +37,24 @@ else fi # --- end runfiles.bash initialization --- -source "$(rlocation bazel_skylib/tests/unittest.bash)" \ +source "$(rlocation $TEST_WORKSPACE/tests/unittest.bash)" \ || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } function import_diff_test() { local -r repo="$1" mkdir -p "${repo}/rules" + mkdir -p "${repo}/lib" + touch "${repo}/lib/BUILD" touch "${repo}/WORKSPACE" - ln -sf "$(rlocation bazel_skylib/rules/diff_test.bzl)" \ + ln -sf "$(rlocation $TEST_WORKSPACE/rules/diff_test.bzl)" \ "${repo}/rules/diff_test.bzl" + ln -sf "$(rlocation $TEST_WORKSPACE/lib/shell.bzl)" \ + "${repo}/lib/shell.bzl" echo "exports_files(['diff_test.bzl'])" > "${repo}/rules/BUILD" } function assert_simple_diff_test() { - local -r flag="$1" + local -r flags="$1" local -r ws="${TEST_TMPDIR}/$2" local -r subdir="$3" @@ -76,17 +80,17 @@ eof echo bar > "$ws/$subdir/b.txt" (cd "$ws" && \ - bazel test "$flag" "//${subdir%/}:same" --test_output=errors 1>"$TEST_log" 2>&1 \ + bazel test ${flags} "//${subdir%/}:same" --test_output=errors 1>"$TEST_log" 2>&1 \ || fail "expected success") (cd "$ws" && \ - bazel test "$flag" "//${subdir%/}:different" --test_output=errors 1>"$TEST_log" 2>&1 \ + bazel test ${flags} "//${subdir%/}:different" --test_output=errors 1>"$TEST_log" 2>&1 \ && fail "expected failure" || true) expect_log "FAIL: files \"${subdir}a.txt\" and \"${subdir}b.txt\" differ" } function assert_from_ext_repo() { - local -r flag="$1" + local -r flags="$1" local -r ws="${TEST_TMPDIR}/$2" # Import the rule to an external repository. @@ -171,47 +175,97 @@ diff_test( eof (cd "$ws/main" && \ - bazel test "$flag" //:same --test_output=errors 1>"$TEST_log" 2>&1 \ + bazel test ${flags} //:same --test_output=errors 1>"$TEST_log" 2>&1 \ || fail "expected success") (cd "$ws/main" && \ - bazel test "$flag" //:different1 --test_output=errors 1>"$TEST_log" 2>&1 \ + bazel test ${flags} //:different1 --test_output=errors 1>"$TEST_log" 2>&1 \ && fail "expected failure" || true) expect_log 'FAIL: files "external/ext1/foo/foo.txt" and "external/ext2/foo/bar.txt" differ' (cd "$ws/main" && \ - bazel test "$flag" //:different2 --test_output=errors 1>"$TEST_log" 2>&1 \ + bazel test ${flags} //:different2 --test_output=errors 1>"$TEST_log" 2>&1 \ && fail "expected failure" || true) expect_log 'FAIL: files "external/ext1/foo/foo.txt" and "ext1/foo/foo.txt" differ' (cd "$ws/main" && \ - bazel test "$flag" //:different3 --test_output=errors 1>"$TEST_log" 2>&1 \ + bazel test ${flags} //:different3 --test_output=errors 1>"$TEST_log" 2>&1 \ && fail "expected failure" || true) expect_log 'FAIL: files "ext2/foo/foo.txt" and "external/ext2/foo/foo.txt" differ' } function test_simple_diff_test_with_legacy_external_runfiles() { - assert_simple_diff_test "--legacy_external_runfiles" "${FUNCNAME[0]}" "" + assert_simple_diff_test "--enable_runfiles --legacy_external_runfiles" "${FUNCNAME[0]}" "" } function test_simple_diff_test_without_legacy_external_runfiles() { - assert_simple_diff_test "--nolegacy_external_runfiles" "${FUNCNAME[0]}" "" + assert_simple_diff_test "--enable_runfiles --nolegacy_external_runfiles" "${FUNCNAME[0]}" "" +} + +function test_simple_diff_test_with_manifest() { + assert_simple_diff_test "--noenable_runfiles" "${FUNCNAME[0]}" "" } function test_directory_named_external_with_legacy_external_runfiles() { - assert_simple_diff_test "--legacy_external_runfiles" "${FUNCNAME[0]}" "path/to/direcotry/external/in/name/" + assert_simple_diff_test "--enable_runfiles --legacy_external_runfiles" "${FUNCNAME[0]}" "path/to/directory/external/in/name/" } function test_directory_named_external_without_legacy_external_runfiles() { - assert_simple_diff_test "--nolegacy_external_runfiles" "${FUNCNAME[0]}" "path/to/direcotry/external/in/name/" + assert_simple_diff_test "--enable_runfiles --nolegacy_external_runfiles" "${FUNCNAME[0]}" "path/to/directory/external/in/name/" +} + +function test_directory_named_external_with_manifest() { + assert_simple_diff_test "--noenable_runfiles" "${FUNCNAME[0]}" "path/to/directory/external/in/name/" } function test_from_ext_repo_with_legacy_external_runfiles() { - assert_from_ext_repo "--legacy_external_runfiles" "${FUNCNAME[0]}" + assert_from_ext_repo "--enable_runfiles --legacy_external_runfiles" "${FUNCNAME[0]}" } function test_from_ext_repo_without_legacy_external_runfiles() { - assert_from_ext_repo "--nolegacy_external_runfiles" "${FUNCNAME[0]}" + assert_from_ext_repo "--enable_runfiles --nolegacy_external_runfiles" "${FUNCNAME[0]}" +} + +function test_from_ext_repo_with_manifest() { + assert_from_ext_repo "--noenable_runfiles" "${FUNCNAME[0]}" +} + +function test_failure_message() { + local -r ws="${TEST_TMPDIR}/${FUNCNAME[0]}" + + import_diff_test "$ws" + touch "$ws/WORKSPACE" + cat >"$ws/BUILD" <<'eof' +load("//rules:diff_test.bzl", "diff_test") + +diff_test( + name = "different_with_message", + failure_message = "This is an `$error`", # TODO(arostovtsev): also test Windows cmd.exe escapes when https://github.com/bazelbuild/bazel-skylib/pull/363 is merged + file1 = "a.txt", + file2 = "b.txt", +) + +diff_test( + name = "different_without_message", + file1 = "c.txt", + file2 = "d.txt", +) +eof + echo foo > "$ws/a.txt" + echo bar > "$ws/b.txt" + echo foo > "$ws/c.txt" + echo bar > "$ws/d.txt" + + (cd "$ws" && \ + bazel test //:different_with_message --test_output=errors 1>"$TEST_log" 2>&1 \ + && fail "expected failure" || true) + # TODO(arostovtsev): also test Windows cmd.exe escapes when https://github.com/bazelbuild/bazel-skylib/pull/363 is merged + expect_log "FAIL: files \"a.txt\" and \"b.txt\" differ. This is an \`\$error\`" + + (cd "$ws" && \ + bazel test //:different_without_message --test_output=errors 1>"$TEST_log" 2>&1 \ + && fail "expected failure" || true) + expect_log "FAIL: files \"c.txt\" and \"d.txt\" differ. $" } cd "$TEST_TMPDIR" diff --git a/tests/expand_template/BUILD b/tests/expand_template/BUILD new file mode 100644 index 0000000..0607111 --- /dev/null +++ b/tests/expand_template/BUILD @@ -0,0 +1,57 @@ +# 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. + +# This package aids testing the 'diff_test' rule. + +load("//rules:expand_template.bzl", "expand_template") + +expand_template( + name = "filled_template", + out = "foo/test.yaml", + substitutions = { + "@name@": "test", + "@version@": "1.1.1", + }, + template = "test.tpl.yaml", +) + +sh_test( + name = "template_test", + srcs = ["template_test.sh"], + data = [ + "foo/test.yaml", + ":filled_template", + "//tests:unittest.bash", + ], + deps = [ + "@bazel_tools//tools/bash/runfiles", + ], +) + +expand_template( + name = "version", + out = "version.h", + substitutions = { + "@VERSION@": "2.3.4", + }, + template = "version.h.in", +) + +cc_test( + name = "test", + srcs = [ + "test.cc", + ":version", + ], +) diff --git a/tests/expand_template/template_test.sh b/tests/expand_template/template_test.sh new file mode 100755 index 0000000..10099a4 --- /dev/null +++ b/tests/expand_template/template_test.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash + +# 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. + +# --- begin runfiles.bash initialization v2 --- +# Copy-pasted from the Bazel Bash runfiles library v2. +set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash +source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \ + source "$0.runfiles/$f" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ + { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e +# --- end runfiles.bash initialization v2 --- + +source "$(rlocation $TEST_WORKSPACE/tests/unittest.bash)" \ + || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } + +function test_expand_template() { + cat "$(rlocation $TEST_WORKSPACE/tests/expand_template/foo/test.yaml)" >"$TEST_log" + expect_log 'name: test' + expect_log 'version: 1.1.1' +} + +run_suite "expand_template_tests test suite"
\ No newline at end of file diff --git a/tests/expand_template/test.cc b/tests/expand_template/test.cc new file mode 100644 index 0000000..3b13e7a --- /dev/null +++ b/tests/expand_template/test.cc @@ -0,0 +1,27 @@ +// 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. + +#include "tests/expand_template/version.h" + +#include <cstring> + +int main(int argc, char **argv) { + // VERSION should be "2.3.4" + if(strcmp(VERSION, "2.3.4") == 0) { + return 0; // success + } + else { + return 1; // failure + } +} diff --git a/tests/expand_template/test.tpl.yaml b/tests/expand_template/test.tpl.yaml new file mode 100644 index 0000000..03d9fdd --- /dev/null +++ b/tests/expand_template/test.tpl.yaml @@ -0,0 +1,2 @@ +name: @name@ +version: @version@ diff --git a/tests/expand_template/version.h.in b/tests/expand_template/version.h.in new file mode 100644 index 0000000..deb8418 --- /dev/null +++ b/tests/expand_template/version.h.in @@ -0,0 +1 @@ +#define VERSION "@VERSION@"
\ No newline at end of file diff --git a/tests/native_binary/BUILD b/tests/native_binary/BUILD index 852d607..997270f 100644 --- a/tests/native_binary/BUILD +++ b/tests/native_binary/BUILD @@ -1,6 +1,6 @@ +load("@rules_cc//cc:defs.bzl", "cc_binary") load("//rules:copy_file.bzl", "copy_file") load("//rules:native_binary.bzl", "native_binary", "native_test") -load("@rules_cc//cc:defs.bzl", "cc_binary") package( default_testonly = 1, @@ -21,6 +21,15 @@ cc_binary( # rule. ) +cc_binary( + name = "assertdata_with_runfiles", + srcs = ["assertdata.cc"], + # This version depends on runfiles directly, to ensure runfiles from the + # binary are picked up by native_test/native_binary + data = ["testdata.txt"], + deps = ["@bazel_tools//tools/cpp/runfiles"], +) + # A rule that copies "assertarg"'s output as an opaque executable, simulating a # binary that's not built from source and needs to be wrapped in native_binary. copy_file( @@ -51,11 +60,7 @@ _ARGS = [ "$(location testdata.txt) $$(location testdata.txt) $(location testdata.txt)", "'$(location testdata.txt) $$(location testdata.txt) $(location testdata.txt)'", "$$TEST_SRCDIR", - - # TODO(laszlocsomor): uncomment this (and its counterpart in assertarg.cc) - # after https://github.com/bazelbuild/bazel/issues/6622 is resolved and the - # Bash-less test wrapper is the default on Windows. - # "$${TEST_SRCDIR}", + "$${TEST_SRCDIR}", ] native_binary( @@ -101,3 +106,12 @@ native_test( out = "data_test.exe", data = ["testdata.txt"], ) + +native_test( + name = "data_from_binary_test", + src = ":assertdata_with_runfiles", + # On Windows we need the ".exe" extension. + # On other platforms the extension doesn't matter. + # Therefore we can use ".exe" on every platform. + out = "data_from_binary_test.exe", +) diff --git a/tests/native_binary/assertarg.cc b/tests/native_binary/assertarg.cc index 02cb0fd..6506635 100644 --- a/tests/native_binary/assertarg.cc +++ b/tests/native_binary/assertarg.cc @@ -25,12 +25,7 @@ int main(int argc, char** argv) { "tests/native_binary/testdata.txt", "tests/native_binary/testdata.txt $(location testdata.txt) tests/native_binary/testdata.txt", "$TEST_SRCDIR", - - // TODO(laszlocsomor): uncomment this (and its counterpart in the BUILD - // file) after https://github.com/bazelbuild/bazel/issues/6622 is resolved - // and the Bash-less test wrapper is the default on Windows. - // "${TEST_SRCDIR}", - + "${TEST_SRCDIR}", "", }; diff --git a/tests/native_binary/assertdata.cc b/tests/native_binary/assertdata.cc index 111aaa2..6ae42e1 100644 --- a/tests/native_binary/assertdata.cc +++ b/tests/native_binary/assertdata.cc @@ -21,7 +21,7 @@ using bazel::tools::cpp::runfiles::Runfiles; -int main(int argc, char** argv) { +int main(int argc, char **argv) { std::string error; std::unique_ptr<Runfiles> runfiles(Runfiles::Create(argv[0], &error)); if (runfiles == nullptr) { @@ -30,17 +30,23 @@ int main(int argc, char** argv) { return 1; } - // Even though this cc_binary rule has no data-dependencies, the - // native_binary that wraps a copy of this binary does, so we have some - // runfiles. - std::string path = - runfiles->Rlocation("bazel_skylib/tests/native_binary/testdata.txt"); - if (path.empty()) { - fprintf(stderr, "ERROR(" __FILE__ ":%d): Could not find runfile\n", + char* workspace = getenv("TEST_WORKSPACE"); + if (workspace == nullptr) { + fprintf(stderr, "ERROR(" __FILE__ ":%d): envvar TEST_WORKSPACE is undefined\n", __LINE__); + return 1; + } + + // This should have runfiles, either from the binary itself or from the + // native_test. + std::string path = + runfiles->Rlocation(std::string(workspace) + "/tests/native_binary/testdata.txt"); + FILE *f = fopen(path.c_str(), "rt"); + if (!f) { + fprintf(stderr, "ERROR(" __FILE__ ":%d): Could not find runfile '%s'\n", + __LINE__, path.c_str()); } - FILE* f = fopen(path.c_str(), "rt"); char buf[6]; size_t s = fread(buf, 1, 5, f); fclose(f); @@ -53,4 +59,3 @@ int main(int argc, char** argv) { return 0; } - diff --git a/tests/run_binary/BUILD b/tests/run_binary/BUILD index 85c10f3..259c84a 100644 --- a/tests/run_binary/BUILD +++ b/tests/run_binary/BUILD @@ -1,7 +1,7 @@ +load("@rules_cc//cc:defs.bzl", "cc_binary") load("//rules:diff_test.bzl", "diff_test") load("//rules:run_binary.bzl", "run_binary") load("//rules:write_file.bzl", "write_file") -load("@rules_cc//cc:defs.bzl", "cc_binary") package( default_testonly = 1, diff --git a/tests/select_file/BUILD b/tests/select_file/BUILD index a498a1e..a91c231 100644 --- a/tests/select_file/BUILD +++ b/tests/select_file/BUILD @@ -1,5 +1,5 @@ -load("//rules:select_file.bzl", "select_file") load("//rules:diff_test.bzl", "diff_test") +load("//rules:select_file.bzl", "select_file") licenses(["notice"]) diff --git a/tests/selects_tests.bzl b/tests/selects_tests.bzl index a7697f2..8227bf8 100644 --- a/tests/selects_tests.bzl +++ b/tests/selects_tests.bzl @@ -261,7 +261,7 @@ and_config_setting_group_multiple_matches_fail_test = analysistest.make( ) def _and_config_setting_group_multiple_matches_fail_test(): - """Test verifying multple conditions mismatch on an ANDing config_setting_group.""" + """Test verifying multiple conditions mismatch on an ANDing config_setting_group.""" boolean_attr_rule( name = "and_config_setting_group_multiple_matches_fail_rule", myboolean = select( @@ -453,7 +453,7 @@ or_config_setting_group_multiple_conds_match_test = analysistest.make( ) def _or_config_setting_group_multiple_conds_match_test(): - """Test verifying multple conditions matching on an ORing config_setting_group.""" + """Test verifying multiple conditions matching on an ORing config_setting_group.""" boolean_attr_rule( name = "or_config_setting_group_multiple_conds_match_rule", myboolean = select( diff --git a/tests/subpackages_tests.bzl b/tests/subpackages_tests.bzl new file mode 100644 index 0000000..3c494d6 --- /dev/null +++ b/tests/subpackages_tests.bzl @@ -0,0 +1,88 @@ +# 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. + +"""Unit tests for subpackages.bzl.""" + +load("//lib:subpackages.bzl", "subpackages") +load("//lib:unittest.bzl", "loadingtest") + +def _all_test(env): + """Unit tests for subpackages.all.""" + + all_pkgs = [ + "common_settings", + "copy_directory", + "copy_file", + "diff_test", + "expand_template", + "select_file", + "write_file", + ] + + # Not all pkgs exist in all test environments. + if subpackages.exists("run_binary"): + all_pkgs.append("run_binary") + + if subpackages.exists("native_binary"): + all_pkgs.append("native_binary") + + # These exist in all cases + filtered_pkgs = [ + "common_settings", + "copy_directory", + "copy_file", + "expand_template", + "select_file", + "write_file", + ] + + # subpackages is always in sorted order: + all_pkgs = sorted(all_pkgs) + + # test defaults + loadingtest.equals( + env, + "all", + ["//tests/" + pkg for pkg in all_pkgs], + subpackages.all(), + ) + + # test non-fully-qualified output + loadingtest.equals( + env, + "all_not_fully_qualified", + all_pkgs, + subpackages.all(fully_qualified = False), + ) + + # test exclusion + loadingtest.equals( + env, + "all_w_exclude", + filtered_pkgs, + subpackages.all(exclude = ["diff_test", "run_binary", "native_binary"], fully_qualified = False), + ) + +def _exists_test(env): + """Unit tests for subpackages.exists.""" + loadingtest.equals(env, "exists_yes", True, subpackages.exists("copy_file")) + loadingtest.equals(env, "exists_no", False, subpackages.exists("never_existed")) + +def subpackages_test_suite(): + """Creates the test targets and test suite for subpackages.bzl tests.""" + + if subpackages.supported(): + env = loadingtest.make("subpackages") + _all_test(env) + _exists_test(env) diff --git a/tests/types_tests.bzl b/tests/types_tests.bzl index aaf7cab..a3c654b 100644 --- a/tests/types_tests.bzl +++ b/tests/types_tests.bzl @@ -13,9 +13,9 @@ # limitations under the License. """Unit tests for types.bzl.""" +load("//lib:new_sets.bzl", "sets") load("//lib:types.bzl", "types") load("//lib:unittest.bzl", "asserts", "unittest") -load("//lib:new_sets.bzl", "sets") def _a_function(): """A dummy function for testing.""" diff --git a/tests/unittest.bash b/tests/unittest.bash index a43678d..9a9a7a5 100755 --- a/tests/unittest.bash +++ b/tests/unittest.bash @@ -238,7 +238,7 @@ capture_test_stderr () { # Force XML_OUTPUT_FILE to an existing path if [ -z "${XML_OUTPUT_FILE:-}" ]; then - XML_OUTPUT_FILE=${TEST_TMPDIR}/ouput.xml + XML_OUTPUT_FILE=${TEST_TMPDIR}/output.xml fi #### Global variables: @@ -674,8 +674,12 @@ if [ "$UNAME" = "linux" ] || [[ "$UNAME" =~ msys_nt* ]]; then } else function timestamp() { - # OS X and FreeBSD do not have %N so python is the best we can do - python -c 'import time; print int(round(time.time() * 1000))' + # macOS and BSDs do not have %N, so Python is the best we can do. + # LC_ALL=C works around python 3.8 and 3.9 crash on macOS when the + # filesystem encoding is unspecified (e.g. when LANG=en_US). + local PYTHON=python + command -v python3 &> /dev/null && PYTHON=python3 + LC_ALL=C "${PYTHON}" -c 'import time; print(int(round(time.time() * 1000)))' } fi diff --git a/tests/unittest_test.sh b/tests/unittest_test.sh index 4795b7e..c773d70 100755 --- a/tests/unittest_test.sh +++ b/tests/unittest_test.sh @@ -42,7 +42,7 @@ else fi # --- end runfiles.bash initialization --- -source "$(rlocation bazel_skylib/tests/unittest.bash)" \ +source "$(rlocation $TEST_WORKSPACE/tests/unittest.bash)" \ || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } function create_pkg() { @@ -64,22 +64,22 @@ EOF cat > tests/BUILD <<EOF exports_files(["*.bzl"]) EOF - ln -sf "$(rlocation bazel_skylib/tests/unittest_tests.bzl)" tests/unittest_tests.bzl + ln -sf "$(rlocation $TEST_WORKSPACE/tests/unittest_tests.bzl)" tests/unittest_tests.bzl mkdir -p lib touch lib/BUILD cat > lib/BUILD <<EOF exports_files(["*.bzl"]) EOF - ln -sf "$(rlocation bazel_skylib/lib/dicts.bzl)" lib/dicts.bzl - ln -sf "$(rlocation bazel_skylib/lib/new_sets.bzl)" lib/new_sets.bzl - ln -sf "$(rlocation bazel_skylib/lib/partial.bzl)" lib/partial.bzl - ln -sf "$(rlocation bazel_skylib/lib/sets.bzl)" lib/sets.bzl - ln -sf "$(rlocation bazel_skylib/lib/types.bzl)" lib/types.bzl - ln -sf "$(rlocation bazel_skylib/lib/unittest.bzl)" lib/unittest.bzl + ln -sf "$(rlocation $TEST_WORKSPACE/lib/dicts.bzl)" lib/dicts.bzl + ln -sf "$(rlocation $TEST_WORKSPACE/lib/new_sets.bzl)" lib/new_sets.bzl + ln -sf "$(rlocation $TEST_WORKSPACE/lib/partial.bzl)" lib/partial.bzl + ln -sf "$(rlocation $TEST_WORKSPACE/lib/sets.bzl)" lib/sets.bzl + ln -sf "$(rlocation $TEST_WORKSPACE/lib/types.bzl)" lib/types.bzl + ln -sf "$(rlocation $TEST_WORKSPACE/lib/unittest.bzl)" lib/unittest.bzl mkdir -p toolchains/unittest - ln -sf "$(rlocation bazel_skylib/toolchains/unittest/BUILD)" toolchains/unittest/BUILD + ln -sf "$(rlocation $TEST_WORKSPACE/toolchains/unittest/BUILD)" toolchains/unittest/BUILD # Create test files. mkdir -p testdir diff --git a/tests/versions_tests.bzl b/tests/versions_tests.bzl index 422cb41..a2e2308 100644 --- a/tests/versions_tests.bzl +++ b/tests/versions_tests.bzl @@ -26,6 +26,9 @@ def _parse_test(ctx): asserts.equals(env, (0, 4, 0), versions.parse("0.4.0")) asserts.equals(env, (0, 4, 0), versions.parse("0.4.0rc")) + # Verify that this doesn't fail - it corresponds to a dev build of Bazel. + versions.parse("") + return unittest.end(env) def _version_comparison_test(ctx): @@ -36,11 +39,13 @@ def _version_comparison_test(ctx): asserts.true(env, versions.is_at_least("0.9.0", "0.10.0rc2")) asserts.true(env, versions.is_at_least("0.9.0", "0.9.0rc3")) asserts.true(env, versions.is_at_least("0.9.0", "1.2.3")) + asserts.true(env, versions.is_at_least("0.9.0", "")) asserts.false(env, versions.is_at_most("0.4.0 123abcd", "0.10.0rc1 abcd123")) asserts.true(env, versions.is_at_most("0.4.0", "0.3.0rc2")) asserts.true(env, versions.is_at_most("0.4.0", "0.4.0rc3")) asserts.true(env, versions.is_at_most("1.4.0", "0.4.0rc3")) + asserts.false(env, versions.is_at_most("1.4.0", "")) return unittest.end(env) diff --git a/tests/write_file/write_file_tests.sh b/tests/write_file/write_file_tests.sh index e7039d0..c146948 100755 --- a/tests/write_file/write_file_tests.sh +++ b/tests/write_file/write_file_tests.sh @@ -37,7 +37,7 @@ else fi # --- end runfiles.bash initialization --- -source "$(rlocation bazel_skylib/tests/unittest.bash)" \ +source "$(rlocation $TEST_WORKSPACE/tests/unittest.bash)" \ || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } function assert_empty_file() { @@ -47,21 +47,21 @@ function assert_empty_file() { } function test_write_empty_text() { - assert_empty_file "$(rlocation bazel_skylib/tests/write_file/out/empty.txt)" + assert_empty_file "$(rlocation $TEST_WORKSPACE/tests/write_file/out/empty.txt)" } function test_write_nonempty_text() { - cat "$(rlocation bazel_skylib/tests/write_file/out/nonempty.txt)" >"$TEST_log" + cat "$(rlocation $TEST_WORKSPACE/tests/write_file/out/nonempty.txt)" >"$TEST_log" expect_log '^aaa' expect_log '^bbb' } function test_write_empty_bin() { - assert_empty_file "$(rlocation bazel_skylib/tests/write_file/empty-bin-out.txt)" + assert_empty_file "$(rlocation $TEST_WORKSPACE/tests/write_file/empty-bin-out.txt)" } function test_write_nonempty_bin() { - cat "$(rlocation bazel_skylib/tests/write_file/nonempty-bin-out.txt)" >"$TEST_log" + cat "$(rlocation $TEST_WORKSPACE/tests/write_file/nonempty-bin-out.txt)" >"$TEST_log" expect_log '^potato' } |