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

Tom*_*ski 5 visual-studio-code vscode-extensions

我的 Visual Studio Code 扩展依赖于工作区配置。因此,在扩展测试中,我想设置工作区配置,然后测试扩展是否正常工作。

扩展贡献定义如下所示:

"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"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    },
Run Code Online (Sandbox Code Playgroud)

示例配置如下所示:

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

当我打电话时:

我尝试过的其他方法包括:

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

它返回错误:

错误:无法写入用户设置,因为 settings.assistant 不是注册配置。

我也尝试过使用 ConfigurationTarget。工作区以及访问设置的各种方式,但似乎都无法正常工作。

该扩展似乎已正确安装在测试 vscode 应用程序中。

如何以编程方式更改 vscode 扩展属性?

小智 1

您需要像对待任何其他 vscode 项目一样对待您的测试环境。如何保存每个 vscode 项目的设置?通过将它们保留在.vscode/settings.json. 因此,解决方案是为您的测试 vscode 创建工作区,并使用上述设置。

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]
        })
// [...]
Run Code Online (Sandbox Code Playgroud)

写入您的设置test-fixtures/.vscode/settings.json,您将能够在测试中访问测试特定配置。