3
3
4
4
import * as _ from 'lodash' ;
5
5
import { CancellationToken , Range , TestItem , Uri , workspace , WorkspaceFolder } from 'vscode' ;
6
- import { JavaTestRunnerDelegateCommands } from '../constants' ;
6
+ import { INVOCATION_PREFIX , JavaTestRunnerDelegateCommands } from '../constants' ;
7
7
import { IJavaTestItem , TestLevel } from '../types' ;
8
8
import { executeJavaLanguageServerCommand } from '../utils/commandUtils' ;
9
9
import { testController } from './testController' ;
@@ -26,6 +26,58 @@ export async function loadJavaProjects(): Promise<void> {
26
26
}
27
27
}
28
28
29
+ /**
30
+ * This method is used to synchronize the test items for the given parent node recursively. which means:
31
+ * - If an existing child is not contained in the childrenData parameter, it will be deleted
32
+ * - If a child does not exist, create it, otherwise, update it as well as its metadata.
33
+ */
34
+ export function synchronizeItemsRecursively ( parent : TestItem , childrenData : IJavaTestItem [ ] | undefined ) : void {
35
+ if ( childrenData ) {
36
+ // remove the out-of-date children
37
+ parent . children . forEach ( ( child : TestItem ) => {
38
+ if ( child . id . startsWith ( INVOCATION_PREFIX ) ) {
39
+ // only remove the invocation items before a new test session starts
40
+ return ;
41
+ }
42
+ const existingItem : IJavaTestItem | undefined = childrenData . find ( ( data : IJavaTestItem ) => data . id === child . id ) ;
43
+ if ( ! existingItem ) {
44
+ parent . children . delete ( child . id ) ;
45
+ }
46
+ } ) ;
47
+ // update/create children
48
+ for ( const child of childrenData ) {
49
+ const childItem : TestItem = updateOrCreateTestItem ( parent , child ) ;
50
+ if ( child . testLevel <= TestLevel . Class ) {
51
+ childItem . canResolveChildren = true ;
52
+ }
53
+ synchronizeItemsRecursively ( childItem , child . children ) ;
54
+ }
55
+ }
56
+ }
57
+
58
+ function updateOrCreateTestItem ( parent : TestItem , childData : IJavaTestItem ) : TestItem {
59
+ let childItem : TestItem | undefined = parent . children . get ( childData . id ) ;
60
+ if ( childItem ) {
61
+ updateTestItem ( childItem , childData ) ;
62
+ } else {
63
+ childItem = createTestItem ( childData , parent ) ;
64
+ }
65
+ return childItem ;
66
+ }
67
+
68
+ function updateTestItem ( testItem : TestItem , metaInfo : IJavaTestItem ) : void {
69
+ testItem . range = asRange ( metaInfo . range ) ;
70
+ if ( metaInfo . testLevel !== TestLevel . Invocation ) {
71
+ dataCache . set ( testItem , {
72
+ jdtHandler : metaInfo . jdtHandler ,
73
+ fullName : metaInfo . fullName ,
74
+ projectName : metaInfo . projectName ,
75
+ testLevel : metaInfo . testLevel ,
76
+ testKind : metaInfo . testKind ,
77
+ } ) ;
78
+ }
79
+ }
80
+
29
81
/**
30
82
* Create test item which will be shown in the test explorer
31
83
* @param metaInfo The data from the server side of the test item
@@ -72,3 +124,8 @@ export async function getJavaProjects(workspaceFolder: WorkspaceFolder, token?:
72
124
return await executeJavaLanguageServerCommand < IJavaTestItem [ ] > (
73
125
JavaTestRunnerDelegateCommands . FIND_JAVA_PROJECTS , workspaceFolder . uri . toString ( ) , token ) || [ ] ;
74
126
}
127
+
128
+ export async function findTestPackagesAndTypes ( handlerId : string , token ?: CancellationToken ) : Promise < IJavaTestItem [ ] > {
129
+ return await executeJavaLanguageServerCommand < IJavaTestItem [ ] > (
130
+ JavaTestRunnerDelegateCommands . FIND_TEST_PACKAGES_AND_TYPES , handlerId , token ) || [ ] ;
131
+ }
0 commit comments