Skip to content

Commit

Permalink
Merge pull request #72 from segayuu/Refactor-spawn-test
Browse files Browse the repository at this point in the history
Refactor test
  • Loading branch information
segayuu authored Aug 21, 2019
2 parents 67de52f + d7c95fe commit 95f791a
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 120 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"license": "MIT",
"devDependencies": {
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"eslint": "^6.0.1",
"eslint-config-hexo": "^3.0.0",
"html-tag-validator": "^1.5.0",
Expand Down
2 changes: 2 additions & 0 deletions test/scripts/cache_stream.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

require('chai').should();

const { Readable } = require('stream');

describe('CacheStream', () => {
Expand Down
17 changes: 4 additions & 13 deletions test/scripts/camel_case_keys.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const should = require('chai').should(); // eslint-disable-line
require('chai').should();

describe('camelCaseKeys', () => {
const camelCaseKeys = require('../../lib/camel_case_keys');
Expand All @@ -17,11 +17,7 @@ describe('camelCaseKeys', () => {
});

it('obj must be an object', () => {
try {
camelCaseKeys();
} catch (err) {
err.should.have.property('message', 'obj must be an object!');
}
camelCaseKeys.should.throw('obj must be an object!');
});

it('setter', () => {
Expand All @@ -48,12 +44,7 @@ describe('camelCaseKeys', () => {
});

it('do nothing if the key is camelCase', () => {
const result = camelCaseKeys({
fooBar: 'test'
});

result.should.eql({
fooBar: 'test'
});
const result = camelCaseKeys({ fooBar: 'test' });
result.should.eql({ fooBar: 'test' });
});
});
2 changes: 2 additions & 0 deletions test/scripts/color.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

require('chai').should();

describe('color', () => {
const Color = require('../../lib/color');

Expand Down
8 changes: 2 additions & 6 deletions test/scripts/escape_diacritic.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const should = require('chai').should(); // eslint-disable-line
require('chai').should();

describe('escapeDiacritic', () => {
const escapeDiacritic = require('../../lib/escape_diacritic');
Expand All @@ -10,10 +10,6 @@ describe('escapeDiacritic', () => {
});

it('str must be a string', () => {
try {
escapeDiacritic();
} catch (err) {
err.should.have.property('message', 'str must be a string!');
}
escapeDiacritic.should.throw('str must be a string!');
});
});
8 changes: 2 additions & 6 deletions test/scripts/escape_html.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const should = require('chai').should(); // eslint-disable-line
require('chai').should();

describe('escapeHTML', () => {
const escapeHTML = require('../../lib/escape_html');
Expand All @@ -10,10 +10,6 @@ describe('escapeHTML', () => {
});

it('str must be a string', () => {
try {
escapeHTML();
} catch (err) {
err.should.have.property('message', 'str must be a string!');
}
escapeHTML.should.throw('str must be a string!');
});
});
8 changes: 2 additions & 6 deletions test/scripts/escape_regexp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const should = require('chai').should(); // eslint-disable-line
require('chai').should();

describe('escapeRegExp', () => {
const escapeRegExp = require('../../lib/escape_regexp');
Expand All @@ -10,10 +10,6 @@ describe('escapeRegExp', () => {
});

it('str must be a string', () => {
try {
escapeRegExp();
} catch (err) {
err.should.have.property('message', 'str must be a string!');
}
escapeRegExp.should.throw('str must be a string!');
});
});
16 changes: 8 additions & 8 deletions test/scripts/hash.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const should = require('chai').should(); // eslint-disable-line
require('chai').should();
const crypto = require('crypto');

function sha1(content) {
Expand Down Expand Up @@ -28,17 +28,17 @@ describe('hash', () => {
stream.read().should.eql(sha1(content));
});

it('createSha1Hash', function() {
var _sha1 = hash.createSha1Hash();
var content = '123456';
it('createSha1Hash', () => {
const _sha1 = hash.createSha1Hash();
const content = '123456';
_sha1.update(content);
_sha1.digest().should.eql(sha1(content));
});

it('createSha1Hash - streamMode', function() {
var content1 = '123456';
var content2 = '654321';
var stream = hash.createSha1Hash();
it('createSha1Hash - streamMode', () => {
const content1 = '123456';
const content2 = '654321';
const stream = hash.createSha1Hash();
// explicit convert
stream.write(Buffer.from(content1));
// implicit convert
Expand Down
29 changes: 6 additions & 23 deletions test/scripts/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,14 @@ function code(str, lang) {
}

const lines = data.value.split('\n');
let result = codeStart;

for (let i = 0, len = lines.length; i < len; i++) {
result += `<span class="line">${lines[i]}</span><br>`;
}

result += codeEnd;

return result;
return lines.reduce((prev, current) => {
return `${prev}<span class="line">${current}</span><br>`;
}, codeStart) + codeEnd;
}

function assertResult(result) {
let expected = start;

for (let i = 1, len = arguments.length; i < len; i++) {
expected += arguments[i];
}

expected += end;

result.should.eql(expected);
function assertResult(result, ...args) {
result.should.eql(start + args.join('') + end);
}

function validateHtmlAsync(str, done) {
Expand All @@ -89,11 +76,7 @@ describe('highlight', () => {
});

it('str must be a string', () => {
try {
highlight();
} catch (err) {
err.should.have.property('message', 'str must be a string!');
}
highlight.should.throw('str must be a string!');
});

it('gutter: false', done => {
Expand Down
2 changes: 1 addition & 1 deletion test/scripts/html_tag.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const should = require('chai').should(); // eslint-disable-line
require('chai').should();

describe('htmlTag', () => {
const htmlTag = require('../../lib/html_tag');
Expand Down
8 changes: 3 additions & 5 deletions test/scripts/pattern.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const should = require('chai').should(); // eslint-disable-line
require('chai').should();

describe('Pattern', () => {
const Pattern = require('../../lib/pattern');
Expand Down Expand Up @@ -60,11 +60,9 @@ describe('Pattern', () => {
});

it('rule is required', () => {
try {
(() => {
// eslint-disable-next-line no-new
new Pattern();
} catch (err) {
err.should.have.property('message', 'rule must be a function, a string or a regular expression.');
}
}).should.throw('rule must be a function, a string or a regular expression.');
});
});
2 changes: 1 addition & 1 deletion test/scripts/permalink.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const should = require('chai').should(); // eslint-disable-line
require('chai').should();

describe('Permalink', () => {
const Permalink = require('../../lib/permalink');
Expand Down
8 changes: 2 additions & 6 deletions test/scripts/slugize.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const should = require('chai').should(); // eslint-disable-line
require('chai').should();

describe('slugize', () => {
const slugize = require('../../lib/slugize');
Expand Down Expand Up @@ -42,10 +42,6 @@ describe('slugize', () => {
});

it('str must be a string', () => {
try {
slugize();
} catch (err) {
err.should.have.property('message', 'str must be a string!');
}
slugize.should.throw('str must be a string!');
});
});
56 changes: 23 additions & 33 deletions test/scripts/spawn.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const should = require('chai').should();
const pathFn = require('path');
const fs = require('fs');
require('chai').use(require('chai-as-promised')).should();
const { join } = require('path');
const { writeFile, unlink } = require('fs');
const rewire = require('rewire');

const isWindows = process.platform === 'win32';
Expand All @@ -11,34 +11,30 @@ const catCommand = isWindows ? 'type' : 'cat';
describe('spawn', () => {
const spawn = require('../../lib/spawn');
const CacheStream = require('../../lib/cache_stream');
const fixturePath = pathFn.join(__dirname, 'spawn_test.txt');
const fixturePath = join(__dirname, 'spawn_test.txt');
const fixture = 'test content';

before(done => {
fs.writeFile(fixturePath, fixture, done);
writeFile(fixturePath, fixture, done);
});

after(done => {
fs.unlink(fixturePath, done);
unlink(fixturePath, done);
});

it('default', () => spawn(catCommand, [fixturePath]).then(content => {
content.should.eql(fixture);
}));
it('default', () => spawn(catCommand, [fixturePath]).should.become(fixture));

it('command is required', () => {
spawn.should.throw('command is required!');
});

it('error', () => spawn(catCommand, ['nothing']).then(() => should.fail(`expected spawn(${catCommand}, ['nothing']) to throw an error`), err => {
it('error', () => {
const promise = spawn(catCommand, ['nothing']);
if (isWindows) {
err.message.trim().should.eql('spawn type ENOENT');
err.code.should.eql('ENOENT');
} else {
err.message.trim().should.eql('cat: nothing: No such file or directory');
err.code.should.eql(1);
return promise.should.rejectedWith('spawn type ENOENT').and.eventually.have.property('code', 'ENOENT');
}
}));
return promise.should.rejectedWith('cat: nothing: No such file or directory').and.eventually.have.property('code', 1);
});

it('verbose - stdout', () => {
const spawn = rewire('../../lib/spawn');
Expand All @@ -48,10 +44,7 @@ describe('spawn', () => {

spawn.__set__('process', Object.assign({}, process, {
stdout: stdoutCache,
stderr: stderrCache,
removeListener: () => {},
on: () => {},
exit: () => {}
stderr: stderrCache
}));

return spawn('echo', [content], {
Expand All @@ -75,32 +68,29 @@ describe('spawn', () => {
stdout: stdoutCache,
stderr: stderrCache,
removeListener: () => {},
on: () => {},
exit: () => {}
on: () => {}
}));

return spawn(catCommand, ['nothing'], {
verbose: true
}).then(() => should.fail(`expected spawn(${catCommand}, ['nothing'], {verbose: true}) to throw an error`), () => {
}).should.rejected.then(() => {
const stderrResult = stderrCache.getCache();
if (isWindows) {
// utf8 support in windows shell (cmd.exe) is difficult.
Buffer.byteLength(stderrResult, 'hex').should.least(1);
} else {
stderrResult.toString('utf8').trim().should.eql('cat: nothing: No such file or directory');
stderrResult.toString('utf8').should.with.match(/^cat: nothing: No such file or directory\n?$/);
}
});
});

it('custom encoding', () => spawn(catCommand, [fixturePath], {encoding: 'hex'}).then(content => {
content.should.eql(Buffer.from(fixture).toString('hex'));
}));
it('custom encoding', () => {
return spawn(catCommand, [fixturePath], {encoding: 'hex'}).should.become(Buffer.from(fixture).toString('hex'));
});

it('encoding = null', () => spawn(catCommand, [fixturePath], {encoding: null}).then(content => {
content.should.eql(Buffer.from(fixture));
}));
it('encoding = null', () => {
return spawn(catCommand, [fixturePath], {encoding: null}).should.become(Buffer.from(fixture));
});

it('stdio = inherit', () => spawn('echo', ['something'], {
stdio: 'inherit'
}));
it('stdio = inherit', () => spawn('echo', ['something'], { stdio: 'inherit' }));
});
8 changes: 2 additions & 6 deletions test/scripts/truncate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const should = require('chai').should(); // eslint-disable-line
require('chai').should();

describe('truncate', () => {
const truncate = require('../../lib/truncate');
Expand Down Expand Up @@ -31,10 +31,6 @@ describe('truncate', () => {
});

it('str must be a string', () => {
try {
truncate();
} catch (err) {
err.should.have.property('message', 'str must be a string!');
}
truncate.should.throw('str must be a string!');
});
});
Loading

0 comments on commit 95f791a

Please sign in to comment.