Skip to content

Commit 2c68977

Browse files
committed
Add a subpath routine to FileSignature.
1 parent 41ace8d commit 2c68977

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/src/main/java/com/diffplug/spotless/FileSignature.java

+9
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,13 @@ public static String pathNativeToUnix(String pathNative) {
119119
public static String pathUnixToNative(String pathUnix) {
120120
return LineEnding.nativeIsWin() ? pathUnix.replace('/', '\\') : pathUnix;
121121
}
122+
123+
/** Asserts that child is a subpath of root. and returns the subpath. */
124+
public static String subpath(String root, String child) {
125+
if (child.startsWith(root)) {
126+
return child.substring(root.length());
127+
} else {
128+
throw new IllegalArgumentException("Expected '" + child + "' to start with '" + root + "'");
129+
}
130+
}
122131
}

testlib/src/test/java/com/diffplug/spotless/FileSignatureTest.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 DiffPlug
2+
* Copyright 2016-2020 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -56,4 +56,8 @@ private List<File> getTestFiles(final String[] paths) throws IOException {
5656
return result;
5757
}
5858

59+
@Test
60+
public void testSubpath() {
61+
assertThat(FileSignature.subpath("root/", "root/child")).isEqualTo("child");
62+
}
5963
}

0 commit comments

Comments
 (0)