Skip to content

Commit

Permalink
Add support for test. directives in main scope
Browse files Browse the repository at this point in the history
  • Loading branch information
MaciejG604 committed Aug 1, 2023
1 parent 66f6078 commit 03a1706
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
24 changes: 19 additions & 5 deletions modules/cli/src/main/scala/scala/cli/commands/fix/Fix.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -185,6 +192,13 @@ object Fix extends ScalaCommand[FixOptions] {
val strictDirectivesWithNewKeys = strictDirectives.flatMap { strictDir =>
val newKeyOpt = allKeysGrouped.find(_.nameAliases.contains(strictDir.key))
.flatMap(_.nameAliases.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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
)

Expand Down

0 comments on commit 03a1706

Please sign in to comment.