Skip to content

Commit

Permalink
bugfix: Only detect 100% sure test names
Browse files Browse the repository at this point in the history
Previously, we would treat any string literal inside the apply of a test function as a name of such function, but each test name can actually be any kind of expression, so we would not detect the correc tname always.

Now, we only detect names of tests if they are of an expected format, either simple string, select or a tag.

Fixes #6617
  • Loading branch information
tgodzik authored and kasiaMarek committed Jul 24, 2024
1 parent 862a352 commit 8eeebaf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,24 +189,22 @@ class MunitTestFinder(
tree: Tree
): Option[(Term.Name, Lit.String)] = {

@tailrec
def extractLiteralName(acc: List[Tree]): Option[Lit.String] = acc match {
case head :: tail =>
head match {
case lit: Lit.String => Some(lit)
case _ => extractLiteralName(head.children ::: tail)
}
case immutable.Nil => None
def extractLiteralName(tree: Tree): Option[Lit.String] = tree match {
case lit: Lit.String => Some(lit)
case Term.Select(lit: Lit.String, _) => Some(lit)
case Term.Apply(Term.Select(lit: Lit.String, Term.Name("tag")), _) =>
Some(lit)
case _ => None
}

@tailrec
// find proper Apply in possibly multiple nested Applies
def loop(acc: List[Tree]): Option[(Term.Name, Lit.String)] = acc match {
case head :: tail =>
head match {
case Term.Apply(term @ Term.Name(name), args)
case Term.Apply(term @ Term.Name(name), List(arg))
if (testFunctionsNames(name)) =>
extractLiteralName(args) match {
extractLiteralName(arg) match {
case Some(lit) =>
Some((term, lit))
case None => loop(tail)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ class TestSuitesProviderSuite extends BaseLspSuite("testSuitesFinderSuite") {
| println(name)
| assertEquals(n1, n2)
| }
| test("test1" + "test2") {}
|}
|""".stripMargin,
List("app/src/main/scala/a/b/c/MunitTestSuite.scala"),
Expand Down

0 comments on commit 8eeebaf

Please sign in to comment.