1
- import { dirname , basename , join , normalize , relative , extname } from "path" ;
1
+ import { dirname , basename , normalize , relative , extname } from "path" ;
2
2
import { Discovery } from "../common/discovery" ;
3
3
import {
4
4
EventEmitter ,
5
5
Event ,
6
6
Uri ,
7
7
RelativePattern ,
8
8
WorkspaceFolder ,
9
- env ,
10
9
} from "vscode" ;
11
10
import { MultiFileSystemWatcher } from "../common/vscode/multi-file-system-watcher" ;
12
11
import { CodeQLCliServer } from "../codeql-cli/cli" ;
13
12
import { pathExists } from "fs-extra" ;
14
-
15
- /**
16
- * A node in the tree of tests. This will be either a `QLTestDirectory` or a `QLTestFile`.
17
- */
18
- export abstract class QLTestNode {
19
- constructor ( private _path : string , private _name : string ) { }
20
-
21
- public get path ( ) : string {
22
- return this . _path ;
23
- }
24
-
25
- public get name ( ) : string {
26
- return this . _name ;
27
- }
28
-
29
- public abstract get children ( ) : readonly QLTestNode [ ] ;
30
-
31
- public abstract finish ( ) : void ;
32
- }
33
-
34
- /**
35
- * A directory containing one or more QL tests or other test directories.
36
- */
37
- export class QLTestDirectory extends QLTestNode {
38
- constructor (
39
- _path : string ,
40
- _name : string ,
41
- private _children : QLTestNode [ ] = [ ] ,
42
- ) {
43
- super ( _path , _name ) ;
44
- }
45
-
46
- public get children ( ) : readonly QLTestNode [ ] {
47
- return this . _children ;
48
- }
49
-
50
- public addChild ( child : QLTestNode ) : void {
51
- this . _children . push ( child ) ;
52
- }
53
-
54
- public createDirectory ( relativePath : string ) : QLTestDirectory {
55
- const dirName = dirname ( relativePath ) ;
56
- if ( dirName === "." ) {
57
- return this . createChildDirectory ( relativePath ) ;
58
- } else {
59
- const parent = this . createDirectory ( dirName ) ;
60
- return parent . createDirectory ( basename ( relativePath ) ) ;
61
- }
62
- }
63
-
64
- public finish ( ) : void {
65
- // remove empty directories
66
- this . _children . filter (
67
- ( child ) => child instanceof QLTestFile || child . children . length > 0 ,
68
- ) ;
69
- this . _children . sort ( ( a , b ) => a . name . localeCompare ( b . name , env . language ) ) ;
70
- this . _children . forEach ( ( child , i ) => {
71
- child . finish ( ) ;
72
- if (
73
- child . children ?. length === 1 &&
74
- child . children [ 0 ] instanceof QLTestDirectory
75
- ) {
76
- // collapse children
77
- const replacement = new QLTestDirectory (
78
- child . children [ 0 ] . path ,
79
- `${ child . name } / ${ child . children [ 0 ] . name } ` ,
80
- Array . from ( child . children [ 0 ] . children ) ,
81
- ) ;
82
- this . _children [ i ] = replacement ;
83
- }
84
- } ) ;
85
- }
86
-
87
- private createChildDirectory ( name : string ) : QLTestDirectory {
88
- const existingChild = this . _children . find ( ( child ) => child . name === name ) ;
89
- if ( existingChild !== undefined ) {
90
- return existingChild as QLTestDirectory ;
91
- } else {
92
- const newChild = new QLTestDirectory ( join ( this . path , name ) , name ) ;
93
- this . addChild ( newChild ) ;
94
- return newChild ;
95
- }
96
- }
97
- }
98
-
99
- /**
100
- * A single QL test. This will be either a `.ql` file or a `.qlref` file.
101
- */
102
- export class QLTestFile extends QLTestNode {
103
- constructor ( _path : string , _name : string ) {
104
- super ( _path , _name ) ;
105
- }
106
-
107
- public get children ( ) : readonly QLTestNode [ ] {
108
- return [ ] ;
109
- }
110
-
111
- public finish ( ) : void {
112
- /**/
113
- }
114
- }
13
+ import { FileTreeDirectory , FileTreeLeaf } from "../common/file-tree-nodes" ;
115
14
116
15
/**
117
16
* The results of discovering QL tests.
@@ -120,7 +19,7 @@ interface QLTestDiscoveryResults {
120
19
/**
121
20
* A directory that contains one or more QL Tests, or other QLTestDirectories.
122
21
*/
123
- testDirectory : QLTestDirectory | undefined ;
22
+ testDirectory : FileTreeDirectory | undefined ;
124
23
125
24
/**
126
25
* The file system path to a directory to watch. If any ql or qlref file changes in
@@ -137,7 +36,7 @@ export class QLTestDiscovery extends Discovery<QLTestDiscoveryResults> {
137
36
private readonly watcher : MultiFileSystemWatcher = this . push (
138
37
new MultiFileSystemWatcher ( ) ,
139
38
) ;
140
- private _testDirectory : QLTestDirectory | undefined ;
39
+ private _testDirectory : FileTreeDirectory | undefined ;
141
40
142
41
constructor (
143
42
private readonly workspaceFolder : WorkspaceFolder ,
@@ -159,7 +58,7 @@ export class QLTestDiscovery extends Discovery<QLTestDiscoveryResults> {
159
58
* The root directory. There is at least one test in this directory, or
160
59
* in a subdirectory of this.
161
60
*/
162
- public get testDirectory ( ) : QLTestDirectory | undefined {
61
+ public get testDirectory ( ) : FileTreeDirectory | undefined {
163
62
return this . _testDirectory ;
164
63
}
165
64
@@ -194,10 +93,10 @@ export class QLTestDiscovery extends Discovery<QLTestDiscoveryResults> {
194
93
* @returns A `QLTestDirectory` object describing the contents of the directory, or `undefined` if
195
94
* no tests were found.
196
95
*/
197
- private async discoverTests ( ) : Promise < QLTestDirectory > {
96
+ private async discoverTests ( ) : Promise < FileTreeDirectory > {
198
97
const fullPath = this . workspaceFolder . uri . fsPath ;
199
98
const name = this . workspaceFolder . name ;
200
- const rootDirectory = new QLTestDirectory ( fullPath , name ) ;
99
+ const rootDirectory = new FileTreeDirectory ( fullPath , name ) ;
201
100
202
101
// Don't try discovery on workspace folders that don't exist on the filesystem
203
102
if ( await pathExists ( fullPath ) ) {
@@ -208,7 +107,9 @@ export class QLTestDiscovery extends Discovery<QLTestDiscoveryResults> {
208
107
const relativePath = normalize ( relative ( fullPath , testPath ) ) ;
209
108
const dirName = dirname ( relativePath ) ;
210
109
const parentDirectory = rootDirectory . createDirectory ( dirName ) ;
211
- parentDirectory . addChild ( new QLTestFile ( testPath , basename ( testPath ) ) ) ;
110
+ parentDirectory . addChild (
111
+ new FileTreeLeaf ( testPath , basename ( testPath ) ) ,
112
+ ) ;
212
113
}
213
114
214
115
rootDirectory . finish ( ) ;
0 commit comments