Skip to content

Commit

Permalink
Support declaring test custom jars from the main scope
Browse files Browse the repository at this point in the history
  • Loading branch information
Gedochao committed Apr 24, 2023
1 parent 52cdd98 commit 7038a03
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ case object ScalaPreprocessor extends Preprocessor {

val usingDirectiveHandlers: Seq[DirectiveHandler[BuildOptions]] =
Seq[DirectiveHandler[_ <: HasBuildOptions]](
directives.CustomJar.handler,
directives.JavaHome.handler,
directives.Jvm.handler,
directives.MainClass.handler,
Expand All @@ -89,6 +88,7 @@ case object ScalaPreprocessor extends Preprocessor {
val usingDirectiveWithReqsHandlers
: Seq[DirectiveHandler[List[WithBuildRequirements[BuildOptions]]]] =
Seq[DirectiveHandler[_ <: HasBuildOptionsWithRequirements]](
directives.CustomJar.handler,
directives.Dependency.handler,
directives.JavaOptions.handler,
directives.JavacOptions.handler,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package scala.build.preprocessing.directives
import scala.build.Ops._
import scala.build.Ops.*
import scala.build.directives.*
import scala.build.errors.{BuildException, CompositeBuildException, WrongJarPathError}
import scala.build.options.{BuildOptions, ClassPathOptions}
import scala.build.options.{BuildOptions, ClassPathOptions, Scope, WithBuildRequirements}
import scala.build.preprocessing.ScopePath
import scala.build.preprocessing.directives.CustomJar.buildOptions
import scala.build.{Logger, Positioned}
import scala.cli.commands.SpecificationLevel
import scala.util.{Failure, Success, Try}

@DirectiveGroupName("Custom JAR")
@DirectiveExamples(
"//> using jar \"/Users/alexandre/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/com/chuusai/shapeless_2.13/2.3.7/shapeless_2.13-2.3.7.jar\""
) @DirectiveExamples(
"//> using test.jar \"/Users/alexandre/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/com/chuusai/shapeless_2.13/2.3.7/shapeless_2.13-2.3.7.jar\""
)
@DirectiveUsage(
"`//> using jar `_path_ | `//> using jars `_path1_, _path2_ …",
Expand All @@ -23,9 +26,30 @@ import scala.util.{Failure, Success, Try}
final case class CustomJar(
@DirectiveName("jars")
jar: DirectiveValueParser.WithScopePath[List[Positioned[String]]] =
DirectiveValueParser.WithScopePath.empty(Nil),
@DirectiveName("test.jar")
@DirectiveName("test.jars")
testJar: DirectiveValueParser.WithScopePath[List[Positioned[String]]] =
DirectiveValueParser.WithScopePath.empty(Nil)
) extends HasBuildOptions {
def buildOptions: Either[BuildException, BuildOptions] = {
) extends HasBuildOptionsWithRequirements {
override def buildOptionsWithRequirements
: Either[BuildException, List[WithBuildRequirements[BuildOptions]]] =
Seq(
buildOptions(jar).map(_.withEmptyRequirements),
buildOptions(testJar).map(_.withScopeRequirement(Scope.Test))
)
.sequence
.left
.map(CompositeBuildException(_))
.map(_.toList)

}

object CustomJar {
val handler: DirectiveHandler[CustomJar] = DirectiveHandler.derive

def buildOptions(jar: DirectiveValueParser.WithScopePath[List[Positioned[String]]])
: Either[BuildException, BuildOptions] = {
val cwd = jar.scopePath
jar.value
.map { posPathStr =>
Expand All @@ -48,7 +72,3 @@ final case class CustomJar(
}
}
}

object CustomJar {
val handler: DirectiveHandler[CustomJar] = DirectiveHandler.derive
}
Original file line number Diff line number Diff line change
Expand Up @@ -1253,4 +1253,78 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
.call(cwd = root, stderr = os.Pipe)
}
}
test("declare test scope custom jar from main scope") {
val projectFile = "project.scala"
val testMessageFile = "TestMessage.scala"
val mainMessageFile = "MainMessage.scala"
val validMainFile = "ValidMain.scala"
val invalidMainFile = "InvalidMain.scala"
val testFile = "Tests.test.scala"
val expectedMessage1 = "Hello"
val expectedMessage2 = " world!"
val jarPathsWithFiles @ Seq((mainMessageJar, _), (testMessageJar, _)) =
Seq(
os.rel / "MainMessage.jar" -> mainMessageFile,
os.rel / "TestMessage.jar" -> testMessageFile
)
TestInputs(
os.rel / projectFile ->
s"""//> using jar "$mainMessageJar"
|//> using test.jar "$testMessageJar"
|//> using test.dep "org.scalameta::munit::0.7.29"
|""".stripMargin,
os.rel / mainMessageFile ->
"""case class MainMessage(value: String)
|""".stripMargin,
os.rel / testMessageFile ->
"""case class TestMessage(value: String)
|""".stripMargin,
os.rel / invalidMainFile ->
s"""object InvalidMain extends App {
| println(TestMessage("$expectedMessage1").value)
|}
|""".stripMargin,
os.rel / validMainFile ->
s"""object ValidMain extends App {
| println(MainMessage("$expectedMessage1").value)
|}
|""".stripMargin,
os.rel / testFile ->
s"""class Tests extends munit.FunSuite {
| val msg1 = MainMessage("$expectedMessage1").value
| val msg2 = TestMessage("$expectedMessage2").value
| val testName = msg1 + msg2
| test(testName) {
| assert(1 + 1 == 2)
| }
|}
|""".stripMargin
).fromRoot { root =>
// package the MainMessage and TestMessage jars
for ((jarPath, sourcePath) <- jarPathsWithFiles)
os.proc(
TestUtil.cli,
"--power",
"package",
sourcePath,
"--library",
"-o",
jarPath,
extraOptions
)
.call(cwd = root)
// running `invalidMainFile` should fail, as it's in the main scope and depends on the test scope jar
val res1 = os.proc(TestUtil.cli, "run", projectFile, invalidMainFile)
.call(cwd = root, check = false)
expect(res1.exitCode == 1)
// running `validMainFile` should succeed, since it only depends on the main scope jar
val res2 = os.proc(TestUtil.cli, "run", projectFile, validMainFile)
.call(cwd = root)
expect(res2.out.trim() == expectedMessage1)
// test scope should have access to both main and test deps
val res3 = os.proc(TestUtil.cli, "test", projectFile, testFile)
.call(cwd = root, stderr = os.Pipe)
expect(res3.out.trim().contains(s"$expectedMessage1$expectedMessage2"))
}
}
}
2 changes: 2 additions & 0 deletions website/docs/reference/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Manually add JAR(s) to the class path
#### Examples
`//> using jar "/Users/alexandre/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/com/chuusai/shapeless_2.13/2.3.7/shapeless_2.13-2.3.7.jar"`

`//> using test.jar "/Users/alexandre/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/com/chuusai/shapeless_2.13/2.3.7/shapeless_2.13-2.3.7.jar"`

### Custom sources

Manually add sources to the project
Expand Down
2 changes: 2 additions & 0 deletions website/docs/reference/scala-command/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ Manually add JAR(s) to the class path
#### Examples
`//> using jar "/Users/alexandre/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/com/chuusai/shapeless_2.13/2.3.7/shapeless_2.13-2.3.7.jar"`

`//> using test.jar "/Users/alexandre/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/com/chuusai/shapeless_2.13/2.3.7/shapeless_2.13-2.3.7.jar"`

### Custom sources

Manually add sources to the project
Expand Down

0 comments on commit 7038a03

Please sign in to comment.