Fix unit test configuration (#4847)
This commit is contained in:
parent
da27f1c7fd
commit
ca43d2359b
|
@ -33,7 +33,7 @@ extension Configuration {
|
|||
excludeByPrefix: Bool = false,
|
||||
fileManager: LintableFileManager = FileManager.default
|
||||
) -> [String] {
|
||||
if path.isFile {
|
||||
if fileManager.isFile(atPath: path) {
|
||||
if forceExclude {
|
||||
return excludeByPrefix
|
||||
? filterExcludedPathsByPrefix(in: [path.absolutePathStandardized()])
|
||||
|
|
|
@ -19,6 +19,13 @@ public protocol LintableFileManager {
|
|||
///
|
||||
/// - returns: A date, if one was determined.
|
||||
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 {
|
||||
|
@ -42,4 +49,8 @@ extension FileManager: LintableFileManager {
|
|||
public func modificationDate(forFileAtPath path: String) -> Date? {
|
||||
return (try? attributesOfItem(atPath: path))?[.modificationDate] as? Date
|
||||
}
|
||||
|
||||
public func isFile(atPath path: String) -> Bool {
|
||||
path.isFile
|
||||
}
|
||||
}
|
||||
|
|
|
@ -226,21 +226,25 @@ class ConfigurationTests: XCTestCase {
|
|||
|
||||
private class TestFileManager: LintableFileManager {
|
||||
func filesToLint(inPath path: String, rootDirectory: String? = nil) -> [String] {
|
||||
var filesToLint: [String] = []
|
||||
switch path {
|
||||
case "directory": return ["directory/File1.swift", "directory/File2.swift",
|
||||
"directory/excluded/Excluded.swift",
|
||||
"directory/ExcludedFile.swift"]
|
||||
case "directory/excluded": return ["directory/excluded/Excluded.swift"]
|
||||
case "directory/ExcludedFile.swift": return ["directory/ExcludedFile.swift"]
|
||||
default: break
|
||||
case "directory": filesToLint = ["directory/File1.swift", "directory/File2.swift",
|
||||
"directory/excluded/Excluded.swift",
|
||||
"directory/ExcludedFile.swift"]
|
||||
case "directory/excluded": filesToLint = ["directory/excluded/Excluded.swift"]
|
||||
case "directory/ExcludedFile.swift": filesToLint = ["directory/ExcludedFile.swift"]
|
||||
default: XCTFail("Should not be called with path \(path)")
|
||||
}
|
||||
XCTFail("Should not be called with path \(path)")
|
||||
return []
|
||||
return filesToLint.absolutePathsStandardized()
|
||||
}
|
||||
|
||||
func modificationDate(forFileAtPath path: String) -> Date? {
|
||||
return nil
|
||||
}
|
||||
|
||||
func isFile(atPath path: String) -> Bool {
|
||||
path.hasSuffix(".swift")
|
||||
}
|
||||
}
|
||||
|
||||
func testExcludedPaths() {
|
||||
|
@ -248,7 +252,7 @@ class ConfigurationTests: XCTestCase {
|
|||
excludedPaths: ["directory/excluded",
|
||||
"directory/ExcludedFile.swift"])
|
||||
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() {
|
||||
|
@ -262,21 +266,21 @@ class ConfigurationTests: XCTestCase {
|
|||
let configuration = Configuration(includedPaths: ["directory"],
|
||||
excludedPaths: ["directory/ExcludedFile.swift", "directory/excluded"])
|
||||
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() {
|
||||
let configuration = Configuration(excludedPaths: ["directory/excluded", "directory/ExcludedFile.swift"])
|
||||
let paths = configuration.lintablePaths(inPath: "directory", forceExclude: true,
|
||||
fileManager: TestFileManager())
|
||||
XCTAssertEqual(["directory/File1.swift", "directory/File2.swift"], paths)
|
||||
XCTAssertEqual(["directory/File1.swift", "directory/File2.swift"].absolutePathsStandardized(), paths)
|
||||
}
|
||||
|
||||
func testForceExcludesDirectoryThatIsNotInExcludedButHasChildrenThatAre() {
|
||||
let configuration = Configuration(excludedPaths: ["directory/excluded", "directory/ExcludedFile.swift"])
|
||||
let paths = configuration.lintablePaths(inPath: "directory", forceExclude: true,
|
||||
fileManager: TestFileManager())
|
||||
XCTAssertEqual(["directory/File1.swift", "directory/File2.swift"], paths)
|
||||
XCTAssertEqual(["directory/File1.swift", "directory/File2.swift"].absolutePathsStandardized(), paths)
|
||||
}
|
||||
|
||||
func testLintablePaths() {
|
||||
|
@ -502,3 +506,9 @@ extension ConfigurationTests {
|
|||
XCTAssertEqual(configuration2.cachePath, "cache/path/1")
|
||||
}
|
||||
}
|
||||
|
||||
private extension Sequence where Element == String {
|
||||
func absolutePathsStandardized() -> [String] {
|
||||
map { $0.absolutePathStandardized() }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -62,6 +62,10 @@ private class TestFileManager: LintableFileManager {
|
|||
fileprivate func modificationDate(forFileAtPath path: String) -> Date? {
|
||||
return stubbedModificationDateByPath[path]
|
||||
}
|
||||
|
||||
fileprivate func isFile(atPath path: String) -> Bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
class LinterCacheTests: XCTestCase {
|
||||
|
|
Loading…
Reference in New Issue