Fix issue where .swift-version file was ignored if not also using config file (#2230)

This commit is contained in:
Cal Stephens
2025-09-24 08:24:55 -07:00
parent 23da5808d3
commit 0b5ab901ea
2 changed files with 84 additions and 4 deletions
+9 -4
View File
@@ -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
+75
View File
@@ -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, [])
}
}