Skip to content

Commit f18f0ef

Browse files
Dotify71ntrogh
andauthored
docs: add section on testing Workspace Trust behavior in extensions (#9784)
* docs: add section on testing Workspace Trust behavior in extensions Adds a new section to the Testing Extensions guide explaining how extension authors can write integration tests that cover both trusted and untrusted workspace scenarios. Covers: - How Workspace Trust state works in the Extension Development Host - Configuring separate test runs per trust state via @vscode/test-cli - Using an isolated --user-data-dir to prevent trust from persisting - Writing assertions using vscode.workspace.isTrusted - Using onDidGrantWorkspaceTrust event in tests - Configuring launch.json for trusted and untrusted test runs Closes #9583 * Edit pass to make the section more concise --------- Co-authored-by: Nick Trogh <ntrogh@hotmail.com>
1 parent b938459 commit f18f0ef

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

api/working-with-extensions/testing-extension.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,67 @@ async function main() {
363363
main();
364364
```
365365

366+
## Testing Workspace Trust behavior
367+
368+
If your extension declares `capabilities.untrustedWorkspaces` in `package.json`, add integration tests for both trusted and untrusted workspaces. You can't programmatically grant or revoke Workspace Trust from an extension test. Use separate test runs for trusted and untrusted states.
369+
370+
When using `@vscode/test-cli`, define separate test configurations so you can run each trust state independently:
371+
372+
- **trustedWorkspaceTests**: Gives you a baseline run where trust restrictions are not applied. This helps verify your extension's full-feature behavior and catch regressions in the trusted path.
373+
- **untrustedWorkspaceTests**: Verifies Restricted Mode behavior with Workspace Trust still enabled. Using a dedicated `--user-data-dir` prevents previously persisted trust decisions from making this run accidentally trusted.
374+
375+
Because each configuration has its own `label`, you can run them independently (for example, `vscode-test --label trustedWorkspaceTests` and `vscode-test --label untrustedWorkspaceTests`) or run both in sequence.
376+
377+
```js
378+
// .vscode-test.js
379+
const { defineConfig } = require('@vscode/test-cli');
380+
const path = require('path');
381+
382+
module.exports = defineConfig([
383+
{
384+
label: 'trustedWorkspaceTests',
385+
files: 'out/test/**/*.test.js',
386+
workspaceFolder: './test/fixtures/trusted-workspace',
387+
// Optional: disables Workspace Trust for this run
388+
launchArgs: ['--disable-workspace-trust'],
389+
},
390+
{
391+
label: 'untrustedWorkspaceTests',
392+
files: 'out/test/**/*.test.js',
393+
workspaceFolder: './test/fixtures/untrusted-workspace',
394+
// Keep Workspace Trust enabled and isolate user data for deterministic runs
395+
launchArgs: [
396+
'--user-data-dir',
397+
path.join(__dirname, '.vscode-test', 'user-data-untrusted'),
398+
],
399+
},
400+
]);
401+
```
402+
403+
In your tests, assert trust-aware behavior by checking `vscode.workspace.isTrusted`:
404+
405+
```ts
406+
import * as assert from 'assert';
407+
import * as vscode from 'vscode';
408+
409+
suite('Workspace Trust Tests', () => {
410+
test('extension behavior changes by trust state', async () => {
411+
const isFeatureAvailable = await vscode.commands.executeCommand<boolean>(
412+
'myExtension.isRestrictedFeatureEnabled'
413+
);
414+
415+
if (vscode.workspace.isTrusted) {
416+
assert.strictEqual(isFeatureAvailable, true);
417+
} else {
418+
assert.strictEqual(isFeatureAvailable, false);
419+
}
420+
});
421+
});
422+
```
423+
424+
For more information on how to declare trust requirements in your extension manifest and how to use the `vscode.workspace.isTrusted` API, see the [Workspace Trust Extension Guide](/api/extension-guides/workspace-trust).
425+
366426
## Next steps
367427

368428
- [Continuous Integration](/api/working-with-extensions/continuous-integration) - Run your extension tests in a Continuous Integration service such as Azure DevOps.
429+
- [Workspace Trust Extension Guide](/api/extension-guides/workspace-trust) - Learn how to declare and handle workspace trust in your extension.

0 commit comments

Comments
 (0)