Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change workspace or global vscode configuration from a extension test?

My Visual Studio Code extension relies on a workspace configuration. So, in extension test, I would like to set the workspace configuration, and then test if the extension works properly.

Extension contribution definition looks like this:

"contributes": {
        "configuration": {
            "properties": {
                "assistant": {
                    "type": "object",
                    "properties": {
                        "modifiers": {
                            "type": "string",
                            "default": "g"
                        },
                        "rules": {
                            "type": "array",
                            "default": [],
                            "items": {
                                "properties": {
                                    "regex": {
                                        "type": "string"
                                    },
                                    "message": {
                                        "type": "string"
                                    },
                                    "modifiers": {
                                        "type": "string",
                                        "default": "g"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    },

An example configuration looks like this:

{
    "folders": [
        {
            "path": "code"
        }
    ],
    "settings": {
        "assistant": {
            "rules": [
                {
                    "regex": "^::ng-deep.*$",
                    "message": "Prepend with :host, so that styles won't leak",
                    "modifiers": "gm"
                },...

When I call:

Among other approaches i tried:

const assistant = vscode.workspace.getConfiguration("settings");
        assistant.update("assistant", "", ConfigurationTarget.Global).then(
            (value) => {
                console.log(assistant.get("assistant"));
            },
            (value) => {
                console.log(value);
            }
        );

It returns the error:

Error: Unable to write to User Settings because settings.assistant is not a registered configuration.

I have tried also with ConfigurationTarget. Workspace, and various ways to access settings but nothing seem to work properly.

The extension seems to be installed properly in the test vscode application.

How to change vscode extension properties programmatically?

like image 975
Tom Smykowski Avatar asked Nov 15 '22 08:11

Tom Smykowski


1 Answers

You need to treat your test environment as if it were any other vscode project. How do you keep your settings per vscode project? By keeping them in .vscode/settings.json. So the solution is to create workspace for your test vscode, with your settings as mentioned.

In the vscode-test docs you can see how to open a specific workspace when running the tests.

// test/runTest.ts
async function go() {
    try {
        const extensionDevelopmentPath = path.resolve(__dirname, '../../../')
        const extensionTestsPath = path.resolve(__dirname, './suite')
        const testWorkspace = path.resolve(__dirname, '../../../test-fixtures/')

        /**
         * Running another test suite on a specific workspace
         */
        await runTests({
            extensionDevelopmentPath,
            extensionTestsPath: extensionTestsPath,
            launchArgs: [testWorkspace]
        })
// [...]

Write your settings in test-fixtures/.vscode/settings.json and you'll be able to access test specific configuration in your tests.

like image 142
higab85 Avatar answered Dec 28 '22 09:12

higab85