From 066be44336f0fe1f618f57ba9e6efaa30a533eda Mon Sep 17 00:00:00 2001 From: j-keck Date: Sat, 20 Jan 2018 19:14:54 +0100 Subject: [PATCH] align exception from file-timestamp functions from different impl. * The native implementations to get / set a file-timestamp throws a 'java.io.FileNotFoundException' where the Java based implementation throws a 'java.nio.file.NoSuchFileException' if the given file does not exists. * 'IO.getModifiedTimeOrZero' and 'IO.setModifiedTimeOrFalse' handles only 'java.io.FileNotFoundException' * Map 'NoSuchFileExeption' to 'FileNotFoundException' in the Java based implementation, so all implementations throws the same error. --- io/src/main/scala/sbt/internal/io/Milli.scala | 20 +++++++++++++------ io/src/test/scala/sbt/io/IOSpec.scala | 4 ++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/io/src/main/scala/sbt/internal/io/Milli.scala b/io/src/main/scala/sbt/internal/io/Milli.scala index a7bb32ef..b9dfb4e4 100644 --- a/io/src/main/scala/sbt/internal/io/Milli.scala +++ b/io/src/main/scala/sbt/internal/io/Milli.scala @@ -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 @@ -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 { diff --git a/io/src/test/scala/sbt/io/IOSpec.scala b/io/src/test/scala/sbt/io/IOSpec.scala index 11621eb7..89711800 100644 --- a/io/src/test/scala/sbt/io/IOSpec.scala +++ b/io/src/test/scala/sbt/io/IOSpec.scala @@ -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) + } }