Reformat .bzl files with buildifier and add format check.

Buildifier 0.12.0 includes initial support for reformatting .bzl files.
- Reformat all the bzl files.
- Expand the travis check to check the .bzl files also.
This commit is contained in:
Thomas Van Lenten 2018-06-12 13:09:57 -04:00
parent a5431b7bab
commit e5203c0f5d
26 changed files with 1727 additions and 1770 deletions

View File

@ -26,9 +26,14 @@ fi
# Asked to do a buildifier run.
if [[ -n "${BUILDIFER:-}" ]]; then
# bazelbuild/buildtools/issues/220 - diff doesn't include the file that needs updating
if ! find . -name BUILD -print | xargs buildifier -d > /dev/null 2>&1 ; then
echo "ERROR: BUILD file formatting issue(s)"
find . -name BUILD -print -exec buildifier -v -d {} \;
if ! find . \( -name BUILD -o -name "*.bzl" \) -print | xargs buildifier -d > /dev/null 2>&1 ; then
echo "ERROR: BUILD/.bzl file formatting issue(s):"
echo ""
find . \( -name BUILD -o -name "*.bzl" \) -print -exec buildifier -v -d {} \;
echo ""
echo "Please download the latest buildifier"
echo " https://github.com/bazelbuild/buildtools/releases"
echo "and run it over the changed BUILD/.bzl files."
exit 1
fi
fi

View File

@ -30,7 +30,6 @@ load("//lib:versions.bzl", _versions="versions")
# the assert functions, while keeping them in the same .bzl file.
load("//lib:unittest.bzl", _asserts = "asserts", _unittest = "unittest")
collections = _collections
dicts = _dicts
new_sets = _new_sets

View File

@ -14,7 +14,6 @@
"""Skylib module containing functions that operate on collections."""
def _after_each(separator, iterable):
"""Inserts `separator` after each item in `iterable`.
@ -32,7 +31,6 @@ def _after_each(separator, iterable):
return result
def _before_each(separator, iterable):
"""Inserts `separator` before each item in `iterable`.
@ -50,7 +48,6 @@ def _before_each(separator, iterable):
return result
def _uniq(iterable):
"""Returns a list of unique elements in `iterable`.
@ -65,7 +62,6 @@ def _uniq(iterable):
unique_elements = {element: None for element in iterable}
return unique_elements.keys()
collections = struct(
after_each = _after_each,
before_each = _before_each,

View File

@ -14,7 +14,6 @@
"""Skylib module containing functions that operate on dictionaries."""
def _add(*dictionaries):
"""Returns a new `dict` that has all the entries of the given dictionaries.
@ -37,7 +36,6 @@ def _add(*dictionaries):
result.update(d)
return result
dicts = struct(
add = _add,
)

View File

@ -22,7 +22,6 @@
load(":dicts.bzl", "dicts")
def _make(elements = None):
"""Creates a new set.
@ -37,7 +36,6 @@ def _make(elements=None):
elements = elements if elements else []
return struct(_values = {e: None for e in elements})
def _copy(s):
"""Creates a new set from another set.
@ -49,7 +47,6 @@ def _copy(s):
"""
return struct(_values = dict(s._values))
def _to_list(s):
"""Creates a list from the values in the set.
@ -61,7 +58,6 @@ def _to_list(s):
"""
return s._values.keys()
def _insert(s, e):
"""Inserts an element into the set.
@ -77,7 +73,6 @@ def _insert(s, e):
s._values[e] = None
return s
def _remove(s, e):
"""Removes an element from the set.
@ -93,7 +88,6 @@ def _remove(s, e):
s._values.pop(e)
return s
def _contains(a, e):
"""Checks for the existence of an element in a set.
@ -106,7 +100,6 @@ def _contains(a, e):
"""
return e in a._values
def _get_shorter_and_longer(a, b):
"""Returns two sets in the order of shortest and longest.
@ -121,7 +114,6 @@ def _get_shorter_and_longer(a, b):
return a, b
return b, a
def _is_equal(a, b):
"""Returns whether two sets are equal.
@ -134,7 +126,6 @@ def _is_equal(a, b):
"""
return a._values == b._values
def _is_subset(a, b):
"""Returns whether `a` is a subset of `b`.
@ -150,7 +141,6 @@ def _is_subset(a, b):
return False
return True
def _disjoint(a, b):
"""Returns whether two sets are disjoint.
@ -169,7 +159,6 @@ def _disjoint(a, b):
return False
return True
def _intersection(a, b):
"""Returns the intersection of two sets.
@ -183,7 +172,6 @@ def _intersection(a, b):
shorter, longer = _get_shorter_and_longer(a, b)
return struct(_values = {e: None for e in shorter._values.keys() if e in longer._values})
def _union(*args):
"""Returns the union of several sets.
@ -195,7 +183,6 @@ def _union(*args):
"""
return struct(_values = dicts.add(*[s._values for s in args]))
def _difference(a, b):
"""Returns the elements in `a` that are not in `b`.
@ -208,7 +195,6 @@ def _difference(a, b):
"""
return struct(_values = {e: None for e in a._values.keys() if e not in b._values})
def _length(s):
"""Returns the number of elements in a set.
@ -231,7 +217,6 @@ def _repr(s):
"""
return repr(s._values.keys())
sets = struct(
make = _make,
copy = _copy,

View File

@ -19,7 +19,6 @@ path separators (forward slash, "/"); they do not handle Windows-style paths
with backslash separators or drive letters.
"""
def _basename(p):
"""Returns the basename (i.e., the file portion) of a path.
@ -36,7 +35,6 @@ def _basename(p):
"""
return p.rpartition("/")[-1]
def _dirname(p):
"""Returns the dirname of a path.
@ -58,7 +56,6 @@ def _dirname(p):
# os.path.dirname does.
return prefix.rstrip("/")
def _is_absolute(path):
"""Returns `True` if `path` is an absolute path.
@ -70,7 +67,6 @@ def _is_absolute(path):
"""
return path.startswith("/") or (len(path) > 2 and path[1] == ":")
def _join(path, *others):
"""Joins one or more path components intelligently.
@ -101,7 +97,6 @@ def _join(path, *others):
return result
def _normalize(path):
"""Normalizes a path, eliminating double slashes and other redundant segments.
@ -158,7 +153,6 @@ def _normalize(path):
return path or "."
def _relativize(path, start):
"""Returns the portion of `path` that is relative to `start`.
@ -195,7 +189,6 @@ def _relativize(path, start):
result_segments = segments[-length:]
return "/".join(result_segments)
def _replace_extension(p, new_extension):
"""Replaces the extension of the file at the end of a path.
@ -211,7 +204,6 @@ def _replace_extension(p, new_extension):
"""
return _split_extension(p)[0] + new_extension
def _split_extension(p):
"""Splits the path `p` into a tuple containing the root and extension.
@ -238,7 +230,6 @@ def _split_extension(p):
dot_distance_from_end = len(b) - last_dot_in_basename
return (p[:-dot_distance_from_end], p[-dot_distance_from_end:])
paths = struct(
basename = _basename,
dirname = _dirname,

View File

@ -14,7 +14,7 @@
"""Skylib module containing convenience interfaces for select()."""
def _with_or(input_dict, no_match_error=''):
def _with_or(input_dict, no_match_error = ""):
"""Drop-in replacement for `select()` that supports ORed keys.
Args:
@ -51,7 +51,6 @@ def _with_or(input_dict, no_match_error=''):
"""
return select(_with_or_dict(input_dict), no_match_error = no_match_error)
def _with_or_dict(input_dict):
"""Variation of `with_or` that returns the dict of the `select()`.
@ -77,8 +76,7 @@ def _with_or_dict(input_dict):
output_dict[key] = value
return output_dict
selects = struct(
with_or = _with_or,
with_or_dict=_with_or_dict
with_or_dict = _with_or_dict,
)

View File

@ -26,7 +26,6 @@ duplicate elements are ignored). Functions that return new sets always return
them as the `set` type, regardless of the types of the inputs.
"""
def _precondition_only_sets_or_lists(*args):
"""Verifies that all arguments are either sets or lists.
@ -41,7 +40,6 @@ def _precondition_only_sets_or_lists(*args):
fail("Expected arguments to be depset or list, but found type %s: %r" %
(t, a))
def _is_equal(a, b):
"""Returns whether two sets are equal.
@ -55,7 +53,6 @@ def _is_equal(a, b):
_precondition_only_sets_or_lists(a, b)
return sorted(depset(a)) == sorted(depset(b))
def _is_subset(a, b):
"""Returns whether `a` is a subset of `b`.
@ -72,7 +69,6 @@ def _is_subset(a, b):
return False
return True
def _disjoint(a, b):
"""Returns whether two sets are disjoint.
@ -91,7 +87,6 @@ def _disjoint(a, b):
return False
return True
def _intersection(a, b):
"""Returns the intersection of two sets.
@ -105,7 +100,6 @@ def _intersection(a, b):
_precondition_only_sets_or_lists(a, b)
return depset([e for e in a if e in b])
def _union(*args):
"""Returns the union of several sets.
@ -119,7 +113,6 @@ def _union(*args):
args_deps = [depset(x) if type(x) == 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`.
@ -133,7 +126,6 @@ def _difference(a, b):
_precondition_only_sets_or_lists(a, b)
return depset([e for e in a if e not in b])
sets = struct(
difference = _difference,
disjoint = _disjoint,

View File

@ -14,7 +14,6 @@
"""Skylib module containing shell utility functions."""
def _array_literal(iterable):
"""Creates a string from a sequence that can be used as a shell array.
@ -35,7 +34,6 @@ def _array_literal(iterable):
"""
return "(" + " ".join([_quote(str(i)) for i in iterable]) + ")"
def _quote(s):
"""Quotes the given string for use in a shell command.
@ -50,7 +48,6 @@ def _quote(s):
"""
return "'" + s.replace("'", "'\\''") + "'"
shell = struct(
array_literal = _array_literal,
quote = _quote,

View File

@ -14,7 +14,6 @@
"""Skylib module containing functions that operate on structs."""
def _to_dict(s):
"""Converts a `struct` to a `dict`.
@ -31,7 +30,6 @@ def _to_dict(s):
attributes.remove("to_proto")
return {key: getattr(s, key) for key in attributes}
structs = struct(
to_dict = _to_dict,
)

View File

@ -13,7 +13,6 @@
# limitations under the License.
"""Skylib module containing functions checking types."""
# create instance singletons to avoid unnecessary allocations
_a_bool = True
_a_dict = {}
@ -22,11 +21,9 @@ _a_string = ""
_a_tuple = ()
_an_int = 1
def _a_function():
pass
def _is_list(v):
"""Returns True if v is an instance of a list.
@ -38,7 +35,6 @@ def _is_list(v):
"""
return type(v) == type(_a_list)
def _is_string(v):
"""Returns True if v is an instance of a string.
@ -50,7 +46,6 @@ def _is_string(v):
"""
return type(v) == type(_a_string)
def _is_bool(v):
"""Returns True if v is an instance of a bool.
@ -62,7 +57,6 @@ def _is_bool(v):
"""
return type(v) == type(_a_bool)
def _is_none(v):
"""Returns True if v has the type of None.
@ -74,7 +68,6 @@ def _is_none(v):
"""
return type(v) == type(None)
def _is_int(v):
"""Returns True if v is an instance of a signed integer.
@ -86,7 +79,6 @@ def _is_int(v):
"""
return type(v) == type(_an_int)
def _is_tuple(v):
"""Returns True if v is an instance of a tuple.
@ -98,7 +90,6 @@ def _is_tuple(v):
"""
return type(v) == type(_a_tuple)
def _is_dict(v):
"""Returns True if v is an instance of a dict.
@ -110,7 +101,6 @@ def _is_dict(v):
"""
return type(v) == type(_a_dict)
def _is_function(v):
"""Returns True if v is an instance of a function.
@ -122,7 +112,6 @@ def _is_function(v):
"""
return type(v) == type(_a_function)
types = struct(
is_list = _is_list,
is_string = _is_string,

View File

@ -22,7 +22,6 @@ assertions used to within tests.
load(":sets.bzl", "sets")
load(":new_sets.bzl", new_sets = "sets")
def _make(impl, attrs = None):
"""Creates a unit test rule from its implementation function.
@ -78,7 +77,6 @@ def _make(impl, attrs=None):
test = True,
)
def _suite(name, *test_rules):
"""Defines a `test_suite` target that contains multiple tests.
@ -131,10 +129,9 @@ def _suite(name, *test_rules):
native.test_suite(
name = name,
tests=[":%s" % t for t in test_names]
tests = [":%s" % t for t in test_names],
)
def _begin(ctx):
"""Begins a unit test.
@ -154,7 +151,6 @@ def _begin(ctx):
"""
return struct(ctx = ctx, failures = [])
def _end(env):
"""Ends a unit test and logs the results.
@ -176,7 +172,6 @@ def _end(env):
executable = True,
)
def _fail(env, msg):
"""Unconditionally causes the current test to fail.
@ -188,8 +183,8 @@ def _fail(env, msg):
print(full_msg)
env.failures.append(full_msg)
def _assert_true(env,
def _assert_true(
env,
condition,
msg = "Expected condition to be true, but was false."):
"""Asserts that the given `condition` is true.
@ -203,8 +198,8 @@ def _assert_true(env,
if not condition:
_fail(env, msg)
def _assert_false(env,
def _assert_false(
env,
condition,
msg = "Expected condition to be false, but was true."):
"""Asserts that the given `condition` is false.
@ -218,7 +213,6 @@ def _assert_false(env,
if condition:
_fail(env, msg)
def _assert_equals(env, expected, actual, msg = None):
"""Asserts that the given `expected` and `actual` values are equal.
@ -237,7 +231,6 @@ def _assert_equals(env, expected, actual, msg=None):
full_msg = expectation_msg
_fail(env, full_msg)
def _assert_set_equals(env, expected, actual, msg = None):
"""Asserts that the given `expected` and `actual` sets are equal.

View File

@ -19,7 +19,6 @@ def _get_bazel_version():
return native.bazel_version
def _extract_version_number(bazel_version):
"""Extracts the semantic version number from a version string
@ -55,7 +54,6 @@ def _parse_bazel_version(bazel_version):
version = _extract_version_number(bazel_version)
return tuple([int(n) for n in version.split(".")])
def _is_at_most(threshold, version):
"""Check that a version is lower or equals to a threshold.
@ -68,7 +66,6 @@ def _is_at_most(threshold, version):
"""
return _parse_bazel_version(version) <= _parse_bazel_version(threshold)
def _is_at_least(threshold, version):
"""Check that a version is higher or equals to a threshold.
@ -82,7 +79,6 @@ def _is_at_least(threshold, version):
return _parse_bazel_version(version) >= _parse_bazel_version(threshold)
def _check_bazel_version(minimum_bazel_version, maximum_bazel_version = None, bazel_version = None):
"""Check that the version of Bazel is valid within the specified range.
@ -103,16 +99,22 @@ def _check_bazel_version(minimum_bazel_version, maximum_bazel_version=None, baze
if not _is_at_least(
threshold = minimum_bazel_version,
version = bazel_version):
version = bazel_version,
):
fail("\nCurrent Bazel version is {}, expected at least {}\n".format(
bazel_version, minimum_bazel_version))
bazel_version,
minimum_bazel_version,
))
if maximum_bazel_version:
if not _is_at_most(
threshold = maximum_bazel_version,
version = bazel_version):
version = bazel_version,
):
fail("\nCurrent Bazel version is {}, expected at most {}\n".format(
bazel_version, maximum_bazel_version))
bazel_version,
maximum_bazel_version,
))
pass

View File

@ -15,11 +15,11 @@
"""Skylib module containing a library rule for aggregating rules files."""
SkylarkLibraryInfo = provider(
'Information on contained Skylark rules.',
"Information on contained Skylark rules.",
fields = {
'srcs': 'Top level rules files.',
'transitive_srcs': 'Transitive closure of rules files required for ' +
'interpretation of the srcs',
"srcs": "Top level rules files.",
"transitive_srcs": "Transitive closure of rules files required for " +
"interpretation of the srcs",
},
)
@ -53,8 +53,8 @@ skylark_library = rule(
providers = [
[SkylarkLibraryInfo],
],
)
}
),
},
)
"""Creates a logical collection of Skylark .bzl files.

View File

@ -14,8 +14,7 @@
"""Unit tests for collections.bzl."""
load("//:lib.bzl", "collections", "asserts", "unittest")
load("//:lib.bzl", "asserts", "collections", "unittest")
def _after_each_test(ctx):
"""Unit tests for collections.after_each."""
@ -23,54 +22,86 @@ def _after_each_test(ctx):
asserts.equals(env, [], collections.after_each("1", []))
asserts.equals(env, ["a", "1"], collections.after_each("1", ["a"]))
asserts.equals(env, ["a", "1", "b", "1"],
collections.after_each("1", ["a", "b"]))
asserts.equals(
env,
["a", "1", "b", "1"],
collections.after_each("1", ["a", "b"]),
)
# We don't care what type the separator is, we just put it there; so None
# should be just as valid as anything else.
asserts.equals(env, ["a", None, "b", None],
collections.after_each(None, ["a", "b"]))
asserts.equals(
env,
["a", None, "b", None],
collections.after_each(None, ["a", "b"]),
)
unittest.end(env)
after_each_test = unittest.make(_after_each_test)
def _before_each_test(ctx):
"""Unit tests for collections.before_each."""
env = unittest.begin(ctx)
asserts.equals(env, [], collections.before_each("1", []))
asserts.equals(env, ["1", "a"], collections.before_each("1", ["a"]))
asserts.equals(env, ["1", "a", "1", "b"],
collections.before_each("1", ["a", "b"]))
asserts.equals(
env,
["1", "a", "1", "b"],
collections.before_each("1", ["a", "b"]),
)
# We don't care what type the separator is, we just put it there; so None
# should be just as valid as anything else.
asserts.equals(env, [None, "a", None, "b"],
collections.before_each(None, ["a", "b"]))
asserts.equals(
env,
[None, "a", None, "b"],
collections.before_each(None, ["a", "b"]),
)
unittest.end(env)
before_each_test = unittest.make(_before_each_test)
def _uniq_test(ctx):
env = unittest.begin(ctx)
asserts.equals(env, collections.uniq([0, 1, 2, 3]), [0, 1, 2, 3])
asserts.equals(env, collections.uniq([]), [])
asserts.equals(env, collections.uniq([1, 1, 1, 1, 1]), [1])
asserts.equals(env, collections.uniq([True, 5, "foo", 5, False, struct(a=1),
True, struct(b=2), "bar", (1,), "foo",
struct(a=1), (1,)]),
[True, 5, "foo", False, struct(a=1), struct(b=2),
"bar", (1,)])
asserts.equals(
env,
collections.uniq([
True,
5,
"foo",
5,
False,
struct(a = 1),
True,
struct(b = 2),
"bar",
(1,),
"foo",
struct(a = 1),
(1,),
]),
[
True,
5,
"foo",
False,
struct(a = 1),
struct(b = 2),
"bar",
(1,),
],
)
unittest.end(env)
uniq_test = unittest.make(_uniq_test)
def collections_test_suite():
"""Creates the test targets and test suite for collections.bzl tests."""
unittest.suite(

View File

@ -14,8 +14,7 @@
"""Unit tests for dicts.bzl."""
load("//:lib.bzl", "dicts", "asserts", "unittest")
load("//:lib.bzl", "asserts", "dicts", "unittest")
def _add_test(ctx):
"""Unit tests for dicts.add."""
@ -29,17 +28,26 @@ def _add_test(ctx):
asserts.equals(env, {"a": 1, "b": 2}, dicts.add({"a": 1}, {"b": 2}))
# Test simple more-than-two-argument behavior.
asserts.equals(env, {"a": 1, "b": 2, "c": 3, "d": 4},
dicts.add({"a": 1}, {"b": 2}, {"c": 3}, {"d": 4}))
asserts.equals(
env,
{"a": 1, "b": 2, "c": 3, "d": 4},
dicts.add({"a": 1}, {"b": 2}, {"c": 3}, {"d": 4}),
)
# Test same-key overriding.
asserts.equals(env, {"a": 100}, dicts.add({"a": 1}, {"a": 100}))
asserts.equals(env, {"a": 10}, dicts.add({"a": 1}, {"a": 100}, {"a": 10}))
asserts.equals(env, {"a": 100, "b": 10},
dicts.add({"a": 1}, {"a": 100}, {"b": 10}))
asserts.equals(
env,
{"a": 100, "b": 10},
dicts.add({"a": 1}, {"a": 100}, {"b": 10}),
)
asserts.equals(env, {"a": 10}, dicts.add({"a": 1}, {}, {"a": 10}))
asserts.equals(env, {"a": 10, "b": 5},
dicts.add({"a": 1}, {"a": 10, "b": 5}))
asserts.equals(
env,
{"a": 10, "b": 5},
dicts.add({"a": 1}, {"a": 10, "b": 5}),
)
# Test some other boundary cases.
asserts.equals(env, {"a": 1}, dicts.add({"a": 1}, {}))
@ -58,7 +66,6 @@ def _add_test(ctx):
add_test = unittest.make(_add_test)
def dicts_test_suite():
"""Creates the test targets and test suite for dicts.bzl tests."""
unittest.suite(

View File

@ -14,8 +14,7 @@
"""Unit tests for new_sets.bzl."""
load("//:lib.bzl", "new_sets", "asserts", "unittest")
load("//:lib.bzl", "asserts", "new_sets", "unittest")
def _is_equal_test(ctx):
"""Unit tests for new_sets.is_equal."""
@ -43,7 +42,6 @@ def _is_equal_test(ctx):
is_equal_test = unittest.make(_is_equal_test)
def _is_subset_test(ctx):
"""Unit tests for new_sets.is_subset."""
env = unittest.begin(ctx)
@ -63,7 +61,6 @@ def _is_subset_test(ctx):
is_subset_test = unittest.make(_is_subset_test)
def _disjoint_test(ctx):
"""Unit tests for new_sets.disjoint."""
env = unittest.begin(ctx)
@ -83,7 +80,6 @@ def _disjoint_test(ctx):
disjoint_test = unittest.make(_disjoint_test)
def _intersection_test(ctx):
"""Unit tests for new_sets.intersection."""
env = unittest.begin(ctx)
@ -103,7 +99,6 @@ def _intersection_test(ctx):
intersection_test = unittest.make(_intersection_test)
def _union_test(ctx):
"""Unit tests for new_sets.union."""
env = unittest.begin(ctx)
@ -125,7 +120,6 @@ def _union_test(ctx):
union_test = unittest.make(_union_test)
def _difference_test(ctx):
"""Unit tests for new_sets.difference."""
env = unittest.begin(ctx)
@ -145,7 +139,6 @@ def _difference_test(ctx):
difference_test = unittest.make(_difference_test)
def _to_list_test(ctx):
"""Unit tests for new_sets.to_list."""
env = unittest.begin(ctx)
@ -158,7 +151,6 @@ def _to_list_test(ctx):
to_list_test = unittest.make(_to_list_test)
def _make_test(ctx):
"""Unit tests for new_sets.make."""
env = unittest.begin(ctx)
@ -171,13 +163,13 @@ def _make_test(ctx):
make_test = unittest.make(_make_test)
def _copy_test(ctx):
"""Unit tests for new_sets.copy."""
env = unittest.begin(ctx)
asserts.new_set_equals(env, new_sets.copy(new_sets.make()), new_sets.make())
asserts.new_set_equals(env, new_sets.copy(new_sets.make([1, 2, 3])), new_sets.make([1, 2, 3]))
# Ensure mutating the copy does not mutate the original
original = new_sets.make([1, 2, 3])
copy = new_sets.copy(original)
@ -188,23 +180,26 @@ def _copy_test(ctx):
copy_test = unittest.make(_copy_test)
def _insert_test(ctx):
"""Unit tests for new_sets.insert."""
env = unittest.begin(ctx)
asserts.new_set_equals(env, new_sets.make([1, 2, 3]), new_sets.insert(new_sets.make([1, 2]), 3))
# Ensure mutating the inserted set does mutate the original set.
original = new_sets.make([1, 2, 3])
after_insert = new_sets.insert(original, 4)
asserts.new_set_equals(env, original, after_insert,
msg="Insert creates a new set which is an O(n) operation, insert should be O(1).")
asserts.new_set_equals(
env,
original,
after_insert,
msg = "Insert creates a new set which is an O(n) operation, insert should be O(1).",
)
unittest.end(env)
insert_test = unittest.make(_insert_test)
def _contains_test(ctx):
"""Unit tests for new_sets.contains."""
env = unittest.begin(ctx)
@ -218,7 +213,6 @@ def _contains_test(ctx):
contains_test = unittest.make(_contains_test)
def _length_test(ctx):
"""Unit test for new_sets.length."""
env = unittest.begin(ctx)
@ -231,12 +225,12 @@ def _length_test(ctx):
length_test = unittest.make(_length_test)
def _remove_test(ctx):
"""Unit test for new_sets.remove."""
env = unittest.begin(ctx)
asserts.new_set_equals(env, new_sets.make([1, 2]), new_sets.remove(new_sets.make([1, 2, 3]), 3))
# Ensure mutating the inserted set does mutate the original set.
original = new_sets.make([1, 2, 3])
after_removal = new_sets.remove(original, 3)
@ -246,7 +240,6 @@ def _remove_test(ctx):
remove_test = unittest.make(_remove_test)
def _repr_str_test(ctx):
"""Unit test for new_sets.repr and new_sets.str."""
env = unittest.begin(ctx)
@ -263,7 +256,6 @@ def _repr_str_test(ctx):
repr_str_test = unittest.make(_repr_str_test)
def new_sets_test_suite():
"""Creates the test targets and test suite for new_sets.bzl tests."""
unittest.suite(

View File

@ -14,8 +14,7 @@
"""Unit tests for partial.bzl."""
load("//:lib.bzl", "partial", "asserts", "unittest")
load("//:lib.bzl", "asserts", "partial", "unittest")
def _make_noargs_nokwargs():
"""Test utility for no args no kwargs case"""
@ -31,15 +30,15 @@ def _make_args_kwargs(arg1, arg2, arg3, **kwargs):
def _call_noargs_nokwargs(call_arg1):
"""Test utility no args no kwargs case where values passed from call site"""
return call_arg1;
return call_arg1
def _call_args_nokwargs(func_arg1, call_arg1):
"""Test utility for args no kwargs case where values passed from call site"""
return func_arg1 + call_arg1;
return func_arg1 + call_arg1
def _call_args_kwargs(func_arg1, call_arg1, func_mult, call_mult):
"""Test utility for args and kwargs case where values passed from call site"""
return (func_arg1 + call_arg1) * func_mult * call_mult;
return (func_arg1 + call_arg1) * func_mult * call_mult
def _make_call_test(ctx):
"""Unit tests for partial.make and partial.call."""
@ -76,7 +75,6 @@ def _make_call_test(ctx):
make_call_test = unittest.make(_make_call_test)
def partial_test_suite():
"""Creates the test targets and test suite for partial.bzl tests."""
unittest.suite(

View File

@ -14,8 +14,7 @@
"""Unit tests for paths.bzl."""
load("//:lib.bzl", "paths", "asserts", "unittest")
load("//:lib.bzl", "asserts", "paths", "unittest")
def _basename_test(ctx):
"""Unit tests for paths.basename."""
@ -41,7 +40,6 @@ def _basename_test(ctx):
basename_test = unittest.make(_basename_test)
def _dirname_test(ctx):
"""Unit tests for paths.dirname."""
env = unittest.begin(ctx)
@ -67,7 +65,6 @@ def _dirname_test(ctx):
dirname_test = unittest.make(_dirname_test)
def _is_absolute_test(ctx):
"""Unit tests for paths.is_absolute."""
env = unittest.begin(ctx)
@ -90,7 +87,6 @@ def _is_absolute_test(ctx):
is_absolute_test = unittest.make(_is_absolute_test)
def _join_test(ctx):
"""Unit tests for paths.join."""
env = unittest.begin(ctx)
@ -135,7 +131,6 @@ def _join_test(ctx):
join_test = unittest.make(_join_test)
def _normalize_test(ctx):
"""Unit tests for paths.normalize."""
env = unittest.begin(ctx)
@ -178,7 +173,6 @@ def _normalize_test(ctx):
normalize_test = unittest.make(_normalize_test)
def _relativize_test(ctx):
"""Unit tests for paths.relativize."""
env = unittest.begin(ctx)
@ -205,7 +199,6 @@ def _relativize_test(ctx):
relativize_test = unittest.make(_relativize_test)
def _replace_extension_test(ctx):
"""Unit tests for paths.replace_extension."""
env = unittest.begin(ctx)
@ -216,8 +209,11 @@ def _replace_extension_test(ctx):
asserts.equals(env, "foo.bar", paths.replace_extension("foo", ".bar"))
# Try a directory with an extension and basename that doesn't have one.
asserts.equals(env, "foo.bar/baz.quux",
paths.replace_extension("foo.bar/baz", ".quux"))
asserts.equals(
env,
"foo.bar/baz.quux",
paths.replace_extension("foo.bar/baz", ".quux"),
)
# Now try some things with legit extensions.
asserts.equals(env, "a.z", paths.replace_extension("a.b", ".z"))
@ -234,7 +230,6 @@ def _replace_extension_test(ctx):
replace_extension_test = unittest.make(_replace_extension_test)
def _split_extension_test(ctx):
"""Unit tests for paths.split_extension."""
env = unittest.begin(ctx)
@ -248,13 +243,19 @@ def _split_extension_test(ctx):
asserts.equals(env, (".", ""), paths.split_extension("."))
asserts.equals(env, (".bashrc", ""), paths.split_extension(".bashrc"))
asserts.equals(env, ("foo/.bashrc", ""), paths.split_extension("foo/.bashrc"))
asserts.equals(env, (".foo/.bashrc", ""),
paths.split_extension(".foo/.bashrc"))
asserts.equals(
env,
(".foo/.bashrc", ""),
paths.split_extension(".foo/.bashrc"),
)
# Try some directories with extensions with basenames that don't have one.
asserts.equals(env, ("foo.bar/baz", ""), paths.split_extension("foo.bar/baz"))
asserts.equals(env, ("foo.bar/.bashrc", ""),
paths.split_extension("foo.bar/.bashrc"))
asserts.equals(
env,
("foo.bar/.bashrc", ""),
paths.split_extension("foo.bar/.bashrc"),
)
# Now try some things that will actually get split.
asserts.equals(env, ("a", ".b"), paths.split_extension("a.b"))
@ -268,7 +269,6 @@ def _split_extension_test(ctx):
split_extension_test = unittest.make(_split_extension_test)
def paths_test_suite():
"""Creates the test targets and test suite for paths.bzl tests."""
unittest.suite(

View File

@ -14,8 +14,7 @@
"""Unit tests for selects.bzl."""
load("//:lib.bzl", "selects", "asserts", "unittest")
load("//:lib.bzl", "asserts", "selects", "unittest")
def _with_or_test(ctx):
"""Unit tests for with_or."""
@ -30,21 +29,33 @@ def _with_or_test(ctx):
# Test OR syntax.
or_dict = {(":foo", ":bar"): ":d1"}
asserts.equals(env, {":foo": ":d1", ":bar": ":d1"},
selects.with_or_dict(or_dict))
asserts.equals(
env,
{":foo": ":d1", ":bar": ":d1"},
selects.with_or_dict(or_dict),
)
# Test mixed syntax.
mixed_dict = {":foo": ":d1", (":bar", ":baz"): ":d2",
"//conditions:default": ":d3"}
asserts.equals(env, {":foo": ":d1", ":bar": ":d2", ":baz": ":d2",
"//conditions:default": ":d3"},
selects.with_or_dict(mixed_dict))
mixed_dict = {
":foo": ":d1",
(":bar", ":baz"): ":d2",
"//conditions:default": ":d3",
}
asserts.equals(
env,
{
":foo": ":d1",
":bar": ":d2",
":baz": ":d2",
"//conditions:default": ":d3",
},
selects.with_or_dict(mixed_dict),
)
unittest.end(env)
with_or_test = unittest.make(_with_or_test)
def selects_test_suite():
"""Creates the test targets and test suite for selects.bzl tests."""
unittest.suite(

View File

@ -14,8 +14,7 @@
"""Unit tests for sets.bzl."""
load("//:lib.bzl", "sets", "asserts", "unittest")
load("//:lib.bzl", "asserts", "sets", "unittest")
def _is_equal_test(ctx):
"""Unit tests for sets.is_equal."""
@ -43,7 +42,6 @@ def _is_equal_test(ctx):
is_equal_test = unittest.make(_is_equal_test)
def _is_subset_test(ctx):
"""Unit tests for sets.is_subset."""
env = unittest.begin(ctx)
@ -63,7 +61,6 @@ def _is_subset_test(ctx):
is_subset_test = unittest.make(_is_subset_test)
def _disjoint_test(ctx):
"""Unit tests for sets.disjoint."""
env = unittest.begin(ctx)
@ -83,7 +80,6 @@ def _disjoint_test(ctx):
disjoint_test = unittest.make(_disjoint_test)
def _intersection_test(ctx):
"""Unit tests for sets.intersection."""
env = unittest.begin(ctx)
@ -103,7 +99,6 @@ def _intersection_test(ctx):
intersection_test = unittest.make(_intersection_test)
def _union_test(ctx):
"""Unit tests for sets.union."""
env = unittest.begin(ctx)
@ -125,7 +120,6 @@ def _union_test(ctx):
union_test = unittest.make(_union_test)
def _difference_test(ctx):
"""Unit tests for sets.difference."""
env = unittest.begin(ctx)
@ -145,7 +139,6 @@ def _difference_test(ctx):
difference_test = unittest.make(_difference_test)
def sets_test_suite():
"""Creates the test targets and test suite for sets.bzl tests."""
unittest.suite(

View File

@ -14,8 +14,7 @@
"""Unit tests for shell.bzl."""
load("//:lib.bzl", "shell", "asserts", "unittest")
load("//:lib.bzl", "asserts", "shell", "unittest")
def _shell_array_literal_test(ctx):
"""Unit tests for shell.array_literal."""
@ -31,7 +30,6 @@ def _shell_array_literal_test(ctx):
shell_array_literal_test = unittest.make(_shell_array_literal_test)
def _shell_quote_test(ctx):
"""Unit tests for shell.quote."""
env = unittest.begin(ctx)
@ -46,14 +44,13 @@ def _shell_quote_test(ctx):
asserts.equals(env, "'$foo'", shell.quote("$foo"))
asserts.equals(env, "'qu\"o\"te'", shell.quote('qu"o"te'))
asserts.equals(env, "'it'\\''s'", shell.quote("it's"))
asserts.equals(env, "'foo\\bar'", shell.quote(r"foo\bar"))
asserts.equals(env, "'back`echo q`uote'", shell.quote(r"back`echo q`uote"))
asserts.equals(env, "'foo\\bar'", shell.quote("foo\\bar"))
asserts.equals(env, "'back`echo q`uote'", shell.quote("back`echo q`uote"))
unittest.end(env)
shell_quote_test = unittest.make(_shell_quote_test)
def _shell_spawn_e2e_test_impl(ctx):
"""Test spawning a real shell."""
args = [
@ -67,7 +64,7 @@ def _shell_spawn_e2e_test_impl(ctx):
"$foo",
'qu"o"te',
"it's",
r"foo\bar",
"foo\\bar",
"back`echo q`uote",
]
script_content = "\n".join([
@ -100,7 +97,6 @@ shell_spawn_e2e_test = rule(
implementation = _shell_spawn_e2e_test_impl,
)
def shell_test_suite():
"""Creates the test targets and test suite for shell.bzl tests."""
unittest.suite(

View File

@ -14,8 +14,7 @@
"""Unit tests for structs.bzl."""
load("//:lib.bzl", "structs", "asserts", "unittest")
load("//:lib.bzl", "asserts", "structs", "unittest")
def _add_test(ctx):
"""Unit tests for dicts.add."""
@ -29,18 +28,23 @@ def _add_test(ctx):
asserts.equals(env, {"a": 1, "b": 2}, structs.to_dict(struct(a = 1, b = 2)))
# Test simple more-than-two-argument behavior.
asserts.equals(env, {"a": 1, "b": 2, "c": 3, "d": 4},
structs.to_dict(struct(a=1, b=2, c=3, d=4)))
asserts.equals(
env,
{"a": 1, "b": 2, "c": 3, "d": 4},
structs.to_dict(struct(a = 1, b = 2, c = 3, d = 4)),
)
# Test transformation is not applied transitively.
asserts.equals(env, {"a": 1, "b": struct(bb=1)},
structs.to_dict(struct(a=1, b=struct(bb=1))))
asserts.equals(
env,
{"a": 1, "b": struct(bb = 1)},
structs.to_dict(struct(a = 1, b = struct(bb = 1))),
)
unittest.end(env)
add_test = unittest.make(_add_test)
def structs_test_suite():
"""Creates the test targets and test suite for structs.bzl tests."""
unittest.suite(

View File

@ -13,14 +13,12 @@
# limitations under the License.
"""Unit tests for types.bzl."""
load("//:lib.bzl", "types", "asserts", "unittest")
load("//:lib.bzl", "asserts", "types", "unittest")
def _a_function():
"""A dummy function for testing."""
pass
def _is_string_test(ctx):
"""Unit tests for types.is_string."""
@ -39,10 +37,8 @@ def _is_string_test(ctx):
unittest.end(env)
is_string_test = unittest.make(_is_string_test)
def _is_bool_test(ctx):
"""Unit tests for types.is_bool."""
@ -61,10 +57,8 @@ def _is_bool_test(ctx):
unittest.end(env)
is_bool_test = unittest.make(_is_bool_test)
def _is_list_test(ctx):
"""Unit tests for types.is_list."""
@ -83,10 +77,8 @@ def _is_list_test(ctx):
unittest.end(env)
is_list_test = unittest.make(_is_list_test)
def _is_none_test(ctx):
"""Unit tests for types.is_none."""
@ -105,10 +97,8 @@ def _is_none_test(ctx):
unittest.end(env)
is_none_test = unittest.make(_is_none_test)
def _is_int_test(ctx):
"""Unit tests for types.is_int."""
@ -128,10 +118,8 @@ def _is_int_test(ctx):
unittest.end(env)
is_int_test = unittest.make(_is_int_test)
def _is_tuple_test(ctx):
"""Unit tests for types.is_tuple."""
@ -151,17 +139,15 @@ def _is_tuple_test(ctx):
unittest.end(env)
is_tuple_test = unittest.make(_is_tuple_test)
def _is_dict_test(ctx):
"""Unit tests for types.is_dict."""
env = unittest.begin(ctx)
asserts.true(env, types.is_dict({}))
asserts.true(env, types.is_dict({'key': 'value'}))
asserts.true(env, types.is_dict({"key": "value"}))
asserts.false(env, types.is_dict(1))
asserts.false(env, types.is_dict("s"))
@ -174,10 +160,8 @@ def _is_dict_test(ctx):
unittest.end(env)
is_dict_test = unittest.make(_is_dict_test)
def _is_function_test(ctx):
"""Unit tests for types.is_dict."""
@ -196,10 +180,8 @@ def _is_function_test(ctx):
unittest.end(env)
is_function_test = unittest.make(_is_function_test)
def types_test_suite():
"""Creates the test targets and test suite for types.bzl tests."""
unittest.suite(

View File

@ -14,7 +14,7 @@
"""Unit tests for versions.bzl."""
load("//:lib.bzl", "versions", "asserts", "unittest")
load("//:lib.bzl", "asserts", "unittest", "versions")
def _parse_test(ctx):
"""Unit tests for versions.parse"""