Skip to content

Commit d12dbb8

Browse files
committed
Moved LicenseHeaderStepTest to testlib.
1 parent 57165d6 commit d12dbb8

File tree

7 files changed

+28
-21
lines changed

7 files changed

+28
-21
lines changed

lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ public static FormatterStep createFromFile(File licenseHeaderFile, Charset encod
5555
}
5656

5757
/** The license that we'd like enforced. */
58-
// TODO: Make package-private when LicenseHeaderStepTest is migrated to testlib
59-
public LicenseHeaderStep(String licenseHeader, String delimiter) {
58+
private LicenseHeaderStep(String licenseHeader, String delimiter) {
6059
if (delimiter.contains("\n")) {
6160
throw new IllegalArgumentException("The delimiter must not contain any newlines.");
6261
}
@@ -70,8 +69,7 @@ public LicenseHeaderStep(String licenseHeader, String delimiter) {
7069
}
7170

7271
/** Reads the license file from the given file. */
73-
// TODO: Make package-private when LicenseHeaderStepTest is migrated to testlib
74-
public LicenseHeaderStep(File licenseFile, Charset encoding, String delimiter) throws IOException {
72+
private LicenseHeaderStep(File licenseFile, Charset encoding, String delimiter) throws IOException {
7573
this(new String(Files.readAllBytes(licenseFile.toPath()), encoding), delimiter);
7674
}
7775

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.gradle.api.plugins.JavaPluginConvention;
2323
import org.gradle.api.tasks.SourceSet;
2424

25+
import com.diffplug.common.annotations.VisibleForTesting;
2526
import com.diffplug.spotless.SerializableFileFilter;
2627
import com.diffplug.spotless.extra.java.EclipseFormatterStep;
2728
import com.diffplug.spotless.generic.LicenseHeaderStep;
@@ -35,7 +36,9 @@ public JavaExtension(SpotlessExtension rootExtension) {
3536
super(NAME, rootExtension);
3637
}
3738

38-
public static final String LICENSE_HEADER_DELIMITER = "package ";
39+
// If this constant changes, don't forget to change the similarly-named one in
40+
// testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java as well
41+
private static final String LICENSE_HEADER_DELIMITER = "package ";
3942

4043
public void licenseHeader(String licenseHeader) {
4144
licenseHeader(licenseHeader, LICENSE_HEADER_DELIMITER);

testlib/src/main/java/com/diffplug/spotless/ResourceHarness.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ protected void assertFileContent(String expectedContent, File actual) throws IOE
134134

135135
/** Reads the given resource from "before", applies the step, and makes sure the result is "after". */
136136
protected void assertOnResources(FormatterStep step, String unformattedPath, String expectedPath) throws Throwable {
137-
assertOnResources(rawUnix -> step.format(rawUnix, null), unformattedPath, expectedPath);
137+
assertOnResources(rawUnix -> step.format(rawUnix, new File("")), unformattedPath, expectedPath);
138138
}
139139

140140
/** Reads the given resource from "before", applies the step, and makes sure the result is "after". */

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/LicenseHeaderStepTest.java renamed to testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java

+21-15
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.diffplug.gradle.spotless;
16+
package com.diffplug.spotless.generic;
1717

18+
import java.io.File;
1819
import java.nio.charset.StandardCharsets;
1920

2021
import org.junit.Assert;
2122
import org.junit.Test;
2223

24+
import com.diffplug.spotless.FormatterStep;
2325
import com.diffplug.spotless.ResourceHarness;
2426
import com.diffplug.spotless.generic.LicenseHeaderStep;
2527

@@ -28,42 +30,46 @@ public class LicenseHeaderStepTest extends ResourceHarness {
2830
private static final String KEY_FILE_NOTAPPLIED = "license/MissingLicense.test";
2931
private static final String KEY_FILE_APPLIED = "license/HasLicense.test";
3032

33+
// If this constant changes, don't forget to change the similarly-named one in
34+
// plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java as well
35+
private static final String LICENSE_HEADER_DELIMITER = "package ";
36+
3137
@Test
32-
public void fromString() throws Throwable {
33-
LicenseHeaderStep step = new LicenseHeaderStep(getTestResource(KEY_LICENSE), JavaExtension.LICENSE_HEADER_DELIMITER);
34-
assertOnResources(step::format, KEY_FILE_NOTAPPLIED, KEY_FILE_APPLIED);
38+
public void fromHeader() throws Throwable {
39+
FormatterStep step = LicenseHeaderStep.createFromHeader(getTestResource(KEY_LICENSE), LICENSE_HEADER_DELIMITER);
40+
assertOnResources(step, KEY_FILE_NOTAPPLIED, KEY_FILE_APPLIED);
3541
}
3642

3743
@Test
3844
public void fromFile() throws Throwable {
39-
LicenseHeaderStep step = new LicenseHeaderStep(createTestFile(KEY_LICENSE), StandardCharsets.UTF_8, JavaExtension.LICENSE_HEADER_DELIMITER);
40-
assertOnResources(step::format, KEY_FILE_NOTAPPLIED, KEY_FILE_APPLIED);
45+
FormatterStep step = LicenseHeaderStep.createFromFile(createTestFile(KEY_LICENSE), StandardCharsets.UTF_8, LICENSE_HEADER_DELIMITER);
46+
assertOnResources(step, KEY_FILE_NOTAPPLIED, KEY_FILE_APPLIED);
4147
}
4248

4349
@Test
4450
public void efficient() throws Throwable {
45-
LicenseHeaderStep step = new LicenseHeaderStep("LicenseHeader\n", "contentstart");
51+
FormatterStep step = LicenseHeaderStep.createFromHeader("LicenseHeader\n", "contentstart");
4652
String alreadyCorrect = "LicenseHeader\ncontentstart";
47-
Assert.assertEquals(alreadyCorrect, step.format(alreadyCorrect));
53+
Assert.assertEquals(alreadyCorrect, step.format(alreadyCorrect, new File("")));
4854
// If no change is required, it should return the exact same string for efficiency reasons
49-
Assert.assertSame(alreadyCorrect, step.format(alreadyCorrect));
55+
Assert.assertSame(alreadyCorrect, step.format(alreadyCorrect, new File("")));
5056
}
5157

5258
@Test
5359
public void sanitized() throws Throwable {
5460
// The sanitizer should add a \n
55-
LicenseHeaderStep step = new LicenseHeaderStep("LicenseHeader", "contentstart");
61+
FormatterStep step = LicenseHeaderStep.createFromHeader("LicenseHeader", "contentstart");
5662
String alreadyCorrect = "LicenseHeader\ncontentstart";
57-
Assert.assertEquals(alreadyCorrect, step.format(alreadyCorrect));
58-
Assert.assertSame(alreadyCorrect, step.format(alreadyCorrect));
63+
Assert.assertEquals(alreadyCorrect, step.format(alreadyCorrect, new File("")));
64+
Assert.assertSame(alreadyCorrect, step.format(alreadyCorrect, new File("")));
5965
}
6066

6167
@Test
6268
public void sanitizerDoesntGoTooFar() throws Throwable {
6369
// if the user wants extra lines after the header, we shouldn't clobber them
64-
LicenseHeaderStep step = new LicenseHeaderStep("LicenseHeader\n\n", "contentstart");
70+
FormatterStep step = LicenseHeaderStep.createFromHeader("LicenseHeader\n\n", "contentstart");
6571
String alreadyCorrect = "LicenseHeader\n\ncontentstart";
66-
Assert.assertEquals(alreadyCorrect, step.format(alreadyCorrect));
67-
Assert.assertSame(alreadyCorrect, step.format(alreadyCorrect));
72+
Assert.assertEquals(alreadyCorrect, step.format(alreadyCorrect, new File("")));
73+
Assert.assertSame(alreadyCorrect, step.format(alreadyCorrect, new File("")));
6874
}
6975
}

0 commit comments

Comments
 (0)