From 0b740f66f5aa9188fdee1c1272a1eb6cc328026b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93lafur=20P=C3=A1ll=20Geirsson?= Date: Tue, 5 Jun 2018 20:17:22 +0200 Subject: [PATCH 1/4] Use default value for isWindows check, fixes #1207 --- .../shared/src/main/scala/org/scalafmt/util/OsSpecific.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/OsSpecific.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/OsSpecific.scala index 6e2e28dbac..ea7cedb44a 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/OsSpecific.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/OsSpecific.scala @@ -3,8 +3,8 @@ package org.scalafmt.util /** Utils related to differences between various operating systems. */ object OsSpecific { val isWindows: Boolean = - System.getProperty("os.name").toLowerCase().startsWith("windows") val lineSeparator: String = System.getProperty("line.separator") + System.getProperty("os.name", "unknown").toLowerCase().startsWith("windows") def fixSeparatorsInPathPattern(unixSpecificPattern: String): String = if (isWindows) unixSpecificPattern.replace("/", """\\""") From d94e8c37fabcc01ccbd4954bf519c5375deac721 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93lafur=20P=C3=A1ll=20Geirsson?= Date: Tue, 5 Jun 2018 20:17:59 +0200 Subject: [PATCH 2/4] Remove redundant OsSpecific.lineSeparator --- .../shared/src/main/scala/org/scalafmt/util/OsSpecific.scala | 1 - scalafmt-tests/src/test/scala/org/scalafmt/EmptyFileTest.scala | 2 +- scalafmt-tests/src/test/scala/org/scalafmt/util/HasTests.scala | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/OsSpecific.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/OsSpecific.scala index ea7cedb44a..82d50b8aed 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/OsSpecific.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/OsSpecific.scala @@ -3,7 +3,6 @@ package org.scalafmt.util /** Utils related to differences between various operating systems. */ object OsSpecific { val isWindows: Boolean = - val lineSeparator: String = System.getProperty("line.separator") System.getProperty("os.name", "unknown").toLowerCase().startsWith("windows") def fixSeparatorsInPathPattern(unixSpecificPattern: String): String = diff --git a/scalafmt-tests/src/test/scala/org/scalafmt/EmptyFileTest.scala b/scalafmt-tests/src/test/scala/org/scalafmt/EmptyFileTest.scala index 08d0dfbc5c..8813a5ef0e 100644 --- a/scalafmt-tests/src/test/scala/org/scalafmt/EmptyFileTest.scala +++ b/scalafmt-tests/src/test/scala/org/scalafmt/EmptyFileTest.scala @@ -1,7 +1,7 @@ package org.scalafmt import org.scalafmt.util.DiffAssertions -import org.scalafmt.util.OsSpecific.lineSeparator +import java.lang.System.lineSeparator import org.scalatest.FunSuite class EmptyFileTest extends FunSuite with DiffAssertions { diff --git a/scalafmt-tests/src/test/scala/org/scalafmt/util/HasTests.scala b/scalafmt-tests/src/test/scala/org/scalafmt/util/HasTests.scala index 0776ed1a3d..ebf5d1f8d0 100644 --- a/scalafmt-tests/src/test/scala/org/scalafmt/util/HasTests.scala +++ b/scalafmt-tests/src/test/scala/org/scalafmt/util/HasTests.scala @@ -83,7 +83,7 @@ trait HasTests extends FunSuiteLike with FormatAssertions { def parseDiffTests(content: String, filename: String): Seq[DiffTest] = { val sep = - if (content.contains(OsSpecific.lineSeparator)) OsSpecific.lineSeparator + if (content.contains(System.lineSeparator)) System.lineSeparator else "\n" val spec = filename.stripPrefix(testDir + File.separator) val moduleOnly = isOnly(content) From 981a302f21a9e2ac9880f08c40d5bddf097f6d08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93lafur=20P=C3=A1ll=20Geirsson?= Date: Tue, 5 Jun 2018 20:56:08 +0200 Subject: [PATCH 3/4] Use java.io.File.separator to implement Windows check. Adds a unit test to make sure config.project.matcher works the same on JVM/JS. --- .../scala/org/scalafmt/util/OsSpecific.scala | 6 ++++-- .../org/scalafmt/ScalafmtConfigTest.scala | 21 +++++++++++++++++++ .../scala/org/scalafmt/ScalafmtTest.scala | 1 + 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 scalafmt-core/shared/src/test/scala/org/scalafmt/ScalafmtConfigTest.scala diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/OsSpecific.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/OsSpecific.scala index 82d50b8aed..6e4aaa7298 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/OsSpecific.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/OsSpecific.scala @@ -2,8 +2,10 @@ package org.scalafmt.util /** Utils related to differences between various operating systems. */ object OsSpecific { - val isWindows: Boolean = - System.getProperty("os.name", "unknown").toLowerCase().startsWith("windows") + def isWindows: Boolean = + // NOTE: org.scalameta:io implements java.io.File for Node.js so this will work correctly + // on Node.js + Window and also in the browser. + java.io.File.separatorChar == '\\' def fixSeparatorsInPathPattern(unixSpecificPattern: String): String = if (isWindows) unixSpecificPattern.replace("/", """\\""") diff --git a/scalafmt-core/shared/src/test/scala/org/scalafmt/ScalafmtConfigTest.scala b/scalafmt-core/shared/src/test/scala/org/scalafmt/ScalafmtConfigTest.scala new file mode 100644 index 0000000000..be7293993d --- /dev/null +++ b/scalafmt-core/shared/src/test/scala/org/scalafmt/ScalafmtConfigTest.scala @@ -0,0 +1,21 @@ +package org.scalafmt + +class ScalafmtConfigTest extends org.scalatest.FunSuite { + + test("project.matcher") { + val config = Scalafmt + .parseHoconConfig( + """ + |project.excludeFilters = [ + | "scalafmt-benchmarks/src/resources" + | "sbt-test" + | "bin/issue" + |] + """.stripMargin + ) + .get + assert(config.project.matcher.matches("qux/Kazbar.scala")) + assert(!config.project.matcher.matches("sbt-test/src/main")) + } + +} diff --git a/scalafmt-core/shared/src/test/scala/org/scalafmt/ScalafmtTest.scala b/scalafmt-core/shared/src/test/scala/org/scalafmt/ScalafmtTest.scala index 6debf32523..1bfdb510dc 100644 --- a/scalafmt-core/shared/src/test/scala/org/scalafmt/ScalafmtTest.scala +++ b/scalafmt-core/shared/src/test/scala/org/scalafmt/ScalafmtTest.scala @@ -50,4 +50,5 @@ class ScalafmtTest extends org.scalatest.FunSuite { |""".stripMargin, config.ScalafmtConfig.default40 ) + } From fa9c2abc73501b1997d4b1a4da601e9b9d054955 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93lafur=20P=C3=A1ll=20Geirsson?= Date: Tue, 5 Jun 2018 20:59:45 +0200 Subject: [PATCH 4/4] Implement fixSeparatorsInPathPattern with java.io.File.separatorChar --- .../shared/src/main/scala/org/scalafmt/util/OsSpecific.scala | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/OsSpecific.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/OsSpecific.scala index 6e4aaa7298..9d8dc327d7 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/OsSpecific.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/OsSpecific.scala @@ -8,8 +8,7 @@ object OsSpecific { java.io.File.separatorChar == '\\' def fixSeparatorsInPathPattern(unixSpecificPattern: String): String = - if (isWindows) unixSpecificPattern.replace("/", """\\""") - else unixSpecificPattern + unixSpecificPattern.replace('/', java.io.File.separatorChar) implicit class XtensionStringAsFilename(string: String) { def asFilename: String = fixSeparatorsInPathPattern(string)