Skip to content

Commit 47808d5

Browse files
Allow filtering which tests are run (#4)
* Allow filtering which tests are run * Fix Linux build
1 parent dc58d77 commit 47808d5

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

Sources/LiteSupport/Run.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ public func runLite(substitutions: [(String, String)],
3636
testDirPath: String?,
3737
testLinePrefix: String,
3838
parallelismLevel: ParallelismLevel = .none,
39-
successMessage: String = "All tests passed! 🎉") throws -> Bool {
39+
successMessage: String = "All tests passed! 🎉",
40+
filters: [NSRegularExpression] = []) throws -> Bool {
4041
let testRunner = try TestRunner(testDirPath: testDirPath,
4142
substitutions: substitutions,
4243
pathExtensions: pathExtensions,
4344
testLinePrefix: testLinePrefix,
4445
parallelismLevel: parallelismLevel,
45-
successMessage: successMessage)
46+
successMessage: successMessage,
47+
filters: filters)
4648
return try testRunner.run()
4749
}

Sources/LiteSupport/TestRunner.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,18 @@ class TestRunner {
5656
/// The message to print if all the tests passed.
5757
let successMessage: String
5858

59+
/// The set of filters to run over the file names, to refine which tests
60+
/// are run.
61+
let filters: [NSRegularExpression]
62+
5963
/// Creates a test runner that will execute all tests in the provided
6064
/// directory.
6165
/// - throws: A LiteError if the test directory is invalid.
6266
init(testDirPath: String?, substitutions: [(String, String)],
6367
pathExtensions: Set<String>, testLinePrefix: String,
6468
parallelismLevel: ParallelismLevel,
65-
successMessage: String) throws {
69+
successMessage: String,
70+
filters: [NSRegularExpression]) throws {
6671
let fm = FileManager.default
6772
var isDir: ObjCBool = false
6873
let testDirPath =
@@ -79,6 +84,7 @@ class TestRunner {
7984
self.testLinePrefix = testLinePrefix
8085
self.parallelismLevel = parallelismLevel
8186
self.successMessage = successMessage
87+
self.filters = filters
8288
}
8389

8490
func discoverTests() throws -> [TestFile] {
@@ -88,6 +94,14 @@ class TestRunner {
8894
var files = [TestFile]()
8995
for case let file as URL in enumerator {
9096
guard pathExtensions.contains(file.pathExtension) else { continue }
97+
let nsPath = NSString(string: file.path)
98+
let matchesFilter = filters.contains {
99+
return $0.numberOfMatches(
100+
in: file.path,
101+
range: NSRange(location: 0, length: nsPath.length)
102+
) != 0
103+
}
104+
guard filters.isEmpty || matchesFilter else { continue }
91105
let runLines = try RunLineParser.parseRunLines(in: file,
92106
prefix: testLinePrefix)
93107
if runLines.isEmpty { continue }

Sources/lite-test/main.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import LiteSupport
66

77
/// Runs `lite` looking for `.test` files and executing them.
88
do {
9-
let allPassed = try runLite(substitutions: [("echo", "echo")],
10-
pathExtensions: ["test"],
11-
testDirPath: nil,
12-
testLinePrefix: "//",
13-
parallelismLevel: .automatic)
9+
let allPassed = try runLite(
10+
substitutions: [("echo", "echo")],
11+
pathExtensions: ["test"],
12+
testDirPath: nil,
13+
testLinePrefix: "//",
14+
parallelismLevel: .automatic)
1415
exit(allPassed ? 0 : -1)
1516
} catch let err as LiteError {
1617
fputs("error: \(err.message)", stderr)

0 commit comments

Comments
 (0)