Skip to content

Commit

Permalink
prioritise shorter contained roots
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Coles committed Nov 16, 2022
1 parent 2bca6d6 commit 491bee4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
* Comparator to allow ordering of Objects by their closeness to a
* given root, assuming their toString provides a path like hierarchy.
* <p>
* Allows paths in same module as a file to be examined before those in other modules
* Allows paths in same module as a file to be examined before those in other modules.
*
* Expects the base path supplied to be deeper than the shared root (e.g.
* /a/b/c/target/pit-reports when shared root is a/b/c)
*
*/
class PathComparator implements Comparator<Object> {

Expand All @@ -30,16 +34,19 @@ private int distanceFromBase(String s1) {
String[] a = s1.split(separator);

for (int i = 0; i != baseParts.length; i++) {
if (a.length == i) {
return i - 1;
}

if (!a[i].equals(baseParts[i])) {
if (a.length == i || !a[i].equals(baseParts[i])) {
return i;
}

if (a.length - 1 == i) {
// horrible fudge. This path is shorter than our base, but matches everything in it.
// We want to prioritise it above ones that mismatch, so we use a magic 1000. So long
// as the directory structure is not bizarrely deep, short fully matching paths will appear
// before longer mismatched paths.
return i + 1000;
}
}

return baseParts.length;
return a.length;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public void identicalPathsCompareAsZero() {
assertThat(underTest.compare(base,base)).isEqualTo(0);
}

@Test
public void shortSubPathsAreWeightedAboveLongPathsThatDoNotMatchBase() {
String base = "a/b/more";
PathComparator underTest = new PathComparator(base, "/");

assertThat(underTest.compare("a/b", "a/b/c/d/e/f")).isLessThan(underTest.compare("a/b/c/d/e", "a/b/c/d/e/f/g/h/i"));
}

@Test
public void identicalPathsCompareAsZeroWhenNotUnderBase() {
PathComparator underTest = new PathComparator(new File("different/path"), File.separator);
Expand All @@ -33,8 +41,7 @@ public void sameRootedPathComparesHigher() {
PathComparator underTest = new PathComparator(base, File.separator);
File sameRoot = new File("start/end/leaf");
File differentRoot = new File("start/different/leaf");
assertThat(underTest.compare(differentRoot, sameRoot)).isEqualTo(1);
assertThat(underTest.compare(sameRoot, differentRoot)).isEqualTo(-1);
assertThat(underTest.compare(differentRoot, sameRoot)).isGreaterThan(underTest.compare(sameRoot, differentRoot));
}

@Test
Expand Down Expand Up @@ -88,6 +95,14 @@ public void modulesUnderRootAlwaysSortedFirst() {
assertThat(paths.get(1)).isEqualTo("a/b/c/d/e");
}

@Test
public void sortsByLengthWhenAllEquallyUnderRoot() {
PathComparator underTest = new PathComparator("a/b/c/irrelevant", "/");
List<String> paths = asList("a/b/x", "a/b/x/x", "a/b/x/x/x", "a/b/x/x/x/x", "a/b");
paths.sort(underTest);
assertThat(paths).containsExactly("a/b", "a/b/x", "a/b/x/x", "a/b/x/x/x", "a/b/x/x/x/x");
}

@Test
public void handlesBaseIndexLongerThanSuppliedPath() {
PathComparator underTest = new PathComparator("/a/b/c/irrelevant", "/");
Expand Down

0 comments on commit 491bee4

Please sign in to comment.