Skip to content
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
10 changes: 7 additions & 3 deletions modules/build/src/main/scala/scala/build/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -778,15 +778,19 @@ object Build {
val watcher =
new Watcher(ListBuffer(), threads.fileWatcher, run(), info.foreach(_._1.shutdown()))

def doWatch(): Unit = {
def doWatch(): Unit = either {
val (crossSources: CrossSources, inputs0: Inputs) =
value(allInputs(inputs, options, logger))
val elements: Seq[Element] =
if res == null then inputs.elements
if res == null then inputs0.elements
else
res
.map { builds =>
val allResourceDirectories =
crossSources.resourceDirs.map(rd => ResourceDirectory(rd.value))
val mainElems = builds.main.inputs.elements
val testElems = builds.get(Scope.Test).map(_.inputs.elements).getOrElse(Nil)
(mainElems ++ testElems).distinct
(mainElems ++ testElems ++ allResourceDirectories).distinct
}
.getOrElse(inputs.elements)
for (elem <- elements) {
Expand Down
19 changes: 4 additions & 15 deletions modules/build/src/main/scala/scala/build/CrossSources.scala
Original file line number Diff line number Diff line change
Expand Up @@ -271,19 +271,8 @@ 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 <- finalInputs.defaultMainClassElement
defaultMainElem <- allInputs.defaultMainClassElement
} yield defaultMainElem.path

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

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

lazy val allPathsWithDirectivesByScope: Map[Scope, Seq[(os.Path, Position.File)]] =
(pathsWithDirectivePositions ++ inMemoryWithDirectivePositions ++ unwrappedScriptsWithDirectivePositions)
Expand Down Expand Up @@ -379,7 +368,7 @@ object CrossSources {
buildOptions,
unwrappedScripts
)
crossSources -> finalInputs
crossSources -> allInputs
}

extension (uri: java.net.URI)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,21 @@ class DirectiveTests extends TestUtil.ScalaCliBuildSuite {
expect(resourceDirs == Seq(path))
}
}
test("do not include test.resourceDir into sources for main scope") {
val testInputs = TestInputs(
os.rel / "simple.sc" ->
"""//> using test.resourceDir foo
|""".stripMargin
)
testInputs.withBuild(baseOptions, buildThreads, bloopConfigOpt, scope = Scope.Main) {
(_, _, maybeBuild) =>
val build =
maybeBuild.toOption.flatMap(_.successfulOpt).getOrElse(sys.error("cannot happen"))
val resourceDirs = build.sources.resourceDirs

expect(resourceDirs.isEmpty)
}
}
test("parse boolean for publish.doc") {
val testInputs = TestInputs(
os.rel / "simple.sc" ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,9 @@ abstract class RunTestDefinitions
): TestInputs =
TestInputs(
os.rel / "src" / "proj" / "resources" / "test" / "data" -> resourceContent,
os.rel / "src" / "proj" / "Test.scala" ->
os.rel / "src" / "proj" / "Example.scala" ->
s"""$directive
|object Test {
|object Example {
| def main(args: Array[String]): Unit = {
| val cl = Thread.currentThread().getContextClassLoader
| val is = cl.getResourceAsStream("test/data")
Expand Down Expand Up @@ -599,6 +599,22 @@ abstract class RunTestDefinitions
expect(res.out.trim() == expectedMessage)
}
}
test("resources via test directive") {
val expectedMessage = "hello"
resourcesInputs(
directive = "//> using test.resourceDirs ./resources",
resourceContent = expectedMessage
)
.fromRoot { root =>
val err = os.proc(TestUtil.cli, "run", ".")
.call(cwd = root, check = false, stderr = os.Pipe)
expect(err.err.trim().contains("java.lang.NullPointerException"))
expect(err.exitCode == 1)
val res = os.proc(TestUtil.cli, "run", ".", "--test")
.call(cwd = root)
expect(res.out.trim() == expectedMessage)
}
}

def argsAsIsTest(): Unit = {
val inputs = TestInputs(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,18 +345,34 @@ trait RunWithWatchTestDefinitions { _: RunTestDefinitions =>

for {
useDirective <- Seq(false, true)
testScope <- if (useDirective) Seq(false, true) else Seq(false)
scopeString = if (testScope) "test" else "main"
// TODO make this pass reliably on Mac CI
if !Properties.isMac || !TestUtil.isCI
directive = if (useDirective) "//> using resourceDirs ./resources" else ""
directive =
useDirective -> testScope match {
case (true, true) => "//> using test.resourceDirs ./resources"
case (true, false) => "//> using resourceDirs ./resources"
case _ => ""
}
resourceOptions = if (useDirective) Nil else Seq("--resource-dirs", "./src/proj/resources")
scopeOptions = if (testScope) Seq("--test") else Nil
title = if (useDirective) "directive" else "command line"
} test(s"resources via $title with --watch") {
} test(s"resources via $title with --watch ($scopeString)") {
val expectedMessage1 = "Hello"
val expectedMessage2 = "world"
resourcesInputs(directive = directive, resourceContent = expectedMessage1)
.fromRoot { root =>
TestUtil.withProcessWatching(
os.proc(TestUtil.cli, "run", "src", "--watch", resourceOptions, extraOptions)
os.proc(
TestUtil.cli,
"run",
"src",
"--watch",
resourceOptions,
scopeOptions,
extraOptions
)
.spawn(cwd = root, stderr = os.Pipe)
) { (proc, timeout, ec) =>
val output1 = TestUtil.readLine(proc.stdout, ec, timeout)
Expand Down