mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-05-17 10:30:35 +00:00
Fix issue where .swift-version file was ignored if not also using config file (#2230)
This commit is contained in:
@@ -353,10 +353,15 @@ private func processDirectory(_ inputURL: URL, with options: inout Options, logg
|
||||
logger?("Ignoring swift-version file at \(versionFile.path)")
|
||||
} else if Version(rawValue: versionString) != nil {
|
||||
logger?("Reading swift-version file at \(versionFile.path) (version \(versionString))")
|
||||
args = args.map {
|
||||
var args = $0
|
||||
args["swift-version"] = versionString
|
||||
return args
|
||||
|
||||
if args.isEmpty {
|
||||
args = [["swift-version": versionString]]
|
||||
} else {
|
||||
args = args.map {
|
||||
var args = $0
|
||||
args["swift-version"] = versionString
|
||||
return args
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Don't treat as error, per: https://github.com/nicklockwood/SwiftFormat/issues/639
|
||||
|
||||
@@ -1423,4 +1423,79 @@ class CommandLineTests: XCTestCase {
|
||||
try FileManager.default.removeItem(at: tempFile)
|
||||
}
|
||||
}
|
||||
|
||||
func testSwiftVersionFileWithNoConfigFile() throws {
|
||||
var errors = [String]()
|
||||
|
||||
CLI.print = { message, type in
|
||||
print(message)
|
||||
if type == .error {
|
||||
errors.append(message)
|
||||
}
|
||||
}
|
||||
|
||||
try withTmpFiles([
|
||||
".swift-version": """
|
||||
6.1
|
||||
""",
|
||||
"Test.swift": """
|
||||
func foo(
|
||||
foo: Foo,
|
||||
bar: Bar
|
||||
) {}
|
||||
""",
|
||||
]) { url in
|
||||
guard url.pathExtension == "swift" else { return }
|
||||
_ = processArguments(["", url.path, "--rules", "trailingCommas"], in: "")
|
||||
|
||||
XCTAssertEqual(try String(contentsOf: url, encoding: .utf8), """
|
||||
func foo(
|
||||
foo: Foo,
|
||||
bar: Bar,
|
||||
) {}
|
||||
""")
|
||||
}
|
||||
|
||||
XCTAssertEqual(errors, [])
|
||||
}
|
||||
|
||||
func testSwiftVersionFileWithConfigFile() throws {
|
||||
var errors = [String]()
|
||||
|
||||
CLI.print = { message, type in
|
||||
print(message)
|
||||
if type == .error {
|
||||
errors.append(message)
|
||||
}
|
||||
}
|
||||
|
||||
try withTmpFiles([
|
||||
".swift-version": """
|
||||
6.1
|
||||
""",
|
||||
".swiftformat": """
|
||||
--rules trailingCommas
|
||||
--rules indent
|
||||
--indent 2
|
||||
""",
|
||||
"Test.swift": """
|
||||
func foo(
|
||||
foo: Foo,
|
||||
bar: Bar
|
||||
) {}
|
||||
""",
|
||||
]) { url in
|
||||
guard url.pathExtension == "swift" else { return }
|
||||
_ = processArguments(["", url.path], in: "")
|
||||
|
||||
XCTAssertEqual(try String(contentsOf: url, encoding: .utf8), """
|
||||
func foo(
|
||||
foo: Foo,
|
||||
bar: Bar,
|
||||
) {}
|
||||
""")
|
||||
}
|
||||
|
||||
XCTAssertEqual(errors, [])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user