diff --git a/scalafmt-cli/src/main/scala/org/scalafmt/cli/Cli.scala b/scalafmt-cli/src/main/scala/org/scalafmt/cli/Cli.scala index b3e1e1416d..c825f81f7f 100644 --- a/scalafmt-cli/src/main/scala/org/scalafmt/cli/Cli.scala +++ b/scalafmt-cli/src/main/scala/org/scalafmt/cli/Cli.scala @@ -22,7 +22,7 @@ object Cli { nGContext.getArgs, CliOptions.default.copy( common = CliOptions.default.common.copy( - cwd = workingDirectory, + cwd = Some(workingDirectory), out = nGContext.out, in = nGContext.in, err = nGContext.err diff --git a/scalafmt-cli/src/main/scala/org/scalafmt/cli/CliOptions.scala b/scalafmt-cli/src/main/scala/org/scalafmt/cli/CliOptions.scala index e03e95af7b..b4ab2635dc 100644 --- a/scalafmt-cli/src/main/scala/org/scalafmt/cli/CliOptions.scala +++ b/scalafmt-cli/src/main/scala/org/scalafmt/cli/CliOptions.scala @@ -74,7 +74,7 @@ object NoopOutputStream extends OutputStream { self => } case class CommonOptions( - private val cwd: AbsoluteFile = null, + private val cwd: Option[AbsoluteFile] = None, out: PrintStream = System.out, in: InputStream = System.in, err: PrintStream = System.err, @@ -82,7 +82,7 @@ case class CommonOptions( info: PrintStream = NoopOutputStream.printStream ) { lazy val workingDirectory: AbsoluteFile = - Option(cwd).getOrElse(AbsoluteFile.userDir) + cwd.getOrElse(AbsoluteFile.userDir) } case class CliOptions( diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/AbsoluteFile.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/AbsoluteFile.scala index bb0970acfa..1d1903357b 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/AbsoluteFile.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/AbsoluteFile.scala @@ -6,12 +6,12 @@ import java.nio.file.Path import scala.io.Codec /** Wrapper around java.io.File with an absolute path. */ -sealed abstract case class AbsoluteFile(jfile: File) { +final class AbsoluteFile(val jfile: File) extends AnyVal { def path: String = jfile.getPath def exists: Boolean = jfile.exists() def /(other: String) = join(FileOps.getFile(other)) - def join(other: Path) = new AbsoluteFile(asPath.resolve(other).toFile) {} + def join(other: Path) = new AbsoluteFile(asPath.resolve(other).toFile) def join(files: Seq[Path]): Seq[AbsoluteFile] = files.map(join) @inline @@ -31,8 +31,8 @@ sealed abstract case class AbsoluteFile(jfile: File) { object AbsoluteFile { def fromPath(path: String): Option[AbsoluteFile] = { val file = new File(path) - if (file.isAbsolute) Some(new AbsoluteFile(file) {}) + if (file.isAbsolute) Some(new AbsoluteFile(file)) else None } - def userDir = new AbsoluteFile(new File(System.getProperty("user.dir"))) {} + def userDir = new AbsoluteFile(new File(System.getProperty("user.dir"))) } diff --git a/scalafmt-tests/src/test/scala/org/scalafmt/cli/CliTest.scala b/scalafmt-tests/src/test/scala/org/scalafmt/cli/CliTest.scala index 0f3f2f5b00..a149b8fdc3 100644 --- a/scalafmt-tests/src/test/scala/org/scalafmt/cli/CliTest.scala +++ b/scalafmt-tests/src/test/scala/org/scalafmt/cli/CliTest.scala @@ -441,7 +441,7 @@ trait CliTestBehavior { this: AbstractCliTest => val workingDir = input / "nested" val options: CliOptions = { val mock = getMockOptions(input, workingDir) - mock.copy(common = mock.common.copy(cwd = workingDir)) + mock.copy(common = mock.common.copy(cwd = Some(workingDir))) } val config = Cli.getConfig(Array("foo.scala"), options).get Cli.run(config) diff --git a/scalafmt-tests/src/test/scala/org/scalafmt/cli/FileTestOps.scala b/scalafmt-tests/src/test/scala/org/scalafmt/cli/FileTestOps.scala index de38be11cb..edee2be588 100644 --- a/scalafmt-tests/src/test/scala/org/scalafmt/cli/FileTestOps.scala +++ b/scalafmt-tests/src/test/scala/org/scalafmt/cli/FileTestOps.scala @@ -54,7 +54,7 @@ object FileTestOps { baseConfig = ScalafmtConfig.default, gitOpsConstructor = _ => new FakeGitOps(baseDir), common = CliOptions.default.common.copy( - cwd = workingDir, + cwd = Some(workingDir), out = out, err = out ) diff --git a/scalafmt-tests/src/test/scala/org/scalafmt/util/GitOpsTest.scala b/scalafmt-tests/src/test/scala/org/scalafmt/util/GitOpsTest.scala index d535068b35..86d812c37e 100644 --- a/scalafmt-tests/src/test/scala/org/scalafmt/util/GitOpsTest.scala +++ b/scalafmt-tests/src/test/scala/org/scalafmt/util/GitOpsTest.scala @@ -44,9 +44,9 @@ class GitOpsTest extends FunSuite { } def touch( name: String = Random.alphanumeric.take(10).mkString, - dir: AbsoluteFile = null + dir: Option[AbsoluteFile] = None ): AbsoluteFile = { - val d = Option(dir).orElse(ops.rootDir).get.jfile + val d = dir.orElse(ops.rootDir).get.jfile val f = File.createTempFile(name, ".ext", d) f.deleteOnExit() AbsoluteFile.fromPath(f.toString).get @@ -107,7 +107,8 @@ class GitOpsTest extends FunSuite { test("#1010: lsTree should return staged files") { val f = touch() add(f) - assert(ls.toSet == Set(f)) + val q = ls + assert(q.toSet == Set(f), q.mkString + " != " + f.toString()) } test("lsTree should return committed files") { @@ -141,7 +142,7 @@ class GitOpsTest extends FunSuite { add(f1) val innerDir = mkDir() - val f2 = touch(dir = innerDir) + val f2 = touch(dir = Some(innerDir)) add(f2) val innerGitOps = new GitOpsImpl(innerDir) @@ -156,10 +157,10 @@ class GitOpsTest extends FunSuite { assert(ls.toSet == Set(f)) } - def diff(br: String = "HEAD", cwd: AbsoluteFile = null)(implicit + def diff(br: String = "HEAD", cwd: Option[AbsoluteFile] = None)(implicit ops: GitOpsImpl ): Seq[AbsoluteFile] = - ops.diff(br, Option(cwd)) + ops.diff(br, cwd) def status(implicit ops: GitOpsImpl): Seq[AbsoluteFile] = ops.status @@ -190,7 +191,7 @@ class GitOpsTest extends FunSuite { } test("diff should return added files against HEAD") { - val dir = mkDir("dir 1") + val dir = Some(mkDir("dir 1")) val f1 = touch() val f2 = touch(dir = dir) add(f1) @@ -204,7 +205,7 @@ class GitOpsTest extends FunSuite { add(f) commit checkoutBr("other") - val dir = mkDir("dir 1") + val dir = Some(mkDir("dir 1")) val f1 = touch() val f2 = touch(dir = dir) add(f1) @@ -221,7 +222,7 @@ class GitOpsTest extends FunSuite { add(f) commit checkoutBr("other") - val dir = mkDir("dir 1") + val dir = Some(mkDir("dir 1")) val f1 = touch() val f2 = touch(dir = dir) add(f1) @@ -275,7 +276,7 @@ class GitOpsTest extends FunSuite { } test("status should return files with spaces in the path") { - val dir = mkDir("dir 1") + val dir = Some(mkDir("dir 1")) val f = touch(dir = dir) add(f) assert(status.toSet == Set(f))