Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6258ca2

Browse files
author
Kartik Raj
authoredOct 5, 2021
Ensure keys whose default value type is an array are not duplicated (#17627) (#17634)
1 parent d3abb97 commit 6258ca2

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed
 

‎src/client/common/persistentState.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ export class PersistentState<T> implements IPersistentState<T> {
5353
}
5454
}
5555

56-
const GLOBAL_PERSISTENT_KEYS_DEPRECATED = 'PYTHON_EXTENSION_GLOBAL_STORAGE_KEYS';
57-
const WORKSPACE_PERSISTENT_KEYS_DEPRECATED = 'PYTHON_EXTENSION_WORKSPACE_STORAGE_KEYS';
56+
export const GLOBAL_PERSISTENT_KEYS_DEPRECATED = 'PYTHON_EXTENSION_GLOBAL_STORAGE_KEYS';
57+
export const WORKSPACE_PERSISTENT_KEYS_DEPRECATED = 'PYTHON_EXTENSION_WORKSPACE_STORAGE_KEYS';
5858

5959
const GLOBAL_PERSISTENT_KEYS = 'PYTHON_GLOBAL_STORAGE_KEYS';
6060
const WORKSPACE_PERSISTENT_KEYS = 'PYTHON_WORKSPACE_STORAGE_KEYS';
6161
type KeysStorageType = 'global' | 'workspace';
62-
type KeysStorage = { key: string; defaultValue: unknown };
62+
export type KeysStorage = { key: string; defaultValue: unknown };
6363

6464
@injectable()
6565
export class PersistentStateFactory implements IPersistentStateFactory, IExtensionSingleActivationService {
@@ -122,7 +122,7 @@ export class PersistentStateFactory implements IPersistentStateFactory, IExtensi
122122
@cache(-1, true)
123123
private async addKeyToStorage<T>(keyStorageType: KeysStorageType, key: string, defaultValue?: T) {
124124
const storage = keyStorageType === 'global' ? this._globalKeysStorage : this._workspaceKeysStorage;
125-
const found = storage.value.find((value) => value.key === key && value.defaultValue === defaultValue);
125+
const found = storage.value.find((value) => value.key === key);
126126
if (!found) {
127127
await storage.updateValue([{ key, defaultValue }, ...storage.value]);
128128
}

‎src/test/common/persistentState.unit.test.ts

+44-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import * as TypeMoq from 'typemoq';
88
import { Memento } from 'vscode';
99
import { ICommandManager } from '../../client/common/application/types';
1010
import { Commands } from '../../client/common/constants';
11-
import { PersistentStateFactory } from '../../client/common/persistentState';
11+
import {
12+
GLOBAL_PERSISTENT_KEYS_DEPRECATED,
13+
KeysStorage,
14+
PersistentStateFactory,
15+
WORKSPACE_PERSISTENT_KEYS_DEPRECATED,
16+
} from '../../client/common/persistentState';
1217
import { IDisposable } from '../../client/common/types';
1318
import { sleep } from '../core';
1419
import { MockMemento } from '../mocks/mementos';
@@ -91,9 +96,9 @@ suite('Persistent State', () => {
9196
test('Ensure internal global storage extension uses to track other storages does not contain duplicate entries', async () => {
9297
persistentStateFactory.createGlobalPersistentState('key1');
9398
await sleep(1);
94-
persistentStateFactory.createGlobalPersistentState('key2', 'defaultValue1');
99+
persistentStateFactory.createGlobalPersistentState('key2', ['defaultValue1']); // Default value type is an array
95100
await sleep(1);
96-
persistentStateFactory.createGlobalPersistentState('key2', 'defaultValue1');
101+
persistentStateFactory.createGlobalPersistentState('key2', ['defaultValue1']);
97102
await sleep(1);
98103
persistentStateFactory.createGlobalPersistentState('key1');
99104
await sleep(1);
@@ -102,13 +107,13 @@ suite('Persistent State', () => {
102107
value.sort((k1, k2) => k1.key.localeCompare(k2.key)),
103108
[
104109
{ key: 'key1', defaultValue: undefined },
105-
{ key: 'key2', defaultValue: 'defaultValue1' },
110+
{ key: 'key2', defaultValue: ['defaultValue1'] },
106111
].sort((k1, k2) => k1.key.localeCompare(k2.key)),
107112
);
108113
});
109114

110115
test('Ensure internal workspace storage extension uses to track other storages does not contain duplicate entries', async () => {
111-
persistentStateFactory.createWorkspacePersistentState('key2', 'defaultValue1');
116+
persistentStateFactory.createWorkspacePersistentState('key2', 'defaultValue1'); // Default value type is a string
112117
await sleep(1);
113118
persistentStateFactory.createWorkspacePersistentState('key1');
114119
await sleep(1);
@@ -125,4 +130,38 @@ suite('Persistent State', () => {
125130
].sort((k1, k2) => k1.key.localeCompare(k2.key)),
126131
);
127132
});
133+
134+
test('Ensure deprecated global storage extension used to track other storages with is reset', async () => {
135+
const global = persistentStateFactory.createGlobalPersistentState<KeysStorage[]>(
136+
GLOBAL_PERSISTENT_KEYS_DEPRECATED,
137+
);
138+
await global.updateValue([
139+
{ key: 'oldKey', defaultValue: [] },
140+
{ key: 'oldKey2', defaultValue: [{}] },
141+
{ key: 'oldKey3', defaultValue: ['1', '2', '3'] },
142+
]);
143+
expect(global.value.length).to.equal(3);
144+
145+
await persistentStateFactory.activate();
146+
await sleep(1);
147+
148+
expect(global.value.length).to.equal(0);
149+
});
150+
151+
test('Ensure deprecated global storage extension used to track other storages with is reset', async () => {
152+
const workspace = persistentStateFactory.createWorkspacePersistentState<KeysStorage[]>(
153+
WORKSPACE_PERSISTENT_KEYS_DEPRECATED,
154+
);
155+
await workspace.updateValue([
156+
{ key: 'oldKey', defaultValue: [] },
157+
{ key: 'oldKey2', defaultValue: [{}] },
158+
{ key: 'oldKey3', defaultValue: ['1', '2', '3'] },
159+
]);
160+
expect(workspace.value.length).to.equal(3);
161+
162+
await persistentStateFactory.activate();
163+
await sleep(1);
164+
165+
expect(workspace.value.length).to.equal(0);
166+
});
128167
});

0 commit comments

Comments
 (0)
Please sign in to comment.