Skip to content

Commit

Permalink
Add test cases for error handling (vscode-icons#1670)
Browse files Browse the repository at this point in the history
  • Loading branch information
JimiC authored and robertohuertasm committed Aug 3, 2018
1 parent 2776571 commit 61b705f
Show file tree
Hide file tree
Showing 9 changed files with 928 additions and 838 deletions.
1,618 changes: 809 additions & 809 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/cleanUp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ export function cleanUpVSCodeSettings(): void {
const saveSettings = content => {
const settings = JSON.stringify(content, null, 4);
writeFile(settingsFilePath, settings, error =>
ErrorHandler.LogError(error),
ErrorHandler.logError(error),
);
};
const cleanUpSettings = (error, content) => {
if (error) {
ErrorHandler.LogError(error, true);
ErrorHandler.logError(error, true);
return;
}
const settings = parseJSON(content);
Expand All @@ -85,5 +85,5 @@ export function cleanUpVSIconsSettings(): void {
getAppUserPath(__dirname),
constants.extensionSettingsFilename,
);
unlink(extensionSettingsFilePath, error => ErrorHandler.LogError(error));
unlink(extensionSettingsFilePath, error => ErrorHandler.logError(error));
}
2 changes: 1 addition & 1 deletion src/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export class ErrorHandler {
public static LogError(error: Error, handled = false): void {
public static logError(error: Error, handled = false): void {
console.error(
`${handled ? 'H' : 'Unh'}andled Error: ${error.stack ||
error.message ||
Expand Down
2 changes: 1 addition & 1 deletion src/icon-manifest/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { extensionSettings } from '../settings';
const iconGenerator = new IconGenerator(
vscode,
schema,
'',
/*configCustomIconFolderPath*/ null,
/*avoidCustomDetection*/ true,
);
const json = iconGenerator.generateJson(files, folders);
Expand Down
4 changes: 2 additions & 2 deletions src/icon-manifest/iconGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export class IconGenerator implements models.IIconGenerator {
// tslint:disable-next-line no-console
console.info('Icons manifest file successfully generated!');
} catch (error) {
ErrorHandler.LogError(error);
ErrorHandler.logError(error);
}
}

Expand All @@ -213,7 +213,7 @@ export class IconGenerator implements models.IIconGenerator {
// tslint:disable-next-line no-console
console.info('package.json updated');
} catch (error) {
ErrorHandler.LogError(error);
ErrorHandler.logError(error);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/settings/settingsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class SettingsManager implements ISettingsManager {
const state = fs.readFileSync(this.settings.settingsFilePath, 'utf8');
return (parseJSON(state) as IState) || defaultState;
} catch (error) {
ErrorHandler.LogError(error, true);
ErrorHandler.logError(error, true);
return defaultState;
}
}
Expand All @@ -93,7 +93,7 @@ export class SettingsManager implements ISettingsManager {
try {
fs.writeFileSync(this.settings.settingsFilePath, JSON.stringify(state));
} catch (error) {
ErrorHandler.LogError(error);
ErrorHandler.logError(error);
}
}

Expand Down
69 changes: 69 additions & 0 deletions test/errorHandler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// tslint:disable only-arrow-functions
// tslint:disable no-unused-expression
import { expect } from 'chai';
import * as sinon from 'sinon';
import { ErrorHandler } from '../src/errorHandler';

describe('ErrorHandler: tests', function() {
context('ensures that', function() {
let sandbox: sinon.SinonSandbox;
let consoleErrorStub: sinon.SinonStub;

beforeEach(function() {
sandbox = sinon.createSandbox();
consoleErrorStub = sandbox.stub(console, 'error');
});

afterEach(function() {
sandbox.restore();
});

context('it logs', function() {
it('the error stack', function() {
const error = new Error();
error.stack = 'contextOfStack';
ErrorHandler.logError(error);
expect(consoleErrorStub.called).to.be.true;
expect(consoleErrorStub.calledWithMatch(/contextOfStack/)).to.be.true;
expect(error)
.to.haveOwnProperty('stack')
.and.to.equal(error.stack);
});

it('the error message, when no error stack is available', function() {
const error = new Error('message');
delete error.stack;
ErrorHandler.logError(error);
expect(consoleErrorStub.called).to.be.true;
expect(consoleErrorStub.calledWithMatch(/message/)).to.be.true;
expect(error).to.not.haveOwnProperty('stack').to.be.true;
});

it('the error iteslef, when no error stack and message are available', function() {
const error = new Error();
delete error.stack;
delete error.message;
ErrorHandler.logError(error);
expect(consoleErrorStub.called).to.be.true;
expect(error).to.not.haveOwnProperty('stack').to.be.true;
expect(error).to.not.haveOwnProperty('message').to.be.true;
});

it('handled errors', function() {
const error = new Error();
ErrorHandler.logError(error, true);
expect(consoleErrorStub.called).to.be.true;
expect(consoleErrorStub.calledWithMatch(/Handled/)).to.be.true;
});

it('unhandled errors', function() {
const error = new Error();
delete error.stack;
delete error.message;
ErrorHandler.logError(error);
expect(consoleErrorStub.called).to.be.true;
expect(consoleErrorStub.calledWithMatch(/Unhandled/)).to.be.true;
});
});
});
});
56 changes: 36 additions & 20 deletions test/settings/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as sinon from 'sinon';
import { vscode } from '../../src/utils';
import { extensionSettings, SettingsManager } from '../../src/settings';
import { ExtensionStatus, IState } from '../../src/models';
import { ErrorHandler } from '../../src/errorHandler';

describe('SettingsManager: tests', function() {
context('ensures that', function() {
Expand Down Expand Up @@ -111,6 +112,19 @@ describe('SettingsManager: tests', function() {
sandbox.restore();
});

it('the Error gets logged when writting the state fails', function() {
const writeToFile = sandbox.stub(fs, 'writeFileSync').throws();
const logStub = sandbox.stub(ErrorHandler, 'logError');
const stateMock: IState = {
version: '0.0.0',
status: ExtensionStatus.notActivated,
welcomeShown: false,
};
settingsManager.setState(stateMock);
expect(logStub.called).to.be.true;
expect(writeToFile.called).to.be.true;
});

it('the state gets written to a settings file', function() {
const writeToFile = sandbox.stub(fs, 'writeFileSync');
const stateMock: IState = {
Expand Down Expand Up @@ -178,28 +192,30 @@ describe('SettingsManager: tests', function() {
expect(Object.keys(state)).to.have.lengthOf(3);
});

it('returns a default state if no settings file exists', function() {
sandbox.stub(fs, 'existsSync').returns(false);
const state = settingsManager.getState();
expect(state).to.be.instanceOf(Object);
expect(state.version).to.be.equal('0.0.0');
});
context('returns a default state when', function() {
it('no settings file exists', function() {
sandbox.stub(fs, 'existsSync').returns(false);
const state = settingsManager.getState();
expect(state).to.be.instanceOf(Object);
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.0.0');
});
it('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.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');
it('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 Down
5 changes: 5 additions & 0 deletions wallaby.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ module.exports = {
"test/**/*.test.ts"
],
filesWithNoCoverageCalculated: [
"src/index.ts",
"src/uninstall.ts",
"src/commands/index.ts",
"src/icon-manifest/build.ts",
"src/init/welcome.ts",
],
preprocessors: {
"**/*.json": (file, done) => done(file.rename(`../${file.path}`).content),
Expand Down

0 comments on commit 61b705f

Please sign in to comment.