Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bazel/dependency/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ string_list_flag(
visibility = ["//visibility:public"],
)

string_list_flag(
name = "_attribution_patterns",
build_setting_default = [],
visibility = ["//visibility:public"],
)

updater(
name = "update",
dependencies = "//:dependency_versions",
Expand Down
592 changes: 504 additions & 88 deletions bazel/dependency/reachability.bzl

Large diffs are not rendered by default.

149 changes: 148 additions & 1 deletion bazel/dependency/test/BUILD
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
load("@rules_shell//shell:sh_test.bzl", "sh_test")
load("//dependency:reachability.bzl", "dependency_reachability")
load(":custom_rule_test.bzl", "custom_library_rule")
load(":reachability_apparent_name_test.bzl", "apparent_name_test")
load(":reachability_config_validation_test.bzl", "reachability_config_validation_test")
load(":reachability_merge_defines_test.bzl", "reachability_merge_defines_test")
load(":reachability_package_pattern_test.bzl", "reachability_package_pattern_test")
load(":reachability_test_rules.bzl", "dependency_reachability_with_mode")

sh_test(
name = "updater_test",
Expand Down Expand Up @@ -48,12 +53,134 @@ dependency_reachability(
],
)

dependency_reachability(
name = "reachability_empty_attribution_patterns",
testonly = True,
roots = [
":core_root",
":test_root",
],
attribution_patterns = [],
)

string_flag(
name = "reachability_mode",
build_setting_default = "default",
values = ["default", "extra"],
)

config_setting(
name = "reachability_mode_extra",
flag_values = {":reachability_mode": "extra"},
)

filegroup(
name = "variant_external_consumer",
srcs = [
"@bazel_skylib//lib:paths",
] + select({
":reachability_mode_extra": ["@jq_toolchains//:resolved_toolchain"],
"//conditions:default": [],
}),
)

filegroup(
name = "variant_core_root",
srcs = [":variant_external_consumer"],
)

filegroup(
name = "variant_test_root",
testonly = True,
srcs = [
":variant_external_consumer",
"@bazel_skylib//lib:sets",
],
)

dependency_reachability_with_mode(
name = "reachability_multiconfig",
testonly = True,
roots = [
":variant_core_root",
":variant_test_root",
],
configs = {
"default": {},
"extra": {"//dependency/test:reachability_mode": "extra"},
},
)

filegroup(
name = "attribution_root",
srcs = [
"//dependency/test/transitive/ext/deep:root",
"//dependency/test/transitive/ext/direct:root",
"//dependency/test/transitive/ext/one:root",
"//dependency/test/transitive/ext/two:root",
],
)

filegroup(
name = "attribution_testonly_root",
testonly = True,
srcs = ["//dependency/test/transitive/ext/testonly:root"],
)

dependency_reachability(
name = "reachability_attribution",
testonly = True,
roots = [
":attribution_root",
":attribution_testonly_root",
],
attribution_patterns = ["//dependency/test/transitive/ext/..."],
)

filegroup(
name = "attribution_variant_root",
srcs = select({
":reachability_mode_extra": ["//dependency/test/transitive/ext/config_extra:root"],
"//conditions:default": ["//dependency/test/transitive/ext/config_default:root"],
}),
)

dependency_reachability_with_mode(
name = "reachability_attribution_multiconfig",
testonly = True,
roots = [":attribution_variant_root"],
attribution_patterns = ["//dependency/test/transitive/ext/..."],
configs = {
"default": {},
"extra": {"//dependency/test:reachability_mode": "extra"},
},
)

dependency_reachability(
name = "reachability_attribution_excluded_baseline",
testonly = True,
roots = ["//dependency/test/transitive/ext/excluded:root"],
attribution_patterns = ["//dependency/test/transitive/ext/..."],
)

dependency_reachability(
name = "reachability_attribution_excluded",
testonly = True,
roots = ["//dependency/test/transitive/ext/excluded:root"],
attribution_patterns = ["//dependency/test/transitive/ext/..."],
excluded_patterns = ["rules_pkg*"],
)

sh_test(
name = "reachability_test",
size = "small",
srcs = ["reachability_test.sh"],
data = [
":reachability",
":reachability_empty_attribution_patterns",
":reachability_attribution",
":reachability_attribution_multiconfig",
":reachability_multiconfig",
"@jq_toolchains//:resolved_toolchain",
],
env = {
Expand Down Expand Up @@ -196,6 +323,8 @@ sh_test(
":reachability_excluded_rules_pkg_transitive",
":reachability_empty_exclusions",
":reachability_empty_exclusions_explicit",
":reachability_attribution_excluded_baseline",
":reachability_attribution_excluded",
"@jq_toolchains//:resolved_toolchain",
],
env = {
Expand All @@ -206,7 +335,13 @@ sh_test(

bzl_library(
name = "reachability_apparent_name_test_lib",
srcs = ["reachability_apparent_name_test.bzl"],
srcs = [
"reachability_apparent_name_test.bzl",
"reachability_config_validation_test.bzl",
"reachability_merge_defines_test.bzl",
"reachability_package_pattern_test.bzl",
"reachability_test_rules.bzl",
],
deps = [
"@bazel_skylib//lib:unittest",
],
Expand All @@ -215,3 +350,15 @@ bzl_library(
apparent_name_test(
name = "apparent_name_test",
)

reachability_config_validation_test(
name = "reachability_config_validation_test",
)

reachability_merge_defines_test(
name = "reachability_merge_defines_test",
)

reachability_package_pattern_test(
name = "reachability_package_pattern_test",
)
17 changes: 17 additions & 0 deletions bazel/dependency/test/exclusions_reachability_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ REACHABILITY_RULES_PKG_TRANSITIVE_JSON="${RUNFILES_DIR}/dependency/test/reachabi
REACHABILITY_EXCLUDED_RULES_PKG_TRANSITIVE_JSON="${RUNFILES_DIR}/dependency/test/reachability_excluded_rules_pkg_transitive.json"
REACHABILITY_EMPTY_EXCLUSIONS_JSON="${RUNFILES_DIR}/dependency/test/reachability_empty_exclusions.json"
REACHABILITY_EMPTY_EXCLUSIONS_EXPLICIT_JSON="${RUNFILES_DIR}/dependency/test/reachability_empty_exclusions_explicit.json"
REACHABILITY_ATTRIBUTION_EXCLUDED_BASELINE_JSON="${RUNFILES_DIR}/dependency/test/reachability_attribution_excluded_baseline.json"
REACHABILITY_ATTRIBUTION_EXCLUDED_JSON="${RUNFILES_DIR}/dependency/test/reachability_attribution_excluded.json"

FAILED=0

Expand Down Expand Up @@ -83,6 +85,21 @@ check "combined excluded_edges and excluded_patterns apply together" \
"0" \
"${REACHABILITY_EDGE_AND_PATTERN_JSON}"

check "baseline attribution records the unexcluded repository" \
'.dependencies | to_entries[] | select(.value.name == "rules_pkg") | .value.attributed_packages[0].package' \
"//dependency/test/transitive/ext/excluded" \
"${REACHABILITY_ATTRIBUTION_EXCLUDED_BASELINE_JSON}"

check "excluded repository is not attributed" \
'[.dependencies[] | select(.name == "rules_pkg")] | length' \
"0" \
"${REACHABILITY_ATTRIBUTION_EXCLUDED_JSON}"

check "nothing is attributed through an excluded repository" \
'[.dependencies[] | .attributed_packages[]?] | length' \
"0" \
"${REACHABILITY_ATTRIBUTION_EXCLUDED_JSON}"

if ! cmp -s "${REACHABILITY_EMPTY_EXCLUSIONS_JSON}" "${REACHABILITY_EMPTY_EXCLUSIONS_EXPLICIT_JSON}"; then
echo "FAIL: explicit empty exclusions must be byte-identical to defaults" >&2
FAILED=1
Expand Down
31 changes: 31 additions & 0 deletions bazel/dependency/test/reachability_config_validation_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
load("//dependency:reachability.bzl", "config_validation_error")

def _reachability_config_validation_test_impl(ctx):
env = unittest.begin(ctx)

message = config_validation_error(
{"invalid": {"//dependency/test:reachability_mode": "extra"}},
[],
False,
)
asserts.equals(
env,
"Config 'invalid' varies '//dependency/test:reachability_mode' but it is not declared in flags. Declared flags: []",
message,
)

message = config_validation_error(
{"invalid_define": {"wasm": "wasmtime"}},
["//dependency/test:reachability_mode"],
False,
)
asserts.equals(
env,
"Config 'invalid_define' varies define 'wasm' but this rule was constructed with defines = False",
message,
)

return unittest.end(env)

reachability_config_validation_test = unittest.make(_reachability_config_validation_test_impl)
27 changes: 27 additions & 0 deletions bazel/dependency/test/reachability_merge_defines_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
load("//dependency:reachability.bzl", "merge_defines")

def _reachability_merge_defines_test_impl(ctx):
env = unittest.begin(ctx)

merged = merge_defines(
[
"existing=old",
"skipmalformed",
"override=old",
],
{
"//dependency/test:reachability_mode": "extra",
"override": "new",
"added": "value",
},
)
asserts.equals(
env,
["added=value", "existing=old", "override=new"],
merged,
)

return unittest.end(env)

reachability_merge_defines_test = unittest.make(_reachability_merge_defines_test_impl)
38 changes: 38 additions & 0 deletions bazel/dependency/test/reachability_package_pattern_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
load("//dependency:reachability.bzl", "package_pattern_matches")

def _reachability_package_pattern_test_impl(ctx):
env = unittest.begin(ctx)

asserts.true(
env,
package_pattern_matches(
"//dependency/test/transitive/ext/direct",
"//dependency/test/transitive/ext/...",
),
)
asserts.true(
env,
package_pattern_matches(
"//dependency/test/transitive/ext",
"//dependency/test/transitive/ext/...",
),
)
asserts.true(
env,
package_pattern_matches(
"//dependency/test/transitive/ext/direct",
"//dependency/test/transitive/ext/direct",
),
)
asserts.false(
env,
package_pattern_matches(
"//dependency/test/transitive/shared",
"//dependency/test/transitive/ext/...",
),
)

return unittest.end(env)

reachability_package_pattern_test = unittest.make(_reachability_package_pattern_test_impl)
Loading