mirror of https://github.com/sbt/sbt.git
Port MapperTest
This commit is contained in:
parent
a2b046e0c3
commit
0b744c1ded
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
Loading…
Reference in New Issue