Tests and coverage analysis.
This commit is contained in:
parent
d8bdab4ffb
commit
48cf0cc008
1
ass-stubs/cli-test.js
Normal file
1
ass-stubs/cli-test.js
Normal file
|
@ -0,0 +1 @@
|
|||
require('ass'); require('../gh-badge.js');
|
3
ass-stubs/server-test.js
Normal file
3
ass-stubs/server-test.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
require('ass'); require('../server.js');
|
||||
console.log('done');
|
||||
process.on('SIGTERM', function() { process.exit(0); });
|
|
@ -23,6 +23,14 @@
|
|||
"es6-promise": "~0.1.1",
|
||||
"camp": "~13.11.9"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ass": "~0.0.4",
|
||||
"should": "~3.0.0",
|
||||
"mocha": "~1.14.0"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha -R spec test.js"
|
||||
},
|
||||
"bin": { "badge": "./gh-badge.js" },
|
||||
"engines": { "node": "0.10.x" }
|
||||
}
|
||||
|
|
30
server.js
30
server.js
|
@ -1,5 +1,5 @@
|
|||
var camp = require('camp').start({
|
||||
port: process.env.PORT||+process.argv[2]||80
|
||||
port: +process.env.PORT||+process.argv[2]||80
|
||||
});
|
||||
var https = require('https');
|
||||
var http = require('http');
|
||||
|
@ -327,6 +327,34 @@ function(data, match, end, ask) {
|
|||
}
|
||||
});
|
||||
|
||||
// Any badge, old version.
|
||||
camp.route(/^\/([^\/]+)\/(.+).png$/,
|
||||
function(data, match, end, ask) {
|
||||
var subject = match[1];
|
||||
var status = match[2];
|
||||
var color = data.color;
|
||||
|
||||
// Cache management.
|
||||
var cacheDuration = (3600*24*1)|0; // 1 day.
|
||||
ask.res.setHeader('Cache-Control', 'public, max-age=' + cacheDuration);
|
||||
if (+(new Date(ask.req.headers['if-modified-since'])) >= +serverStartTime) {
|
||||
ask.res.statusCode = 304;
|
||||
ask.res.end(); // not modified.
|
||||
return;
|
||||
}
|
||||
ask.res.setHeader('Last-Modified', serverStartTime.toGMTString());
|
||||
|
||||
// Badge creation.
|
||||
try {
|
||||
var badgeData = {text: [subject, status]};
|
||||
badgeData.colorscheme = color;
|
||||
badge(badgeData, makeSend('png', ask.res, end));
|
||||
} catch(e) {
|
||||
badge({text: ['error', 'bad badge'], colorscheme: 'red'},
|
||||
makeSend('png', ask.res, end));
|
||||
}
|
||||
});
|
||||
|
||||
// Escapes `t` using the format specified in
|
||||
// <https://github.com/espadrine/gh-badges/issues/12#issuecomment-31518129>
|
||||
function escapeFormat(t) {
|
||||
|
|
107
test.js
Normal file
107
test.js
Normal file
|
@ -0,0 +1,107 @@
|
|||
var ass = require('ass').enable();
|
||||
var should = require('should');
|
||||
|
||||
var http = require('http');
|
||||
var cproc = require('child_process');
|
||||
var fs = require('fs');
|
||||
|
||||
describe('the CLI', function() {
|
||||
it('should provide a help message', function(done) {
|
||||
var child = cproc.spawn('node', ['ass-stubs/cli-test.js']);
|
||||
var buffer = '';
|
||||
child.stdout.on('data', function(chunk) {
|
||||
buffer += ''+chunk;
|
||||
});
|
||||
child.stdout.on('end', function() {
|
||||
buffer.should.startWith('Usage');
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('should produce default badges', function(done) {
|
||||
var child = cproc.spawn('node',
|
||||
['ass-stubs/cli-test.js', 'cactus', 'grown']);
|
||||
child.stdout.on('data', function(chunk) {
|
||||
var buffer = ''+chunk;
|
||||
buffer.should.startWith('<svg');
|
||||
buffer.should.containEql('cactus');
|
||||
buffer.should.containEql('grown');
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('should produce colorschemed badges', function(done) {
|
||||
child = cproc.spawn('node',
|
||||
['ass-stubs/cli-test.js', 'cactus', 'grown', ':green']);
|
||||
child.stdout.on('data', function(chunk) {
|
||||
var buffer = ''+chunk;
|
||||
buffer.should.startWith('<svg');
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('should produce right-color badges', function(done) {
|
||||
child = cproc.spawn('node',
|
||||
['ass-stubs/cli-test.js', 'cactus', 'grown', '#abcdef']);
|
||||
child.stdout.on('data', function(chunk) {
|
||||
var buffer = ''+chunk;
|
||||
buffer.should.containEql('#abcdef');
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('should produce PNG badges', function(done) {
|
||||
child = cproc.spawn('node',
|
||||
['ass-stubs/cli-test.js', 'cactus', 'grown', '.png']);
|
||||
child.stdout.on('data', function(chunk) {
|
||||
var buffer = ''+chunk;
|
||||
// Check the PNG magic number.
|
||||
chunk[0].should.equal(0x89);
|
||||
chunk[1].should.equal(0x50);
|
||||
chunk[2].should.equal(0x4e);
|
||||
chunk[3].should.equal(0x47);
|
||||
chunk[4].should.equal(0x0d);
|
||||
chunk[5].should.equal(0x0a);
|
||||
chunk[6].should.equal(0x1a);
|
||||
chunk[7].should.equal(0x0a);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('the server', function() {
|
||||
var port = '1111';
|
||||
var server;
|
||||
|
||||
// Start running the server.
|
||||
it('should start', function(done) {
|
||||
server = cproc.spawn('node', ['ass-stubs/server-test.js', port]);
|
||||
server.stdout.on('data', function(data) { done(); });
|
||||
server.stderr.on('data', function(data) { console.log(''+data); });
|
||||
});
|
||||
|
||||
it('should produce colorscheme badges', function(done) {
|
||||
http.get('http://127.0.0.1:' + port + '/:fruit-apple-green.svg',
|
||||
function(res) {
|
||||
var buffer = '';
|
||||
res.on('data', function(chunk) { buffer += ''+chunk; });
|
||||
res.on('end', function() {
|
||||
buffer.should.startWith('<svg');
|
||||
buffer.should.containEql('fruit');
|
||||
buffer.should.containEql('apple');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should shut down', function(done) {
|
||||
server.kill();
|
||||
server.on('exit', function() { done(); });
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('the ass wrap-up', function() {
|
||||
it('should write coverage', function(done) {
|
||||
ass.report('html', function(err, r) {
|
||||
fs.writeFileSync('web/coverage.html', r);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user