Skip to content

Commit 7f1e90f

Browse files
committed
Skip formatting tests if the swift-format executable in the host toolchain doesn’t support - to indicate that it’s reading the source file from stdin
1 parent f9fc58d commit 7f1e90f

File tree

4 files changed

+62
-6
lines changed

4 files changed

+62
-6
lines changed

Sources/SKTestSupport/SkipUnless.swift

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,7 @@ package actor SkipUnless {
218218
return try await withTestScratchDir { scratchDirectory in
219219
let input = scratchDirectory.appendingPathComponent("Input.swift")
220220
guard FileManager.default.createFile(atPath: input.path, contents: nil) else {
221-
struct FailedToCrateInputFileError: Error {}
222-
throw FailedToCrateInputFileError()
221+
throw GenericError("Failed to create input file")
223222
}
224223
// If we can't compile for wasm, this fails complaining that it can't find the stdlib for wasm.
225224
let process = Process(
@@ -249,10 +248,7 @@ package actor SkipUnless {
249248
) async throws {
250249
return try await shared.skipUnlessSupportedByToolchain(swiftVersion: SwiftVersion(6, 2), file: file, line: line) {
251250
guard let sourcekitdPath = await ToolchainRegistry.forTesting.default?.sourcekitd else {
252-
struct NoSourceKitdFound: Error, CustomStringConvertible {
253-
var description: String = "Could not find SourceKitD"
254-
}
255-
throw NoSourceKitdFound()
251+
throw GenericError("Could not find SourceKitD")
256252
}
257253
let sourcekitd = try await DynamicallyLoadedSourceKitD.getOrCreate(
258254
dylibPath: sourcekitdPath,
@@ -354,6 +350,30 @@ package actor SkipUnless {
354350
return .featureSupported
355351
}
356352
}
353+
354+
/// Checks that swift-format supports running as `swift-format format -` to indicate that the source file should be
355+
/// read from stdin, ie. that swift-format contains https://github.com/swiftlang/swift-format/pull/914.
356+
package static func swiftFormatSupportsDashToIndicateReadingFromStdin(
357+
file: StaticString = #filePath,
358+
line: UInt = #line
359+
) async throws {
360+
return try await shared.skipUnlessSupportedByToolchain(swiftVersion: SwiftVersion(6, 1), file: file, line: line) {
361+
guard let swiftFormatPath = await ToolchainRegistry.forTesting.default?.swiftFormat else {
362+
throw GenericError("Could not find swift-format")
363+
}
364+
let process = TSCBasic.Process(arguments: [try swiftFormatPath.filePath, "format", "-"])
365+
let writeStream = try process.launch()
366+
writeStream.send("let x = 1")
367+
try writeStream.close()
368+
let result = try await process.waitUntilExitStoppingProcessOnTaskCancellation()
369+
let output = try result.utf8Output()
370+
switch output {
371+
case "": return false
372+
case "let x = 1": return true
373+
default: throw GenericError("Received unexpected formatting output: \(output)")
374+
}
375+
}
376+
}
357377
}
358378

359379
// MARK: - Parsing Swift compiler version
@@ -369,3 +389,11 @@ fileprivate extension String {
369389
}
370390
}
371391
}
392+
393+
private struct GenericError: Error, CustomStringConvertible {
394+
var description: String
395+
396+
init(_ message: String) {
397+
self.description = message
398+
}
399+
}

Tests/SourceKitLSPTests/FormattingTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import XCTest
1818

1919
final class FormattingTests: XCTestCase {
2020
func testFormatting() async throws {
21+
try await SkipUnless.swiftFormatSupportsDashToIndicateReadingFromStdin()
22+
2123
let testClient = try await TestSourceKitLSPClient()
2224
let uri = DocumentURI(for: .swift)
2325

@@ -51,6 +53,8 @@ final class FormattingTests: XCTestCase {
5153
}
5254

5355
func testFormattingNoEdits() async throws {
56+
try await SkipUnless.swiftFormatSupportsDashToIndicateReadingFromStdin()
57+
5458
let testClient = try await TestSourceKitLSPClient()
5559
let uri = DocumentURI(for: .swift)
5660

@@ -76,6 +80,8 @@ final class FormattingTests: XCTestCase {
7680
}
7781

7882
func testConfigFileOnDisk() async throws {
83+
try await SkipUnless.swiftFormatSupportsDashToIndicateReadingFromStdin()
84+
7985
// We pick up an invalid swift-format configuration file and thus don't set the user-provided options.
8086
let project = try await MultiFileTestProject(files: [
8187
".swift-format": """
@@ -110,6 +116,8 @@ final class FormattingTests: XCTestCase {
110116
}
111117

112118
func testConfigFileInParentDirectory() async throws {
119+
try await SkipUnless.swiftFormatSupportsDashToIndicateReadingFromStdin()
120+
113121
// We pick up an invalid swift-format configuration file and thus don't set the user-provided options.
114122
let project = try await MultiFileTestProject(files: [
115123
".swift-format": """
@@ -144,6 +152,8 @@ final class FormattingTests: XCTestCase {
144152
}
145153

146154
func testConfigFileInNestedDirectory() async throws {
155+
try await SkipUnless.swiftFormatSupportsDashToIndicateReadingFromStdin()
156+
147157
// We pick up an invalid swift-format configuration file and thus don't set the user-provided options.
148158
let project = try await MultiFileTestProject(files: [
149159
".swift-format": """
@@ -186,6 +196,8 @@ final class FormattingTests: XCTestCase {
186196
}
187197

188198
func testInvalidConfigurationFile() async throws {
199+
try await SkipUnless.swiftFormatSupportsDashToIndicateReadingFromStdin()
200+
189201
// We pick up an invalid swift-format configuration file and thus don't set the user-provided options.
190202
// The swift-format default is 2 spaces.
191203
let project = try await MultiFileTestProject(files: [
@@ -210,6 +222,8 @@ final class FormattingTests: XCTestCase {
210222
}
211223

212224
func testInsertAndRemove() async throws {
225+
try await SkipUnless.swiftFormatSupportsDashToIndicateReadingFromStdin()
226+
213227
let testClient = try await TestSourceKitLSPClient()
214228
let uri = DocumentURI(for: .swift)
215229

@@ -241,6 +255,8 @@ final class FormattingTests: XCTestCase {
241255
}
242256

243257
func testMultiLineStringInsertion() async throws {
258+
try await SkipUnless.swiftFormatSupportsDashToIndicateReadingFromStdin()
259+
244260
let testClient = try await TestSourceKitLSPClient()
245261
let uri = DocumentURI(for: .swift)
246262

Tests/SourceKitLSPTests/OnTypeFormattingTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import XCTest
1818

1919
final class OnTypeFormattingTests: XCTestCase {
2020
func testOnlyFormatsSpecifiedLine() async throws {
21+
try await SkipUnless.swiftFormatSupportsDashToIndicateReadingFromStdin()
22+
2123
let testClient = try await TestSourceKitLSPClient()
2224
let uri = DocumentURI(for: .swift)
2325

@@ -51,6 +53,8 @@ final class OnTypeFormattingTests: XCTestCase {
5153
}
5254

5355
func testFormatsFullLineAndDoesNotFormatNextLine() async throws {
56+
try await SkipUnless.swiftFormatSupportsDashToIndicateReadingFromStdin()
57+
5458
let testClient = try await TestSourceKitLSPClient()
5559
let uri = DocumentURI(for: .swift)
5660

@@ -88,6 +92,8 @@ final class OnTypeFormattingTests: XCTestCase {
8892
/// Otherwise could mess up writing code. You'd write {} and try to go into the braces to write more code,
8993
/// only for on-type formatting to immediately close the braces again.
9094
func testDoesNothingWhenInAnEmptyLine() async throws {
95+
try await SkipUnless.swiftFormatSupportsDashToIndicateReadingFromStdin()
96+
9197
let testClient = try await TestSourceKitLSPClient()
9298
let uri = DocumentURI(for: .swift)
9399

Tests/SourceKitLSPTests/RangeFormattingTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import XCTest
1818

1919
final class RangeFormattingTests: XCTestCase {
2020
func testOnlyFormatsSpecifiedLines() async throws {
21+
try await SkipUnless.swiftFormatSupportsDashToIndicateReadingFromStdin()
22+
2123
let testClient = try await TestSourceKitLSPClient()
2224
let uri = DocumentURI(for: .swift)
2325

@@ -50,6 +52,8 @@ final class RangeFormattingTests: XCTestCase {
5052
}
5153

5254
func testOnlyFormatsSpecifiedColumns() async throws {
55+
try await SkipUnless.swiftFormatSupportsDashToIndicateReadingFromStdin()
56+
5357
let testClient = try await TestSourceKitLSPClient()
5458
let uri = DocumentURI(for: .swift)
5559

@@ -82,6 +86,8 @@ final class RangeFormattingTests: XCTestCase {
8286
}
8387

8488
func testFormatsMultipleLines() async throws {
89+
try await SkipUnless.swiftFormatSupportsDashToIndicateReadingFromStdin()
90+
8591
let testClient = try await TestSourceKitLSPClient()
8692
let uri = DocumentURI(for: .swift)
8793

0 commit comments

Comments
 (0)