mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
The current algorithm is like "collect all included files and subtract all excluded files".
Collecting all included and all excluded files relies on the file system. This can become slow
when the patterns used to exclude files resolve to a large number of files.
The new approach only collects all lintable files and checks them against the exclude patterns.
This can be done by in-memory string-regex-match and does therefore not require file system accesses.
The new implementation also no longer traverses directories that already match an exclude pattern.
(cherry picked from commit 152355e36f)
61 lines
2.3 KiB
Swift
61 lines
2.3 KiB
Swift
import Foundation
|
|
import SourceKittenFramework
|
|
import SwiftLintCore
|
|
import SwiftLintFramework
|
|
import TestHelpers
|
|
import XCTest
|
|
|
|
private let config: Configuration = {
|
|
let bazelWorkspaceDirectory = ProcessInfo.processInfo.environment["BUILD_WORKSPACE_DIRECTORY"]
|
|
let rootProjectDirectory = bazelWorkspaceDirectory ?? #filePath.bridge()
|
|
.deletingLastPathComponent.bridge()
|
|
.deletingLastPathComponent.bridge()
|
|
.deletingLastPathComponent
|
|
_ = FileManager.default.changeCurrentDirectoryPath(rootProjectDirectory)
|
|
return Configuration(configurationFiles: [Configuration.defaultFileName])
|
|
}()
|
|
|
|
final class IntegrationTests: SwiftLintTestCase {
|
|
func testSwiftLintLints() throws {
|
|
try XCTSkipUnless(
|
|
ProcessInfo.processInfo.environment["SKIP_INTEGRATION_TESTS"] == nil,
|
|
"Will be covered by separate linting job"
|
|
)
|
|
// This is as close as we're ever going to get to a self-hosting linter.
|
|
let swiftFiles = config.lintableFiles(
|
|
inPath: "",
|
|
forceExclude: false,
|
|
excludeByPrefix: false)
|
|
XCTAssert(
|
|
swiftFiles.contains(where: { #filePath.bridge().absolutePathRepresentation() == $0.path }),
|
|
"current file should be included"
|
|
)
|
|
|
|
let storage = RuleStorage()
|
|
let violations = swiftFiles.parallelFlatMap {
|
|
Linter(file: $0, configuration: config).collect(into: storage).styleViolations(using: storage)
|
|
}
|
|
violations.forEach { violation in
|
|
violation.location.file!.withStaticString {
|
|
XCTFail(violation.reason, file: $0, line: UInt(violation.location.line!))
|
|
}
|
|
}
|
|
}
|
|
|
|
func testSwiftLintAutoCorrects() throws {
|
|
try XCTSkipUnless(
|
|
ProcessInfo.processInfo.environment["SKIP_INTEGRATION_TESTS"] == nil,
|
|
"Corrections are not verified in CI"
|
|
)
|
|
let swiftFiles = config.lintableFiles(
|
|
inPath: "",
|
|
forceExclude: false,
|
|
excludeByPrefix: false)
|
|
let storage = RuleStorage()
|
|
let corrections = swiftFiles.parallelMap {
|
|
Linter(file: $0, configuration: config).collect(into: storage).correct(using: storage)
|
|
}
|
|
XCTAssert(corrections.allSatisfy(\.isEmpty), "Unexpected corrections have been applied")
|
|
}
|
|
}
|