@@ -273,6 +273,7 @@ extension SourceKitLSPServer {
273
273
. flatMap { $0 }
274
274
. sorted { $0. testItem. location < $1. testItem. location }
275
275
. mergingTestsInExtensions ( )
276
+ . deduplicatingIds ( )
276
277
}
277
278
278
279
func documentTests(
@@ -283,6 +284,7 @@ extension SourceKitLSPServer {
283
284
return try await documentTestsWithoutMergingExtensions ( req, workspace: workspace, languageService: languageService)
284
285
. prefixTestsWithModuleName ( workspace: workspace)
285
286
. mergingTestsInExtensions ( )
287
+ . deduplicatingIds ( )
286
288
}
287
289
288
290
private func documentTestsWithoutMergingExtensions(
@@ -441,16 +443,20 @@ fileprivate extension Array<AnnotatedTestItem> {
441
443
// as the root item.
442
444
if rootItem. isExtension && !item. isExtension {
443
445
var newItem = item
444
- newItem. testItem. children = ( newItem . testItem . children + rootItem. testItem. children) . deduplicatingIds ( )
446
+ newItem. testItem. children += rootItem. testItem. children
445
447
rootItem = newItem
446
- } else if rootItem. testItem. children. isEmpty && item. testItem. children. isEmpty {
447
- itemDict [ item. testItem. ambiguousTestDifferentiator] = item
448
- continue
449
448
} else {
450
- rootItem. testItem. children = ( rootItem . testItem . children + item. testItem. children) . deduplicatingIds ( )
449
+ rootItem. testItem. children += item. testItem. children
451
450
}
452
451
453
- itemDict [ id] = rootItem
452
+ // If this item shares an ID with a sibling and both are leaf
453
+ // test items, store it by its disambiguated id to ensure we
454
+ // don't overwrite the existing element.
455
+ if rootItem. testItem. children. isEmpty && item. testItem. children. isEmpty {
456
+ itemDict [ item. testItem. ambiguousTestDifferentiator] = item
457
+ } else {
458
+ itemDict [ id] = rootItem
459
+ }
454
460
} else {
455
461
itemDict [ id] = item
456
462
}
@@ -487,7 +493,7 @@ fileprivate extension Array<AnnotatedTestItem> {
487
493
. mergingTestsInExtensions ( )
488
494
return newItem
489
495
}
490
- return result. deduplicatingIds ( )
496
+ return result
491
497
}
492
498
493
499
func prefixTestsWithModuleName( workspace: Workspace ) async -> Self {
@@ -520,32 +526,18 @@ fileprivate extension Array<TestItem> {
520
526
/// by appending `/filename:filename:lineNumber`.
521
527
func deduplicatingIds( ) -> [ TestItem ] {
522
528
var idCounts : [ String : Int ] = [ : ]
523
- var result : [ TestItem ] = [ ]
524
- var hasDuplicates = false
525
- result. reserveCapacity ( self . count)
526
-
527
529
for element in self where element. children. isEmpty {
528
530
idCounts [ element. id, default: 0 ] += 1
529
- if idCounts [ element. id, default: 0 ] > 1 {
530
- hasDuplicates = true
531
- }
532
531
}
533
532
534
- if !hasDuplicates {
535
- return self
536
- }
537
-
538
- for element in self {
539
- if idCounts [ element. id, default: 0 ] > 1 {
540
- var newItem = element
533
+ return self . map {
534
+ var newItem = $0
535
+ newItem. children = newItem. children. deduplicatingIds ( )
536
+ if idCounts [ newItem. id, default: 0 ] > 1 {
541
537
newItem. id = newItem. ambiguousTestDifferentiator
542
- result. append ( newItem)
543
- } else {
544
- result. append ( element)
545
538
}
539
+ return newItem
546
540
}
547
-
548
- return result
549
541
}
550
542
}
551
543
0 commit comments