aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBlaze Rules Copybara <blaze-rules@google.com>2023-07-11 09:07:59 -0700
committerBlaze Rules Copybara <blaze-rules@google.com>2023-07-11 09:07:59 -0700
commit401ac37a025983db11bc4c008e37fd47fbe1ab2e (patch)
tree00e600e433d4a89cd3c6f4f2d679759d387d459b
parent1e41a0d717ab4db07ed1a63808727dc15163eded (diff)
parent9168001eff709efc65298f0810ce68f347b5c1a5 (diff)
downloadbazelbuild-rules_testing-401ac37a025983db11bc4c008e37fd47fbe1ab2e.tar.gz
Merge pull request #62 from rickeylev:default.info.subject
PiperOrigin-RevId: 547207066
-rw-r--r--CHANGELOG.md4
-rw-r--r--docgen/BUILD1
-rw-r--r--lib/BUILD1
-rw-r--r--lib/private/BUILD10
-rw-r--r--lib/private/default_info_subject.bzl127
-rw-r--r--lib/truth.bzl2
-rw-r--r--tests/default_info_subject/BUILD.bazel17
-rw-r--r--tests/default_info_subject/default_info_subject_tests.bzl126
8 files changed, 288 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7707d6a..270f10b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,10 @@
be set on tests themselves. This allows skipping tests based on platform
or filtering out tests using `--test_tag_filters`
([#43](https://github.com/bazelbuild/rules_testing/issues/43))
+ * DefaultInfoSubject for asserting the builtin DefaultInfo provider
+ ([#52](https://github.com/bazelbuild/rules_testing/issues/52))
+ * StructSubject for asserting arbitrary structs.
+ ([#53](https://github.com/bazelbuild/rules_testing/issues/53))
* (docs) Created human-friendly changelog
## [0.3.0] - 2023-07-06
diff --git a/docgen/BUILD b/docgen/BUILD
index 7c11e2a..dbb8391 100644
--- a/docgen/BUILD
+++ b/docgen/BUILD
@@ -45,6 +45,7 @@ sphinx_stardocs(
"//lib/private:str_subject_bzl",
"//lib/private:struct_subject_bzl",
"//lib/private:target_subject_bzl",
+ "//lib/private:default_info_subject_bzl",
],
tags = ["docs"],
)
diff --git a/lib/BUILD b/lib/BUILD
index 1495a2d..e2c3af7 100644
--- a/lib/BUILD
+++ b/lib/BUILD
@@ -41,6 +41,7 @@ bzl_library(
deps = [
"//lib/private:bool_subject_bzl",
"//lib/private:collection_subject_bzl",
+ "//lib/private:default_info_subject_bzl",
"//lib/private:depset_file_subject_bzl",
"//lib/private:expect_bzl",
"//lib/private:int_subject_bzl",
diff --git a/lib/private/BUILD b/lib/private/BUILD
index bc9963f..07f9b99 100644
--- a/lib/private/BUILD
+++ b/lib/private/BUILD
@@ -126,6 +126,16 @@ bzl_library(
)
bzl_library(
+ name = "default_info_subject_bzl",
+ srcs = ["default_info_subject.bzl"],
+ deps = [
+ ":depset_file_subject_bzl",
+ ":file_subject_bzl",
+ ":runfiles_subject_bzl",
+ ],
+)
+
+bzl_library(
name = "depset_file_subject_bzl",
srcs = ["depset_file_subject.bzl"],
deps = [
diff --git a/lib/private/default_info_subject.bzl b/lib/private/default_info_subject.bzl
new file mode 100644
index 0000000..3a66a48
--- /dev/null
+++ b/lib/private/default_info_subject.bzl
@@ -0,0 +1,127 @@
+# 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.
+
+"""# DefaultInfoSubject"""
+
+load(":runfiles_subject.bzl", "RunfilesSubject")
+load(":depset_file_subject.bzl", "DepsetFileSubject")
+load(":file_subject.bzl", "FileSubject")
+
+def _default_info_subject_new(info, *, meta):
+ """Creates a `DefaultInfoSubject`
+
+ Args:
+ info: ([`DefaultInfo`]) the DefaultInfo object to wrap.
+ meta: ([`ExpectMeta`]) call chain information.
+
+ Returns:
+ [`DefaultInfoSubject`] object.
+ """
+ self = struct(actual = info, meta = meta)
+ public = struct(
+ # keep sorted start
+ actual = info,
+ runfiles = lambda *a, **k: _default_info_subject_runfiles(self, *a, **k),
+ data_runfiles = lambda *a, **k: _default_info_subject_data_runfiles(self, *a, **k),
+ default_outputs = lambda *a, **k: _default_info_subject_default_outputs(self, *a, **k),
+ executable = lambda *a, **k: _default_info_subject_executable(self, *a, **k),
+ runfiles_manifest = lambda *a, **k: _default_info_subject_runfiles_manifest(self, *a, **k),
+ # keep sorted end
+ )
+ return public
+
+def _default_info_subject_runfiles(self):
+ """Creates a subject for the default runfiles.
+
+ Args:
+ self: implicitly added.
+
+ Returns:
+ [`RunfilesSubject`] object
+ """
+ return RunfilesSubject.new(
+ self.actual.default_runfiles,
+ meta = self.meta.derive("runfiles()"),
+ kind = "default",
+ )
+
+def _default_info_subject_data_runfiles(self):
+ """Creates a subject for the data runfiles.
+
+ Args:
+ self: implicitly added.
+
+ Returns:
+ [`RunfilesSubject`] object
+ """
+ return RunfilesSubject.new(
+ self.actual.data_runfiles,
+ meta = self.meta.derive("data_runfiles()"),
+ kind = "data",
+ )
+
+def _default_info_subject_default_outputs(self):
+ """Creates a subject for the default outputs.
+
+ Args:
+ self: implicitly added.
+
+ Returns:
+ [`DepsetFileSubject`] object.
+ """
+ return DepsetFileSubject.new(
+ self.actual.files,
+ meta = self.meta.derive("default_outputs()"),
+ )
+
+def _default_info_subject_executable(self):
+ """Creates a subject for the executable file.
+
+ Args:
+ self: implicitly added.
+
+ Returns:
+ [`FileSubject`] object.
+ """
+ return FileSubject.new(
+ self.actual.files_to_run.executable,
+ meta = self.meta.derive("executable()"),
+ )
+
+def _default_info_subject_runfiles_manifest(self):
+ """Creates a subject for the runfiles manifest.
+
+ Args:
+ self: implicitly added.
+
+ Returns:
+ [`FileSubject`] object.
+ """
+ return FileSubject.new(
+ self.actual.files_to_run.runfiles_manifest,
+ meta = self.meta.derive("runfiles_manifest()"),
+ )
+
+# We use this name so it shows up nice in docs.
+# buildifier: disable=name-conventions
+DefaultInfoSubject = struct(
+ # keep sorted start
+ new = _default_info_subject_new,
+ runfiles = _default_info_subject_runfiles,
+ data_runfiles = _default_info_subject_data_runfiles,
+ default_outputs = _default_info_subject_default_outputs,
+ executable = _default_info_subject_executable,
+ runfiles_manifest = _default_info_subject_runfiles_manifest,
+ # keep sorted end
+)
diff --git a/lib/truth.bzl b/lib/truth.bzl
index e1736e9..3072f65 100644
--- a/lib/truth.bzl
+++ b/lib/truth.bzl
@@ -44,6 +44,7 @@ def foo_test(env, target):
load("//lib/private:bool_subject.bzl", "BoolSubject")
load("//lib/private:collection_subject.bzl", "CollectionSubject")
+load("//lib/private:default_info_subject.bzl", "DefaultInfoSubject")
load("//lib/private:depset_file_subject.bzl", "DepsetFileSubject")
load("//lib/private:dict_subject.bzl", "DictSubject")
load("//lib/private:expect.bzl", "Expect")
@@ -69,6 +70,7 @@ subjects = struct(
# keep sorted start
bool = BoolSubject.new,
collection = CollectionSubject.new,
+ default_info = DefaultInfoSubject.new,
depset_file = DepsetFileSubject.new,
dict = DictSubject.new,
file = FileSubject.new,
diff --git a/tests/default_info_subject/BUILD.bazel b/tests/default_info_subject/BUILD.bazel
new file mode 100644
index 0000000..99a29af
--- /dev/null
+++ b/tests/default_info_subject/BUILD.bazel
@@ -0,0 +1,17 @@
+# 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.
+
+load(":default_info_subject_tests.bzl", "default_info_subject_test_suite")
+
+default_info_subject_test_suite(name = "default_info_subject_tests")
diff --git a/tests/default_info_subject/default_info_subject_tests.bzl b/tests/default_info_subject/default_info_subject_tests.bzl
new file mode 100644
index 0000000..e6cfc10
--- /dev/null
+++ b/tests/default_info_subject/default_info_subject_tests.bzl
@@ -0,0 +1,126 @@
+# 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.
+
+"""Tests for DefaultInfoSubject."""
+
+load("//lib:analysis_test.bzl", "analysis_test")
+load("//lib:test_suite.bzl", "test_suite")
+load("//lib:truth.bzl", "matching", "subjects")
+load("//lib:util.bzl", "util")
+load("//tests:test_util.bzl", "test_util")
+
+_tests = []
+
+def _default_info_subject_test(name):
+ util.helper_target(
+ _simple,
+ name = name + "_subject",
+ )
+ analysis_test(
+ name = name,
+ target = name + "_subject",
+ impl = _default_info_subject_test_impl,
+ )
+
+def _default_info_subject_test_impl(env, target):
+ fake_meta = test_util.fake_meta(env)
+ actual = subjects.default_info(
+ target[DefaultInfo],
+ meta = fake_meta,
+ )
+
+ actual.runfiles().contains_predicate(
+ matching.str_matches("default_runfile.txt"),
+ )
+ test_util.expect_no_failures(env, fake_meta, "check default runfiles success")
+
+ actual.runfiles().contains_predicate(
+ matching.str_matches("not-present.txt"),
+ )
+ test_util.expect_failures(
+ env,
+ fake_meta,
+ "check default runfiles failure",
+ "not-present.txt",
+ )
+
+ actual.data_runfiles().contains_predicate(
+ matching.str_matches("data_runfile.txt"),
+ )
+ test_util.expect_no_failures(env, fake_meta, "check data runfiles success")
+
+ actual.data_runfiles().contains_predicate(
+ matching.str_matches("not-present.txt"),
+ )
+ test_util.expect_failures(
+ env,
+ fake_meta,
+ "check data runfiles failure",
+ "not-present.txt",
+ )
+
+ actual.default_outputs().contains_predicate(
+ matching.file_path_matches("default_output.txt"),
+ )
+ test_util.expect_no_failures(env, fake_meta, "check executable success")
+
+ actual.default_outputs().contains_predicate(
+ matching.file_path_matches("not-present.txt"),
+ )
+ test_util.expect_failures(
+ env,
+ fake_meta,
+ "check executable failure",
+ "not-present.txt",
+ )
+
+ actual.executable().path().contains("subject")
+ test_util.expect_no_failures(env, fake_meta, "check executable success")
+
+ actual.executable().path().contains("not-present")
+ test_util.expect_failures(
+ env,
+ fake_meta,
+ "check executable failure",
+ "not-present",
+ )
+ actual.runfiles_manifest().path().contains("MANIFEST")
+ test_util.expect_no_failures(env, fake_meta, "check runfiles_manifest success")
+
+_tests.append(_default_info_subject_test)
+
+def default_info_subject_test_suite(name):
+ test_suite(
+ name = name,
+ tests = _tests,
+ )
+
+def _simple_impl(ctx):
+ executable = ctx.actions.declare_file(ctx.label.name)
+ ctx.actions.write(executable, "")
+ return [DefaultInfo(
+ files = depset([ctx.file.default_output]),
+ default_runfiles = ctx.runfiles([ctx.file.default_runfile, executable]),
+ data_runfiles = ctx.runfiles([ctx.file.data_runfile]),
+ executable = executable,
+ )]
+
+_simple = rule(
+ implementation = _simple_impl,
+ attrs = {
+ "default_output": attr.label(default = "default_output.txt", allow_single_file = True),
+ "default_runfile": attr.label(default = "default_runfile.txt", allow_single_file = True),
+ "data_runfile": attr.label(default = "data_runfile.txt", allow_single_file = True),
+ },
+)