Fix unit test configuration (#4847)

This commit is contained in:
Martin Redington 2023-03-30 21:46:31 +01:00 committed by GitHub
parent da27f1c7fd
commit ca43d2359b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 13 deletions

View File

@ -33,7 +33,7 @@ extension Configuration {
excludeByPrefix: Bool = false, excludeByPrefix: Bool = false,
fileManager: LintableFileManager = FileManager.default fileManager: LintableFileManager = FileManager.default
) -> [String] { ) -> [String] {
if path.isFile { if fileManager.isFile(atPath: path) {
if forceExclude { if forceExclude {
return excludeByPrefix return excludeByPrefix
? filterExcludedPathsByPrefix(in: [path.absolutePathStandardized()]) ? filterExcludedPathsByPrefix(in: [path.absolutePathStandardized()])

View File

@ -19,6 +19,13 @@ public protocol LintableFileManager {
/// ///
/// - returns: A date, if one was determined. /// - returns: A date, if one was determined.
func modificationDate(forFileAtPath path: String) -> Date? func modificationDate(forFileAtPath path: String) -> Date?
/// Returns true if a file (but not a directory) exists at the specified path.
///
/// - parameter path: The path that should be checked to see if it is a file.
///
/// - returns: true if the specified path is a file.
func isFile(atPath path: String) -> Bool
} }
extension FileManager: LintableFileManager { extension FileManager: LintableFileManager {
@ -42,4 +49,8 @@ extension FileManager: LintableFileManager {
public func modificationDate(forFileAtPath path: String) -> Date? { public func modificationDate(forFileAtPath path: String) -> Date? {
return (try? attributesOfItem(atPath: path))?[.modificationDate] as? Date return (try? attributesOfItem(atPath: path))?[.modificationDate] as? Date
} }
public func isFile(atPath path: String) -> Bool {
path.isFile
}
} }

View File

@ -226,21 +226,25 @@ class ConfigurationTests: XCTestCase {
private class TestFileManager: LintableFileManager { private class TestFileManager: LintableFileManager {
func filesToLint(inPath path: String, rootDirectory: String? = nil) -> [String] { func filesToLint(inPath path: String, rootDirectory: String? = nil) -> [String] {
var filesToLint: [String] = []
switch path { switch path {
case "directory": return ["directory/File1.swift", "directory/File2.swift", case "directory": filesToLint = ["directory/File1.swift", "directory/File2.swift",
"directory/excluded/Excluded.swift", "directory/excluded/Excluded.swift",
"directory/ExcludedFile.swift"] "directory/ExcludedFile.swift"]
case "directory/excluded": return ["directory/excluded/Excluded.swift"] case "directory/excluded": filesToLint = ["directory/excluded/Excluded.swift"]
case "directory/ExcludedFile.swift": return ["directory/ExcludedFile.swift"] case "directory/ExcludedFile.swift": filesToLint = ["directory/ExcludedFile.swift"]
default: break default: XCTFail("Should not be called with path \(path)")
} }
XCTFail("Should not be called with path \(path)") return filesToLint.absolutePathsStandardized()
return []
} }
func modificationDate(forFileAtPath path: String) -> Date? { func modificationDate(forFileAtPath path: String) -> Date? {
return nil return nil
} }
func isFile(atPath path: String) -> Bool {
path.hasSuffix(".swift")
}
} }
func testExcludedPaths() { func testExcludedPaths() {
@ -248,7 +252,7 @@ class ConfigurationTests: XCTestCase {
excludedPaths: ["directory/excluded", excludedPaths: ["directory/excluded",
"directory/ExcludedFile.swift"]) "directory/ExcludedFile.swift"])
let paths = configuration.lintablePaths(inPath: "", forceExclude: false, fileManager: TestFileManager()) let paths = configuration.lintablePaths(inPath: "", forceExclude: false, fileManager: TestFileManager())
XCTAssertEqual(["directory/File1.swift", "directory/File2.swift"], paths) XCTAssertEqual(["directory/File1.swift", "directory/File2.swift"].absolutePathsStandardized(), paths)
} }
func testForceExcludesFile() { func testForceExcludesFile() {
@ -262,21 +266,21 @@ class ConfigurationTests: XCTestCase {
let configuration = Configuration(includedPaths: ["directory"], let configuration = Configuration(includedPaths: ["directory"],
excludedPaths: ["directory/ExcludedFile.swift", "directory/excluded"]) excludedPaths: ["directory/ExcludedFile.swift", "directory/excluded"])
let paths = configuration.lintablePaths(inPath: "", forceExclude: true, fileManager: TestFileManager()) let paths = configuration.lintablePaths(inPath: "", forceExclude: true, fileManager: TestFileManager())
XCTAssertEqual(["directory/File1.swift", "directory/File2.swift"], paths) XCTAssertEqual(["directory/File1.swift", "directory/File2.swift"].absolutePathsStandardized(), paths)
} }
func testForceExcludesDirectory() { func testForceExcludesDirectory() {
let configuration = Configuration(excludedPaths: ["directory/excluded", "directory/ExcludedFile.swift"]) let configuration = Configuration(excludedPaths: ["directory/excluded", "directory/ExcludedFile.swift"])
let paths = configuration.lintablePaths(inPath: "directory", forceExclude: true, let paths = configuration.lintablePaths(inPath: "directory", forceExclude: true,
fileManager: TestFileManager()) fileManager: TestFileManager())
XCTAssertEqual(["directory/File1.swift", "directory/File2.swift"], paths) XCTAssertEqual(["directory/File1.swift", "directory/File2.swift"].absolutePathsStandardized(), paths)
} }
func testForceExcludesDirectoryThatIsNotInExcludedButHasChildrenThatAre() { func testForceExcludesDirectoryThatIsNotInExcludedButHasChildrenThatAre() {
let configuration = Configuration(excludedPaths: ["directory/excluded", "directory/ExcludedFile.swift"]) let configuration = Configuration(excludedPaths: ["directory/excluded", "directory/ExcludedFile.swift"])
let paths = configuration.lintablePaths(inPath: "directory", forceExclude: true, let paths = configuration.lintablePaths(inPath: "directory", forceExclude: true,
fileManager: TestFileManager()) fileManager: TestFileManager())
XCTAssertEqual(["directory/File1.swift", "directory/File2.swift"], paths) XCTAssertEqual(["directory/File1.swift", "directory/File2.swift"].absolutePathsStandardized(), paths)
} }
func testLintablePaths() { func testLintablePaths() {
@ -502,3 +506,9 @@ extension ConfigurationTests {
XCTAssertEqual(configuration2.cachePath, "cache/path/1") XCTAssertEqual(configuration2.cachePath, "cache/path/1")
} }
} }
private extension Sequence where Element == String {
func absolutePathsStandardized() -> [String] {
map { $0.absolutePathStandardized() }
}
}

View File

@ -62,6 +62,10 @@ private class TestFileManager: LintableFileManager {
fileprivate func modificationDate(forFileAtPath path: String) -> Date? { fileprivate func modificationDate(forFileAtPath path: String) -> Date? {
return stubbedModificationDateByPath[path] return stubbedModificationDateByPath[path]
} }
fileprivate func isFile(atPath path: String) -> Bool {
false
}
} }
class LinterCacheTests: XCTestCase { class LinterCacheTests: XCTestCase {