From 4a588c3fe9df08192395bf18283a7fa9c0b177ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ferreira?= Date: Tue, 10 Dec 2024 18:51:55 +0000 Subject: [PATCH 1/5] Path.directory scripted test --- .../package/mappings-directory/build.sbt | 16 ++++++++++++++++ .../src/sbt-test/package/mappings-directory/test | 2 ++ .../mappings-directory/test-directory/test | 0 3 files changed, 18 insertions(+) create mode 100644 sbt-app/src/sbt-test/package/mappings-directory/build.sbt create mode 100644 sbt-app/src/sbt-test/package/mappings-directory/test create mode 100644 sbt-app/src/sbt-test/package/mappings-directory/test-directory/test diff --git a/sbt-app/src/sbt-test/package/mappings-directory/build.sbt b/sbt-app/src/sbt-test/package/mappings-directory/build.sbt new file mode 100644 index 000000000..ba90f90a7 --- /dev/null +++ b/sbt-app/src/sbt-test/package/mappings-directory/build.sbt @@ -0,0 +1,16 @@ +name := "Mappings Test" + +scalaVersion := "3.3.1" +version := "0.2" + +Compile / packageBin / mappings ++= { + val converter = fileConverter.value + Path.directory(file("test")).map { case (f,s) => converter.toVirtualFile(f.toPath) -> s } +} + +lazy val unzipPackage = taskKey[Unit]("extract jar file") +unzipPackage := { + val converter = fileConverter.value + val p = converter.toPath((Compile / packageBin).value) + IO.unzip(p.toFile(), target.value / "extracted") +} diff --git a/sbt-app/src/sbt-test/package/mappings-directory/test b/sbt-app/src/sbt-test/package/mappings-directory/test new file mode 100644 index 000000000..30589f08d --- /dev/null +++ b/sbt-app/src/sbt-test/package/mappings-directory/test @@ -0,0 +1,2 @@ +> unzipPackage +$ exists target/out/jvm/scala-3.3.1/mappings-test/extracted/test diff --git a/sbt-app/src/sbt-test/package/mappings-directory/test-directory/test b/sbt-app/src/sbt-test/package/mappings-directory/test-directory/test new file mode 100644 index 000000000..e69de29bb From f4ae48bf72c7c69a91e2bc259665a6179f2ca1cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ferreira?= Date: Tue, 10 Dec 2024 18:53:23 +0000 Subject: [PATCH 2/5] move Mapper from sbt/io --- main-actions/src/main/scala/sbt/Mapper.scala | 81 +++++++++++ .../src/test/scala/sbt/MapperSpec.scala | 130 ++++++++++++++++++ 2 files changed, 211 insertions(+) create mode 100644 main-actions/src/main/scala/sbt/Mapper.scala create mode 100644 main-actions/src/test/scala/sbt/MapperSpec.scala diff --git a/main-actions/src/main/scala/sbt/Mapper.scala b/main-actions/src/main/scala/sbt/Mapper.scala new file mode 100644 index 000000000..01efce99e --- /dev/null +++ b/main-actions/src/main/scala/sbt/Mapper.scala @@ -0,0 +1,81 @@ +package sbt + +import sbt.io.{ AllPassFilter, FileFilter, PathFinder } +import sbt.io.Path.* + +import java.io.File + +object Mapper { + + /** + * Selects all descendants of `base` directory and maps them to a path relative to `base`. + * `base` itself is not included. + */ + def allSubpaths(base: File): Seq[(File, String)] = + selectSubpaths(base, AllPassFilter) + + /** + * Selects descendants of `base` directory matching `filter` and maps them to a path relative to `base`. + * `base` itself is not included. + */ + def selectSubpaths(base: File, filter: FileFilter): Seq[(File, String)] = + PathFinder(base).globRecursive(filter).get().collect { + case f if f != base => f -> base.toPath.relativize(f.toPath).toString + } + + /** + * return a Seq of mappings which effect is to add a whole directory in the generated package + * + * @example In order to create mappings for a static directory "extra" add + * {{{ + * mappings ++= directory(baseDirectory.value / "extra") + * }}} + * + * The resulting mappings sequence will look something like this + * + * {{{ + * File(baseDirectory/extras) -> "extras" + * File(baseDirectory/extras/file1) -> "extras/file1" + * File(baseDirectory/extras/file2) -> "extras/file2" + * ... + * }}} + * + * @param baseDirectory The directory that should be turned into a mappings sequence. + * @return mappings The `baseDirectory` and all of its contents + */ + def directory(baseDirectory: File): Seq[(File, String)] = + Option(baseDirectory.getParentFile) + .map(parent => PathFinder(baseDirectory).allPaths pair relativeTo(parent)) + .getOrElse(PathFinder(baseDirectory).allPaths pair basic) + + /** + * return a Seq of mappings excluding the directory itself. + * + * @example In order to create mappings for a static directory "extra" add + * {{{ + * mappings ++= contentOf(baseDirectory.value / "extra") + * }}} + * + * The resulting mappings sequence will look something like this + * + * {{{ + * File(baseDirectory/extras/file1) -> "file1" + * File(baseDirectory/extras/file2) -> "file2" + * ... + * }}} + * + * @example Add a static directory "extra" and re-map the destination to a different path + * {{{ + * mappings ++= contentOf(baseDirectory.value / "extra").map { + * case (src, destination) => src -> s"new/path/destination" + * } + * }}} + * + * @param baseDirectory The directory that should be turned into a mappings sequence. + * @return mappings - The `basicDirectory`'s contents exlcuding `basicDirectory` itself + */ + def contentOf(baseDirectory: File): Seq[(File, String)] = ( + (PathFinder(baseDirectory).allPaths --- PathFinder(baseDirectory)) + pair relativeTo(baseDirectory) + ) +} diff --git a/main-actions/src/test/scala/sbt/MapperSpec.scala b/main-actions/src/test/scala/sbt/MapperSpec.scala new file mode 100644 index 000000000..cefe59da8 --- /dev/null +++ b/main-actions/src/test/scala/sbt/MapperSpec.scala @@ -0,0 +1,130 @@ +/* + * sbt IO + * Copyright Scala Center, Lightbend, and Mark Harrah + * + * Licensed under Apache License 2.0 + * SPDX-License-Identifier: Apache-2.0 + * + * See the NOTICE file distributed with this work for + * additional information regarding copyright ownership. + */ + +package sbt + +import java.nio.file.{ Files, Path => NioPath } + +import org.scalatest.Outcome +import org.scalatest.flatspec +import org.scalatest.matchers.should.Matchers +import sbt.io.IO +import sbt.io.syntax._ + +class MapperSpec extends flatspec.FixtureAnyFlatSpec with Matchers { + + type FixtureParam = NioPath + + "directory" should "create mappings including the baseDirectory" in { tempDirectory => + val nestedFile1 = Files.createFile(tempDirectory.resolve("file1")).toFile + val nestedFile2 = Files.createFile(tempDirectory.resolve("file2")).toFile + val nestedDir = Files.createDirectory(tempDirectory.resolve("dir1")) + val nestedDirFile = Files.createDirectory(nestedDir.resolve("dir1-file1")).toFile + + IO.touch(nestedFile1) + IO.touch(nestedFile2) + IO.createDirectory(nestedDir.toFile) + IO.touch(nestedDirFile) + + val mappings = Mapper.directory(tempDirectory.toFile).map { case (f, s) => (f, file(s)) } + + mappings should contain theSameElementsAs List( + tempDirectory.toFile -> file(s"${tempDirectory.getFileName}"), + nestedFile1 -> file(s"${tempDirectory.getFileName}/file1"), + nestedFile2 -> file(s"${tempDirectory.getFileName}/file2"), + nestedDir.toFile -> file(s"${tempDirectory.getFileName}/dir1"), + nestedDirFile -> file(s"${tempDirectory.getFileName}/dir1/dir1-file1") + ) + } + + it should "create one mapping entry for an empty directory" in { tempDirectory => + val mappings = Mapper.directory(tempDirectory.toFile) + + mappings should contain theSameElementsAs List[(File, String)]( + tempDirectory.toFile -> s"${tempDirectory.getFileName}" + ) + } + + it should "create an empty mappings sequence for a non-existing directory" in { tempDirectory => + val nonExistingDirectory = tempDirectory.resolve("imaginary") + val mappings = Mapper.directory(nonExistingDirectory.toFile) + + mappings should be(empty) + } + + it should "create one mapping entry if the directory is a file" in { tempDirectory => + val file = tempDirectory.resolve("file").toFile + IO.touch(file) + val mappings = Mapper.directory(file) + + mappings should contain theSameElementsAs List[(File, String)]( + file -> s"${file.getName}" + ) + } + + "contentOf" should "create mappings excluding the baseDirectory" in { tempDirectory => + val nestedFile1 = Files.createFile(tempDirectory.resolve("file1")).toFile + val nestedFile2 = Files.createFile(tempDirectory.resolve("file2")).toFile + val nestedDir = Files.createDirectory(tempDirectory.resolve("dir1")) + val nestedDirFile = Files.createDirectory(nestedDir.resolve("dir1-file1")).toFile + + IO.touch(nestedFile1) + IO.touch(nestedFile2) + IO.createDirectory(nestedDir.toFile) + IO.touch(nestedDirFile) + + val mappings = Mapper.contentOf(tempDirectory.toFile).map { case (f, s) => (f, file(s)) } + + mappings should contain theSameElementsAs List( + nestedFile1 -> file(s"file1"), + nestedFile2 -> file(s"file2"), + nestedDir.toFile -> file(s"dir1"), + nestedDirFile -> file(s"dir1/dir1-file1") + ) + } + + it should "create an empty mappings sequence for an empty directory" in { tempDirectory => + val mappings = Mapper.contentOf(tempDirectory.toFile) + + mappings should be(empty) + } + + it should "create an empty mappings sequence for a non-existing directory" in { tempDirectory => + val nonExistingDirectory = tempDirectory.resolve("imaginary") + val mappings = Mapper.contentOf(nonExistingDirectory.toFile) + + mappings should be(empty) + } + + it should "create an empty mappings sequence if the directory is a file" in { tempDirectory => + val file = tempDirectory.resolve("file").toFile + val mappings = Mapper.contentOf(file) + + mappings should be(empty) + } + + "allSubpaths" should "not include the base directory" in { tempDirectory => + val file = Files.createFile(tempDirectory.resolve("file")) + val paths = Mapper.allSubpaths(tempDirectory.toFile).toVector.map(_._1.toPath).toSet + assert(paths.contains(file)) + assert(!paths.contains(tempDirectory)) + } + + override protected def withFixture(test: OneArgTest): Outcome = { + val tmpDir = Files.createTempDirectory("mappings") + try { + withFixture(test.toNoArgTest(tmpDir)) + } finally { + // cleanup an delete the temp directory + IO.delete(tmpDir.toFile) + } + } +} From a2b046e0c31c3b1c563b0b75116c6a90e7004079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ferreira?= Date: Tue, 10 Dec 2024 19:22:24 +0000 Subject: [PATCH 3/5] translate mapper to use VirtualFile --- main-actions/src/main/scala/sbt/Mapper.scala | 13 +++++++++---- .../sbt-test/package/mappings-directory/build.sbt | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/main-actions/src/main/scala/sbt/Mapper.scala b/main-actions/src/main/scala/sbt/Mapper.scala index 01efce99e..bb43a6301 100644 --- a/main-actions/src/main/scala/sbt/Mapper.scala +++ b/main-actions/src/main/scala/sbt/Mapper.scala @@ -2,6 +2,7 @@ package sbt import sbt.io.{ AllPassFilter, FileFilter, PathFinder } import sbt.io.Path.* +import xsbti.{ FileConverter, VirtualFile } import java.io.File @@ -11,16 +12,19 @@ object Mapper { * Selects all descendants of `base` directory and maps them to a path relative to `base`. * `base` itself is not included. */ - def allSubpaths(base: File): Seq[(File, String)] = + def allSubpaths(base: File)(implicit conv: FileConverter): Seq[(VirtualFile, String)] = selectSubpaths(base, AllPassFilter) /** * Selects descendants of `base` directory matching `filter` and maps them to a path relative to `base`. * `base` itself is not included. */ - def selectSubpaths(base: File, filter: FileFilter): Seq[(File, String)] = + def selectSubpaths(base: File, filter: FileFilter)(implicit + conv: FileConverter + ): Seq[(VirtualFile, String)] = PathFinder(base).globRecursive(filter).get().collect { - case f if f != base => f -> base.toPath.relativize(f.toPath).toString + case f if f != base => + conv.toVirtualFile(f.toPath) -> base.toPath.relativize(f.toPath).toString } /** @@ -43,10 +47,11 @@ object Mapper { * @param baseDirectory The directory that should be turned into a mappings sequence. * @return mappings The `baseDirectory` and all of its contents */ - def directory(baseDirectory: File): Seq[(File, String)] = + def directory(baseDirectory: File)(implicit conv: FileConverter): Seq[(VirtualFile, String)] = Option(baseDirectory.getParentFile) .map(parent => PathFinder(baseDirectory).allPaths pair relativeTo(parent)) .getOrElse(PathFinder(baseDirectory).allPaths pair basic) + .map { case (f, s) => conv.toVirtualFile(f.toPath) -> s } /** * return a Seq of mappings excluding the directory itself. diff --git a/sbt-app/src/sbt-test/package/mappings-directory/build.sbt b/sbt-app/src/sbt-test/package/mappings-directory/build.sbt index ba90f90a7..7e6a058a3 100644 --- a/sbt-app/src/sbt-test/package/mappings-directory/build.sbt +++ b/sbt-app/src/sbt-test/package/mappings-directory/build.sbt @@ -5,7 +5,7 @@ version := "0.2" Compile / packageBin / mappings ++= { val converter = fileConverter.value - Path.directory(file("test")).map { case (f,s) => converter.toVirtualFile(f.toPath) -> s } + Mapper.directory(file("test"))(using converter) } lazy val unzipPackage = taskKey[Unit]("extract jar file") From 0b744c1ded72263770b14580a0ea153f04e9b8d0 Mon Sep 17 00:00:00 2001 From: Eugene Yokota Date: Mon, 16 Dec 2024 00:22:29 -0500 Subject: [PATCH 4/5] Port MapperTest --- main-actions/src/main/scala/sbt/Mapper.scala | 16 +- .../src/test/scala/sbt/MapperSpec.scala | 130 -------------- .../src/test/scala/sbt/MapperTest.scala | 161 ++++++++++++++++++ 3 files changed, 169 insertions(+), 138 deletions(-) delete mode 100644 main-actions/src/test/scala/sbt/MapperSpec.scala create mode 100644 main-actions/src/test/scala/sbt/MapperTest.scala diff --git a/main-actions/src/main/scala/sbt/Mapper.scala b/main-actions/src/main/scala/sbt/Mapper.scala index bb43a6301..728a67b36 100644 --- a/main-actions/src/main/scala/sbt/Mapper.scala +++ b/main-actions/src/main/scala/sbt/Mapper.scala @@ -6,20 +6,20 @@ import xsbti.{ FileConverter, VirtualFile } import java.io.File -object Mapper { +object Mapper: /** * Selects all descendants of `base` directory and maps them to a path relative to `base`. * `base` itself is not included. */ - def allSubpaths(base: File)(implicit conv: FileConverter): Seq[(VirtualFile, String)] = + def allSubpaths(base: File)(using conv: FileConverter): Seq[(VirtualFile, String)] = selectSubpaths(base, AllPassFilter) /** * Selects descendants of `base` directory matching `filter` and maps them to a path relative to `base`. * `base` itself is not included. */ - def selectSubpaths(base: File, filter: FileFilter)(implicit + def selectSubpaths(base: File, filter: FileFilter)(using conv: FileConverter ): Seq[(VirtualFile, String)] = PathFinder(base).globRecursive(filter).get().collect { @@ -47,7 +47,7 @@ object Mapper { * @param baseDirectory The directory that should be turned into a mappings sequence. * @return mappings The `baseDirectory` and all of its contents */ - def directory(baseDirectory: File)(implicit conv: FileConverter): Seq[(VirtualFile, String)] = + def directory(baseDirectory: File)(using conv: FileConverter): Seq[(VirtualFile, String)] = Option(baseDirectory.getParentFile) .map(parent => PathFinder(baseDirectory).allPaths pair relativeTo(parent)) .getOrElse(PathFinder(baseDirectory).allPaths pair basic) @@ -79,8 +79,8 @@ object Mapper { * @param baseDirectory The directory that should be turned into a mappings sequence. * @return mappings - The `basicDirectory`'s contents exlcuding `basicDirectory` itself */ - def contentOf(baseDirectory: File): Seq[(File, String)] = ( + def contentOf(baseDirectory: File)(using conv: FileConverter): Seq[(VirtualFile, String)] = (PathFinder(baseDirectory).allPaths --- PathFinder(baseDirectory)) - pair relativeTo(baseDirectory) - ) -} + .pair(relativeTo(baseDirectory)) + .map { case (f, s) => conv.toVirtualFile(f.toPath) -> s } +end Mapper diff --git a/main-actions/src/test/scala/sbt/MapperSpec.scala b/main-actions/src/test/scala/sbt/MapperSpec.scala deleted file mode 100644 index cefe59da8..000000000 --- a/main-actions/src/test/scala/sbt/MapperSpec.scala +++ /dev/null @@ -1,130 +0,0 @@ -/* - * sbt IO - * Copyright Scala Center, Lightbend, and Mark Harrah - * - * Licensed under Apache License 2.0 - * SPDX-License-Identifier: Apache-2.0 - * - * See the NOTICE file distributed with this work for - * additional information regarding copyright ownership. - */ - -package sbt - -import java.nio.file.{ Files, Path => NioPath } - -import org.scalatest.Outcome -import org.scalatest.flatspec -import org.scalatest.matchers.should.Matchers -import sbt.io.IO -import sbt.io.syntax._ - -class MapperSpec extends flatspec.FixtureAnyFlatSpec with Matchers { - - type FixtureParam = NioPath - - "directory" should "create mappings including the baseDirectory" in { tempDirectory => - val nestedFile1 = Files.createFile(tempDirectory.resolve("file1")).toFile - val nestedFile2 = Files.createFile(tempDirectory.resolve("file2")).toFile - val nestedDir = Files.createDirectory(tempDirectory.resolve("dir1")) - val nestedDirFile = Files.createDirectory(nestedDir.resolve("dir1-file1")).toFile - - IO.touch(nestedFile1) - IO.touch(nestedFile2) - IO.createDirectory(nestedDir.toFile) - IO.touch(nestedDirFile) - - val mappings = Mapper.directory(tempDirectory.toFile).map { case (f, s) => (f, file(s)) } - - mappings should contain theSameElementsAs List( - tempDirectory.toFile -> file(s"${tempDirectory.getFileName}"), - nestedFile1 -> file(s"${tempDirectory.getFileName}/file1"), - nestedFile2 -> file(s"${tempDirectory.getFileName}/file2"), - nestedDir.toFile -> file(s"${tempDirectory.getFileName}/dir1"), - nestedDirFile -> file(s"${tempDirectory.getFileName}/dir1/dir1-file1") - ) - } - - it should "create one mapping entry for an empty directory" in { tempDirectory => - val mappings = Mapper.directory(tempDirectory.toFile) - - mappings should contain theSameElementsAs List[(File, String)]( - tempDirectory.toFile -> s"${tempDirectory.getFileName}" - ) - } - - it should "create an empty mappings sequence for a non-existing directory" in { tempDirectory => - val nonExistingDirectory = tempDirectory.resolve("imaginary") - val mappings = Mapper.directory(nonExistingDirectory.toFile) - - mappings should be(empty) - } - - it should "create one mapping entry if the directory is a file" in { tempDirectory => - val file = tempDirectory.resolve("file").toFile - IO.touch(file) - val mappings = Mapper.directory(file) - - mappings should contain theSameElementsAs List[(File, String)]( - file -> s"${file.getName}" - ) - } - - "contentOf" should "create mappings excluding the baseDirectory" in { tempDirectory => - val nestedFile1 = Files.createFile(tempDirectory.resolve("file1")).toFile - val nestedFile2 = Files.createFile(tempDirectory.resolve("file2")).toFile - val nestedDir = Files.createDirectory(tempDirectory.resolve("dir1")) - val nestedDirFile = Files.createDirectory(nestedDir.resolve("dir1-file1")).toFile - - IO.touch(nestedFile1) - IO.touch(nestedFile2) - IO.createDirectory(nestedDir.toFile) - IO.touch(nestedDirFile) - - val mappings = Mapper.contentOf(tempDirectory.toFile).map { case (f, s) => (f, file(s)) } - - mappings should contain theSameElementsAs List( - nestedFile1 -> file(s"file1"), - nestedFile2 -> file(s"file2"), - nestedDir.toFile -> file(s"dir1"), - nestedDirFile -> file(s"dir1/dir1-file1") - ) - } - - it should "create an empty mappings sequence for an empty directory" in { tempDirectory => - val mappings = Mapper.contentOf(tempDirectory.toFile) - - mappings should be(empty) - } - - it should "create an empty mappings sequence for a non-existing directory" in { tempDirectory => - val nonExistingDirectory = tempDirectory.resolve("imaginary") - val mappings = Mapper.contentOf(nonExistingDirectory.toFile) - - mappings should be(empty) - } - - it should "create an empty mappings sequence if the directory is a file" in { tempDirectory => - val file = tempDirectory.resolve("file").toFile - val mappings = Mapper.contentOf(file) - - mappings should be(empty) - } - - "allSubpaths" should "not include the base directory" in { tempDirectory => - val file = Files.createFile(tempDirectory.resolve("file")) - val paths = Mapper.allSubpaths(tempDirectory.toFile).toVector.map(_._1.toPath).toSet - assert(paths.contains(file)) - assert(!paths.contains(tempDirectory)) - } - - override protected def withFixture(test: OneArgTest): Outcome = { - val tmpDir = Files.createTempDirectory("mappings") - try { - withFixture(test.toNoArgTest(tmpDir)) - } finally { - // cleanup an delete the temp directory - IO.delete(tmpDir.toFile) - } - } -} diff --git a/main-actions/src/test/scala/sbt/MapperTest.scala b/main-actions/src/test/scala/sbt/MapperTest.scala new file mode 100644 index 000000000..0c94f4129 --- /dev/null +++ b/main-actions/src/test/scala/sbt/MapperTest.scala @@ -0,0 +1,161 @@ +/* + * sbt + * Copyright 2023, Scala center + * Copyright 2011 - 2022, Lightbend, Inc. + * Copyright 2008 - 2010, Mark Harrah + * Licensed under Apache License 2.0 (see LICENSE) + */ + +package sbt + +import java.nio.file.Files + +import sbt.internal.inc.MappedFileConverter +import sbt.io.IO +import sbt.io.syntax.* +import xsbti.FileConverter + +object MapperTest extends verify.BasicTestSuite: + test("directory should create mappings including the baseDirectory") { + withTempDirectory: tempDirectory => + given FileConverter = MappedFileConverter(Map("BASE" -> tempDirectory.toPath()), true) + val nestedFile1 = tempDirectory / "file1" + val nestedFile2 = tempDirectory / "file2" + val nestedDir = tempDirectory / "dir1" + val nestedDirFile = nestedDir / "dir1-file1" + + IO.touch(nestedFile1) + IO.touch(nestedFile2) + IO.createDirectory(nestedDir) + IO.touch(nestedDirFile) + + val mappings = Mapper + .directory(tempDirectory) + .map { case (h, p) => + (h.toString, p) + } + + Predef.assert( + mappings.toSet == List( + "${BASE}/" -> s"${tempDirectory.getName}", + "${BASE}/file1" -> s"${tempDirectory.getName}/file1", + "${BASE}/file2" -> s"${tempDirectory.getName}/file2", + "${BASE}/dir1" -> s"${tempDirectory.getName}/dir1", + "${BASE}/dir1/dir1-file1" -> s"${tempDirectory.getName}/dir1/dir1-file1", + ).toSet + ) + } + + test("it should create one mapping entry for an empty directory") { + withTempDirectory: tempDirectory => + given FileConverter = MappedFileConverter(Map("BASE" -> tempDirectory.toPath()), true) + val mappings = Mapper + .directory(tempDirectory) + .map { case (h, p) => + (h.toString, p) + } + Predef.assert( + mappings.toSet == List( + "${BASE}/" -> "foo" + ).toSet, + s"found $mappings" + ) + } + + test("it should create an empty mappings sequence for a non-existing directory") { + withTempDirectory: tempDirectory => + val conv0: FileConverter = MappedFileConverter(Map("BASE" -> tempDirectory.toPath()), true) + given FileConverter = conv0 + val nonExistingDirectory = tempDirectory / "imaginary" + val mappings = Mapper.directory(nonExistingDirectory) + assert(mappings.isEmpty) + } + + test("it should create one mapping entry if the directory is a file") { + withTempDirectory: tempDirectory => + val conv0: FileConverter = MappedFileConverter(Map("BASE" -> tempDirectory.toPath()), true) + given FileConverter = conv0 + val file = tempDirectory / "file" + IO.touch(file) + val mappings = Mapper.directory(file).map { case (h, p) => + (h.toString, p) + } + Predef.assert( + mappings.toSet == Set("${BASE}/file" -> s"${file.getName}"), + s"actual: $mappings" + ) + } + + test("contentOf should create mappings excluding the baseDirectory") { + withTempDirectory: tempDirectory => + val conv0: FileConverter = MappedFileConverter(Map("BASE" -> tempDirectory.toPath()), true) + given FileConverter = conv0 + val nestedFile1 = tempDirectory / "file1" + val nestedFile2 = tempDirectory / "file2" + val nestedDir = tempDirectory / "dir1" + val nestedDirFile = nestedDir / "dir1-file1" + IO.touch(nestedFile1) + IO.touch(nestedFile2) + IO.createDirectory(nestedDir) + IO.touch(nestedDirFile) + + val mappings = Mapper.contentOf(tempDirectory).map { case (h, p) => + (h.toString, p) + } + Predef.assert( + mappings.toSet == List( + "${BASE}/file1" -> "file1", + "${BASE}/file2" -> "file2", + "${BASE}/dir1" -> "dir1", + "${BASE}/dir1/dir1-file1" -> "dir1/dir1-file1", + ).toSet, + s"actual: $mappings" + ) + } + + test("it should create an empty mappings sequence for an empty directory") { + withTempDirectory: tempDirectory => + given FileConverter = MappedFileConverter(Map("BASE" -> tempDirectory.toPath()), true) + val mappings = Mapper.contentOf(tempDirectory) + assert(mappings.isEmpty) + } + + test("it should create an empty mappings sequence for a non-existing directory") { + withTempDirectory: tempDirectory => + given FileConverter = MappedFileConverter(Map("BASE" -> tempDirectory.toPath()), true) + val nonExistingDirectory = tempDirectory / "imaginary" + val mappings = Mapper.contentOf(nonExistingDirectory) + assert(mappings.isEmpty) + } + + test("it should create an empty mappings sequence if the directory is a file") { + withTempDirectory: tempDirectory => + given FileConverter = MappedFileConverter(Map("BASE" -> tempDirectory.toPath()), true) + val file = tempDirectory / "file" + val mappings = Mapper.contentOf(file) + assert(mappings.isEmpty) + } + + test("it should create an empty mappings sequence if the directory is a file") { + withTempDirectory: tempDirectory => + given FileConverter = MappedFileConverter(Map("BASE" -> tempDirectory.toPath()), true) + val file = tempDirectory / "file" + val mappings = Mapper.contentOf(file) + assert(mappings.isEmpty) + } + + test("allSubpaths should not include the base directory") { + withTempDirectory: tempDirectory => + given FileConverter = MappedFileConverter(Map("BASE" -> tempDirectory.toPath()), true) + val file = Files.createFile((tempDirectory / "file").toPath) + val paths = Mapper.allSubpaths(tempDirectory).toVector.map(_._1.toString).toSet + assert(paths.contains("${BASE}/file")) + assert(!paths.contains("${BASE}")) + } + + def withTempDirectory[A1](f: File => A1): A1 = + IO.withTemporaryDirectory: tempDirectory0 => + val tempDirectory = tempDirectory0 / "foo" + IO.createDirectory(tempDirectory) + f(tempDirectory) +end MapperTest From 28331a74442b05b74403a31565aa10a4935a5136 Mon Sep 17 00:00:00 2001 From: Eugene Yokota Date: Mon, 16 Dec 2024 00:31:07 -0500 Subject: [PATCH 5/5] Remove Mapper alias --- sbt-app/src/main/scala/sbt/Import.scala | 1 - 1 file changed, 1 deletion(-) diff --git a/sbt-app/src/main/scala/sbt/Import.scala b/sbt-app/src/main/scala/sbt/Import.scala index 51af25035..cbeadd22f 100644 --- a/sbt-app/src/main/scala/sbt/Import.scala +++ b/sbt-app/src/main/scala/sbt/Import.scala @@ -54,7 +54,6 @@ trait Import { val Hash = sbt.io.Hash val HiddenFileFilter = sbt.io.HiddenFileFilter val IO = sbt.io.IO - type Mapper = sbt.io.Mapper val NameFilter = sbt.io.NameFilter type NameFilter = sbt.io.NameFilter val NothingFilter = sbt.io.NothingFilter