Skip to content

Commit

Permalink
testkit: include last subpath of test input source directory
Browse files Browse the repository at this point in the history
This allows rules to inspect whether files are in `scala` or `scala-3` (like
they would do with regular Input.File), at the cost of some verbosity in test
names.
  • Loading branch information
bjaglin committed Jan 30, 2024
1 parent 8984220 commit 2876430
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions scalafix-testkit/src/main/scala/scalafix/testkit/TestkitPath.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import scala.meta.io.RelativePath
* @param input
* the absolute path to the input file.
* @param testPath
* the input file relativized by the input source directory. Used to compute
* the test name and the expected output file.
* the input file relativized by input source directory (but including its
* last subpath). Used to compute the test name and the expected output file.
* @param semanticdbPath
* the input file relativized by the SemanticDB sourceroot. Used to compute
* the path to the SemanticDB payload.
Expand All @@ -29,8 +29,12 @@ final class TestkitPath(
def toInput: Input =
Input.VirtualFile(testName, FileIO.slurp(input, StandardCharsets.UTF_8))
def resolveOutput(props: TestkitProperties): Either[String, AbsolutePath] = {
val testPathStripped =
RelativePath(testPath.toNIO.subpath(1, testPath.toNIO.getNameCount))
val candidates =
props.outputSourceDirectories.map(dir => dir.resolve(testPath))
props.outputSourceDirectories.map(dir => dir.resolve(testPathStripped)) ++
// for backwards compatibility, try the old output directory
props.outputSourceDirectories.map(dir => dir.resolve(testPath))
def tried: String = candidates.mkString("\n ", "\n ", "")
candidates.filter(_.isFile) match {
case head :: Nil =>
Expand All @@ -50,8 +54,10 @@ object TestkitPath {
props.inputSourceDirectories.flatMap { sourceDirectory =>
val ls = FileIO.listAllFilesRecursively(sourceDirectory)
val scalaFiles = ls.files.filter(path => isScalaFile.matches(path.toNIO))
scalaFiles.map { testPath =>
val input = sourceDirectory.resolve(testPath)
scalaFiles.map { scalaFile =>
val input = sourceDirectory.resolve(scalaFile)
val testPath =
input.toRelative(AbsolutePath(sourceDirectory.toNIO.getParent))
val semanticdbPath = input.toRelative(props.sourceroot)
new TestkitPath(input, testPath, semanticdbPath)
}
Expand Down

0 comments on commit 2876430

Please sign in to comment.