test/integration/statusbar: fix/delete broken tests
Use of async in setup isn't working.
And, delete #updateGoVarsFromConfig test suite.
The configuration API is asynchronous and I can't figure
out writing reliable tests using it. Focusing on refactoring
and making the code under test be testable will be better
than attempting to build more workaround.
Fixes golang/vscode-go#261
Change-Id: I46f9482c333ae5b8a0c2fe3a21f3dce2cc2011df
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/310852
Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/test/integration/statusbar.test.ts b/test/integration/statusbar.test.ts
index fd22f9e..a00be2e 100644
--- a/test/integration/statusbar.test.ts
+++ b/test/integration/statusbar.test.ts
@@ -7,13 +7,11 @@
*--------------------------------------------------------*/
import * as assert from 'assert';
-import * as cp from 'child_process';
import * as fs from 'fs-extra';
import { describe, it } from 'mocha';
import * as os from 'os';
import * as path from 'path';
import * as sinon from 'sinon';
-import * as util from 'util';
import * as vscode from 'vscode';
import {
formatGoVersion,
@@ -25,7 +23,6 @@
import { updateGoVarsFromConfig } from '../../src/goInstallTools';
import { disposeGoStatusBar } from '../../src/goStatus';
import { getWorkspaceState, setWorkspaceState } from '../../src/stateUtils';
-import { getCurrentGoRoot } from '../../src/utils/pathUtils';
import { MockMemento } from '../mocks/MockMemento';
import ourutil = require('../../src/util');
@@ -55,15 +52,15 @@
});
});
-describe('#setSelectedGo()', async function () {
+describe('#setSelectedGo()', function () {
this.timeout(40000);
let sandbox: sinon.SinonSandbox | undefined;
let goOption: GoEnvironmentOption;
let defaultMemento: vscode.Memento;
- const version = await ourutil.getGoVersion();
- const defaultGoOption = new GoEnvironmentOption(version.binaryPath, formatGoVersion(version));
this.beforeAll(async () => {
+ const version = await ourutil.getGoVersion();
+ const defaultGoOption = new GoEnvironmentOption(version.binaryPath, formatGoVersion(version));
defaultMemento = getWorkspaceState();
setWorkspaceState(new MockMemento());
await setSelectedGo(defaultGoOption);
@@ -110,109 +107,3 @@
process.env = envCache;
});
});
-
-describe('#updateGoVarsFromConfig()', async function () {
- this.timeout(10000);
-
- let defaultMemento: vscode.Memento;
- let tmpRoot: string | undefined;
- let tmpRootBin: string | undefined;
- let sandbox: sinon.SinonSandbox | undefined;
- const version = await ourutil.getGoVersion();
- const defaultGoOption = new GoEnvironmentOption(version.binaryPath, formatGoVersion(version));
-
- this.beforeAll(async () => {
- defaultMemento = getWorkspaceState();
- setWorkspaceState(new MockMemento());
- await setSelectedGo(defaultGoOption);
-
- tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'rootchangetest'));
- tmpRootBin = path.join(tmpRoot, 'bin');
-
- // build a fake go binary and place it in tmpRootBin.
- fs.mkdirSync(tmpRootBin);
-
- const fixtureSourcePath = path.join(__dirname, '..', '..', '..', 'test', 'testdata', 'testhelpers');
- const execFile = util.promisify(cp.execFile);
- const goRuntimePath = ourutil.getBinPath('go');
- const { stderr } = await execFile(goRuntimePath, [
- 'build',
- '-o',
- path.join(tmpRootBin, 'go'),
- path.join(fixtureSourcePath, 'fakego.go')
- ]);
- if (stderr) {
- assert.fail(`failed to build the fake go binary required for testing: ${stderr}`);
- }
- });
-
- this.afterAll(async () => {
- setWorkspaceState(defaultMemento);
- ourutil.rmdirRecursive(tmpRoot);
- await updateGoVarsFromConfig();
- });
-
- this.beforeEach(() => {
- sandbox = sinon.createSandbox();
- });
-
- this.afterEach(() => {
- sandbox.restore();
- });
-
- function pathEnvVar(): string[] {
- let paths = [] as string[];
- if (process.env.hasOwnProperty('PATH')) {
- paths = process.env['PATH'].split(path.delimiter);
- } else if (process.platform === 'win32' && process.env.hasOwnProperty('Path')) {
- paths = process.env['Path'].split(path.delimiter);
- }
- return paths;
- }
-
- it('should have a sensible goroot with the default setting', async () => {
- await updateGoVarsFromConfig();
-
- const b = getGoEnvironmentStatusbarItem();
- assert.ok(b.text.startsWith('Go'), `go env statusbar item = ${b.text}, want "Go..."`);
- assert.equal(
- pathEnvVar()[0],
- [path.join(getCurrentGoRoot(), 'bin')],
- 'the first element in PATH must match the current GOROOT/bin'
- );
- });
-
- it('should recognize the adjusted goroot using go.goroot', async () => {
- // adjust the fake go binary's behavior.
- process.env['FAKEGOROOT'] = tmpRoot;
- process.env['FAKEGOVERSION'] = 'go version go2.0.0 darwin/amd64';
-
- await updateGoVarsFromConfig();
-
- assert.equal((await ourutil.getGoVersion()).format(), '2.0.0');
- assert.equal(getGoEnvironmentStatusbarItem().text, 'Go 2.0.0');
- assert.equal(
- pathEnvVar()[0],
- [path.join(getCurrentGoRoot(), 'bin')],
- 'the first element in PATH must match the current GOROOT/bin'
- );
- });
-
- it('should recognize the adjusted goroot using go.alternateTools', async () => {
- // "go.alternateTools" : {"go": "go3"}
- fs.copyFileSync(path.join(tmpRootBin, 'go'), path.join(tmpRootBin, 'go3'));
-
- process.env['FAKEGOROOT'] = tmpRoot;
- process.env['FAKEGOVERSION'] = 'go version go3.0.0 darwin/amd64';
-
- await updateGoVarsFromConfig();
-
- assert.equal((await ourutil.getGoVersion()).format(), '3.0.0');
- assert.equal(getGoEnvironmentStatusbarItem().text, 'Go 3.0.0');
- assert.equal(
- pathEnvVar()[0],
- [path.join(getCurrentGoRoot(), 'bin')],
- 'the first element in PATH must match the current GOROOT/bin'
- );
- });
-});
diff --git a/test/testdata/testhelpers/fakego.go b/test/testdata/testhelpers/fakego.go
deleted file mode 100644
index 8116833..0000000
--- a/test/testdata/testhelpers/fakego.go
+++ /dev/null
@@ -1,47 +0,0 @@
-// Copyright 2020 The Go Authors. All rights reserved.
-// Licensed under the MIT License.
-// See LICENSE in the project root for license information.
-
-// This is a helper used to fake a go binary.
-// It currently fakes `go env` and `go version` commands.
-// For `go env`, it returns FAKEGOROOT for 'GOROOT', and an empty string for others.
-// For `go version`, it returns FAKEGOVERSION or a default dev version string.
-package main
-
-import (
- "fmt"
- "os"
-)
-
-func main() {
- args := os.Args
-
- if len(args) <= 1 {
- return
- }
- switch args[1] {
- case "env":
- fakeEnv(args[2:])
- case "version":
- fakeVersion()
- default:
- fmt.Fprintf(os.Stderr, "not implemented")
- os.Exit(1)
- }
- os.Exit(0)
-}
-
-func fakeEnv(args []string) {
- for _, a := range args {
- fmt.Println(os.Getenv("FAKE" + a))
- }
-}
-
-func fakeVersion() {
- ver := os.Getenv("FAKEGOVERSION")
- if ver != "" {
- fmt.Println(ver)
- return
- }
- fmt.Println("go version devel +a07e2819 Thu Jun 18 20:58:26 2020 +0000 darwin/amd64")
-}