Skip to content

Commit

Permalink
src/stateUtils.ts: add command to reset memento state
Browse files Browse the repository at this point in the history
It may sometimes be necessary to reset the memento state,
particularly for testing the extension's behavior when the
state is different.

Change-Id: I8cfed3b8b49a0f5064b4927e86772d32ea358a54
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/285679
Trust: Suzy Mueller <[email protected]>
Run-TryBot: Suzy Mueller <[email protected]>
TryBot-Result: kokoro <[email protected]>
Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
Reviewed-by: Rebecca Stambler <[email protected]>
  • Loading branch information
suzmue committed Jan 29, 2021
1 parent 4d6ed04 commit db2234c
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 5 deletions.
8 changes: 8 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ Show the current Go survey configuration

Reset the current Go survey configuration history

### `Go: Reset Workspace State`

Reset keys in workspace state to undefined.

### `Go: Reset Global State`

Reset keys in global state to undefined.

### `Go: Toggle Workspace Trust Flag`

Toggle the workspace trust flag. Workspace settings that determine tool locations are disabled by default in untrusted workspaces.
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,16 @@
"title": "Go: Reset Survey Configuration",
"description": "Reset the current Go survey configuration history"
},
{
"command": "go.workspace.resetState",
"title": "Go: Reset Workspace State",
"description": "Reset keys in workspace state to undefined."
},
{
"command": "go.global.resetState",
"title": "Go: Reset Global State",
"description": "Reset keys in global state to undefined."
},
{
"command": "go.workspace.isTrusted.toggle",
"title": "Go: Toggle Workspace Trust Flag",
Expand Down
23 changes: 18 additions & 5 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ import { getConfiguredTools } from './goTools';
import { vetCode } from './goVet';
import { pickProcess } from './pickProcess';
import {
getFromGlobalState, getFromWorkspaceState, setGlobalState, setWorkspaceState, updateGlobalState,
getFromGlobalState,
getFromWorkspaceState,
resetGlobalState,
resetWorkspaceState,
setGlobalState,
setWorkspaceState,
updateGlobalState,
updateWorkspaceState
} from './stateUtils';
import { cancelRunningTests, showTestOutput } from './testUtils';
Expand Down Expand Up @@ -507,10 +513,17 @@ https://github.com/golang/vscode-go/issues/50.`;
showServerOutputChannel();
}));

ctx.subscriptions.push(
vscode.commands.registerCommand('go.welcome', () => {
WelcomePanel.createOrShow(ctx.extensionUri);
}));
ctx.subscriptions.push(vscode.commands.registerCommand('go.welcome', () => {
WelcomePanel.createOrShow(ctx.extensionUri);
}));

ctx.subscriptions.push(vscode.commands.registerCommand('go.workspace.resetState', () => {
resetWorkspaceState();
}));

ctx.subscriptions.push(vscode.commands.registerCommand('go.global.resetState', () => {
resetGlobalState();
}));

ctx.subscriptions.push(vscode.commands.registerCommand('go.toggle.gc_details', () => {
if (!languageServerIsRunning) {
Expand Down
41 changes: 41 additions & 0 deletions src/stateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export function getGlobalState() {
return globalState;
}

export function resetGlobalState() {
resetStateQuickPick(globalState, updateGlobalState);
}

export function getFromWorkspaceState(key: string, defaultValue?: any) {
if (!workspaceState) {
return defaultValue;
Expand All @@ -51,3 +55,40 @@ export function setWorkspaceState(state: vscode.Memento) {
export function getWorkspaceState(): vscode.Memento {
return workspaceState;
}

export function resetWorkspaceState() {
const keys = getMementoKeys(workspaceState);
resetStateQuickPick(workspaceState, updateWorkspaceState);
}

export function getMementoKeys(state: vscode.Memento): string[] {
if (!state) {
return [];
}
// tslint:disable-next-line: no-empty
if ((state as any)._value) {
const keys = Object.keys((state as any)._value);
// Filter out keys with undefined values, so they are not shown
// in the quick pick menu.
return keys.filter((key) => state.get(key) !== undefined);
}
return [];
}

async function resetStateQuickPick(state: vscode.Memento, updateFn: (key: string, value: any) => {}) {
const items = await vscode.window.showQuickPick(
getMementoKeys(state),
{
canPickMany: true,
placeHolder: 'Select the keys to reset.'
}
);
resetItemsState(items, updateFn);
}

export function resetItemsState(items: string[], updateFn: (key: string, value: any) => {}) {
if (!items) {
return;
}
items.forEach((item) => updateFn(item, undefined));
}
100 changes: 100 additions & 0 deletions test/integration/stateUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*---------------------------------------------------------
* Copyright 2021 The Go Authors. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------*/

import * as assert from 'assert';
import * as vscode from 'vscode';
import { getMementoKeys, getWorkspaceState, resetItemsState, setWorkspaceState, updateWorkspaceState } from '../../src/stateUtils';
import { MockMemento } from '../mocks/MockMemento';

suite('Workspace State Modification Tests', () => {
let defaultMemento: vscode.Memento;

setup(async () => {
defaultMemento = getWorkspaceState();
});

teardown(async () => {
setWorkspaceState(defaultMemento);
});

test('test getMementoKeys', () => {
interface TestCase {
keys: string[];
values: any[];
want: string[];
}
const testCases: TestCase[] = [
{keys: [], values: [], want: []},
{keys: ['hello'], values: [false], want: ['hello']},
{keys: ['hello', 'goodbye'], values: [false, 25], want: ['hello', 'goodbye']},
];

testCases.forEach((tc) => {
setWorkspaceState(new MockMemento());

const keys = tc.keys;
const values = tc.values;
assert.strictEqual(keys.length, values.length, 'List of keys and values does not have same length');

for (let i = 0; i < keys.length; i ++) {
updateWorkspaceState(keys[i], values[i]);
}

const got = getMementoKeys(getWorkspaceState());
const want = tc.want;

assert.strictEqual(got.length, tc.want.length);
got.forEach((key) => {
assert.ok(want.includes(key));
});
});
});

test('test resetItemsState', () => {
interface TestCase {
keys: string[];
values: any[];
items: string[];
want: string[];
}
const testCases: TestCase[] = [
{keys: [], values: [], items: undefined, want: []},
{keys: ['hello'], values: [false], items: undefined, want: ['hello']},
{keys: ['hello', 'goodbye'], values: [false, 25], items: undefined, want: ['hello', 'goodbye']},

{keys: [], values: [], items: [], want: []},
{keys: ['hello'], values: [false], items: [], want: ['hello']},
{keys: ['hello', 'goodbye'], values: [false, 25], items: [], want: ['hello', 'goodbye']},

{keys: ['hello'], values: [false], items: ['hello'], want: []},
{keys: ['hello', 'goodbye'], values: [false, 25], items: ['hello'], want: ['goodbye']},

{keys: ['hello'], values: [false], items: ['hello'], want: []},
{keys: ['hello', 'goodbye'], values: [false, 25], items: ['hello', 'goodbye'], want: []},
];

testCases.forEach((tc) => {
setWorkspaceState(new MockMemento());

const keys = tc.keys;
const values = tc.values;
assert.strictEqual(keys.length, values.length, 'List of keys and values does not have same length');

for (let i = 0; i < keys.length; i ++) {
updateWorkspaceState(keys[i], values[i]);
}

resetItemsState(tc.items, updateWorkspaceState);
const got = getMementoKeys(getWorkspaceState());
const want = tc.want;

assert.strictEqual(got.length, want.length);
got.forEach((key) => {
assert.ok(want.includes(key));
});
});
});

});

0 comments on commit db2234c

Please sign in to comment.