Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use URL(fileURLWithPath:isDirectory) to avoid file system call #3976

Merged
merged 1 commit into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ extension Configuration {
internal var cacheURL: URL {
let baseURL: URL
if let path = cachePath {
baseURL = URL(fileURLWithPath: path)
baseURL = URL(fileURLWithPath: path, isDirectory: true)
} else {
#if os(Linux)
baseURL = URL(fileURLWithPath: "/var/tmp/")
baseURL = URL(fileURLWithPath: "/var/tmp/", isDirectory: true)
#else
baseURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ extension SwiftLintFile {
queuedFatalError("can't encode '\(string)' with UTF8")
}
do {
try stringData.write(to: URL(fileURLWithPath: path), options: .atomic)
try stringData.write(to: URL(fileURLWithPath: path, isDirectory: false), options: .atomic)
} catch {
queuedFatalError("can't write file to \(path)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ public struct FileTypesOrderRule: ConfigurationProviderRule, OptInRule {
return self.mainTypeSubstructure(in: dict)
}

let fileName = URL(fileURLWithPath: filePath).lastPathComponent.replacingOccurrences(of: ".swift", with: "")
let fileName = URL(fileURLWithPath: filePath, isDirectory: false)
.lastPathComponent.replacingOccurrences(of: ".swift", with: "")
guard let mainTypeSubstructure = dict.substructure.first(where: { $0.name == fileName }) else {
return self.mainTypeSubstructure(in: file.structureDictionary)
}
Expand Down
2 changes: 1 addition & 1 deletion Source/swiftlint/Commands/Docs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension SwiftLint {
private func open(_ url: URL) {
let process = Process()
#if os(Linux)
process.executableURL = URL(fileURLWithPath: "/usr/bin/env")
process.executableURL = URL(fileURLWithPath: "/usr/bin/env", isDirectory: false)
let command = "xdg-open"
process.arguments = [command, url.absoluteString]
try? process.run()
Expand Down
2 changes: 1 addition & 1 deletion Source/swiftlint/Commands/GenerateDocs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extension SwiftLint {

mutating func run() throws {
try RuleListDocumentation(primaryRuleList)
.write(to: URL(fileURLWithPath: path))
.write(to: URL(fileURLWithPath: path, isDirectory: true))
}
}
}
2 changes: 1 addition & 1 deletion Source/swiftlint/Helpers/Benchmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct Benchmark {
return "\(numberFormatter.string(from: NSNumber(value: time))!): \(id)"
}
let string: String = lines.joined(separator: "\n") + "\n"
let url = URL(fileURLWithPath: "benchmark_\(name)_\(timestamp).txt")
let url = URL(fileURLWithPath: "benchmark_\(name)_\(timestamp).txt", isDirectory: false)
try? string.data(using: .utf8)?.write(to: url, options: [.atomic])
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/swiftlint/Helpers/CompilerArgumentsExtractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ extension Array where Element == String {
return [arg]
}
let responseFile = String(arg.dropFirst())
return (try? String(contentsOf: URL(fileURLWithPath: responseFile))).flatMap {
return (try? String(contentsOf: URL(fileURLWithPath: responseFile, isDirectory: false))).flatMap {
$0.trimmingCharacters(in: .newlines)
.components(separatedBy: "\n")
.expandingResponseFiles
Expand Down
3 changes: 2 additions & 1 deletion Tests/SwiftLintFrameworkTests/IntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ private func prepareSandbox() -> (testSwiftURL: URL, seatbeltURL: URL)? {
// ├── AADA6B05-2E06-4E7F-BA48-8B3AF44415E3.sb
do {
// `/private/tmp` is standardized to `/tmp` that is symbolic link to `/private/tmp`.
let temporaryDirectoryURL = URL(fileURLWithPath: "/tmp").appendingPathComponent(UUID().uuidString)
let temporaryDirectoryURL = URL(fileURLWithPath: "/tmp", isDirectory: true)
.appendingPathComponent(UUID().uuidString)
try FileManager.default.createDirectory(at: temporaryDirectoryURL, withIntermediateDirectories: true)

let seatbeltURL = temporaryDirectoryURL.appendingPathExtension("sb")
Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftLintFrameworkTests/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ private let violationMarker = "↓"

private extension SwiftLintFile {
static func temporary(withContents contents: String) -> SwiftLintFile {
let url = URL(fileURLWithPath: NSTemporaryDirectory())
let url = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
.appendingPathComponent(UUID().uuidString)
.appendingPathExtension("swift")
_ = try? contents.data(using: .utf8)!.write(to: url)
Expand All @@ -18,7 +18,7 @@ private extension SwiftLintFile {

func makeCompilerArguments() -> [String] {
let sdk = sdkPath()
let frameworks = URL(fileURLWithPath: sdk)
let frameworks = URL(fileURLWithPath: sdk, isDirectory: true)
.deletingLastPathComponent()
.deletingLastPathComponent()
.appendingPathComponent("Library")
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftLintFrameworkTests/XCTestCase+BundlePath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import XCTest

enum TestResources {
static var path: String {
URL(fileURLWithPath: #file)
URL(fileURLWithPath: #file, isDirectory: false)
.deletingLastPathComponent()
.appendingPathComponent("Resources")
.path
Expand Down