Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

align exception from file-timestamp functions from different impl. #121

Merged
merged 1 commit into from
Jan 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions io/src/main/scala/sbt/internal/io/Milli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import java.io.File
import java.util.Date
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.file.Files
import java.nio.file.{ Files, NoSuchFileException }
import java.nio.file.{ Paths => JPaths }
import java.nio.file.attribute.FileTime

Expand Down Expand Up @@ -319,11 +319,19 @@ private abstract class MilliMilliseconds extends Milli {

private object JavaMilli extends MilliMilliseconds {
def getModifiedTime(filePath: String): Long =
Files.getLastModifiedTime(JPaths.get(filePath)).toMillis
def setModifiedTime(filePath: String, mtime: Long): Unit = {
Files.setLastModifiedTime(JPaths.get(filePath), FileTime.fromMillis(mtime))
()
}
mapNoSuchFileException(Files.getLastModifiedTime(JPaths.get(filePath)).toMillis)
def setModifiedTime(filePath: String, mtime: Long): Unit =
mapNoSuchFileException {
Files.setLastModifiedTime(JPaths.get(filePath), FileTime.fromMillis(mtime))
()
}

private def mapNoSuchFileException[A](f: => A): A =
try {
f
} catch {
case e: NoSuchFileException => throw new FileNotFoundException(e.getFile).initCause(e)
}
}

object Milli {
Expand Down
4 changes: 4 additions & 0 deletions io/src/test/scala/sbt/io/IOSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ class IOSpec extends FlatSpec with Matchers {
val u = IO.toURI(file("src") / "main" / "scala")
assert(IO.toFile(u) == (file("src") / "main" / "scala"))
}

"getModifiedTimeOrZero" should "return 0L if the file doesn't exists" in {
assert(IO.getModifiedTimeOrZero(file("/not/existing/path")) == 0L)
}
}