@@ -40,60 +40,57 @@ export class Utils {
4040 return file . path . endsWith ( '.cpp' ) || file . path . endsWith ( '.cc' ) ;
4141 }
4242
43- public async discoverAllTestsInWorkspace ( ) {
44- if ( ! vscode . workspace . workspaceFolders ) {
45- return [ ] ; // handle the case of no open folders
46- }
43+ public async discoverAllTestsInWorkspace ( bazelWorkspaceDir : string ) {
44+ this . workspaceDirPath = bazelWorkspaceDir ;
45+
46+ await glob ( "**/WORKSPACE*" , { nodir : true , absolute : true , cwd : this . workspaceDirPath } ) . then ( ( workspaceFiles ) => {
47+ this . workspaceDirPath = path . dirname ( workspaceFiles [ 0 ] ) ;
48+ logger . info ( `WORKSPACE file found in: ${ this . workspaceDirPath } ` ) ;
49+ } ) . catch ( ( ) => {
50+ logger . error ( `No WORKSPACE file found in ${ this . workspaceDirPath } ` ) ;
51+ } ) ;
4752
48- return Promise . all (
49- vscode . workspace . workspaceFolders . map ( async workspaceFolder => {
50- this . workspaceDirPath = workspaceFolder . uri . fsPath ;
53+ const bazelTestTargetsQuery = await runCommand ( "bazel" , [ "query" , `kind(\"cc_test\", ${ this . testDiscoverLabel } )` ] , this . workspaceDirPath ) ;
5154
52- await glob ( "**/WORKSPACE*" , { nodir : true , absolute : true , cwd : this . workspaceDirPath } ) . then ( ( workspaceFiles ) => {
53- this . workspaceDirPath = path . dirname ( workspaceFiles [ 0 ] ) ;
54- logger . info ( `WORKSPACE file found in: ${ this . workspaceDirPath } ` ) ;
55- } ) . catch ( ( ) => {
56- logger . error ( `No WORKSPACE file found in ${ this . workspaceDirPath } ` ) ;
57- } ) ;
55+ if ( bazelTestTargetsQuery . error ) {
56+ throw new Error ( `bazel query failed:\n ${ bazelTestTargetsQuery . error . message } ` ) ;
57+ }
5858
59- const bazelTestTargetsQuery = await runCommand ( "bazel" , [ "query" , `kind(\"cc_test\", ${ this . testDiscoverLabel } )` ] , this . workspaceDirPath ) ;
59+ const bazelTestTargets = bazelTestTargetsQuery . stdout ;
6060
61- if ( bazelTestTargetsQuery . error ) {
62- throw new Error ( `bazel query failed:\n ${ bazelTestTargetsQuery . error . message } ` ) ;
61+ for ( const testTarget of bazelTestTargets ) {
62+ const bazelSrcsQuery = await runCommand ( "bazel" , [ "query" , `labels(srcs, ${ testTarget } )` , "--output=location" ] , this . workspaceDirPath ) ;
63+ if ( bazelSrcsQuery . error ) {
64+ throw new Error ( `bazel query failed:\n ${ bazelSrcsQuery . error . message } ` ) ;
65+ }
66+ for ( const bazelSrc of bazelSrcsQuery . stdout ) {
67+ const srcMatch = bazelSrc . match ( / ^ ( .* ) : 1 : 1 : .* / ) ;
68+ if ( ! srcMatch ) { // not a src file
69+ continue ;
6370 }
64-
65- const bazelTestTargets = bazelTestTargetsQuery . stdout ;
66-
67- for ( const testTarget of bazelTestTargets ) {
68- const bazelSrcsQuery = await runCommand ( "bazel" , [ "query" , `labels(srcs, ${ testTarget } )` , "--output=location" ] , this . workspaceDirPath ) ;
69- if ( bazelSrcsQuery . error ) {
70- throw new Error ( `bazel query failed:\n ${ bazelSrcsQuery . error . message } ` ) ;
71- }
72- for ( const bazelSrc of bazelSrcsQuery . stdout ) {
73- const srcFileUri = vscode . Uri . file ( bazelSrc . match ( / ^ ( .* ) : 1 : 1 : .* / ) [ 1 ] ) ;
74- if ( ! Utils . isCppTestFile ( srcFileUri ) ) {
75- continue ;
76- }
77- const testItem = this . getOrCreateFile ( srcFileUri ) ;
78- bazelTestLabels . set ( testItem , testTarget ) ;
79- await this . updateFromDisk ( testItem ) ;
80- }
71+ const srcFileUri = vscode . Uri . file ( srcMatch [ 1 ] ) ;
72+ if ( ! Utils . isCppTestFile ( srcFileUri ) ) {
73+ continue ;
8174 }
75+ const testItem = this . getOrCreateFile ( srcFileUri ) ;
76+ bazelTestLabels . set ( testItem , testTarget ) ;
77+ await this . updateFromDisk ( testItem ) ;
78+ }
79+ }
80+
81+ // const pattern = new vscode.RelativePattern(workspaceFolder, '**/*.cpp');
82+ // const watcher = vscode.workspace.createFileSystemWatcher(pattern);
8283
83- // const pattern = new vscode.RelativePattern(workspaceFolder, '**/*.cpp');
84- // const watcher = vscode.workspace.createFileSystemWatcher(pattern);
84+ // // When files are created, make sure there's a corresponding "file" node in the tree
85+ // watcher.onDidCreate(uri => this.getOrCreateFile(uri));
86+ // watcher.onDidChange(uri => this.parseTestsInFileContents(this.getOrCreateFile(uri)));
87+ // watcher.onDidDelete(uri => this.controller.items.delete(uri.toString()));
8588
86- // // When files are created, make sure there's a corresponding " file" node in the tree
87- // watcher.onDidCreate(uri => this.getOrCreateFile(uri) );
88- // watcher.onDidChange(uri => this.parseTestsInFileContents(this.getOrCreateFile(uri)));
89- // watcher.onDidDelete(uri => this.controller.items.delete(uri.toString())) ;
89+ // for (const file of await vscode.workspace.findFiles(pattern)) {
90+ // this.getOrCreateFile(file );
91+ // }
92+ // return watcher ;
9093
91- // for (const file of await vscode.workspace.findFiles(pattern)) {
92- // this.getOrCreateFile(file);
93- // }
94- // return watcher;
95- } )
96- ) ;
9794 }
9895
9996 getOrCreateFile ( uri : vscode . Uri ) {
0 commit comments