Skip to content

Commit

Permalink
AbsoluteFile: turn into a final AnyVal class
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Nov 6, 2021
1 parent fb15770 commit b66c37c
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion scalafmt-cli/src/main/scala/org/scalafmt/cli/Cli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions scalafmt-cli/src/main/scala/org/scalafmt/cli/CliOptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ 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,
debug: PrintStream = NoopOutputStream.printStream,
info: PrintStream = NoopOutputStream.printStream
) {
lazy val workingDirectory: AbsoluteFile =
Option(cwd).getOrElse(AbsoluteFile.userDir)
cwd.getOrElse(AbsoluteFile.userDir)
}

case class CliOptions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")))
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
21 changes: 11 additions & 10 deletions scalafmt-tests/src/test/scala/org/scalafmt/util/GitOpsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit b66c37c

Please sign in to comment.