|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package chisel3.internal |
| 4 | + |
| 5 | +import java.nio.file.{FileSystems, PathMatcher, Paths} |
| 6 | + |
| 7 | +class InlineTestIncluder private (includeModuleGlobs: Seq[String], includeTestNameGlobs: Seq[String]) { |
| 8 | + private def copy( |
| 9 | + includeModuleGlobs: Seq[String] = this.includeModuleGlobs, |
| 10 | + includeTestNameGlobs: Seq[String] = this.includeTestNameGlobs |
| 11 | + ) = new InlineTestIncluder(includeModuleGlobs, includeTestNameGlobs) |
| 12 | + |
| 13 | + def includeModule(glob: String) = copy(includeModuleGlobs = includeModuleGlobs ++ Seq(glob)) |
| 14 | + def includeTest(glob: String) = copy(includeTestNameGlobs = includeTestNameGlobs ++ Seq(glob)) |
| 15 | + |
| 16 | + private val filesystem = FileSystems.getDefault() |
| 17 | + |
| 18 | + private def matchesGlob(glob: String, path: String): Boolean = { |
| 19 | + val matcher = filesystem.getPathMatcher(s"glob:$glob") |
| 20 | + matcher.matches(Paths.get(path)) |
| 21 | + } |
| 22 | + |
| 23 | + def shouldElaborateTest(moduleDesiredName: String, testName: String): Boolean = { |
| 24 | + val (resolvedModuleGlobs, resolvedTestNameGlobs) = (includeModuleGlobs, includeTestNameGlobs) match { |
| 25 | + case x @ (Seq(), Seq()) => x |
| 26 | + // If only one type of glob is provided, default to "*" for the other. |
| 27 | + case (Seq(), ts) => (Seq("*"), ts) |
| 28 | + case (ms, Seq()) => (ms, Seq("*")) |
| 29 | + case x => x |
| 30 | + } |
| 31 | + |
| 32 | + resolvedModuleGlobs.exists { glob => matchesGlob(glob, moduleDesiredName) } && |
| 33 | + resolvedTestNameGlobs.exists { glob => matchesGlob(glob, testName) } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +object InlineTestIncluder { |
| 38 | + |
| 39 | + /** Create an InlineTestIncluder that does not include any tests */ |
| 40 | + def none: InlineTestIncluder = new InlineTestIncluder(Nil, Nil) |
| 41 | + |
| 42 | + /** Create an InlineTestIncluder that includes all tests */ |
| 43 | + def all: InlineTestIncluder = new InlineTestIncluder(Seq("*"), Seq("*")) |
| 44 | + |
| 45 | + /** Create an InlineTestIncluder with module and test name globs */ |
| 46 | + def apply( |
| 47 | + includeModuleGlobs: Seq[String], |
| 48 | + includeTestNameGlobs: Seq[String] |
| 49 | + ): InlineTestIncluder = new InlineTestIncluder(includeModuleGlobs, includeModuleGlobs) |
| 50 | +} |
0 commit comments