Skip to content

Commit ebadf31

Browse files
committed
[Tests] swift fixit: Add test against fixture package
1 parent 323df9d commit ebadf31

File tree

6 files changed

+101
-0
lines changed

6 files changed

+101
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// swift-tools-version:5.9
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "SwiftFixItPackage",
7+
targets: [
8+
.target(name: "Diagnostics", path: "Sources", exclude: ["Fixed"]),
9+
]
10+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func foo(_: any P, _: any P) {}
2+
3+
func throwing() throws -> Int {}
4+
5+
func foo() throws {
6+
do {
7+
_ = 0
8+
}
9+
do {
10+
_ = try throwing()
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
protocol P {
2+
associatedtype A
3+
}
4+
5+
func bar(_: any P) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func foo(_: P, _: P) {}
2+
3+
func throwing() throws -> Int {}
4+
5+
func foo() throws {
6+
do {
7+
var x = 0
8+
}
9+
do {
10+
let x = throwing()
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
protocol P {
2+
associatedtype A
3+
}
4+
5+
func bar(_: P) {}

Tests/CommandsTests/SwiftFixItCommandTests.swift

+57
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import _InternalTestSupport
14+
import struct Basics.AbsolutePath
15+
import var Basics.localFileSystem
1416
@testable
1517
import Commands
18+
import class PackageModel.UserToolchain
1619
import XCTest
1720

1821
final class FixItCommandTests: CommandsTestCase {
@@ -22,4 +25,58 @@ final class FixItCommandTests: CommandsTestCase {
2225
XCTAssert(stdout.contains("USAGE: swift fixit"), stdout)
2326
XCTAssert(stdout.contains("-h, -help, --help"), stdout)
2427
}
28+
29+
func testApplyFixIts() async throws {
30+
try await fixture(name: "SwiftFixIt/SwiftFixItPackage") { fixturePath in
31+
let sourcePaths: [AbsolutePath]
32+
let fixedSourcePaths: [AbsolutePath]
33+
do {
34+
let sourcesPath = fixturePath.appending(components: "Sources")
35+
let fixedSourcesPath = sourcesPath.appending("Fixed")
36+
37+
sourcePaths = try localFileSystem.getDirectoryContents(sourcesPath).filter { filename in
38+
filename.hasSuffix(".swift")
39+
}.sorted().map { filename in
40+
sourcesPath.appending(filename)
41+
}
42+
fixedSourcePaths = try localFileSystem.getDirectoryContents(fixedSourcesPath).filter { filename in
43+
filename.hasSuffix(".swift")
44+
}.sorted().map { filename in
45+
fixedSourcesPath.appending(filename)
46+
}
47+
}
48+
49+
XCTAssertEqual(sourcePaths.count, fixedSourcePaths.count)
50+
51+
let targetName = "Diagnostics"
52+
53+
_ = try? await executeSwiftBuild(fixturePath, extraArgs: ["--target", targetName])
54+
55+
do {
56+
let artifactsPath = try fixturePath.appending(
57+
components: ".build",
58+
UserToolchain.default.targetTriple.platformBuildPathComponent,
59+
"debug",
60+
"\(targetName).build"
61+
)
62+
let diaFilePaths = try localFileSystem.getDirectoryContents(artifactsPath).filter { filename in
63+
// Ignore "*.emit-module.dia".
64+
filename.split(".").1 == "dia"
65+
}.map { filename in
66+
artifactsPath.appending(component: filename).pathString
67+
}
68+
69+
XCTAssertEqual(sourcePaths.count, diaFilePaths.count)
70+
71+
_ = try await SwiftPM.fixit.execute(diaFilePaths)
72+
}
73+
74+
for (sourcePath, fixedSourcePath) in zip(sourcePaths, fixedSourcePaths) {
75+
try XCTAssertEqual(
76+
localFileSystem.readFileContents(sourcePath),
77+
localFileSystem.readFileContents(fixedSourcePath)
78+
)
79+
}
80+
}
81+
}
2582
}

0 commit comments

Comments
 (0)