Skip to content

Commit

Permalink
Add constants and a unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-abalik committed Jan 25, 2024
1 parent 782f471 commit d556502
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class VirtualIncludesHandler {
private static final int EXTERNAL_WORKSPACE_NAME_IDX = 4;
private static final int WORKSPACE_PATH_START_FOR_EXTERNAL_WORKSPACE = 5;
private static final int WORKSPACE_PATH_START_FOR_LOCAL_WORKSPACE = 3;
private static final String ABSOLUTE_LABEL_PREFIX = "//";
private static final int ABSOLUTE_LABEL_PREFIX_LENGTH = ABSOLUTE_LABEL_PREFIX.length();

private static List<Path> splitExecutionPath(ExecutionRootPath executionRootPath) {
return Lists.newArrayList(executionRootPath.getAbsoluteOrRelativeFile().toPath().iterator());
Expand Down Expand Up @@ -97,8 +99,8 @@ static ImmutableList<File> resolveVirtualInclude(ExecutionRootPath executionRoot
stripPrefix = stripPrefix.substring(0, stripPrefix.length() - 1);
}
String externalWorkspaceName = key.getLabel().externalWorkspaceName();
WorkspacePath stripPrefixWorkspacePath = stripPrefix.startsWith("//") ?
new WorkspacePath(stripPrefix.substring(2)) :
WorkspacePath stripPrefixWorkspacePath = stripPrefix.startsWith(ABSOLUTE_LABEL_PREFIX) ?
new WorkspacePath(stripPrefix.substring(ABSOLUTE_LABEL_PREFIX_LENGTH)) :
new WorkspacePath(key.getLabel().blazePackage(), stripPrefix);
if (key.getLabel().externalWorkspaceName() != null) {
ExecutionRootPath external = new ExecutionRootPath(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public class ExecutionRootPathResolverTest extends BlazeTestCase {
private static final List<TargetName> TARGET_NAMES = List.of(SIMPLE_TARGET, ADVANCED_TARGET);
private static final String INCLUDE_PREFIX = "generated-include-prefix";

private static final TargetName TARGET_WITH_ABSOLUTE_STRIP_PREFIX = TargetName.create("absolute_strip_prefix");
private static final WorkspacePath ABSOLUTE_STRIP_PREFIX_WORKSPACE_PATH = new WorkspacePath("foo/bar");
private static final String ABSOLUTE_STRIP_PREFIX_PATH = "foo";
private static final String ABSOLUTE_STRIP_PREFIX = "//" + ABSOLUTE_STRIP_PREFIX_PATH;

private ExecutionRootPathResolver pathResolver;

@NotNull
Expand All @@ -79,7 +84,10 @@ private static String getStripPrefix(TargetName targetName) {
return advancedStripPrefix;
} else if (targetName.equals(TARGET_WITH_INCLUDE_PREFIX)) {
return simpleStripPrefix;
} else {
} else if (targetName.equals(TARGET_WITH_ABSOLUTE_STRIP_PREFIX)) {
return ABSOLUTE_STRIP_PREFIX;
}
else {
throw new IllegalArgumentException("Unexpected targetName");
}
}
Expand Down Expand Up @@ -109,6 +117,11 @@ private static TargetMap getTargetMap() {
getTargetIdeInfo(targetName));
}
}

builder.put(
TargetKey.forPlainTarget(
Label.create(workspaceName, ABSOLUTE_STRIP_PREFIX_WORKSPACE_PATH, TARGET_WITH_ABSOLUTE_STRIP_PREFIX)),
getTargetIdeInfo(TARGET_WITH_ABSOLUTE_STRIP_PREFIX));
}

builder.put(
Expand Down Expand Up @@ -210,6 +223,38 @@ public void testVirtualIncludes() {
}
}

@Test
public void testVirtualIncludesWithAbsoluteStripPrefix() {
for (String workspaceName : WORKSPACES) {
String workspaceNameString = workspaceName != null ? workspaceName : "";

ExecutionRootPath generatedPath = new ExecutionRootPath(Path.of(
"bazel-out/k8-fastbuild/bin",
(workspaceName == null ? "" : ExecutionRootPathResolver.externalPath.getPath()),
workspaceNameString,
ABSOLUTE_STRIP_PREFIX_WORKSPACE_PATH.toString(),
VirtualIncludesHandler.VIRTUAL_INCLUDES_DIRECTORY.toString(),
TARGET_WITH_ABSOLUTE_STRIP_PREFIX.toString()).toFile());

ImmutableList<File> files =
pathResolver.resolveToIncludeDirectories(generatedPath);

String expectedPath = Path.of(workspaceNameString,
ABSOLUTE_STRIP_PREFIX_PATH).toString();

if (workspaceName != null) {
// check external workspace
assertThat(files).containsExactly(
Path.of(OUTPUT_BASE, ExecutionRootPathResolver.externalPath.getPath(), expectedPath)
.toFile());
} else {
// check local
assertThat(files).containsExactly(
WORKSPACE_ROOT.fileForPath(new WorkspacePath(expectedPath)));
}
}
}

@Test
public void testVirtualIncludesWithIncludePrefix() {
File fileToBeResolved = Path.of(
Expand Down

0 comments on commit d556502

Please sign in to comment.