File tree Expand file tree Collapse file tree 6 files changed +57
-1
lines changed Expand file tree Collapse file tree 6 files changed +57
-1
lines changed Original file line number Diff line number Diff line change @@ -775,6 +775,12 @@ package actor BuildSystemManager: QueueBasedMessageHandler {
775
775
return result
776
776
}
777
777
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
+
778
784
package func sourceFiles( in targets: Set < BuildTargetIdentifier > ) async throws -> [ SourcesItem ] {
779
785
guard let connectionToBuildSystem = await connectionToBuildSystem else {
780
786
return [ ]
Original file line number Diff line number Diff line change @@ -453,7 +453,10 @@ package actor SwiftPMBuildSystem: BuiltInBuildSystem {
453
453
454
454
package func buildTargets( request: WorkspaceBuildTargetsRequest ) async throws -> WorkspaceBuildTargetsResponse {
455
455
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
+ }
457
460
if !target. isPartOfRootPackage {
458
461
tags. append ( . dependency)
459
462
}
Original file line number Diff line number Diff line change @@ -522,6 +522,14 @@ extension SwiftLanguageService {
522
522
for uri: DocumentURI ,
523
523
in workspace: Workspace
524
524
) 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
+ }
525
533
let snapshot = try documentManager. latestSnapshot ( uri)
526
534
let semanticSymbols = workspace. index ( checkedFor: . deletedFiles) ? . symbols ( inFilePath: snapshot. uri. pseudoPath)
527
535
let xctestSymbols = await SyntacticSwiftXCTestScanner . findTestSymbols (
Original file line number Diff line number Diff line change @@ -81,4 +81,11 @@ extension Sequence {
81
81
82
82
return nil
83
83
}
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
+ }
84
91
}
Original file line number Diff line number Diff line change @@ -1424,4 +1424,21 @@ final class DocumentTestDiscoveryTests: XCTestCase {
1424
1424
]
1425
1425
)
1426
1426
}
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
+ }
1427
1444
}
Original file line number Diff line number Diff line change @@ -1063,6 +1063,21 @@ final class WorkspaceTestDiscoveryTests: XCTestCase {
1063
1063
]
1064
1064
)
1065
1065
}
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
+ }
1066
1081
}
1067
1082
1068
1083
extension TestItem {
You can’t perform that action at this time.
0 commit comments