From e583e822a0bb99e04cd4a2b9a3baf5e1088a214d Mon Sep 17 00:00:00 2001 From: Laurent Le Brun Date: Mon, 3 Feb 2020 16:39:21 +0100 Subject: [PATCH] Remove old_sets.bzl (#231) It has been deprecated for a while, the code is not really compatible with Bazel depset-related changes. --- lib/BUILD | 7 +- lib/old_sets.bzl | 147 +------------------------------------ lib/unittest.bzl | 21 +----- tests/BUILD | 4 - tests/old_sets_tests.bzl | 153 --------------------------------------- tests/unittest_test.sh | 1 - 6 files changed, 6 insertions(+), 327 deletions(-) delete mode 100644 tests/old_sets_tests.bzl diff --git a/lib/BUILD b/lib/BUILD index d5c8983b..44acf469 100644 --- a/lib/BUILD +++ b/lib/BUILD @@ -31,17 +31,14 @@ bzl_library( bzl_library( name = "sets", - srcs = [ - "old_sets.bzl", - "sets.bzl", - ], + srcs = ["sets.bzl"], ) bzl_library( name = "new_sets", srcs = ["new_sets.bzl"], deps = [ - ":dicts", + ":dicts", ], ) diff --git a/lib/old_sets.bzl b/lib/old_sets.bzl index 2ac6c6ba..20245b29 100644 --- a/lib/old_sets.bzl +++ b/lib/old_sets.bzl @@ -12,149 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Skylib module containing common set algorithms. +"""Obsolete file, see sets.bzl instead.""" -CAUTION: Operating on sets, particularly sets contained in providers, may -asymptotically slow down the analysis phase. While constructing large sets with -addition/union is fast (there is no linear-time copy involved), the -`difference` function and various comparison predicates involve linear-time -traversals. - -For convenience, the functions in this module can take either sets or lists as -inputs; operations that take lists treat them as if they were sets (i.e., -duplicate elements are ignored). Functions that return new sets always return -them as the `set` type, regardless of the types of the inputs. -""" - -_depset_type = type(depset()) -_list_type = type([]) - -def _precondition_only_sets_or_lists(*args): - """Verifies that all arguments are either sets or lists. - - The build will fail if any of the arguments is neither a set nor a list. - - Args: - *args: A list of values that must be sets or lists. - """ - for a in args: - t = type(a) - if t not in (_depset_type, _list_type): - fail("Expected arguments to be depset or list, but found type %s: %r" % - (t, a)) - -def _depset_to_list(val): - """Converts a depset to a list. - - If the given value is a depset, will return the list representation of - the depset. Otherwise, will return the value itself. - - Args: - val: The value to be optionally converted and returned. - - Returns: - The converted value. - """ - if type(val) == _depset_type: - return val.to_list() - else: - return val - -def _is_equal(a, b): - """Returns whether two sets are equal. - - Args: - a: A depset or a list. - b: A depset or a list. - - Returns: - True if `a` is equal to `b`, False otherwise. - """ - _precondition_only_sets_or_lists(a, b) - - # Convert both values to a depset then back to a list to remove duplicates. - a = _depset_to_list(depset(a)) - b = _depset_to_list(depset(b)) - return sorted(a) == sorted(b) - -def _is_subset(a, b): - """Returns whether `a` is a subset of `b`. - - Args: - a: A depset or a list. - b: A depset or a list. - - Returns: - True if `a` is a subset of `b`, False otherwise. - """ - _precondition_only_sets_or_lists(a, b) - for e in _depset_to_list(a): - if e not in _depset_to_list(b): - return False - return True - -def _disjoint(a, b): - """Returns whether two sets are disjoint. - - Two sets are disjoint if they have no elements in common. - - Args: - a: A set or list. - b: A set or list. - - Returns: - True if `a` and `b` are disjoint, False otherwise. - """ - _precondition_only_sets_or_lists(a, b) - for e in _depset_to_list(a): - if e in _depset_to_list(b): - return False - return True - -def _intersection(a, b): - """Returns the intersection of two sets. - - Args: - a: A set or list. - b: A set or list. - - Returns: - A set containing the elements that are in both `a` and `b`. - """ - _precondition_only_sets_or_lists(a, b) - return depset([e for e in _depset_to_list(a) if e in _depset_to_list(b)]) - -def _union(*args): - """Returns the union of several sets. - - Args: - *args: An arbitrary number of sets or lists. - - Returns: - The set union of all sets or lists in `*args`. - """ - _precondition_only_sets_or_lists(*args) - args_deps = [depset(x) if type(x) == _list_type else x for x in args] - return depset(transitive = args_deps) - -def _difference(a, b): - """Returns the elements in `a` that are not in `b`. - - Args: - a: A set or list. - b: A set or list. - - Returns: - A set containing the elements that are in `a` but not in `b`. - """ - _precondition_only_sets_or_lists(a, b) - return depset([e for e in _depset_to_list(a) if e not in _depset_to_list(b)]) - -sets = struct( - difference = _difference, - disjoint = _disjoint, - intersection = _intersection, - is_equal = _is_equal, - is_subset = _is_subset, - union = _union, -) +fail("old_sets.bzl has been removed, please use sets.bzl instead") diff --git a/lib/unittest.bzl b/lib/unittest.bzl index 4506c112..648b3130 100644 --- a/lib/unittest.bzl +++ b/lib/unittest.bzl @@ -20,7 +20,6 @@ assertions used to within tests. """ load(":new_sets.bzl", new_sets = "sets") -load(":old_sets.bzl", "sets") load(":types.bzl", "types") # The following function should only be called from WORKSPACE files and workspace macros. @@ -401,24 +400,6 @@ def _assert_equals(env, expected, actual, msg = None): def _assert_set_equals(env, expected, actual, msg = None): """Asserts that the given `expected` and `actual` sets are equal. - Args: - env: The test environment returned by `unittest.begin`. - expected: The expected set resulting from some computation. - actual: The actual set returned by some computation. - msg: An optional message that will be printed that describes the failure. - If omitted, a default will be used. - """ - if type(actual) != type(depset()) or not sets.is_equal(expected, actual): - expectation_msg = "Expected %r, but got %r" % (expected, actual) - if msg: - full_msg = "%s (%s)" % (msg, expectation_msg) - else: - full_msg = expectation_msg - _fail(env, full_msg) - -def _assert_new_set_equals(env, expected, actual, msg = None): - """Asserts that the given `expected` and `actual` sets are equal. - Args: env: The test environment returned by `unittest.begin`. expected: The expected set resulting from some computation. @@ -434,6 +415,8 @@ def _assert_new_set_equals(env, expected, actual, msg = None): full_msg = expectation_msg _fail(env, full_msg) +_assert_new_set_equals = _assert_set_equals + def _expect_failure(env, expected_failure_msg = ""): """Asserts that the target under test has failed with a given error message. diff --git a/tests/BUILD b/tests/BUILD index fa159e97..4e69ecab 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -2,7 +2,6 @@ load("//:bzl_library.bzl", "bzl_library") load(":build_test_tests.bzl", "build_test_test_suite") load(":collections_tests.bzl", "collections_test_suite") load(":dicts_tests.bzl", "dicts_test_suite") -load(":old_sets_tests.bzl", "old_sets_test_suite") load(":new_sets_tests.bzl", "new_sets_test_suite") load(":partial_tests.bzl", "partial_test_suite") load(":paths_tests.bzl", "paths_test_suite") @@ -26,8 +25,6 @@ collections_test_suite() dicts_test_suite() -old_sets_test_suite() - new_sets_test_suite() partial_test_suite() @@ -61,7 +58,6 @@ sh_test( ":unittest_tests_bzl", "//lib:dicts.bzl", "//lib:new_sets.bzl", - "//lib:old_sets.bzl", "//lib:sets.bzl", "//lib:types.bzl", "//lib:unittest.bzl", diff --git a/tests/old_sets_tests.bzl b/tests/old_sets_tests.bzl deleted file mode 100644 index 638a9c34..00000000 --- a/tests/old_sets_tests.bzl +++ /dev/null @@ -1,153 +0,0 @@ -# Copyright 2017 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 old_sets.bzl.""" - -load("//lib:old_sets.bzl", "sets") -load("//lib:unittest.bzl", "asserts", "unittest") - -def _is_equal_test(ctx): - """Unit tests for sets.is_equal.""" - - # Note that if this test fails, the results for the other `sets` tests will - # be inconclusive because they use `asserts.set_equals`, which in turn calls - # `sets.is_equal`. - env = unittest.begin(ctx) - - asserts.true(env, sets.is_equal([], [])) - asserts.false(env, sets.is_equal([], [1])) - asserts.false(env, sets.is_equal([1], [])) - asserts.true(env, sets.is_equal([1], [1])) - asserts.false(env, sets.is_equal([1], [1, 2])) - asserts.false(env, sets.is_equal([1], [2])) - asserts.false(env, sets.is_equal([1], depset([1, 2]))) - - # Verify that the implementation is not using == on the sets directly. - asserts.true(env, sets.is_equal(depset([1]), depset([1]))) - - # If passing a list, verify that duplicate elements are ignored. - asserts.true(env, sets.is_equal([1, 1], [1])) - - return unittest.end(env) - -is_equal_test = unittest.make(_is_equal_test) - -def _is_subset_test(ctx): - """Unit tests for sets.is_subset.""" - env = unittest.begin(ctx) - - asserts.true(env, sets.is_subset([], [])) - asserts.true(env, sets.is_subset([], [1])) - asserts.false(env, sets.is_subset([1], [])) - asserts.true(env, sets.is_subset([1], [1])) - asserts.true(env, sets.is_subset([1], [1, 2])) - asserts.false(env, sets.is_subset([1], [2])) - asserts.true(env, sets.is_subset([1], depset([1, 2]))) - - # If passing a list, verify that duplicate elements are ignored. - asserts.true(env, sets.is_subset([1, 1], [1, 2])) - - return unittest.end(env) - -is_subset_test = unittest.make(_is_subset_test) - -def _disjoint_test(ctx): - """Unit tests for sets.disjoint.""" - env = unittest.begin(ctx) - - asserts.true(env, sets.disjoint([], [])) - asserts.true(env, sets.disjoint([], [1])) - asserts.true(env, sets.disjoint([1], [])) - asserts.false(env, sets.disjoint([1], [1])) - asserts.false(env, sets.disjoint([1], [1, 2])) - asserts.true(env, sets.disjoint([1], [2])) - asserts.true(env, sets.disjoint([1], depset([2]))) - - # If passing a list, verify that duplicate elements are ignored. - asserts.false(env, sets.disjoint([1, 1], [1, 2])) - - return unittest.end(env) - -disjoint_test = unittest.make(_disjoint_test) - -def _intersection_test(ctx): - """Unit tests for sets.intersection.""" - env = unittest.begin(ctx) - - asserts.set_equals(env, [], sets.intersection([], [])) - asserts.set_equals(env, [], sets.intersection([], [1])) - asserts.set_equals(env, [], sets.intersection([1], [])) - asserts.set_equals(env, [1], sets.intersection([1], [1])) - asserts.set_equals(env, [1], sets.intersection([1], [1, 2])) - asserts.set_equals(env, [], sets.intersection([1], [2])) - asserts.set_equals(env, [1], sets.intersection([1], depset([1]))) - - # If passing a list, verify that duplicate elements are ignored. - asserts.set_equals(env, [1], sets.intersection([1, 1], [1, 2])) - - return unittest.end(env) - -intersection_test = unittest.make(_intersection_test) - -def _union_test(ctx): - """Unit tests for sets.union.""" - env = unittest.begin(ctx) - - asserts.set_equals(env, [], sets.union()) - asserts.set_equals(env, [1], sets.union([1])) - asserts.set_equals(env, [], sets.union([], [])) - asserts.set_equals(env, [1], sets.union([], [1])) - asserts.set_equals(env, [1], sets.union([1], [])) - asserts.set_equals(env, [1], sets.union([1], [1])) - asserts.set_equals(env, [1, 2], sets.union([1], [1, 2])) - asserts.set_equals(env, [1, 2], sets.union([1], [2])) - asserts.set_equals(env, [1], sets.union([1], depset([1]))) - - # If passing a list, verify that duplicate elements are ignored. - asserts.set_equals(env, [1, 2], sets.union([1, 1], [1, 2])) - - return unittest.end(env) - -union_test = unittest.make(_union_test) - -def _difference_test(ctx): - """Unit tests for sets.difference.""" - env = unittest.begin(ctx) - - asserts.set_equals(env, [], sets.difference([], [])) - asserts.set_equals(env, [], sets.difference([], [1])) - asserts.set_equals(env, [1], sets.difference([1], [])) - asserts.set_equals(env, [], sets.difference([1], [1])) - asserts.set_equals(env, [], sets.difference([1], [1, 2])) - asserts.set_equals(env, [1], sets.difference([1], [2])) - asserts.set_equals(env, [], sets.difference([1], depset([1]))) - - # If passing a list, verify that duplicate elements are ignored. - asserts.set_equals(env, [2], sets.difference([1, 2], [1, 1])) - - return unittest.end(env) - -difference_test = unittest.make(_difference_test) - -def old_sets_test_suite(): - """Creates the test targets and test suite for old_sets.bzl tests.""" - unittest.suite( - "old_sets_tests", - disjoint_test, - intersection_test, - is_equal_test, - is_subset_test, - difference_test, - union_test, - ) diff --git a/tests/unittest_test.sh b/tests/unittest_test.sh index a74c1df5..baed4909 100755 --- a/tests/unittest_test.sh +++ b/tests/unittest_test.sh @@ -73,7 +73,6 @@ 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/old_sets.bzl)" lib/old_sets.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