Skip to content
This repository has been archived by the owner on Oct 4, 2020. It is now read-only.

Commit

Permalink
coverage for createPluginError (#101)
Browse files Browse the repository at this point in the history
* coverage for createPluginError

* update CHANGELOG.md
  • Loading branch information
gucong3000 authored Aug 10, 2017
1 parent 2e7f8b0 commit 30badff
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 30 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 2.4.2
- bugfix: Break valid block comment. [#97](https://github.com/jedmao/eclint/pull/97)
- bugfix: Generated empty locales file after execute eclint command. [#99](https://github.com/jedmao/eclint/pull/99)
- bugfix: fail to recognize BOM [#98](https://github.com/jedmao/eclint/pull/98)
- update: coverage for `createPluginError` [#101](https://github.com/jedmao/eclint/pull/101)

## 2.4.1
bugfix: Break valid block comment. [#97](https://github.com/jedmao/eclint/pull/97)

## 2.4.0
- Fix: [yargs/y18n#48](https://github.com/yargs/y18n/issues/48) [#93](https://github.com/jedmao/eclint/pull/93)
- Fix: [thomas-lebeau/gulp-gitignore#2](https://github.com/thomas-lebeau/gulp-gitignore/issues/2) [#84](https://github.com/jedmao/eclint/pull/84)
Expand Down
1 change: 1 addition & 0 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ function handler(yargs: Argv): Stream.Transform {
yargs.globs = globs;
return vfs.src(globs, {
stripBOM: false,
removeBOM: false,
})
.pipe(filter(excludeBinaryFile))
.pipe(gitignore());
Expand Down
93 changes: 93 additions & 0 deletions lib/eclint.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gutil = require('gulp-util');
import sinon = require('sinon');
import common = require('./test-common');
import eclint = require('./eclint');
import vfs = require('vinyl-fs');
Expand Down Expand Up @@ -449,4 +450,96 @@ describe('eclint gulp plugin', () => {
eclint.configure.call(null);
}).not.to.throw();
});
describe('should plugin error', () => {
const charset = require('./rules/charset');
const editorconfig = require('editorconfig');
let stub;

afterEach(() => {
if (stub) {
stub.restore();
stub = null;
}
});

it('editorconfig.parse in eclint.check', done => {
stub = sinon.stub(editorconfig, 'parse').rejects(new Error('check editorconfig testcase'));
var stream = eclint.check();

stream.on('error', (error: gutil.PluginError) => {
expect(error).haveOwnProperty('plugin').and.to.be.equal('ECLint');
expect(error).haveOwnProperty('message').and.to.be.equal('check editorconfig testcase');
done();
});

stream.write(new File({
path: path.join(__dirname, 'testcase.js'),
contents: new Buffer('testcase')
}));
});

it('charset.check', done => {
stub = sinon.stub(charset, 'check').throws(new Error('check testcase'));
var stream = eclint.check();

stream.on('error', (error: gutil.PluginError) => {
expect(error).haveOwnProperty('plugin').and.to.be.equal('ECLint');
expect(error).haveOwnProperty('message').and.to.be.equal('check testcase');
done();
});

stream.write(new File({
path: path.join(__dirname, 'testcase.js'),
contents: new Buffer('testcase')
}));
});

it('editorconfig.parse in eclint.fix', done => {
stub = sinon.stub(editorconfig, 'parse').rejects(new Error('fix editorconfig testcase'));
var stream = eclint.fix();

stream.on('error', (error: gutil.PluginError) => {
expect(error).haveOwnProperty('plugin').and.to.be.equal('ECLint');
expect(error).haveOwnProperty('message').and.to.be.equal('fix editorconfig testcase');
done();
});

stream.write(new File({
path: path.join(__dirname, 'testcase.js'),
contents: new Buffer('testcase')
}));
});

it('charset.fix', done => {
stub = sinon.stub(charset, 'fix').throws(new Error('fix testcase'));
var stream = eclint.fix();

stream.on('error', (error: gutil.PluginError) => {
expect(error).haveOwnProperty('plugin').and.to.be.equal('ECLint');
expect(error).haveOwnProperty('message').and.to.be.equal('fix testcase');
done();
});

stream.write(new File({
path: path.join(__dirname, 'testcase.js'),
contents: new Buffer('testcase')
}));
});

it('charset.infer', done => {
stub = sinon.stub(charset, 'infer').throws(new Error('infer testcase'));
var stream = eclint.infer();

stream.on('error', (error: gutil.PluginError) => {
expect(error).haveOwnProperty('plugin').and.to.be.equal('ECLint');
expect(error).haveOwnProperty('message').and.to.be.equal('infer testcase');
done();
});

stream.write(new File({
path: path.join(__dirname, 'testcase.js'),
contents: new Buffer('testcase')
}));
});
});
});
46 changes: 18 additions & 28 deletions lib/eclint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import os = require('os');
import _ = require('lodash');
import gutil = require('gulp-util');
import through = require('through2');
var editorconfig = require('editorconfig');
import editorconfig = require('editorconfig');

import * as linez from 'linez';
import * as doc from './doc';
Expand Down Expand Up @@ -119,9 +119,7 @@ module eclint {
var PLUGIN_NAME = 'ECLint';

function createPluginError(err: Error | string) {
return new PluginError(PLUGIN_NAME, _.get(err, 'message', <string>err), {
showStack: typeof err !== 'string'
});
return new PluginError(PLUGIN_NAME, err);
}

export var ruleNames = [
Expand Down Expand Up @@ -203,17 +201,13 @@ module eclint {
if (_.isUndefined(rule)) {
return;
}
try {
if (rule.type === 'DocumentRule') {
(<DocumentRule>rule).check(settings, document).forEach(addError);
} else {
var check = (<LineRule>rule).check;
document.lines.forEach(line => {
addError(check(settings, line));
});
}
} catch (err) {
done(createPluginError(err));
if (rule.type === 'DocumentRule') {
(<DocumentRule>rule).check(settings, document).forEach(addError);
} else {
var check = (<LineRule>rule).check;
document.lines.forEach(line => {
addError(check(settings, line));
});
}
});

Expand All @@ -229,7 +223,7 @@ module eclint {

done(null, file);

}, (err: Error) => {
}).catch((err: Error) => {
done(createPluginError(err));
});
});
Expand Down Expand Up @@ -262,17 +256,13 @@ module eclint {
if (_.isUndefined(rule)) {
return;
}
try {
if (rule.type === 'DocumentRule') {
(<DocumentRule>rule).fix(settings, document);
} else {
var fix = (<LineRule>rule).fix;
document.lines.forEach(line => {
fix(settings, line);
});
}
} catch (err) {
done(createPluginError(err));
if (rule.type === 'DocumentRule') {
(<DocumentRule>rule).fix(settings, document);
} else {
var fix = (<LineRule>rule).fix;
document.lines.forEach(line => {
fix(settings, line);
});
}
});

Expand All @@ -286,7 +276,7 @@ module eclint {

done(null, file);

}, (err: Error) => {
}).catch((err: Error) => {
done(createPluginError(err));
});
});
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eclint",
"version": "2.4.1",
"version": "2.4.2",
"description": "Validate or fix code that doesn't adhere to EditorConfig settings or infer settings from existing code.",
"keywords": [
"editorconfig",
Expand Down Expand Up @@ -89,7 +89,7 @@
"@types/sinon-chai": "^2.7.28",
"@types/through2": "^2.0.33",
"@types/vinyl": "^2.0.0",
"@types/vinyl-fs": "^2.4.5",
"@types/vinyl-fs": "^2.4.6",
"@types/yargs": "^8.0.2",
"chai": "^4.1.0",
"event-stream": "^3.3.4",
Expand Down

0 comments on commit 30badff

Please sign in to comment.