Skip to content

Commit d8b41d4

Browse files
authored
Merge pull request #1685 from ahoppen/only-scan-test-targets
Only scan test targets for tests
2 parents 24f8803 + 120bd86 commit d8b41d4

File tree

6 files changed

+57
-1
lines changed

6 files changed

+57
-1
lines changed

Sources/BuildSystemIntegration/BuildSystemManager.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,12 @@ package actor BuildSystemManager: QueueBasedMessageHandler {
775775
return result
776776
}
777777

778+
package func buildTarget(named identifier: BuildTargetIdentifier) async -> BuildTarget? {
779+
return await orLog("Getting built target with ID") {
780+
try await buildTargets()[identifier]?.target
781+
}
782+
}
783+
778784
package func sourceFiles(in targets: Set<BuildTargetIdentifier>) async throws -> [SourcesItem] {
779785
guard let connectionToBuildSystem = await connectionToBuildSystem else {
780786
return []

Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,10 @@ package actor SwiftPMBuildSystem: BuiltInBuildSystem {
453453

454454
package func buildTargets(request: WorkspaceBuildTargetsRequest) async throws -> WorkspaceBuildTargetsResponse {
455455
var targets = self.swiftPMTargets.map { (targetId, target) in
456-
var tags: [BuildTargetTag] = [.test]
456+
var tags: [BuildTargetTag] = []
457+
if target.isTestTarget {
458+
tags.append(.test)
459+
}
457460
if !target.isPartOfRootPackage {
458461
tags.append(.dependency)
459462
}

Sources/SourceKitLSP/TestDiscovery.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,14 @@ extension SwiftLanguageService {
522522
for uri: DocumentURI,
523523
in workspace: Workspace
524524
) async throws -> [AnnotatedTestItem]? {
525+
let targetIdentifiers = await workspace.buildSystemManager.targets(for: uri)
526+
let isInTestTarget = await targetIdentifiers.asyncContains(where: {
527+
await workspace.buildSystemManager.buildTarget(named: $0)?.tags.contains(.test) ?? true
528+
})
529+
if !targetIdentifiers.isEmpty && !isInTestTarget {
530+
// If we know the targets for the file and the file is not part of any test target, don't scan it for tests.
531+
return nil
532+
}
525533
let snapshot = try documentManager.latestSnapshot(uri)
526534
let semanticSymbols = workspace.index(checkedFor: .deletedFiles)?.symbols(inFilePath: snapshot.uri.pseudoPath)
527535
let xctestSymbols = await SyntacticSwiftXCTestScanner.findTestSymbols(

Sources/SwiftExtensions/Sequence+AsyncMap.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,11 @@ extension Sequence {
8181

8282
return nil
8383
}
84+
85+
/// Just like `Sequence.contains` but allows an `async` predicate function.
86+
package func asyncContains(
87+
@_inheritActorContext where predicate: @Sendable (Element) async throws -> Bool
88+
) async rethrows -> Bool {
89+
return try await asyncFirst(predicate) != nil
90+
}
8491
}

Tests/SourceKitLSPTests/DocumentTestDiscoveryTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,4 +1424,21 @@ final class DocumentTestDiscoveryTests: XCTestCase {
14241424
]
14251425
)
14261426
}
1427+
1428+
func testSwiftTestingTestsAreNotReportedInNonTestTargets() async throws {
1429+
let project = try await SwiftPMTestProject(
1430+
files: [
1431+
"FileA.swift": """
1432+
@Suite struct MyTests {
1433+
@Test func inStruct() {}
1434+
}
1435+
"""
1436+
]
1437+
)
1438+
1439+
let (uri, _) = try project.openDocument("FileA.swift")
1440+
1441+
let tests = try await project.testClient.send(DocumentTestsRequest(textDocument: TextDocumentIdentifier(uri)))
1442+
XCTAssertEqual(tests, [])
1443+
}
14271444
}

Tests/SourceKitLSPTests/WorkspaceTestDiscoveryTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,21 @@ final class WorkspaceTestDiscoveryTests: XCTestCase {
10631063
]
10641064
)
10651065
}
1066+
1067+
func testSwiftTestingTestsAreNotDiscoveredInNonTestTargets() async throws {
1068+
let project = try await SwiftPMTestProject(
1069+
files: [
1070+
"FileA.swift": """
1071+
@Suite struct MyTests {
1072+
@Test func inStruct() {}
1073+
}
1074+
"""
1075+
]
1076+
)
1077+
1078+
let tests = try await project.testClient.send(WorkspaceTestsRequest())
1079+
XCTAssertEqual(tests, [])
1080+
}
10661081
}
10671082

10681083
extension TestItem {

0 commit comments

Comments
 (0)