Skip to content

Instantly share code, notes, and snippets.

@JohnSundell
Created April 5, 2017 19:51
Show Gist options
  • Save JohnSundell/fe713deb2f18ed92fe0fccaecfedd9bd to your computer and use it in GitHub Desktop.
Save JohnSundell/fe713deb2f18ed92fe0fccaecfedd9bd to your computer and use it in GitHub Desktop.
A script that verifies that all tests within a folder structure has at least one assert
import Foundation
import Files
var testsMissingAsserts = [String]()
for file in FileSystem().currentFolder.makeFileSequence(recursive: true) {
guard file.extension == "swift" else {
continue
}
guard file.name.contains("Tests") else {
continue
}
var testName: String?
var assertFound = false
for line in try file.readAsString().components(separatedBy: .newlines) {
let line = line.trimmingCharacters(in: .whitespaces)
guard !line.hasPrefix("func test") else {
if let testName = testName {
if !assertFound {
testsMissingAsserts.append(testName)
}
}
let rawTestName = line.substring(from: line.index(line.startIndex, offsetBy: 5))
testName = rawTestName.components(separatedBy: "(")[0]
assertFound = false
continue
}
if !assertFound {
assertFound = !line.hasPrefix("//") && line.contains("XCTAssert")
}
}
if let testName = testName {
if !assertFound {
testsMissingAsserts.append(testName)
}
}
}
guard !testsMissingAsserts.isEmpty else {
print("✅ All your tests have asserts!")
exit(0)
}
print("⚠️ The following tests do not contain at least one assert:")
for test in testsMissingAsserts {
print("- \(test)")
}
exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment