Skip to content

Commit 797e548

Browse files
committed
Add setting for test discovery label
1 parent 3a117a2 commit 797e548

File tree

5 files changed

+28
-9
lines changed

5 files changed

+28
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4545

4646
- Support official vscode testing API to discover, run, debug and anlyze `cc_test` targets via test explorer UI
4747
- Additional setting to switch on/off testing feature
48+
- Additional setting to reduce the search space for large projects with many build targets
4849

4950
### Fixed
5051

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ If the `activateTesting` setting is set to true (default), the extension tries t
3131

3232
![test_explorer_example](images/test_explorer_example.png)
3333

34-
This might take a while, depending on the size of your C++ project and the number of existing tests. In case of unexpected failures, check the extension output for error logs.
34+
Depending on the size of your C++ project, the test discovery process might take a while. You can reduce the search space by the extension setting `testDiscoverLabel`. In case of unexpected failures, check the extension output for error logs.
3535

3636
## Extension Settings
3737

3838
* `vsc-bazel-tools.customCompileCommandsTarget`: Specifies a custom bazel target (label) to generate the compile commands.
3939
* `vsc-bazel-tools.activateTesting`: Discover and publish tests via test explorer UI.
40+
* `vsc-bazel-tools.testDiscoverLabel`: Bazel label for test discovery (e.g. `//my/package/...`).

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@
5252
],
5353
"default": true,
5454
"description": "Discover tests in test explorer UI."
55+
},
56+
"vsc-bazel-tools.testDiscoverLabel": {
57+
"type": [
58+
"string",
59+
"//..."
60+
],
61+
"default": "//...",
62+
"description": "This label will be used to search for tests. Defaults to workspace search (//...)"
5563
}
5664
}
5765
}

src/extension.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ let extensionOutputChannel: vscode.OutputChannel | undefined;
99
let compileCommandsGenerator: vscode.Disposable | undefined;
1010
let bazelTestCtrl: vscode.TestController | undefined;
1111

12+
const bazelTools = "vsc-bazel-tools";
1213
const activeTestingSettingName = "activateTesting";
14+
const testDiscoverLabelSettingName = "testDiscoverLabel";
1315
const activeTestingSettingDefault = true;
1416
let testingActivated = false;
1517

1618
export async function activate(context: vscode.ExtensionContext) {
17-
extensionOutputChannel = vscode.window.createOutputChannel("vsc-bazel-tools");
19+
extensionOutputChannel = vscode.window.createOutputChannel(bazelTools);
1820
extensionOutputChannel.show();
1921
logger.attachTransport((logObj) => {
2022
extensionOutputChannel.appendLine(logObj['0'].toString());
@@ -31,7 +33,7 @@ export async function activate(context: vscode.ExtensionContext) {
3133
const bazelWorkspaceDir = path.dirname(foundWorkspaceFiles[0]);
3234

3335
logger.info("Retrieving configuration.");
34-
const config = vscode.workspace.getConfiguration("vsc-bazel-tools");
36+
const config = vscode.workspace.getConfiguration(bazelTools);
3537

3638
compileCommandsGenerator = vscode.commands.registerCommand('vsc-bazel-tools.generateCompileCommands', async () => {
3739
vscode.window.withProgress({
@@ -69,7 +71,7 @@ export async function activate(context: vscode.ExtensionContext) {
6971
const toggleTestingFeature = async () => {
7072
if (!testingActivated) {
7173
bazelTestCtrl = vscode.tests.createTestController('bazelTestController', 'Unit tests');
72-
const utils = new Utils(bazelTestCtrl);
74+
const utils = new Utils(bazelTestCtrl, vscode.workspace.getConfiguration(bazelTools).get(testDiscoverLabelSettingName));
7375

7476
bazelTestCtrl.resolveHandler = async test => {
7577
if (!test) {
@@ -116,14 +118,20 @@ export async function activate(context: vscode.ExtensionContext) {
116118
};
117119

118120
// activate/deactivate testing feature initially
119-
if (config.get(activeTestingSettingName, activeTestingSettingDefault)) {
121+
if (vscode.workspace.getConfiguration(bazelTools).get(activeTestingSettingName, activeTestingSettingDefault)) {
120122
await toggleTestingFeature();
121123
}
122124

123125
// activate/deactivate testing feature on config change
124126
vscode.workspace.onDidChangeConfiguration(async e => {
125-
if (e.affectsConfiguration(`vsc-bazel-tools.${activeTestingSettingName}`) &&
126-
config.get(activeTestingSettingName, activeTestingSettingDefault) === true) {
127+
const activeTestingFlagChanged = e.affectsConfiguration(`vsc-bazel-tools.${activeTestingSettingName}`);
128+
const testDiscoverLabelChanged = e.affectsConfiguration(`vsc-bazel-tools.${testDiscoverLabelSettingName}`);
129+
const activeTestingSetting = vscode.workspace.getConfiguration(bazelTools).get(activeTestingSettingName, activeTestingSettingDefault);
130+
if (activeTestingFlagChanged && activeTestingSetting === true || testDiscoverLabelChanged) {
131+
if (testDiscoverLabelChanged) {
132+
bazelTestCtrl?.dispose();
133+
testingActivated = false; // trigger refresh
134+
}
127135
await toggleTestingFeature();
128136
}
129137
});
@@ -132,4 +140,5 @@ export async function activate(context: vscode.ExtensionContext) {
132140
export function deactivate() {
133141
compileCommandsGenerator?.dispose();
134142
bazelTestCtrl?.dispose();
143+
testingActivated = false;
135144
}

src/test_controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export class Utils {
3333

3434
constructor(
3535
private readonly controller: vscode.TestController,
36+
private readonly testDiscoverLabel: string,
3637
) { }
3738

3839
public async discoverAllTestsInWorkspace() {
@@ -51,8 +52,7 @@ export class Utils {
5152
logger.error(`No WORKSPACE file found in ${this.workspaceDirPath}`);
5253
});
5354

54-
const bazelTestTargetsQuery = await runCommand("bazel", ["query", "kind(\"cc_test\", //...)"], this.workspaceDirPath);
55-
// TODO: get search dir/label from UI instead of using global //...?
55+
const bazelTestTargetsQuery = await runCommand("bazel", ["query", `kind(\"cc_test\", ${this.testDiscoverLabel})`], this.workspaceDirPath);
5656

5757
if (bazelTestTargetsQuery.error) {
5858
throw new Error(`bazel query failed:\n ${bazelTestTargetsQuery.error.message}`);

0 commit comments

Comments
 (0)