Skip to content

Commit bc5daea

Browse files
JimiCrobertohuertasm
authored andcommitted
Fix the version format for the default state. (vscode-icons#1098)
* Fix the version format for the default state. * Exclude typings files from bithound analysis.
1 parent 78b82f7 commit bc5daea

File tree

3 files changed

+44
-18
lines changed

3 files changed

+44
-18
lines changed

.bithoundrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"ignore": [
3-
"**/.vscode/**",
43
"**/node_modules/**",
5-
"**/out/**"
4+
"**/out/**",
5+
"**/src/*.d.ts"
66
],
77
"test": [
88
"**/test/**"

src/settings/settingsManager.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,20 @@ export class SettingsManager implements ISettingsManager {
3939

4040
public getState(): IState {
4141
const defaultState: IState = {
42-
version: '0',
42+
version: '0.0.0',
4343
status: ExtensionStatus.notActivated,
4444
welcomeShown: false,
4545
};
46-
47-
let state;
46+
if (!fs.existsSync(this.settings.settingsPath)) {
47+
return defaultState;
48+
}
4849
try {
49-
state = fs.readFileSync(this.settings.settingsPath, 'utf8');
50+
const state = fs.readFileSync(this.settings.settingsPath, 'utf8');
51+
return (parseJSON(state) as IState) || defaultState;
5052
} catch (error) {
5153
console.error(error);
5254
return defaultState;
5355
}
54-
55-
const json: IState = parseJSON(state);
56-
return json || defaultState;
5756
}
5857

5958
public setState(state: IState): void {

test/settings/settings.test.ts

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ describe('SettingsManager: tests', function () {
103103
function () {
104104
const writeToFile = sandbox.stub(fs, 'writeFileSync');
105105
const stateMock: IState = {
106-
version: '0',
106+
version: '0.0.0',
107107
status: ExtensionStatus.notActivated,
108108
welcomeShown: false,
109109
};
@@ -138,8 +138,13 @@ describe('SettingsManager: tests', function () {
138138

139139
it('returns the state from the settings file',
140140
function () {
141-
const stateMock = '{ "version": "1.0.0", "status": 0, "welcomeShown": true}';
142-
sandbox.stub(fs, 'readFileSync').returns(stateMock);
141+
const stateMock: IState = {
142+
version: "1.0.0",
143+
status: ExtensionStatus.enabled,
144+
welcomeShown: true,
145+
};
146+
sandbox.stub(fs, 'existsSync').returns(true);
147+
sandbox.stub(fs, 'readFileSync').returns(JSON.stringify(stateMock));
143148
const state = settingsManager.getState();
144149
expect(state).to.be.an.instanceOf(Object);
145150
expect(state).to.have.all.keys('version', 'status', 'welcomeShown');
@@ -148,19 +153,29 @@ describe('SettingsManager: tests', function () {
148153

149154
it('returns a default state if no settings file exists',
150155
function () {
151-
sandbox.stub(fs, 'readFileSync').returns('test');
156+
sandbox.stub(fs, 'existsSync').returns(false);
152157
const state = settingsManager.getState();
153158
expect(state).to.be.instanceOf(Object);
154-
expect(state.version).to.be.equal('0');
159+
expect(state.version).to.be.equal('0.0.0');
155160
});
156161

157162
it('returns a default state if reading the file fails',
158163
function () {
164+
sandbox.stub(fs, 'existsSync').returns(true);
159165
sandbox.stub(fs, 'readFileSync').throws(Error);
160166
sandbox.stub(console, 'error');
161167
const state = settingsManager.getState();
162168
expect(state).to.be.instanceOf(Object);
163-
expect(state.version).to.be.equal('0');
169+
expect(state.version).to.be.equal('0.0.0');
170+
});
171+
172+
it('returns a default state if parsing the file content fails',
173+
function () {
174+
sandbox.stub(fs, 'existsSync').returns(true);
175+
sandbox.stub(fs, 'readFileSync').returns('test');
176+
const state = settingsManager.getState();
177+
expect(state).to.be.instanceOf(Object);
178+
expect(state.version).to.be.equal('0.0.0');
164179
});
165180

166181
});
@@ -169,7 +184,11 @@ describe('SettingsManager: tests', function () {
169184

170185
it('truthy for a new extension version',
171186
function () {
172-
const stateMock = { version: "1.0.0", status: 2, welcomeShown: true };
187+
const stateMock: IState = {
188+
version: "1.0.0",
189+
status: ExtensionStatus.notActivated,
190+
welcomeShown: true,
191+
};
173192
const getState = sinon.stub(settingsManager, 'getState').returns(stateMock);
174193
settingsManager.getSettings();
175194
expect(settingsManager.isNewVersion()).to.be.true;
@@ -178,7 +197,11 @@ describe('SettingsManager: tests', function () {
178197

179198
it('falsy for the same extension version',
180199
function () {
181-
const stateMock = { version: extensionSettings.version, status: 2, welcomeShown: true };
200+
const stateMock: IState = {
201+
version: extensionSettings.version,
202+
status: ExtensionStatus.notActivated,
203+
welcomeShown: true,
204+
};
182205
const getState = sinon.stub(settingsManager, 'getState').returns(stateMock);
183206
settingsManager.getSettings();
184207
expect(settingsManager.isNewVersion()).to.be.false;
@@ -187,7 +210,11 @@ describe('SettingsManager: tests', function () {
187210

188211
it('falsy for an older extension version',
189212
function () {
190-
const stateMock = { version: "100.0.0", status: 2, welcomeShown: true };
213+
const stateMock: IState = {
214+
version: "100.0.0",
215+
status: ExtensionStatus.notActivated,
216+
welcomeShown: true,
217+
};
191218
const getState = sinon.stub(settingsManager, 'getState').returns(stateMock);
192219
settingsManager.getSettings();
193220
expect(settingsManager.isNewVersion()).to.be.false;

0 commit comments

Comments
 (0)