Skip to content

Commit 3ed80b0

Browse files
committed
correct incorrect handling of file paths
`URL.path` is not a usable rendering of the path. `URL.path` represents the URI path. If the path is meant to be consumable as a file path, the file system representation (aka FSR) must be retrieved. This improves the file path handling and makes additional tests now pass.
1 parent 339d250 commit 3ed80b0

File tree

6 files changed

+29
-21
lines changed

6 files changed

+29
-21
lines changed

Source/SwiftLintCore/Extensions/String+SwiftLint.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public extension String {
7070
///
7171
/// - returns: A new `String`.
7272
func absolutePathStandardized() -> String {
73-
bridge().absolutePathRepresentation().bridge().standardizingPath
73+
URL(fileURLWithPath: bridge().standardizingPath.absolutePathRepresentation()).filepath
7474
}
7575

7676
var isFile: Bool {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Foundation
2+
3+
public extension URL {
4+
var filepath: String {
5+
self.withUnsafeFileSystemRepresentation { String(cString: $0!) }
6+
}
7+
}

Source/SwiftLintFramework/Extensions/FileManager+SwiftLint.swift

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,20 @@ public protocol LintableFileManager {
3131

3232
extension FileManager: LintableFileManager {
3333
public func filesToLint(inPath path: String, rootDirectory: String? = nil) -> [String] {
34-
let absolutePath = path.bridge()
35-
.absolutePathRepresentation(rootDirectory: rootDirectory ?? currentDirectoryPath).bridge()
36-
.standardizingPath
37-
34+
let root =
35+
URL(fileURLWithPath: path.absolutePathRepresentation(rootDirectory: rootDirectory ?? currentDirectoryPath))
3836
// if path is a file, it won't be returned in `enumerator(atPath:)`
39-
if absolutePath.bridge().isSwiftFile(), absolutePath.isFile {
40-
return [absolutePath]
37+
if FileManager.default.isFile(atPath: root.path), root.pathExtension == "swift" {
38+
return [root.standardized.filepath]
4139
}
4240

43-
return subpaths(atPath: absolutePath)?.parallelCompactMap { element -> String? in
44-
guard element.bridge().isSwiftFile() else { return nil }
45-
let absoluteElementPath = absolutePath.bridge().appendingPathComponent(element)
46-
return absoluteElementPath.isFile ? absoluteElementPath : nil
41+
return subpaths(atPath: root.path)?.parallelCompactMap { element -> String? in
42+
let elementURL = URL(fileURLWithPath: element, relativeTo: root)
43+
guard elementURL.pathExtension == "swift" else { return nil }
44+
if FileManager.default.isFile(atPath: elementURL.path) {
45+
return elementURL.standardized.filepath
46+
}
47+
return nil
4748
} ?? []
4849
}
4950

Source/swiftlint-dev/Rules+Template.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,40 +49,40 @@ extension SwiftLintDev.Rules {
4949
.appendingPathComponent("SwiftLintBuiltInRules", isDirectory: true)
5050
.appendingPathComponent("Rules", isDirectory: true)
5151
let ruleLocation = ruleDirectory.appendingPathComponent(kind.rawValue.capitalized, isDirectory: true)
52-
guard FileManager.default.fileExists(atPath: ruleLocation.path) else {
52+
guard FileManager.default.fileExists(atPath: ruleLocation.filepath) else {
5353
throw ValidationError("Command must be run from the root of the SwiftLint repository.")
5454
}
5555
print("Creating template(s) for new rule \"\(ruleName)\" identified by '\(ruleId)' ...")
5656
let rulePath = ruleLocation.appendingPathComponent("\(name)Rule.swift", isDirectory: false)
57-
guard overwrite || !FileManager.default.fileExists(atPath: rulePath.path) else {
57+
guard overwrite || !FileManager.default.fileExists(atPath: rulePath.filepath) else {
5858
throw ValidationError("Rule file already exists at \(rulePath.relativeToCurrentDirectory).")
5959
}
60-
try ruleTemplate.write(toFile: rulePath.path, atomically: true, encoding: .utf8)
60+
try ruleTemplate.write(toFile: rulePath.filepath, atomically: true, encoding: .utf8)
6161
print("Rule file created at \(rulePath.relativeToCurrentDirectory).")
6262
if config {
6363
let configPath = ruleDirectory
6464
.appendingPathComponent("RuleConfigurations", isDirectory: true)
6565
.appendingPathComponent("\(name)Configuration.swift", isDirectory: false)
66-
guard overwrite || !FileManager.default.fileExists(atPath: configPath.path) else {
66+
guard overwrite || !FileManager.default.fileExists(atPath: configPath.filepath) else {
6767
throw ValidationError(
6868
"Configuration file already exists at \(configPath.relativeToCurrentDirectory)."
6969
)
7070
}
71-
try configTemplate.write(toFile: configPath.path, atomically: true, encoding: .utf8)
71+
try configTemplate.write(toFile: configPath.filepath, atomically: true, encoding: .utf8)
7272
print("Configuration file created at \(configPath.relativeToCurrentDirectory).")
7373
}
7474
if test {
7575
let testDirectory = rootDirectory
7676
.appendingPathComponent("Tests", isDirectory: true)
7777
.appendingPathComponent("BuiltInRulesTests", isDirectory: true)
7878
let testPath = testDirectory.appendingPathComponent("\(name)RuleTests.swift", isDirectory: false)
79-
guard FileManager.default.fileExists(atPath: testDirectory.path) else {
79+
guard FileManager.default.fileExists(atPath: testDirectory.filepath) else {
8080
throw ValidationError("Command must be run from the root of the SwiftLint repository.")
8181
}
82-
guard overwrite || !FileManager.default.fileExists(atPath: testPath.path) else {
82+
guard overwrite || !FileManager.default.fileExists(atPath: testPath.filepath) else {
8383
throw ValidationError("Test file already exists at \(testPath.relativeToCurrentDirectory).")
8484
}
85-
try testTemplate.write(toFile: testPath.path, atomically: true, encoding: .utf8)
85+
try testTemplate.write(toFile: testPath.filepath, atomically: true, encoding: .utf8)
8686
print("Test file created at \(testPath.relativeToCurrentDirectory).")
8787
}
8888
if !skipRegistration {

Tests/FileSystemAccessTests/BaselineTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ private var temporaryDirectoryPath: String {
88
let result = URL(
99
fileURLWithPath: NSTemporaryDirectory(),
1010
isDirectory: true
11-
).path
11+
).filepath
1212

1313
#if os(macOS)
1414
return "/private" + result

Tests/TestHelpers/TestHelpers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private extension SwiftLintFile {
3939

4040
public extension String {
4141
func stringByAppendingPathComponent(_ pathComponent: String) -> String {
42-
bridge().appendingPathComponent(pathComponent)
42+
URL(fileURLWithPath: self).appendingPathComponent(pathComponent).filepath
4343
}
4444
}
4545

0 commit comments

Comments
 (0)