Skip to content

Commit 9e8b565

Browse files
committed
SwiftFixIt: Add test for directory exclusion
Also tidy up excluded directory checks in the diagnostic filter and add missing `@Test` to a test function.
1 parent 25fb01c commit 9e8b565

File tree

3 files changed

+114
-19
lines changed

3 files changed

+114
-19
lines changed

Sources/SwiftFixIt/SwiftFixIt.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ private struct PrimaryDiagnosticFilter<Diagnostic: AnyDiagnostic>: ~Copyable {
143143
}
144144

145145
// Skip if no location.
146-
if diagnostic.hasNoLocation {
146+
guard let location = diagnostic.location else {
147147
return true
148148
}
149149

@@ -155,9 +155,12 @@ private struct PrimaryDiagnosticFilter<Diagnostic: AnyDiagnostic>: ~Copyable {
155155
}
156156
}
157157

158-
// Skip if the source file the diagnostic appears in is in an excluded directory.
159-
if let sourceFilePath = try? diagnostic.location.map({ try AbsolutePath(validating: $0.filename) }) {
160-
guard !self.excludedSourceDirectories.contains(where: { $0.isAncestor(of: sourceFilePath) }) else {
158+
// Skip if excluded directories were given and the source file the
159+
// diagnostic appears in is in any of them.
160+
if !self.excludedSourceDirectories.isEmpty {
161+
if let sourceFilePath = try? AbsolutePath(validating: location.filename),
162+
self.excludedSourceDirectories.contains(where: sourceFilePath.isDescendant(of:))
163+
{
161164
return true
162165
}
163166
}

Tests/SwiftFixItTests/FilteringTests.swift

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ struct FilteringTests {
412412
}
413413
}
414414

415+
@Test
415416
func testDuplicateInsertionFixIts() throws {
416417
try testAPI1File { path in
417418
.init(
@@ -526,4 +527,91 @@ struct FilteringTests {
526527
)
527528
}
528529
}
530+
531+
@Test
532+
func testExcludedSourceDirectories() throws {
533+
// Each generated file has a distinct parent directory.
534+
try testAPI2Files { path1, path2 in
535+
.init(
536+
edits: (
537+
.init(input: "var x = 1", result: "var x = 1"),
538+
.init(input: "var x = 1", result: "var y = 2")
539+
),
540+
summary: .init(
541+
// 6 because skipped by SwiftIDEUtils.FixItApplier, not SwiftFixIt.
542+
numberOfFixItsApplied: 2,
543+
numberOfFilesChanged: 1
544+
),
545+
excludedSourceDirectories: [path1.parentDirectory],
546+
diagnostics: [
547+
// path1.
548+
PrimaryDiagnostic(
549+
level: .error,
550+
text: "error1_fixit1",
551+
location: .init(path: path1, line: 1, column: 5),
552+
fixIts: [
553+
// Skipped, excluded directory.
554+
.init(
555+
start: .init(path: path1, line: 1, column: 5),
556+
end: .init(path: path1, line: 1, column: 6),
557+
text: "y"
558+
),
559+
]
560+
),
561+
PrimaryDiagnostic(
562+
level: .error,
563+
text: "error2_fixit2",
564+
location: .init(path: path1, line: 1, column: 9),
565+
notes: [
566+
Note(
567+
text: "error2_note1",
568+
location: .init(path: path1, line: 1, column: 9),
569+
fixIts: [
570+
// Skipped, excluded directory.
571+
.init(
572+
start: .init(path: path1, line: 1, column: 9),
573+
end: .init(path: path1, line: 1, column: 10),
574+
text: "2"
575+
),
576+
]
577+
),
578+
]
579+
),
580+
// path2.
581+
PrimaryDiagnostic(
582+
level: .error,
583+
text: "error1_fixit1",
584+
location: .init(path: path2, line: 1, column: 5),
585+
fixIts: [
586+
// Applied.
587+
.init(
588+
start: .init(path: path2, line: 1, column: 5),
589+
end: .init(path: path2, line: 1, column: 6),
590+
text: "y"
591+
),
592+
]
593+
),
594+
PrimaryDiagnostic(
595+
level: .error,
596+
text: "error2_fixit2",
597+
location: .init(path: path2, line: 1, column: 9),
598+
notes: [
599+
Note(
600+
text: "error2_note1",
601+
location: .init(path: path2, line: 1, column: 9),
602+
fixIts: [
603+
// Applied.
604+
.init(
605+
start: .init(path: path2, line: 1, column: 9),
606+
end: .init(path: path2, line: 1, column: 10),
607+
text: "2"
608+
),
609+
]
610+
),
611+
]
612+
),
613+
]
614+
)
615+
}
616+
}
529617
}

Tests/SwiftFixItTests/Utilities.swift

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,10 @@ struct Summary {
178178
}
179179

180180
struct TestCase<T> {
181-
let edits: T
182-
let summary: Summary
183-
let diagnostics: [PrimaryDiagnostic]
181+
var edits: T
182+
var summary: Summary
183+
var excludedSourceDirectories: Set<AbsolutePath> = []
184+
var diagnostics: [PrimaryDiagnostic]
184185
}
185186

186187
extension Testing.Issue {
@@ -211,6 +212,7 @@ private func _testAPI(
211212
_ expectedSummary: Summary,
212213
_ diagnostics: [PrimaryDiagnostic],
213214
_ categories: Set<String>,
215+
_ excludedSourceDirectories: Set<AbsolutePath>,
214216
) throws {
215217
for (path, edit) in sourceFilePathsAndEdits {
216218
try localFileSystem.writeFileContents(path, string: edit.input)
@@ -233,7 +235,7 @@ private func _testAPI(
233235
let swiftFixIt = try SwiftFixIt(
234236
diagnostics: flatDiagnostics,
235237
categories: categories,
236-
excludedSourceDirectories: [],
238+
excludedSourceDirectories: excludedSourceDirectories,
237239
fileSystem: localFileSystem
238240
)
239241
let actualSummary = try swiftFixIt.applyFixIts()
@@ -261,44 +263,46 @@ private func _testAPI(
261263
}
262264
}
263265

264-
private func uniqueSwiftFileName() -> String {
265-
"\(UUID().uuidString).swift"
266-
}
267-
268266
// Cannot use variadic generics: crashes.
269267
func testAPI1File(
268+
function: StaticString = #function,
270269
categories: Set<String> = [],
271270
_ getTestCase: (AbsolutePath) -> TestCase<SourceFileEdit>
272271
) throws {
273-
try testWithTemporaryDirectory { fixturePath in
274-
let sourceFilePath = fixturePath.appending(uniqueSwiftFileName())
272+
try testWithTemporaryDirectory(function: function) { fixturePath in
273+
let sourceFilePath = fixturePath.appending("file.swift")
275274

276275
let testCase = getTestCase(sourceFilePath)
277276

278277
try _testAPI(
279278
[(sourceFilePath, testCase.edits)],
280279
testCase.summary,
281280
testCase.diagnostics,
282-
categories
281+
categories,
282+
testCase.excludedSourceDirectories,
283283
)
284284
}
285285
}
286286

287287
func testAPI2Files(
288+
function: StaticString = #function,
288289
categories: Set<String> = [],
289290
_ getTestCase: (AbsolutePath, AbsolutePath) -> TestCase<(SourceFileEdit, SourceFileEdit)>,
290291
) throws {
291-
try testWithTemporaryDirectory { fixturePath in
292-
let sourceFilePath1 = fixturePath.appending(uniqueSwiftFileName())
293-
let sourceFilePath2 = fixturePath.appending(uniqueSwiftFileName())
292+
try testWithTemporaryDirectory(function: function) { fixturePath in
293+
// Create each file in a separate subdirectory so that we can test
294+
// directory exclusion.
295+
let sourceFilePath1 = fixturePath.appending(components: [UUID().uuidString, "file.swift"])
296+
let sourceFilePath2 = fixturePath.appending(components: [UUID().uuidString, "file.swift"])
294297

295298
let testCase = getTestCase(sourceFilePath1, sourceFilePath2)
296299

297300
try _testAPI(
298301
[(sourceFilePath1, testCase.edits.0), (sourceFilePath2, testCase.edits.1)],
299302
testCase.summary,
300303
testCase.diagnostics,
301-
categories
304+
categories,
305+
testCase.excludedSourceDirectories,
302306
)
303307
}
304308
}

0 commit comments

Comments
 (0)