Tests: Convert to ES6, use node-fetch, refactor, and clean up style (#971)

This commit is contained in:
Paul Melnikow
2017-04-30 10:51:18 -04:00
committed by GitHub
parent 138c1ea4d5
commit 653f79fbde
6 changed files with 95 additions and 117 deletions

View File

@@ -1,61 +1,49 @@
var assert = require('assert');
var cproc = require('child_process');
var isPng = require('is-png');
var isSvg = require('is-svg');
const assert = require('assert');
const isPng = require('is-png');
const isSvg = require('is-svg');
const {spawn} = require('child-process-promise');
function runCli (args) {
return spawn('node', ['gh-badge.js', ...args], { capture: ['stdout'] })
.then(result => result.stdout);
}
describe('The CLI', function () {
it('should provide a help message', function(done) {
var child = cproc.spawn('node', ['gh-badge.js']);
var buffer = '';
child.stdout.on('data', function(chunk) {
buffer += ''+chunk;
});
child.stdout.on('end', function() {
assert(buffer.startsWith('Usage'));
done();
it('should provide a help message', function () {
return runCli([]).then(stdout => {
assert(stdout.startsWith('Usage'));
});
});
it('should produce default badges', function(done) {
var child = cproc.spawn('node',
['gh-badge.js', 'cactus', 'grown']);
child.stdout.once('data', function(chunk) {
var buffer = ''+chunk;
assert.ok(isSvg(buffer));
assert(buffer.includes('cactus'), 'cactus');
assert(buffer.includes('grown'), 'grown');
done();
it('should produce default badges', function () {
return runCli(['cactus', 'grown']).then(stdout => {
assert.ok(isSvg(stdout));
assert.ok(stdout.includes('cactus'), 'cactus');
assert.ok(stdout.includes('grown'), 'grown');
});
});
it('should produce colorschemed badges', function(done) {
var child = cproc.spawn('node',
['gh-badge.js', 'cactus', 'grown', ':green']);
child.stdout.once('data', function(chunk) {
var buffer = ''+chunk;
assert.ok(isSvg(buffer));
done();
it('should produce colorschemed badges', function () {
return runCli(['cactus', 'grown', ':green']).then(stdout => {
assert.ok(isSvg(stdout));
});
});
it('should produce right-color badges', function(done) {
var child = cproc.spawn('node',
['gh-badge.js', 'cactus', 'grown', '#abcdef']);
child.stdout.once('data', function(chunk) {
var buffer = ''+chunk;
assert(buffer.includes('#abcdef'), '#abcdef');
done();
it('should produce right-color badges', function () {
return runCli(['cactus', 'grown', '#abcdef']).then(stdout => {
assert.ok(isSvg(stdout));
assert.ok(stdout.includes('#abcdef'), '#abcdef');
});
});
it('should produce PNG badges', function(done) {
var child = cproc.spawn('node',
['gh-badge.js', 'cactus', 'grown', '.png']);
child.stdout.once('data', function(chunk) {
assert.ok(isPng(chunk));
done();
});
});
it('should produce PNG badges', function () {
const child = runCli(['cactus', 'grown', '.png']);
// The buffering done by `child-process-promise` doesn't seem correctly to
// handle binary data.
let chunk;
child.childProcess.stdout.once('data', data => { chunk = data; });
return child.then(() => { assert.ok(isPng(chunk)); });
});
});

View File

@@ -1,10 +1,9 @@
const assert = require('assert');
const badge = require('./badge');
const isSvg = require('is-svg');
const badge = require('./badge');
describe('The badge generator', function () {
it('should produce SVG', function(done) {
it('should produce SVG', function (done) {
badge({ text: ['cactus', 'grown'], format: 'svg' }, svg => {
assert.ok(isSvg(svg));
assert(svg.includes('cactus'), 'cactus');

View File

@@ -1,22 +1,20 @@
var assert = require('assert');
var LRU = require('./lru-cache');
const assert = require('assert');
const LRU = require('./lru-cache');
describe('The LRU cache', function () {
it("should support being called without new", function() {
var cache = LRU(1);
it('should support being called without new', function () {
const cache = LRU(1);
assert(cache instanceof LRU);
});
it("should support a zero capacity", function() {
var cache = new LRU(0);
it('should support a zero capacity', function () {
const cache = new LRU(0);
cache.set('key', 'value');
assert.equal(cache.cache.size, 0);
});
it("should support a one capacity", function() {
var cache = new LRU(1);
it('should support a one capacity', function () {
const cache = new LRU(1);
cache.set('key1', 'value1');
assert.equal(cache.cache.size, 1);
assert.equal(cache.newest, cache.cache.get('key1'));
@@ -29,14 +27,14 @@ describe('The LRU cache', function () {
assert.equal(cache.get('key2'), 'value2');
});
it("should remove the oldest element when reaching capacity", function() {
var cache = new LRU(2);
it('should remove the oldest element when reaching capacity', function () {
const cache = new LRU(2);
cache.set('key1', 'value1');
cache.set('key2', 'value2');
cache.set('key3', 'value3');
cache.cache.get('key1');
var slot2 = cache.cache.get('key2');
var slot3 = cache.cache.get('key3');
const slot2 = cache.cache.get('key2');
const slot3 = cache.cache.get('key3');
assert.equal(cache.cache.size, 2);
assert.equal(cache.oldest, slot2);
assert.equal(cache.newest, slot3);
@@ -50,8 +48,8 @@ describe('The LRU cache', function () {
assert.equal(cache.get('key3'), 'value3');
});
it("should make sure that resetting a key in cache makes it newest", function() {
var cache = new LRU(2);
it('should make sure that resetting a key in cache makes it newest', function () {
const cache = new LRU(2);
cache.set('key', 'value');
cache.set('key2', 'value2');
assert.equal(cache.oldest, cache.cache.get('key'));
@@ -61,11 +59,11 @@ describe('The LRU cache', function () {
assert.equal(cache.newest, cache.cache.get('key'));
});
it("should make sure that getting a key in cache makes it newest", function() {
var slot1, slot2, slot3;
it('should make sure that getting a key in cache makes it newest', function () {
let slot1, slot2, slot3, cache;
// When the key is oldest.
var cache = new LRU(2);
cache = new LRU(2);
cache.set('key1', 'value1');
cache.set('key2', 'value2');
slot1 = cache.cache.get('key1');

View File

@@ -1,8 +1,7 @@
const assert = require('assert');
const sinon = require('sinon');
const isPng = require('is-png');
const badge = require('./badge');
const isPng = require('is-png');
const sinon = require('sinon');
const svg2img = require('./svg-to-img');
describe('The rasterizer', function () {

View File

@@ -62,6 +62,7 @@
"logo"
],
"devDependencies": {
"child-process-promise": "^2.2.1",
"eslint": "^3.18.0",
"glob": "^7.1.1",
"icedfrisby": "^1.1.0",

View File

@@ -1,18 +1,17 @@
var assert = require('assert');
var sinon = require('sinon');
var http = require('http');
var fs = require('fs');
var path = require('path');
var isPng = require('is-png');
var isSvg = require('is-svg');
const assert = require('assert');
const config = require('./lib/test-config');
const fetch = require('node-fetch');
var svg2img = require('./lib/svg-to-img');
const fs = require('fs');
const isPng = require('is-png');
const isSvg = require('is-svg');
const path = require('path');
const serverHelpers = require('./lib/in-process-server-test-helpers');
var port = '1111';
var url = 'http://127.0.0.1:' + port + '/';
const sinon = require('sinon');
const svg2img = require('./lib/svg-to-img');
describe('The server', function () {
const baseUri = `http://127.0.0.1:${config.port}`;
let server;
before('Start running the server', function () {
this.timeout(5000);
@@ -20,61 +19,55 @@ describe('The server', function () {
});
after('Shut down the server', function () { serverHelpers.stop(server); });
it('should produce colorscheme badges', function(done) {
http.get(url + ':fruit-apple-green.svg',
function(res) {
var buffer = '';
res.on('data', function(chunk) { buffer += ''+chunk; });
res.on('end', function() {
assert.ok(isSvg(buffer));
assert(buffer.includes('fruit'), 'fruit');
assert(buffer.includes('apple'), 'apple');
done();
});
});
it('should produce colorscheme badges', function () {
return fetch(`${baseUri}/:fruit-apple-green.svg`)
.then(res => {
assert.ok(res.ok);
return res.text();
}).then(text => {
assert.ok(isSvg(text));
assert(text.includes('fruit'), 'fruit');
assert(text.includes('apple'), 'apple');
});
});
it('should produce colorscheme PNG badges', function(done) {
http.get(url + ':fruit-apple-green.png',
function(res) {
res.once('data', function(chunk) {
assert.ok(isPng(chunk));
done();
});
});
it('should produce colorscheme PNG badges', function () {
return fetch(`${baseUri}/:fruit-apple-green.png`)
.then(res => {
assert.ok(res.ok);
return res.buffer();
}).then(data => {
assert.ok(isPng(data));
});
});
context('with svg2img error', function () {
var expectedError = fs.readFileSync(path.resolve(__dirname, 'public', '500.html'));
const expectedError = fs.readFileSync(path.resolve(__dirname, 'public', '500.html'));
var toBufferStub;
let toBufferStub;
beforeEach(function () {
toBufferStub = sinon.stub(svg2img._imageMagick.prototype, 'toBuffer')
.callsArgWith(1, Error('whoops'));
});
afterEach(function () { toBufferStub.restore(); });
it('should emit the 500 message', function (done) {
http.get(url + ':some_new-badge-green.png',
function(res) {
it('should emit the 500 message', function () {
return fetch(`${baseUri}/:some_new-badge-green.png`)
.then(res => {
// This emits status code 200, though 500 would be preferable.
assert.equal(res.statusCode, 200);
var buffer = '';
res.on('data', function(chunk) { buffer += ''+chunk; });
res.on('end', function() {
assert.equal(buffer, expectedError);
done();
});
});
assert.equal(res.status, 200);
return res.text();
}).then(text => {
assert.equal(text, expectedError);
});
});
});
describe('analytics endpoint', function () {
it('should return analytics in the expected format', function () {
return fetch(`${url}$analytics/v1`)
return fetch(`${baseUri}/$analytics/v1`)
.then(res => {
assert(res.ok);
assert.ok(res.ok);
return res.json();
}).then(json => {
const keys = Object.keys(json);