-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move macro visibility appending out of Package.Builder
`Package.Builder` has a utility method `copyAppendingCurrentMacroLocation`, that doesn't really need to be there. Ahead of further refactoring of this class, I figured I'd spin off this method. (The purpose of the method is to determine the munged visibility values for targets declared within symbolic macros.) The method is re-expressed as two helpers in `RuleVisibility` and one in `MacroInstance`, each of which does a narrower task more suited for its new home. Made the convenience method `Builder#currentMacro` public, since it's much terser than `getCurrentMacroFrame().macroInstance`. Added a drive-by TODO in `StarlarkNativeModule`. Work toward #19922. PiperOrigin-RevId: 678786589 Change-Id: I5cc190bd6ef908a3e67094eacd13e9c91ef43d7c
- Loading branch information
1 parent
62dc3b5
commit ee3fa9d
Showing
6 changed files
with
157 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/test/java/com/google/devtools/build/lib/packages/RuleVisibilityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2024 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 com.google.common.collect.ImmutableList; | ||
import com.google.devtools.build.lib.cmdline.Label; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** Tests for {@link RuleVisibility}. */ | ||
@RunWith(JUnit4.class) | ||
public final class RuleVisibilityTest { | ||
|
||
private static Label label(String labelString) { | ||
return Label.parseCanonicalUnchecked(labelString); | ||
} | ||
|
||
private static RuleVisibility ruleVisibility(String... labelStrings) { | ||
ImmutableList.Builder<Label> labels = ImmutableList.builder(); | ||
for (String labelString : labelStrings) { | ||
labels.add(label(labelString)); | ||
} | ||
return RuleVisibility.parseUnchecked(labels.build()); | ||
} | ||
|
||
// Needed because RuleVisibility has no equals() override. | ||
private static void assertEqual(RuleVisibility vis1, RuleVisibility vis2) { | ||
assertThat(vis1.getDependencyLabels()).isEqualTo(vis2.getDependencyLabels()); | ||
assertThat(vis1.getDeclaredLabels()).isEqualTo(vis2.getDeclaredLabels()); | ||
} | ||
|
||
@Test | ||
public void concatenation() throws Exception { | ||
RuleVisibility normalVis = ruleVisibility("//a:__pkg__", "//b:__pkg__"); | ||
RuleVisibility publicVis = RuleVisibility.PUBLIC; | ||
RuleVisibility privateVis = RuleVisibility.PRIVATE; | ||
// Technically the empty visibility is a distinct object from private visibility, even though | ||
// it has the same semantics. | ||
RuleVisibility emptyVis = ruleVisibility(); | ||
|
||
assertEqual( | ||
RuleVisibility.concatWithElement(normalVis, label("//c:__pkg__")), | ||
ruleVisibility("//a:__pkg__", "//b:__pkg__", "//c:__pkg__")); | ||
assertEqual( | ||
RuleVisibility.concatWithElement(normalVis, label("//visibility:public")), publicVis); | ||
assertEqual( | ||
RuleVisibility.concatWithElement(normalVis, label("//visibility:private")), normalVis); | ||
|
||
assertEqual(RuleVisibility.concatWithElement(publicVis, label("//c:__pkg__")), publicVis); | ||
assertEqual( | ||
RuleVisibility.concatWithElement(publicVis, label("//visibility:public")), publicVis); | ||
assertEqual( | ||
RuleVisibility.concatWithElement(publicVis, label("//visibility:private")), publicVis); | ||
|
||
assertEqual( | ||
RuleVisibility.concatWithElement(privateVis, label("//c:__pkg__")), | ||
ruleVisibility("//c:__pkg__")); | ||
assertEqual( | ||
RuleVisibility.concatWithElement(privateVis, label("//visibility:public")), publicVis); | ||
assertEqual( | ||
RuleVisibility.concatWithElement(privateVis, label("//visibility:private")), privateVis); | ||
|
||
assertEqual( | ||
RuleVisibility.concatWithElement(emptyVis, label("//c:__pkg__")), | ||
ruleVisibility("//c:__pkg__")); | ||
assertEqual( | ||
RuleVisibility.concatWithElement(emptyVis, label("//visibility:public")), publicVis); | ||
assertEqual( | ||
RuleVisibility.concatWithElement(emptyVis, label("//visibility:private")), emptyVis); | ||
} | ||
} |