-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add exclude directive and command line options to exclude sources
- Loading branch information
Showing
23 changed files
with
472 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
modules/build/src/main/scala/scala/build/internal/util/RegexUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package scala.build.internal.util | ||
|
||
import java.util.regex.Pattern | ||
|
||
object RegexUtils { | ||
|
||
/** Based on junit-interface [GlobFilter. | ||
* compileGlobPattern](https://github.com/sbt/junit-interface/blob/f8c6372ed01ce86f15393b890323d96afbe6d594/src/main/java/com/novocode/junit/GlobFilter.java#L37) | ||
* | ||
* @return | ||
* Pattern allows to regex input which contains only *, for example `*foo*` match to | ||
* `MyTests.foo` | ||
*/ | ||
def globPattern(expr: String): Pattern = { | ||
val a = expr.split("\\*", -1) | ||
val b = new StringBuilder() | ||
for (i <- 0 until a.length) { | ||
if (i != 0) b.append(".*") | ||
if (a(i).nonEmpty) b.append(Pattern.quote(a(i).replaceAll("\n", "\\n"))) | ||
} | ||
Pattern.compile(b.toString) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 183 additions & 0 deletions
183
modules/build/src/test/scala/scala/build/tests/ExcludeTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package scala.build.tests | ||
|
||
import com.eed3si9n.expecty.Expecty.expect | ||
import coursier.cache.{ArchiveCache, Cache} | ||
import coursier.util.{Artifact, Task} | ||
|
||
import java.io.File | ||
import scala.build.Ops.* | ||
import scala.build.Sources | ||
import scala.build.internal.CustomCodeWrapper | ||
import scala.build.CrossSources | ||
import scala.build.errors.ExcludeDefinitionError | ||
import scala.build.options.{BuildOptions, Scope, SuppressWarningOptions} | ||
|
||
class ExcludeTests extends munit.FunSuite { | ||
|
||
val preprocessors = Sources.defaultPreprocessors( | ||
CustomCodeWrapper, | ||
ArchiveCache().withCache( | ||
new Cache[Task] { | ||
def fetch = _ => sys.error("shouldn't be used") | ||
|
||
def file(artifact: Artifact) = sys.error("shouldn't be used") | ||
|
||
def ec = sys.error("shouldn't be used") | ||
} | ||
), | ||
None, | ||
() => sys.error("shouldn't be used") | ||
) | ||
|
||
test("throw error when exclude found in multiple file") { | ||
val testInputs = TestInputs( | ||
os.rel / "Hello.scala" -> | ||
"""//> using exclude "*.sc" | ||
|""".stripMargin, | ||
os.rel / "Main.scala" -> | ||
"""//> using exclude "*/test/*" | ||
|""".stripMargin | ||
) | ||
testInputs.withInputs { (_, inputs) => | ||
val crossSources = | ||
CrossSources.forInputs( | ||
inputs, | ||
preprocessors, | ||
TestLogger(), | ||
SuppressWarningOptions() | ||
) | ||
crossSources match { | ||
case Left(_: ExcludeDefinitionError) => | ||
case o => fail("Exception expected", clues(o)) | ||
} | ||
} | ||
} | ||
|
||
test("throw error when exclude found in non top-level project.scala and file") { | ||
val testInputs = TestInputs( | ||
os.rel / "Main.scala" -> | ||
"""//> using exclude "*/test/*" | ||
|""".stripMargin, | ||
os.rel / "src" / "project.scala" -> | ||
s"""//> using exclude "*.sc" """ | ||
) | ||
testInputs.withInputs { (_, inputs) => | ||
val crossSources = | ||
CrossSources.forInputs( | ||
inputs, | ||
preprocessors, | ||
TestLogger(), | ||
SuppressWarningOptions() | ||
) | ||
crossSources match { | ||
case Left(_: ExcludeDefinitionError) => | ||
case o => fail("Exception expected", clues(o)) | ||
} | ||
} | ||
} | ||
|
||
test("exclude relative paths") { | ||
val testInputs = TestInputs( | ||
os.rel / "Hello.scala" -> "object Hello", | ||
os.rel / "Main.scala" -> | ||
"""object Main { | ||
|}""".stripMargin, | ||
os.rel / "project.scala" -> | ||
s"""//> using exclude "Main.scala" """ | ||
) | ||
testInputs.withInputs { (_, inputs) => | ||
val (crossSources, _) = | ||
CrossSources.forInputs( | ||
inputs, | ||
preprocessors, | ||
TestLogger(), | ||
SuppressWarningOptions() | ||
).orThrow | ||
val scopedSources = crossSources.scopedSources(BuildOptions()).orThrow | ||
val sources = scopedSources.sources(Scope.Main, crossSources.sharedOptions(BuildOptions())) | ||
|
||
expect(sources.paths.nonEmpty) | ||
expect(sources.paths.length == 2) | ||
expect(sources.paths.map(_._2) == Seq(os.rel / "Hello.scala", os.rel / "project.scala")) | ||
} | ||
} | ||
|
||
test("exclude absolute file paths") { | ||
val testInputs = TestInputs( | ||
os.rel / "Hello.scala" -> "object Hello", | ||
os.rel / "Main.scala" -> | ||
"""object Main { | ||
|}""".stripMargin, | ||
os.rel / "project.scala" -> | ||
s"""//> using exclude "$${.}${File.separator}Main.scala" """ | ||
) | ||
testInputs.withInputs { (_, inputs) => | ||
val (crossSources, _) = | ||
CrossSources.forInputs( | ||
inputs, | ||
preprocessors, | ||
TestLogger(), | ||
SuppressWarningOptions() | ||
).orThrow | ||
val scopedSources = crossSources.scopedSources(BuildOptions()).orThrow | ||
val sources = scopedSources.sources(Scope.Main, crossSources.sharedOptions(BuildOptions())) | ||
|
||
expect(sources.paths.nonEmpty) | ||
expect(sources.paths.length == 2) | ||
expect(sources.paths.map(_._2) == Seq(os.rel / "Hello.scala", os.rel / "project.scala")) | ||
} | ||
} | ||
|
||
test("exclude relative directory paths") { | ||
val testInputs = TestInputs( | ||
os.rel / "Hello.scala" -> "object Hello", | ||
os.rel / "src" / "scala" / "Main.scala" -> | ||
"""object Main { | ||
|}""".stripMargin, | ||
os.rel / "project.scala" -> | ||
"""//> using exclude "src/*.scala" """ | ||
) | ||
testInputs.withInputs { (_, inputs) => | ||
val (crossSources, _) = | ||
CrossSources.forInputs( | ||
inputs, | ||
preprocessors, | ||
TestLogger(), | ||
SuppressWarningOptions() | ||
).orThrow | ||
val scopedSources = crossSources.scopedSources(BuildOptions()).orThrow | ||
val sources = scopedSources.sources(Scope.Main, crossSources.sharedOptions(BuildOptions())) | ||
|
||
expect(sources.paths.nonEmpty) | ||
expect(sources.paths.length == 2) | ||
expect(sources.paths.map(_._2) == Seq(os.rel / "Hello.scala", os.rel / "project.scala")) | ||
} | ||
} | ||
|
||
test("exclude relative directory paths with glob pattern") { | ||
val testInputs = TestInputs( | ||
os.rel / "Hello.scala" -> "object Hello", | ||
os.rel / "src" / "scala" / "Main.scala" -> | ||
"""object Main { | ||
|}""".stripMargin, | ||
os.rel / "project.scala" -> | ||
"""//> using exclude "src/*.scala" """ | ||
) | ||
testInputs.withInputs { (_, inputs) => | ||
val (crossSources, _) = | ||
CrossSources.forInputs( | ||
inputs, | ||
preprocessors, | ||
TestLogger(), | ||
SuppressWarningOptions() | ||
).orThrow | ||
val scopedSources = crossSources.scopedSources(BuildOptions()).orThrow | ||
val sources = scopedSources.sources(Scope.Main, crossSources.sharedOptions(BuildOptions())) | ||
|
||
expect(sources.paths.nonEmpty) | ||
expect(sources.paths.length == 2) | ||
expect(sources.paths.map(_._2) == Seq(os.rel / "Hello.scala", os.rel / "project.scala")) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.