1- import { dirname , basename , join , normalize , relative , extname } from "path" ;
1+ import { dirname , basename , normalize , relative , extname } from "path" ;
22import { Discovery } from "../common/discovery" ;
33import {
44 EventEmitter ,
55 Event ,
66 Uri ,
77 RelativePattern ,
88 WorkspaceFolder ,
9- env ,
109} from "vscode" ;
1110import { MultiFileSystemWatcher } from "../common/vscode/multi-file-system-watcher" ;
1211import { CodeQLCliServer } from "../codeql-cli/cli" ;
1312import { 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" ;
11514
11615/**
11716 * The results of discovering QL tests.
@@ -120,7 +19,7 @@ interface QLTestDiscoveryResults {
12019 /**
12120 * A directory that contains one or more QL Tests, or other QLTestDirectories.
12221 */
123- testDirectory : QLTestDirectory | undefined ;
22+ testDirectory : FileTreeDirectory | undefined ;
12423
12524 /**
12625 * 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> {
13736 private readonly watcher : MultiFileSystemWatcher = this . push (
13837 new MultiFileSystemWatcher ( ) ,
13938 ) ;
140- private _testDirectory : QLTestDirectory | undefined ;
39+ private _testDirectory : FileTreeDirectory | undefined ;
14140
14241 constructor (
14342 private readonly workspaceFolder : WorkspaceFolder ,
@@ -159,7 +58,7 @@ export class QLTestDiscovery extends Discovery<QLTestDiscoveryResults> {
15958 * The root directory. There is at least one test in this directory, or
16059 * in a subdirectory of this.
16160 */
162- public get testDirectory ( ) : QLTestDirectory | undefined {
61+ public get testDirectory ( ) : FileTreeDirectory | undefined {
16362 return this . _testDirectory ;
16463 }
16564
@@ -194,10 +93,10 @@ export class QLTestDiscovery extends Discovery<QLTestDiscoveryResults> {
19493 * @returns A `QLTestDirectory` object describing the contents of the directory, or `undefined` if
19594 * no tests were found.
19695 */
197- private async discoverTests ( ) : Promise < QLTestDirectory > {
96+ private async discoverTests ( ) : Promise < FileTreeDirectory > {
19897 const fullPath = this . workspaceFolder . uri . fsPath ;
19998 const name = this . workspaceFolder . name ;
200- const rootDirectory = new QLTestDirectory ( fullPath , name ) ;
99+ const rootDirectory = new FileTreeDirectory ( fullPath , name ) ;
201100
202101 // Don't try discovery on workspace folders that don't exist on the filesystem
203102 if ( await pathExists ( fullPath ) ) {
@@ -208,7 +107,9 @@ export class QLTestDiscovery extends Discovery<QLTestDiscoveryResults> {
208107 const relativePath = normalize ( relative ( fullPath , testPath ) ) ;
209108 const dirName = dirname ( relativePath ) ;
210109 const parentDirectory = rootDirectory . createDirectory ( dirName ) ;
211- parentDirectory . addChild ( new QLTestFile ( testPath , basename ( testPath ) ) ) ;
110+ parentDirectory . addChild (
111+ new FileTreeLeaf ( testPath , basename ( testPath ) ) ,
112+ ) ;
212113 }
213114
214115 rootDirectory . finish ( ) ;
0 commit comments