Skip to content

Commit

Permalink
Remove old_sets.bzl (#231)
Browse files Browse the repository at this point in the history
It has been deprecated for a while, the code is not really compatible with Bazel depset-related changes.
  • Loading branch information
laurentlb committed Feb 3, 2020
1 parent 4b25373 commit e583e82
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 327 deletions.
7 changes: 2 additions & 5 deletions lib/BUILD
Expand Up @@ -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",
],
)

Expand Down
147 changes: 2 additions & 145 deletions lib/old_sets.bzl
Expand Up @@ -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")
21 changes: 2 additions & 19 deletions lib/unittest.bzl
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
4 changes: 0 additions & 4 deletions tests/BUILD
Expand Up @@ -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")
Expand All @@ -26,8 +25,6 @@ collections_test_suite()

dicts_test_suite()

old_sets_test_suite()

new_sets_test_suite()

partial_test_suite()
Expand Down Expand Up @@ -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",
Expand Down
153 changes: 0 additions & 153 deletions tests/old_sets_tests.bzl

This file was deleted.

0 comments on commit e583e82

Please sign in to comment.