Skip to content

Commit

Permalink
Fix the version format for the default state. (vscode-icons#1098)
Browse files Browse the repository at this point in the history
* Fix the version format for the default state.

* Exclude typings files from bithound analysis.
  • Loading branch information
JimiC authored and robertohuertasm committed Jul 26, 2017
1 parent 78b82f7 commit bc5daea
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .bithoundrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"ignore": [
"**/.vscode/**",
"**/node_modules/**",
"**/out/**"
"**/out/**",
"**/src/*.d.ts"
],
"test": [
"**/test/**"
Expand Down
13 changes: 6 additions & 7 deletions src/settings/settingsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,20 @@ export class SettingsManager implements ISettingsManager {

public getState(): IState {
const defaultState: IState = {
version: '0',
version: '0.0.0',
status: ExtensionStatus.notActivated,
welcomeShown: false,
};

let state;
if (!fs.existsSync(this.settings.settingsPath)) {
return defaultState;
}
try {
state = fs.readFileSync(this.settings.settingsPath, 'utf8');
const state = fs.readFileSync(this.settings.settingsPath, 'utf8');
return (parseJSON(state) as IState) || defaultState;
} catch (error) {
console.error(error);
return defaultState;
}

const json: IState = parseJSON(state);
return json || defaultState;
}

public setState(state: IState): void {
Expand Down
45 changes: 36 additions & 9 deletions test/settings/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe('SettingsManager: tests', function () {
function () {
const writeToFile = sandbox.stub(fs, 'writeFileSync');
const stateMock: IState = {
version: '0',
version: '0.0.0',
status: ExtensionStatus.notActivated,
welcomeShown: false,
};
Expand Down Expand Up @@ -138,8 +138,13 @@ describe('SettingsManager: tests', function () {

it('returns the state from the settings file',
function () {
const stateMock = '{ "version": "1.0.0", "status": 0, "welcomeShown": true}';
sandbox.stub(fs, 'readFileSync').returns(stateMock);
const stateMock: IState = {
version: "1.0.0",
status: ExtensionStatus.enabled,
welcomeShown: true,
};
sandbox.stub(fs, 'existsSync').returns(true);
sandbox.stub(fs, 'readFileSync').returns(JSON.stringify(stateMock));
const state = settingsManager.getState();
expect(state).to.be.an.instanceOf(Object);
expect(state).to.have.all.keys('version', 'status', 'welcomeShown');
Expand All @@ -148,19 +153,29 @@ describe('SettingsManager: tests', function () {

it('returns a default state if no settings file exists',
function () {
sandbox.stub(fs, 'readFileSync').returns('test');
sandbox.stub(fs, 'existsSync').returns(false);
const state = settingsManager.getState();
expect(state).to.be.instanceOf(Object);
expect(state.version).to.be.equal('0');
expect(state.version).to.be.equal('0.0.0');
});

it('returns a default state if reading the file fails',
function () {
sandbox.stub(fs, 'existsSync').returns(true);
sandbox.stub(fs, 'readFileSync').throws(Error);
sandbox.stub(console, 'error');
const state = settingsManager.getState();
expect(state).to.be.instanceOf(Object);
expect(state.version).to.be.equal('0');
expect(state.version).to.be.equal('0.0.0');
});

it('returns a default state if parsing the file content fails',
function () {
sandbox.stub(fs, 'existsSync').returns(true);
sandbox.stub(fs, 'readFileSync').returns('test');
const state = settingsManager.getState();
expect(state).to.be.instanceOf(Object);
expect(state.version).to.be.equal('0.0.0');
});

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

it('truthy for a new extension version',
function () {
const stateMock = { version: "1.0.0", status: 2, welcomeShown: true };
const stateMock: IState = {
version: "1.0.0",
status: ExtensionStatus.notActivated,
welcomeShown: true,
};
const getState = sinon.stub(settingsManager, 'getState').returns(stateMock);
settingsManager.getSettings();
expect(settingsManager.isNewVersion()).to.be.true;
Expand All @@ -178,7 +197,11 @@ describe('SettingsManager: tests', function () {

it('falsy for the same extension version',
function () {
const stateMock = { version: extensionSettings.version, status: 2, welcomeShown: true };
const stateMock: IState = {
version: extensionSettings.version,
status: ExtensionStatus.notActivated,
welcomeShown: true,
};
const getState = sinon.stub(settingsManager, 'getState').returns(stateMock);
settingsManager.getSettings();
expect(settingsManager.isNewVersion()).to.be.false;
Expand All @@ -187,7 +210,11 @@ describe('SettingsManager: tests', function () {

it('falsy for an older extension version',
function () {
const stateMock = { version: "100.0.0", status: 2, welcomeShown: true };
const stateMock: IState = {
version: "100.0.0",
status: ExtensionStatus.notActivated,
welcomeShown: true,
};
const getState = sinon.stub(settingsManager, 'getState').returns(stateMock);
settingsManager.getSettings();
expect(settingsManager.isNewVersion()).to.be.false;
Expand Down

0 comments on commit bc5daea

Please sign in to comment.