From 719aa09b262c0fd40b314948a37c54524f86d3ed Mon Sep 17 00:00:00 2001 From: Alex Simkin Date: Fri, 20 Aug 2021 06:43:02 +1000 Subject: [PATCH] Bump default version. Add tests --- .../diffplug/spotless/scala/ScalaFmtStep.java | 4 +-- .../gradle/spotless/ScalaExtensionTest.java | 4 +-- .../spotless/maven/IncludesExcludesTest.java | 2 +- .../maven/MultiModuleProjectTest.java | 4 +-- .../spotless/maven/scala/ScalafmtTest.java | 4 +-- .../scalafmt/basic.cleanWithCustomConf_3.0.0 | 27 +++++++++++++++++++ .../scala/scalafmt/basic.clean_3.0.0 | 20 ++++++++++++++ .../scala/scalafmt/basicPost3.0.0.clean | 20 ++++++++++++++ .../basicPost3.0.0.cleanWithCustomConf | 27 +++++++++++++++++++ .../spotless/scala/ScalaFmtStepTest.java | 22 +++++++++++++++ 10 files changed, 125 insertions(+), 9 deletions(-) create mode 100644 testlib/src/main/resources/scala/scalafmt/basic.cleanWithCustomConf_3.0.0 create mode 100644 testlib/src/main/resources/scala/scalafmt/basic.clean_3.0.0 create mode 100644 testlib/src/main/resources/scala/scalafmt/basicPost3.0.0.clean create mode 100644 testlib/src/main/resources/scala/scalafmt/basicPost3.0.0.cleanWithCustomConf diff --git a/lib/src/main/java/com/diffplug/spotless/scala/ScalaFmtStep.java b/lib/src/main/java/com/diffplug/spotless/scala/ScalaFmtStep.java index 2ab1942381..7667a78447 100644 --- a/lib/src/main/java/com/diffplug/spotless/scala/ScalaFmtStep.java +++ b/lib/src/main/java/com/diffplug/spotless/scala/ScalaFmtStep.java @@ -34,14 +34,14 @@ import com.diffplug.spotless.JarState; import com.diffplug.spotless.Provisioner; -/** Wraps up [scalafmt](https://github.com/olafurpg/scalafmt) as a FormatterStep. */ +/** Wraps up [scalafmt](https://github.com/scalameta/scalafmt) as a FormatterStep. */ public class ScalaFmtStep { // prevent direct instantiation private ScalaFmtStep() {} private static final Pattern VERSION_PRE_2_0 = Pattern.compile("[10]\\.(\\d+)\\.\\d+"); private static final Pattern VERSION_PRE_3_0 = Pattern.compile("2\\.(\\d+)\\.\\d+"); - private static final String DEFAULT_VERSION = "2.0.1"; + private static final String DEFAULT_VERSION = "3.0.0"; static final String NAME = "scalafmt"; static final String MAVEN_COORDINATE_PRE_2_0 = "com.geirsson:scalafmt-core_2.11:"; static final String MAVEN_COORDINATE_PRE_3_0 = "org.scalameta:scalafmt-core_2.11:"; diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ScalaExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ScalaExtensionTest.java index aa3bf7e926..865c660712 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ScalaExtensionTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ScalaExtensionTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 DiffPlug + * Copyright 2016-2021 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,6 +36,6 @@ public void integration() throws IOException { setFile("scalafmt.conf").toResource("scala/scalafmt/scalafmt.conf"); setFile("src/main/scala/basic.scala").toResource("scala/scalafmt/basic.dirty"); gradleRunner().withArguments("spotlessApply").build(); - assertFile("src/main/scala/basic.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf_2.0.1"); + assertFile("src/main/scala/basic.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf_3.0.0"); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/IncludesExcludesTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/IncludesExcludesTest.java index 2d3c5d4ea3..509cf12d97 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/IncludesExcludesTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/IncludesExcludesTest.java @@ -24,7 +24,7 @@ public class IncludesExcludesTest extends MavenIntegrationHarness { private static final String JAVA_FORMATTED = "java/eclipse/JavaCodeFormatted.test"; private static final String JAVA_UNFORMATTED = "java/eclipse/JavaCodeUnformatted.test"; private static final String SCALA_UNFORMATTED = "scala/scalafmt/basic.dirty"; - private static final String SCALA_FORMATTED = "scala/scalafmt/basic.clean_2.0.1"; + private static final String SCALA_FORMATTED = "scala/scalafmt/basic.clean_3.0.0"; @Test public void testDefaultIncludesJava() throws Exception { diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MultiModuleProjectTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MultiModuleProjectTest.java index b2206a7ab6..328608c96d 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MultiModuleProjectTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MultiModuleProjectTest.java @@ -93,7 +93,7 @@ public void testConfigurationDependency() throws Exception { assertFile("two/src/main/java/test1.java").sameAsResource("java/eclipse/JavaCodeFormatted.test"); assertFile("two/src/test/java/test2.java").sameAsResource("java/eclipse/JavaCodeFormatted.test"); - assertFile("three/src/main/scala/test1.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf_2.0.1"); - assertFile("three/src/test/scala/test2.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf_2.0.1"); + assertFile("three/src/main/scala/test1.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf_3.0.0"); + assertFile("three/src/test/scala/test2.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf_3.0.0"); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/scala/ScalafmtTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/scala/ScalafmtTest.java index c1837d92e4..80de1b734d 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/scala/ScalafmtTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/scala/ScalafmtTest.java @@ -24,7 +24,7 @@ public class ScalafmtTest extends MavenIntegrationHarness { public void testScalafmtWithDefaultConfig() throws Exception { writePomWithScalaSteps(""); - runTest("scala/scalafmt/basic.clean_2.0.1"); + runTest("scala/scalafmt/basic.clean_3.0.0"); } @Test @@ -36,7 +36,7 @@ public void testScalafmtWithCustomConfig() throws Exception { " ${project.basedir}/scalafmt.conf", ""); - runTest("scala/scalafmt/basic.cleanWithCustomConf_2.0.1"); + runTest("scala/scalafmt/basic.cleanWithCustomConf_3.0.0"); } private void runTest(String s) throws Exception { diff --git a/testlib/src/main/resources/scala/scalafmt/basic.cleanWithCustomConf_3.0.0 b/testlib/src/main/resources/scala/scalafmt/basic.cleanWithCustomConf_3.0.0 new file mode 100644 index 0000000000..fae835000a --- /dev/null +++ b/testlib/src/main/resources/scala/scalafmt/basic.cleanWithCustomConf_3.0.0 @@ -0,0 +1,27 @@ +@foobar( + "annot", { + val x = 2 + val y = 2 // y=2 + x + y + } +) +object a + extends b + with c { + def foo[ + T: Int#Double#Triple, + R <% String + ]( + @annot1 + x: Int @annot2 = + 2, + y: Int = 3 + ): Int = { + "match" match { + case 1 | 2 => + 3 + case 2 => + 2 + } + } +} diff --git a/testlib/src/main/resources/scala/scalafmt/basic.clean_3.0.0 b/testlib/src/main/resources/scala/scalafmt/basic.clean_3.0.0 new file mode 100644 index 0000000000..b531873bfa --- /dev/null +++ b/testlib/src/main/resources/scala/scalafmt/basic.clean_3.0.0 @@ -0,0 +1,20 @@ +@foobar( + "annot", { + val x = 2 + val y = 2 // y=2 + x + y + } +) +object a extends b with c { + def foo[T: Int#Double#Triple, R <% String]( + @annot1 + x: Int @annot2 = 2, + y: Int = 3 + ): Int = { + "match" match { + case 1 | 2 => + 3 + case 2 => 2 + } + } +} diff --git a/testlib/src/main/resources/scala/scalafmt/basicPost3.0.0.clean b/testlib/src/main/resources/scala/scalafmt/basicPost3.0.0.clean new file mode 100644 index 0000000000..b531873bfa --- /dev/null +++ b/testlib/src/main/resources/scala/scalafmt/basicPost3.0.0.clean @@ -0,0 +1,20 @@ +@foobar( + "annot", { + val x = 2 + val y = 2 // y=2 + x + y + } +) +object a extends b with c { + def foo[T: Int#Double#Triple, R <% String]( + @annot1 + x: Int @annot2 = 2, + y: Int = 3 + ): Int = { + "match" match { + case 1 | 2 => + 3 + case 2 => 2 + } + } +} diff --git a/testlib/src/main/resources/scala/scalafmt/basicPost3.0.0.cleanWithCustomConf b/testlib/src/main/resources/scala/scalafmt/basicPost3.0.0.cleanWithCustomConf new file mode 100644 index 0000000000..fae835000a --- /dev/null +++ b/testlib/src/main/resources/scala/scalafmt/basicPost3.0.0.cleanWithCustomConf @@ -0,0 +1,27 @@ +@foobar( + "annot", { + val x = 2 + val y = 2 // y=2 + x + y + } +) +object a + extends b + with c { + def foo[ + T: Int#Double#Triple, + R <% String + ]( + @annot1 + x: Int @annot2 = + 2, + y: Int = 3 + ): Int = { + "match" match { + case 1 | 2 => + 3 + case 2 => + 2 + } + } +} diff --git a/testlib/src/test/java/com/diffplug/spotless/scala/ScalaFmtStepTest.java b/testlib/src/test/java/com/diffplug/spotless/scala/ScalaFmtStepTest.java index 4e789bf32b..982510df97 100644 --- a/testlib/src/test/java/com/diffplug/spotless/scala/ScalaFmtStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/scala/ScalaFmtStepTest.java @@ -39,6 +39,8 @@ public void behaviorDefaultConfig() throws Exception { .testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.clean_1.1.0"); StepHarness.forStep(ScalaFmtStep.create("2.0.1", TestProvisioner.mavenCentral(), null)) .testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.clean_2.0.1"); + StepHarness.forStep(ScalaFmtStep.create("3.0.0", TestProvisioner.mavenCentral(), null)) + .testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.clean_3.0.0"); } @Test @@ -47,6 +49,8 @@ public void behaviorCustomConfig() throws Exception { .testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.cleanWithCustomConf_1.1.0"); StepHarness.forStep(ScalaFmtStep.create("2.0.1", TestProvisioner.mavenCentral(), createTestFile("scala/scalafmt/scalafmt.conf"))) .testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.cleanWithCustomConf_2.0.1"); + StepHarness.forStep(ScalaFmtStep.create("3.0.0", TestProvisioner.mavenCentral(), createTestFile("scala/scalafmt/scalafmt.conf"))) + .testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.cleanWithCustomConf_3.0.0"); } @Test @@ -63,6 +67,20 @@ public void behaviorCustomConfigVersion_2_0_0() throws Exception { .testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basicPost2.0.0.cleanWithCustomConf"); } + @Test + public void behaviorDefaultConfigVersion_3_0_0() throws Exception { + FormatterStep step = ScalaFmtStep.create("3.0.0", TestProvisioner.mavenCentral(), null); + StepHarness.forStep(step) + .testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basicPost3.0.0.clean"); + } + + @Test + public void behaviorCustomConfigVersion_3_0_0() throws Exception { + FormatterStep step = ScalaFmtStep.create("3.0.0", TestProvisioner.mavenCentral(), createTestFile("scala/scalafmt/scalafmt.conf")); + StepHarness.forStep(step) + .testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basicPost3.0.0.cleanWithCustomConf"); + } + @Test public void equality() throws Exception { new SerializableEqualityTester() { @@ -106,5 +124,9 @@ public void invalidConfiguration() throws Exception { exception = assertThrows(InvocationTargetException.class, () -> StepHarness.forStep(ScalaFmtStep.create("2.0.1", provisioner, invalidConfFile)).test("", "")); assertThat(exception.getTargetException().getMessage(), containsString("Invalid field: invalidScalaFmtConfigField")); + + exception = assertThrows(InvocationTargetException.class, + () -> StepHarness.forStep(ScalaFmtStep.create("3.0.0", provisioner, invalidConfFile)).test("", "")); + assertThat(exception.getTargetException().getMessage(), containsString("found option 'invalidScalaFmtConfigField' which wasn't expected")); } }