@@ -28,6 +28,15 @@ object BuiltInRules extends CommandHelpers {
2828 DirectivesPreprocessingUtils .usingDirectiveWithReqsHandlers
2929 .flatMap(_.keys)
3030
31+ private lazy val directiveTestPrefix = " test."
32+ extension (strictDirective : StrictDirective ) {
33+ private def hasTestPrefix : Boolean = strictDirective.key.startsWith(directiveTestPrefix)
34+ private def existsTestEquivalent : Boolean =
35+ ! strictDirective.hasTestPrefix &&
36+ usingDirectivesWithTestPrefixKeysGrouped
37+ .exists(_.nameAliases.contains(directiveTestPrefix + strictDirective.key))
38+ }
39+
3140 private val newLine : String = System .lineSeparator()
3241
3342 def runRules (
@@ -114,7 +123,13 @@ object BuiltInRules extends CommandHelpers {
114123
115124 val allDirectives = for {
116125 directivesWithTestPrefix <- transformedTestDirectives.map(_.withTestPrefix)
117- directive <- directivesWithTestPrefix ++ testDirectivesFromMain
126+ directivesWithNoTestPrefixEquivalents <-
127+ transformedTestDirectives.map {
128+ _.noTestPrefixAvailable
129+ .filter(_.existsTestEquivalent)
130+ }
131+ directive <-
132+ directivesWithTestPrefix ++ directivesWithNoTestPrefixEquivalents ++ testDirectivesFromMain
118133 } yield directive
119134
120135 createFormattedLinesAndAppend(allDirectives, projectFileContents, isTest = true )
@@ -139,7 +154,12 @@ object BuiltInRules extends CommandHelpers {
139154 .foreach(d => removeDirectivesFrom(d.position))
140155 directivesFromWritableTestInputs
141156 .filterNot(ttd => isProjectFile(ttd.positions))
142- .foreach(ttd => removeDirectivesFrom(ttd.positions, toKeep = ttd.noTestPrefixAvailable))
157+ .foreach(ttd =>
158+ removeDirectivesFrom(
159+ position = ttd.positions,
160+ toKeep = ttd.noTestPrefixAvailable.filterNot(_.existsTestEquivalent)
161+ )
162+ )
143163 }
144164
145165 private def getProjectSources (inputs : Inputs , logger : Logger )(using
@@ -262,18 +282,15 @@ object BuiltInRules extends CommandHelpers {
262282 extractedFromSingleElement <- extractedDirectives
263283 directives = extractedFromSingleElement.directives
264284 } yield {
265- val (withTestEquivalent, noTestEquivalent) = directives.partition { directive =>
266- usingDirectivesWithTestPrefixKeysGrouped.exists(
267- _.nameAliases.contains(" test." + directive.key)
268- )
269- }
270-
285+ val (withInitialTestPrefix, noInitialTestPrefix) = directives.partition(_.hasTestPrefix)
286+ val (withTestEquivalent, noTestEquivalent) =
287+ noInitialTestPrefix.partition(_.existsTestEquivalent)
271288 val transformedToTestEquivalents = withTestEquivalent.map {
272289 case StrictDirective (key, values, _) => StrictDirective (" test." + key, values)
273290 }
274291
275292 TransformedTestDirectives (
276- withTestPrefix = transformedToTestEquivalents,
293+ withTestPrefix = transformedToTestEquivalents ++ withInitialTestPrefix ,
277294 noTestPrefixAvailable = noTestEquivalent,
278295 positions = extractedFromSingleElement.position
279296 )
@@ -328,7 +345,9 @@ object BuiltInRules extends CommandHelpers {
328345 StrictDirective (key, directives.flatMap(_.values))
329346 }
330347 // group by key prefixes to create splits between groups
331- .groupBy(dir => (if (isTest) dir.key.stripPrefix(" test." ) else dir.key).takeWhile(_ != '.' ))
348+ .groupBy(dir =>
349+ (if (isTest) dir.key.stripPrefix(directiveTestPrefix) else dir.key).takeWhile(_ != '.' )
350+ )
332351 .map { (_, directives) =>
333352 directives.flatMap(_.explodeToStringsWithColLimit()).toSeq.sorted
334353 }
0 commit comments