diff --git a/modules/cli/src/main/scala/scala/cli/commands/fix/Fix.scala b/modules/cli/src/main/scala/scala/cli/commands/fix/Fix.scala index 6f8c5a0379..c18f09255e 100644 --- a/modules/cli/src/main/scala/scala/cli/commands/fix/Fix.scala +++ b/modules/cli/src/main/scala/scala/cli/commands/fix/Fix.scala @@ -51,7 +51,7 @@ object Fix extends ScalaCommand[FixOptions] { given LoggingUtilities(logger, inputs.workspace) // Deal with directives from the Main scope - { + val testDirectivesFromMain = { val originalMainDirectives = getExtractedDirectives(mainSources) .filterNot(hasTargetDirectives) @@ -67,16 +67,23 @@ object Fix extends ScalaCommand[FixOptions] { directive <- transformedMainDirective.directives } yield directive - if (allDirectives.nonEmpty) + val (testScopeDirectives, allMainDirectives) = + allDirectives.partition(_.key.startsWith("test")) + + if (allMainDirectives.nonEmpty) projectFileContents .append("// Main") .append(newLine) - createFormattedLinesAndAppend(allDirectives, projectFileContents, isTest = false) + createFormattedLinesAndAppend(allMainDirectives, projectFileContents, isTest = false) + + testScopeDirectives } // Deal with directives from the Test scope - if (testSources.paths.nonEmpty || testSources.inMemory.nonEmpty) { + if ( + testSources.paths.nonEmpty || testSources.inMemory.nonEmpty || testDirectivesFromMain.nonEmpty + ) { val originalTestDirectives = getExtractedDirectives(testSources) .filterNot(hasTargetDirectives) @@ -97,7 +104,7 @@ object Fix extends ScalaCommand[FixOptions] { val allDirectives = for { directivesWithTestPrefix <- transformedTestDirectives.map(_.withTestPrefix) - directive <- directivesWithTestPrefix + directive <- directivesWithTestPrefix ++ testDirectivesFromMain } yield directive createFormattedLinesAndAppend(allDirectives, projectFileContents, isTest = true) @@ -185,6 +192,13 @@ object Fix extends ScalaCommand[FixOptions] { val strictDirectivesWithNewKeys = strictDirectives.flatMap { strictDir => val newKeyOpt = allKeysGrouped.find(_.contains(strictDir.key)) .flatMap(_.headOption) + .map { key => + if (key.startsWith("test")) + val withTestStripped = key.stripPrefix("test").stripPrefix(".") + "test." + withTestStripped.take(1).toLowerCase + withTestStripped.drop(1) + else + key + } newKeyOpt.map(newKey => strictDir.copy(key = newKey)) } diff --git a/modules/directives/src/main/scala/scala/build/preprocessing/directives/Tests.scala b/modules/directives/src/main/scala/scala/build/preprocessing/directives/Tests.scala index 23e154f72d..d748900115 100644 --- a/modules/directives/src/main/scala/scala/build/preprocessing/directives/Tests.scala +++ b/modules/directives/src/main/scala/scala/build/preprocessing/directives/Tests.scala @@ -16,6 +16,7 @@ import scala.cli.commands.SpecificationLevel @DirectiveLevel(SpecificationLevel.SHOULD) // format: off final case class Tests( + @DirectiveName("test.framework") testFramework: Option[String] = None ) extends HasBuildOptions { // format: on diff --git a/modules/integration/src/test/scala/scala/cli/integration/FixTests.scala b/modules/integration/src/test/scala/scala/cli/integration/FixTests.scala index aabfa725dd..097d073d97 100644 --- a/modules/integration/src/test/scala/scala/cli/integration/FixTests.scala +++ b/modules/integration/src/test/scala/scala/cli/integration/FixTests.scala @@ -81,6 +81,8 @@ class FixTests extends ScalaCliSuite { s"""//> using objectWrapper |//> using dep "com.lihaoyi::os-lib:0.9.1" | + |//> using test.dep "org.typelevel::cats-core:2.9.0" + | |package com.foo.main | |object Main extends App { @@ -132,7 +134,7 @@ class FixTests extends ScalaCliSuite { | |// Test |//> using test.options "-Xasync", "-Xfatal-warnings" - |//> using test.dependency "org.scalameta::munit::0.7.29" + |//> using test.dependency "org.scalameta::munit::0.7.29", "org.typelevel::cats-core:2.9.0" |""".stripMargin ) @@ -191,6 +193,8 @@ class FixTests extends ScalaCliSuite { |//> using dep "com.lihaoyi::os-lib:0.9.1" |//> using scala 3.3.0 | + |//> using test.dep "org.typelevel::cats-core:2.9.0" + | |package com.foo.main | |object Main extends App { @@ -273,7 +277,7 @@ class FixTests extends ScalaCliSuite { | |// Test |//> using test.options "-Xasync", "-Xfatal-warnings" - |//> using test.dependency "org.scalameta::munit::0.7.29" + |//> using test.dependency "org.scalameta::munit::0.7.29", "org.typelevel::cats-core:2.9.0" |""".stripMargin )