Skip to content

Commit

Permalink
Implement `bloop autocomplete --format <shell> --mode testsfqcn --pro…
Browse files Browse the repository at this point in the history
…ject <project>`

This commit enable bloop to return testsFQCN in a project
using `bloop autocomplete --format <shell> --mode testsfqcn --project <project>`.

scalacenter#426
  • Loading branch information
tanishiking committed Dec 5, 2018
1 parent 601b4f6 commit 27d661e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
9 changes: 6 additions & 3 deletions frontend/src/main/scala/bloop/engine/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import bloop.cli.completion.{Case, Mode}
import bloop.io.{AbsolutePath, RelativePath, SourceWatcher}
import bloop.logging.DebugFilter
import bloop.testing.{LoggingEventHandler, TestInternals}
import bloop.engine.tasks.{CompilationTask, LinkTask, Tasks}
import bloop.engine.tasks.{CompilationTask, LinkTask, Tasks, TestTask}
import bloop.cli.Commands.CompilingCommand
import bloop.data.{Platform, Project}
import bloop.engine.Feedback.XMessageString
Expand Down Expand Up @@ -275,10 +275,13 @@ object Interpreter {
completion <- cmd.format.showMainName(main)
} state.logger.info(completion)
case Mode.TestsFQCN =>
import ExecutionContext.scheduler
for {
projectName <- cmd.project
placeholder <- List.empty[String]
completion <- cmd.format.showTestName(placeholder)
project <- state.build.getProjectFor(projectName)
testsFQCN <- TestTask.findTestsFQCN(project, state)
testFQCN <- testsFQCN
completion <- cmd.format.showTestName(testFQCN)
} state.logger.info(completion)
}

Expand Down
30 changes: 30 additions & 0 deletions frontend/src/main/scala/bloop/engine/tasks/TestTask.scala
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,36 @@ object TestTask {
tasks.mapValues(_.toList).toMap
}

/**
* Finds testsFQCN in `project`.
*
* @param state The current state of Bloop.
* @param project The project for which to find the testsFQCN.
* @return An array containing all the testsFQCN that were detected.
*/
private[bloop] def findTestsFQCN(
project: Project,
state: State
): Task[List[String]] = {
import state.logger
TestTask.discoverTestFrameworks(project, state).map {
case None => List.empty[String]
case Some(found) =>
val frameworks = found.frameworks
val lastCompileResult = state.results.lastSuccessfulResultOrEmpty(project)
val analysis = lastCompileResult.analysis().toOption.getOrElse {
logger.warn(
s"TestsFQCN was triggered, but no compilation detected for ${project.name}")
Analysis.empty
}
val tests = discoverTests(analysis, frameworks)
tests.toList.flatMap {
case (framework, tasks) => tasks.map(t => (framework, t))
}.map(_._2.fullyQualifiedName)
}
}


/**
* Fixes the test arguments for a given framework.
*
Expand Down
20 changes: 20 additions & 0 deletions frontend/src/test/scala/bloop/tasks/JsTestSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,24 @@ class JsTestSpec(
()
}
}

@Test
def testsFQCNAreDetected(): Unit = {
val expectedTestsFQCN = List(
"hello.EternalUTest", "hello.ScalaCheckTest", "hello.Specs2Test", "hello.WritingTest",
"hello.JUnitTest", "hello.ScalaTestTest", "hello.UTestTest"
)
TestUtil.quietIfSuccess(testState.logger) { logger =>
val testsFQCNTask = TestTask.findTestsFQCN(testProject, testState).map { testsFQCN =>
expectedTestsFQCN.foreach { expected =>
assertTrue(
s"$expected not detected.",
testsFQCN.contains(expected)
)
}
}
TestUtil.blockOnTask(testsFQCNTask, 3)
()
}
}
}
16 changes: 16 additions & 0 deletions frontend/src/test/scala/bloop/tasks/JvmTestSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,20 @@ class JvmTestSpec(
()
}
}

@Test
def testsFQCNAreDetected(): Unit = {
TestUtil.quietIfSuccess(testState.logger) { logger =>
val testsFQCNTask = TestTask.findTestsFQCN(testProject, testState).map { testsFQCN =>
targetFrameworks.foreach { framework =>
assertTrue(
s"$framework not detected.",
testsFQCN.contains(s"hello.${framework}Test")
)
}
}
TestUtil.blockOnTask(testsFQCNTask, 5)
()
}
}
}

0 comments on commit 27d661e

Please sign in to comment.