blob: d3f550188617017afdb86e1813e347af23f1ac97 [file]
// Copyright 2026 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.
package com.google.devtools.build.lib.packages;
import static com.google.common.truth.Truth.assertThat;
import static com.google.devtools.build.lib.packages.BuiltinRestriction.externalRepoAllowlistEntry;
import static com.google.devtools.build.lib.packages.BuiltinRestriction.mainRepoAllowlistEntry;
import static org.junit.Assert.assertThrows;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
import com.google.testing.junit.testparameterinjector.TestParameters;
import javax.annotation.Nullable;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.Sequence;
import net.starlark.java.eval.StarlarkList;
import net.starlark.java.eval.Tuple;
import org.junit.Test;
import org.junit.runner.RunWith;
/** Tests for {@link BuiltinRestriction}. */
@RunWith(TestParameterInjector.class)
public final class BuiltinRestrictionTest {
private static final BuiltinRestriction.Allowlist ALLOWLIST =
BuiltinRestriction.Allowlist.of(
mainRepoAllowlistEntry("allowed"),
externalRepoAllowlistEntry("rules_cc", "pkg"),
externalRepoAllowlistEntry("bazel_tools", "tools"));
@Test
@TestParameters("{label: '//allowed:defs.bzl', moduleRepoName: '', allowed: true}")
@TestParameters("{label: '//allowed/sub:defs.bzl', moduleRepoName: '', allowed: true}")
@TestParameters("{label: '//allowed_other:defs.bzl', moduleRepoName: '', allowed: false}")
@TestParameters("{label: '//pkg:defs.bzl', moduleRepoName: 'rules_cc', allowed: true}")
@TestParameters("{label: '//pkg:defs.bzl', moduleRepoName: 'custom', allowed: false}")
@TestParameters("{label: '//pkg:defs.bzl', moduleRepoName: '', allowed: false}")
@TestParameters("{label: '//pkg_other:defs.bzl', moduleRepoName: 'rules_cc', allowed: false}")
@TestParameters("{label: '@@rules_cc+//pkg:defs.bzl', moduleRepoName: 'custom', allowed: true}")
@TestParameters("{label: '@@rules_cc+1.0//pkg/sub:defs.bzl', moduleRepoName: '', allowed: true}")
@TestParameters(
"{label: '@@rules_cc++ext+repo//pkg:defs.bzl', moduleRepoName: '', allowed: true}")
@TestParameters("{label: '@@rules_cc_extra+//pkg:defs.bzl', moduleRepoName: '', allowed: false}")
@TestParameters("{label: '@@other+//pkg:defs.bzl', moduleRepoName: 'rules_cc', allowed: false}")
@TestParameters("{label: '@@other+//allowed:defs.bzl', moduleRepoName: '', allowed: false}")
@TestParameters(
"{label: '@@+ext+repo//allowed:defs.bzl', moduleRepoName: 'rules_cc', allowed: false}")
@TestParameters("{label: '@@bazel_tools//tools:defs.bzl', moduleRepoName: null, allowed: true}")
@TestParameters("{label: '@@bazel_tools//other:defs.bzl', moduleRepoName: null, allowed: false}")
@TestParameters("{label: '@@_builtins//other:defs.bzl', moduleRepoName: null, allowed: true}")
public void allowlistMatches(String label, @Nullable String moduleRepoName, boolean allowed)
throws Exception {
assertThat(
BuiltinRestriction.isNotAllowed(Label.parseCanonical(label), moduleRepoName, ALLOWLIST))
.isEqualTo(!allowed);
}
@Test
public void testAllowlistFromStarlark() throws Exception {
Sequence<Tuple> starlarkAllowlist =
StarlarkList.of(null, Tuple.of("repo", "package"), Tuple.of("_builtins", ""));
BuiltinRestriction.Allowlist allowlist =
BuiltinRestriction.allowlistFromStarlark(starlarkAllowlist);
assertThat(
BuiltinRestriction.isNotAllowed(
Label.parseCanonical("//package:defs.bzl"), "repo", allowlist))
.isFalse();
assertThat(
BuiltinRestriction.isNotAllowed(
Label.parseCanonical("//literally/anything:defs.bzl"), "_builtins", allowlist))
.isFalse();
assertThat(
BuiltinRestriction.isNotAllowed(
Label.parseCanonical("//some_other_package:defs.bzl"), "repo", allowlist))
.isTrue();
assertThat(
BuiltinRestriction.isNotAllowed(
Label.parseCanonical("//package:defs.bzl"), "other_repo", allowlist))
.isTrue();
}
@Test
public void testInvalidStarlarkAllowlistThrowsEvalException() throws Exception {
Object notSequence = new Object();
Object notTuples = StarlarkList.of(null, "repo", "package");
Object invalidTupleTypes = StarlarkList.of(null, Tuple.of("abc", 1234));
Object invalidTupleLength = StarlarkList.of(null, Tuple.of("repo", "package", "foo"));
assertThat(
assertThrows(
EvalException.class, () -> BuiltinRestriction.allowlistFromStarlark(notSequence)))
.hasMessageThat()
.contains("for allowlist, got unknown, want sequence");
assertThat(
assertThrows(
EvalException.class, () -> BuiltinRestriction.allowlistFromStarlark(notTuples)))
.hasMessageThat()
.contains("at index 0 of allowlist, got element of type string, want tuple");
assertThat(
assertThrows(
EvalException.class,
() -> BuiltinRestriction.allowlistFromStarlark(invalidTupleTypes)))
.hasMessageThat()
.contains("Expected '(string, string)' tuple for allowlist elements; got (\"abc\", 1234)");
assertThat(
assertThrows(
EvalException.class,
() -> BuiltinRestriction.allowlistFromStarlark(invalidTupleLength)))
.hasMessageThat()
.contains(
"Expected '(string, string)' tuple for allowlist elements; got (\"repo\", \"package\","
+ " \"foo\")");
}
}