Skip to content

Ensure resource directories passed via a using directive aren't ignored in --watch mode #3221

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

Merged
merged 3 commits into from
Oct 22, 2024
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
35 changes: 22 additions & 13 deletions modules/build/src/main/scala/scala/build/CrossSources.scala
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,19 @@ object CrossSources {
)
}).flatten

val resourceDirectoriesFromDirectives = {
val resourceDirsFromCli =
allInputs.elements.flatMap { case rd: ResourceDirectory => Some(rd.path); case _ => None }
val resourceDirsFromBuildOptions: Seq[os.Path] =
buildOptions.flatMap(_.value.classPathOptions.resourcesDir).distinct
resourceDirsFromBuildOptions
.filter(!resourceDirsFromCli.contains(_))
.map(ResourceDirectory(_))
}
val finalInputs = allInputs.add(resourceDirectoriesFromDirectives)

val defaultMainElemPath = for {
defaultMainElem <- allInputs.defaultMainClassElement
defaultMainElem <- finalInputs.defaultMainClassElement
} yield defaultMainElem.path

val pathsWithDirectivePositions
Expand All @@ -284,7 +295,7 @@ object CrossSources {
val baseReqs0 = baseReqs(d.scopePath)
WithBuildRequirements(
d.requirements.fold(baseReqs0)(_ orElse baseReqs0),
(d.path, d.path.relativeTo(allInputs.workspace))
(d.path, d.path.relativeTo(finalInputs.workspace))
) -> d.directivesPositions
}
val inMemoryWithDirectivePositions
Expand All @@ -309,7 +320,7 @@ object CrossSources {
}

val resourceDirs: Seq[WithBuildRequirements[os.Path]] =
resolveResourceDirs(allInputs, preprocessedSources)
resolveResourceDirs(finalInputs, preprocessedSources)

lazy val allPathsWithDirectivesByScope: Map[Scope, Seq[(os.Path, Position.File)]] =
(pathsWithDirectivePositions ++ inMemoryWithDirectivePositions ++ unwrappedScriptsWithDirectivePositions)
Expand Down Expand Up @@ -362,17 +373,15 @@ object CrossSources {
val paths = pathsWithDirectivePositions.map(_._1)
val inMemory = inMemoryWithDirectivePositions.map(_._1)
val unwrappedScripts = unwrappedScriptsWithDirectivePositions.map(_._1)
(
CrossSources(
paths,
inMemory,
defaultMainElemPath,
resourceDirs,
buildOptions,
unwrappedScripts
),
allInputs
val crossSources = CrossSources(
paths,
inMemory,
defaultMainElemPath,
resourceDirs,
buildOptions,
unwrappedScripts
)
crossSources -> finalInputs
}

/** @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,10 @@ abstract class RunTestDefinitions
forbiddenDirTest()
}

private def resourcesInputs(directive: String = "") = {
val resourceContent = "Hello from resources"
protected def resourcesInputs(
directive: String = "",
resourceContent: String = "Hello from resources"
): TestInputs =
TestInputs(
os.rel / "src" / "proj" / "resources" / "test" / "data" -> resourceContent,
os.rel / "src" / "proj" / "Test.scala" ->
Expand All @@ -571,24 +573,30 @@ abstract class RunTestDefinitions
| val cl = Thread.currentThread().getContextClassLoader
| val is = cl.getResourceAsStream("test/data")
| val content = scala.io.Source.fromInputStream(is)(scala.io.Codec.UTF8).mkString
| assert(content == "$resourceContent")
| println(content)
| }
|}
|""".stripMargin
)
}

test("resources") {
resourcesInputs().fromRoot { root =>
os.proc(TestUtil.cli, "run", "src", "--resource-dirs", "./src/proj/resources").call(cwd =
root
)
test("resources via command line") {
val expectedMessage = "hello"
resourcesInputs(resourceContent = expectedMessage).fromRoot { root =>
val res = os.proc(TestUtil.cli, "run", "src", "--resource-dirs", "./src/proj/resources")
.call(cwd = root)
expect(res.out.trim() == expectedMessage)
}
}
test("resources via directive") {
resourcesInputs("//> using resourceDirs \"./resources\"").fromRoot { root =>
os.proc(TestUtil.cli, "run", ".").call(cwd = root)
}
val expectedMessage = "hello"
resourcesInputs(
directive = "//> using resourceDirs \"./resources\"",
resourceContent = expectedMessage
)
.fromRoot { root =>
val res = os.proc(TestUtil.cli, "run", ".").call(cwd = root)
expect(res.out.trim() == expectedMessage)
}
}

def argsAsIsTest(): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ trait RunWithWatchTestDefinitions { _: RunTestDefinitions =>
TestInputs(inputPath -> code(expectedMessage1)),
code(expectedMessage2)
)
}, {
val inputPath = os.rel / "Main.java"
def code(message: String) = s"""public class Main {
| public static void main(String[] args) {
| System.out.println("$message");
| }
|}""".stripMargin
(
inputPath,
TestInputs(inputPath -> code(expectedMessage1)),
code(expectedMessage2)
)
}, {
val inputPath = os.rel / "markdown.md"
def code(message: String) =
Expand Down Expand Up @@ -296,4 +308,34 @@ trait RunWithWatchTestDefinitions { _: RunTestDefinitions =>
}
}
}

for {
useDirective <- Seq(false, true)
// TODO make this pass reliably on Mac CI
if !Properties.isMac || !TestUtil.isCI
directive = if (useDirective) "//> using resourceDirs ./resources" else ""
resourceOptions = if (useDirective) Nil else Seq("--resource-dirs", "./src/proj/resources")
title = if (useDirective) "directive" else "command line"
} test(s"resources via $title with --watch") {
val expectedMessage1 = "Hello"
val expectedMessage2 = "world"
resourcesInputs(directive = directive, resourceContent = expectedMessage1)
.fromRoot { root =>
TestUtil.withProcessWatching(
os.proc(TestUtil.cli, "run", "src", "--watch", resourceOptions, extraOptions)
.spawn(cwd = root, stderr = os.Pipe)
) { (proc, timeout, ec) =>
val output1 = TestUtil.readLine(proc.stdout, ec, timeout)
expect(output1 == expectedMessage1)
proc.printStderrUntilRerun(timeout)(ec)
val Some((resourcePath, newResourceContent)) =
resourcesInputs(directive = directive, resourceContent = expectedMessage2)
.files
.find(_._1.toString.contains("resources"))
os.write.over(root / resourcePath, newResourceContent)
val output2 = TestUtil.readLine(proc.stdout, ec, timeout)
expect(output2 == expectedMessage2)
}
}
}
}
Loading