|
| 1 | +/*--------------------------------------------------------- |
| 2 | + * Copyright 2022 The Go Authors. All rights reserved. |
| 3 | + * Licensed under the MIT License. See LICENSE in the project root for license information. |
| 4 | + *--------------------------------------------------------*/ |
| 5 | + |
| 6 | +import assert from 'assert'; |
| 7 | +import path from 'path'; |
| 8 | +import { TreeItem, Uri, window, workspace } from 'vscode'; |
| 9 | +import { getGoConfig, getGoplsConfig } from '../../src/config'; |
| 10 | + |
| 11 | +import { GoExplorerProvider } from '../../src/goExplorer'; |
| 12 | +import { getConfiguredTools } from '../../src/goTools'; |
| 13 | +import { getGoVersion } from '../../src/util'; |
| 14 | +import { resolveHomeDir } from '../../src/utils/pathUtils'; |
| 15 | +import { MockExtensionContext } from '../mocks/MockContext'; |
| 16 | + |
| 17 | +suite('GoExplorerProvider', () => { |
| 18 | + const fixtureDir = path.join(__dirname, '../../../test/testdata/baseTest'); |
| 19 | + const ctx = MockExtensionContext.new(); |
| 20 | + let explorer: GoExplorerProvider; |
| 21 | + |
| 22 | + suiteSetup(async () => { |
| 23 | + explorer = GoExplorerProvider.setup(ctx); |
| 24 | + const uri = Uri.file(path.join(fixtureDir, 'test.go')); |
| 25 | + await workspace.openTextDocument(uri); |
| 26 | + await window.showTextDocument(uri); |
| 27 | + }); |
| 28 | + |
| 29 | + suiteTeardown(() => { |
| 30 | + ctx.teardown(); |
| 31 | + }); |
| 32 | + |
| 33 | + test('env tree', async () => { |
| 34 | + const [env] = await explorer.getChildren(); |
| 35 | + assert.strictEqual(env.label, 'env'); |
| 36 | + assert.strictEqual(env.contextValue, 'go:explorer:env'); |
| 37 | + }); |
| 38 | + |
| 39 | + test('env tree items', async () => { |
| 40 | + const [env] = await explorer.getChildren(); |
| 41 | + const [goenv, gomod] = (await explorer.getChildren(env)) as { key: string; value: string }[]; |
| 42 | + assert.strictEqual(goenv.key, 'GOENV'); |
| 43 | + assert.strictEqual(gomod.key, 'GOMOD'); |
| 44 | + assert.strictEqual(resolveHomeDir(gomod.value), `${fixtureDir}/go.mod`); |
| 45 | + }); |
| 46 | + |
| 47 | + test('tools tree', async () => { |
| 48 | + const [, tools] = await explorer.getChildren(); |
| 49 | + assert(tools.label === 'tools'); |
| 50 | + assert.strictEqual(tools.contextValue, 'go:explorer:tools'); |
| 51 | + }); |
| 52 | + |
| 53 | + test('tools tree items', async () => { |
| 54 | + const goVersion = await getGoVersion(); |
| 55 | + const allTools = getConfiguredTools(goVersion, getGoConfig(), getGoplsConfig()); |
| 56 | + const expectTools = allTools.map((t) => t.name); |
| 57 | + const [, tools] = await explorer.getChildren(); |
| 58 | + const items = (await explorer.getChildren(tools)) as TreeItem[]; |
| 59 | + assert.deepStrictEqual( |
| 60 | + items.map((t) => t.label), |
| 61 | + expectTools |
| 62 | + ); |
| 63 | + }); |
| 64 | +}); |
0 commit comments