Skip to content

Commit

Permalink
Roll forward commit 8375185.
Browse files Browse the repository at this point in the history
Consider /src/ in the path when locating the java root directory.

--
MOS_MIGRATED_REVID=123469898
  • Loading branch information
Googler authored and dslomov committed May 30, 2016
1 parent bc101b9 commit 4ab4f05
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,13 @@ public static String getJavaClassName(PathFragment path) {
}

/**
* Find the index of the "java" or "javatests" segment in a Java path fragment
* that precedes the source root.
* Finds the index of the segment in a Java path fragment that precedes the source root.
* Starts from the first "java" or "javatests" or "src" segment.
* If the found item was "src", check if this is followed by "main" or "test" and then "java"
* or "resources" (maven layout).
* If the found item was "src", or "java"/"javatests" at the first segment, check for a nested
* root directory (src, java or javatests). A nested root must be followed by (com|net|org),
* or matching maven structure for nested "src", to be accepted, to avoid false positives.
*
* @param path a Java source dir or file path
* @return the index of the java segment or -1 iff no java segment was found.
Expand All @@ -71,7 +76,42 @@ private static int javaSegmentIndex(PathFragment path) {
if (path.isAbsolute()) {
throw new IllegalArgumentException("path must not be absolute: '" + path + "'");
}
return path.getFirstSegment(ImmutableSet.of("java", "javatests"));
int rootIndex = path.getFirstSegment(ImmutableSet.of("java", "javatests", "src"));
if (rootIndex < 0) {
return rootIndex;
}
final boolean isSrc = "src".equals(path.getSegment(rootIndex));
int checkMavenIndex = isSrc ? rootIndex : -1;
if (rootIndex == 0 || isSrc) {
// Check for a nested "src" directory.
// Also, to support an existing case, "javatests" within "src".
for (int i = rootIndex + 1, max = path.segmentCount() - 2; i <= max; i++) {
String segment = path.getSegment(i);
if ("src".equals(segment)
|| (isSrc && ("javatests".equals(segment) || "java".equals(segment)))) {
String next = path.getSegment(i + 1);
if ("com".equals(next) || "org".equals(next) || "net".equals(next)) {
// Check for common first element of java package, to avoid false positives.
rootIndex = i;
} else if ("src".equals(segment)) {
// Also accept maven style src/(main|test)/(java|resources).
checkMavenIndex = i;
}
break;
}
}
}
// Check for (main|test)/(java|resources) after /src/.
if (checkMavenIndex >= 0 && checkMavenIndex + 2 < path.segmentCount()) {
String next = path.getSegment(checkMavenIndex + 1);
if ("main".equals(next) || "test".equals(next)) {
next = path.getSegment(checkMavenIndex + 2);
if ("java".equals(next) || "resources".equals(next)) {
rootIndex = checkMavenIndex + 2;
}
}
}
return rootIndex;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,66 @@ public void testGetJavaRoot() {
.isNull();
assertThat(getRootPath("java/com/google/common/case")).isEqualTo("java");
assertThat(getRootPath("javatests/com/google/common/case")).isEqualTo("javatests");
assertThat(getRootPath("src/com/myproject/util")).isEqualTo("src");
assertThat(getRootPath("project/java")).isEqualTo("project/java");
assertThat(getRootPath("project/java/anything")).isEqualTo("project/java");
assertThat(getRootPath("project/javatests")).isEqualTo("project/javatests");
assertThat(getRootPath("project/javatests/anything")).isEqualTo("project/javatests");
assertThat(getRootPath("project/src")).isEqualTo("project/src");
assertThat(getRootPath("project/src/anything")).isEqualTo("project/src");
assertThat(getRootPath("third_party/java_src/project/src/main/java/foo"))
.isEqualTo("third_party/java_src/project/src/main/java");
assertThat(getRootPath("third_party/java_src/project/src/test/java/foo"))
.isEqualTo("third_party/java_src/project/src/test/java");
assertThat(getRootPath("third_party/java_src/project/src/main/resources/foo"))
.isEqualTo("third_party/java_src/project/src/main/resources");
assertThat(getRootPath("third_party/java_src/project/src/test/resources/foo"))
.isEqualTo("third_party/java_src/project/src/test/resources");
assertThat(getRootPath("third_party/java_src/project/javatests/foo"))
.isEqualTo("third_party/java_src/project/javatests");

// Cases covering nested /src/ directories.
assertThat(getRootPath("java/com/google/project/module/src/com"))
.isEqualTo("java/com/google/project/module/src");
assertThat(getRootPath("java/com/google/project/module/src/org"))
.isEqualTo("java/com/google/project/module/src");
assertThat(getRootPath("java/com/google/project/module/src/net"))
.isEqualTo("java/com/google/project/module/src");
assertThat(getRootPath("java/com/google/project/module/src/main/java"))
.isEqualTo("java/com/google/project/module/src/main/java");
assertThat(getRootPath("java/com/google/project/module/src/test/java"))
.isEqualTo("java/com/google/project/module/src/test/java");
assertThat(getRootPath("javatests/com/google/project/src/com"))
.isEqualTo("javatests/com/google/project/src");
assertThat(getRootPath("src/com/google/project/src/main/java"))
.isEqualTo("src/com/google/project/src/main/java");
assertThat(getRootPath("java/com/google/project/module/src/somethingelse"))
.isEqualTo("java");
assertThat(getRootPath("java/com/google/project/module/src/foo/java"))
.isEqualTo("java");
assertThat(getRootPath("java/com/google/project/module/src/main/com"))
.isEqualTo("java");
assertThat(getRootPath("java/com/google/project/module/src/test/org"))
.isEqualTo("java");
assertThat(getRootPath("java/com/google/project/module/src/java/com"))
.isEqualTo("java");
assertThat(getRootPath("foo/java/com/google/project/src/com"))
.isEqualTo("foo/java");
assertThat(getRootPath("src/com/google/java/javac"))
.isEqualTo("src");

assertThat(getRootPath("src/java_tools/buildjar/javatests/com"))
.isEqualTo("src/java_tools/buildjar/javatests");
assertThat(getRootPath("third_party/project/src/java_tools/buildjar/javatests/com"))
.isEqualTo("third_party/project/src/java_tools/buildjar/javatests");
assertThat(getRootPath("third_party/project/src/java_tools/buildjar/java/net"))
.isEqualTo("third_party/project/src/java_tools/buildjar/java");
assertThat(getRootPath("src/java_tools/buildjar/javatests/foo"))
.isEqualTo("src");
assertThat(getRootPath("src/tools/workspace/src/test/java/foo"))
.isEqualTo("src/tools/workspace/src/test/java");
assertThat(getRootPath("foo/src/tools/workspace/src/test/java/foo"))
.isEqualTo("foo/src/tools/workspace/src/test/java");
}

@Test
Expand Down

0 comments on commit 4ab4f05

Please sign in to comment.