Skip to content
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

Create target path of -h ScalaTest test option #732

Merged
merged 1 commit into from
Nov 22, 2018
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
38 changes: 38 additions & 0 deletions config/src/main/scala/bloop/config/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,49 @@ object Config {
case class Java(options: List[String])

case class TestFramework(names: List[String])

object TestFramework {
val ScalaCheck = Config.TestFramework(
List(
"org.scalacheck.ScalaCheckFramework"
)
)

val ScalaTest = Config.TestFramework(
List(
"org.scalatest.tools.Framework",
"org.scalatest.tools.ScalaTestFramework"
)
)

val Specs2 = Config.TestFramework(
List(
"org.specs.runner.SpecsFramework",
"org.specs2.runner.Specs2Framework",
"org.specs2.runner.SpecsFramework"
)
)

val JUnit = Config.TestFramework(
List(
"com.novocode.junit.JUnitFramework"
)
)

val DefaultFrameworks = List(JUnit, ScalaTest, ScalaCheck, Specs2)
}

case class TestArgument(args: List[String], framework: Option[TestFramework])
case class TestOptions(excludes: List[String], arguments: List[TestArgument])
object TestOptions { val empty = TestOptions(Nil, Nil) }

case class Test(frameworks: List[TestFramework], options: TestOptions)
object Test {
def defaultConfiguration: Test = {
val junit = List(Config.TestArgument(List("-v", "-a"), Some(Config.TestFramework.JUnit)))
Config.Test(Config.TestFramework.DefaultFrameworks, Config.TestOptions(Nil, junit))
}
}

case class Sbt(
sbtVersion: String,
Expand Down
53 changes: 52 additions & 1 deletion frontend/src/main/scala/bloop/engine/tasks/TestTask.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ object TestTask {
else logger.debug(s"Found test frameworks: ${frameworks.map(_.name).mkString(", ")}")

val frameworkArgs = considerFrameworkArgs(frameworks, userTestOptions, logger)
val args = project.testOptions.arguments ++ frameworkArgs
val args = fixTestOptions(project, project.testOptions.arguments ++ frameworkArgs)
logger.debug(s"Running test suites with arguments: $args")

val lastCompileResult = state.results.lastSuccessfulResultOrEmpty(project)
Expand Down Expand Up @@ -247,4 +247,55 @@ object TestTask {
}
tasks.mapValues(_.toList).toMap
}

/**
* Fixes the test arguments for a given framework.
*
* This is a generic function that accumulates fixes we do to the test arguments
* that a build has exported. https://github.com/scalacenter/bloop/issues/658 is
* a good example of a test option (`-h` in Scalatest) which requires us to check
* that its path exists.
*
* @param project The project we fix test options for.
* @param args The test arguments.
* @return The list of fixed test arguments.
*/
def fixTestOptions(
project: Project,
args: List[Config.TestArgument]
): List[Config.TestArgument] = {
import java.nio.file.{Files, Paths}
args.map {
case Config.TestArgument(testArg :: testArgs, f @ Some(Config.TestFramework.ScalaTest)) =>
val fixedArgs = testArgs.foldLeft(List(testArg)) {
case (Nil, current) => current :: Nil
case (rest @ previous :: _, current) =>
if (previous != "-h") current :: rest
else {
val currentPath = Paths.get(current)
val path = {
if (currentPath.isAbsolute) currentPath
else {
val potentialPath = project.baseDirectory.resolve(current)
if (potentialPath.exists) potentialPath.underlying
else {
if (potentialPath.getParent.exists)
Files.createFile(potentialPath.underlying)
else {
Files.createDirectories(potentialPath.getParent.underlying)
Files.createFile(potentialPath.underlying)
}
}
}
}

path.toAbsolutePath.toString :: rest
}
}

Config.TestArgument(fixedArgs, f)
case a => a
}
}

}
3 changes: 2 additions & 1 deletion frontend/src/test/resources/cross-test-build-0.6/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ lazy val `test-project` =
Tests.Exclude("hello.WritingTest" :: Nil),
Tests.Exclude("hello.EternalUTest" :: Nil),
Tests.Argument("-o"),
Tests.Argument(TestFrameworks.JUnit, "-v", "+q", "-n")
Tests.Argument(TestFrameworks.JUnit, "-v", "+q", "-n"),
Tests.Argument(TestFrameworks.ScalaTest, "-h", "target/test-reports")
),
)
.jsConfigure(_.enablePlugins(ScalaJSJUnitPlugin))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ final class BloopConverter(parameters: BloopParameters) {
`scala` = scalaConfig,
java = getJavaConfig(project, sourceSet),
sbt = None,
test = Some(Config.Test(testFrameworks, defaultTestOptions)), // TODO: make this configurable?
test = Some(Config.Test.defaultConfiguration), // TODO: make this configurable?
platform = None,
resolution = Some(resolution)
)
Expand Down Expand Up @@ -280,37 +280,6 @@ final class BloopConverter(parameters: BloopParameters) {
private def splitFlags(values: List[String]): List[String] = {
values.flatMap(value => value.split(argumentSpaceSeparator))
}

private val scalaCheckFramework = Config.TestFramework(
List(
"org.scalacheck.ScalaCheckFramework"
))

private val scalaTestFramework = Config.TestFramework(
List(
"org.scalatest.tools.Framework",
"org.scalatest.tools.ScalaTestFramework"
)
)

private val specsFramework = Config.TestFramework(
List(
"org.specs.runner.SpecsFramework",
"org.specs2.runner.Specs2Framework",
"org.specs2.runner.SpecsFramework"
)
)

private val jUnitFramework = Config.TestFramework(
List(
"com.novocode.junit.JUnitFramework"
)
)

private val testFrameworks: List[Config.TestFramework] =
List(scalaCheckFramework, scalaTestFramework, specsFramework, jUnitFramework)
private val defaultTestOptions =
Config.TestOptions(Nil, List(Config.TestArgument(List("-v", "-a"), Some(jUnitFramework))))
}

object BloopConverter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,36 +42,6 @@ object MojoImplementation {

private val emptyLauncher = new AppLauncher("", "", Array(), Array())

val ScalaCheckFramework = Config.TestFramework(
List(
"org.scalacheck.ScalaCheckFramework"
))
val ScalaTestFramework = Config.TestFramework(
List(
"org.scalatest.tools.Framework",
"org.scalatest.tools.ScalaTestFramework"
))
val SpecsFramework = Config.TestFramework(
List(
"org.specs.runner.SpecsFramework",
"org.specs2.runner.Specs2Framework",
"org.specs2.runner.SpecsFramework"
))
val JUnitFramework = Config.TestFramework(
List(
"com.novocode.junit.JUnitFramework"
))

private val testFrameworks: List[Config.TestFramework] = List(
ScalaCheckFramework,
ScalaTestFramework,
SpecsFramework,
JUnitFramework
)

private val DefaultTestOptions =
Config.TestOptions(Nil, List(Config.TestArgument(List("-v", "-a"), Some(JUnitFramework))))

def writeConfig(
asScala: mutable.Buffer[File],
getTestOutputDir: File,
Expand Down Expand Up @@ -137,7 +107,7 @@ object MojoImplementation {
// FORMAT: OFF
val config = {
val sbt = None
val test = Some(Config.Test(testFrameworks, DefaultTestOptions))
val test = Some(Config.Test.defaultConfiguration)
val java = Some(Config.Java(mojo.getJavacArgs().asScala.toList))
val `scala` = Some(Config.Scala(mojo.getScalaOrganization(), mojo.getScalaArtifactID(), mojo.getScalaVersion(), scalacArgs, allScalaJars, analysisOut, Some(compileSetup)))
val javaHome = Some(abs(mojo.getJavaHome().getParentFile.getParentFile))
Expand Down