简体   繁体   English

如何从扩展测试更改工作区或全局 vscode 配置?

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

My Visual Studio Code extension relies on a workspace configuration.我的 Visual Studio Code 扩展依赖于工作区配置。 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.错误:无法写入用户设置,因为 settings.assistant 不是已注册的配置。

I have tried also with ConfigurationTarget.我也尝试过使用 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.该扩展似乎已正确安装在测试 vscode 应用程序中。

How to change vscode extension properties programmatically?如何以编程方式更改 vscode 扩展属性?

You need to treat your test environment as if it were any other vscode project.您需要像对待任何其他 vscode 项目一样对待您的测试环境。 How do you keep your settings per vscode project?您如何保留每个 vscode 项目的设置? By keeping them in .vscode/settings.json .通过将它们保存在.vscode/settings.json So the solution is to create workspace for your test vscode, with your settings as mentioned.所以解决方案是为您的测试 vscode 创建工作区,并使用您提到的设置。

In the vscode-test docs you can see how to open a specific workspace when running the tests.vscode-test 文档中,您可以看到如何在运行测试时打开特定的工作区。

// 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.test-fixtures/.vscode/settings.json中写入您的设置,您将能够在测试中访问特定于测试的配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM