Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Move status colouring to cli.

  • Loading branch information...
commit 2d9b1f58f0da54f15ee73082ad2aa15d5b690702 1 parent 1efff4b
@cliffano authored
View
2  CHANGELOG.md
@@ -1,5 +1,5 @@
### 0.1.1-pre
-*
+* Move status colouring to cli so that when lib/jenkins is used programatically then it gets plain uncoloured text
### 0.1.0
* Move commands setup to conf/commands.json, cli handling to bag.cli.command
View
12 lib/cli.js
@@ -1,6 +1,14 @@
/*jshint esnext: true */
+const COLORS = {
+ OK: 'green',
+ ABORTED: 'grey',
+ FAIL: 'red',
+ WARN: 'yellow'
+};
+
var _ = require('underscore'),
bag = require('bagofholding'),
+ colors = require('colors'),
irc = require('./irc'),
Jenkins = require('./jenkins'),
jenkins = new Jenkins(process.env.JENKINS_URL);
@@ -54,7 +62,7 @@ function _dashboard() {
console.log('Jobless Jenkins');
} else {
result.forEach(function (job) {
- console.log('%s - %s', job.status, job.name);
+ console.log('%s - %s', job.status[COLORS[job.status] || 'grey'], job.name);
});
}
}));
@@ -90,7 +98,7 @@ function _executor() {
function _job(name) {
jenkins.job(name, bag.cli.exitCb(null, function (result) {
- console.log('%s | %s', name, result.status);
+ console.log('%s | %s', name, result.status[COLORS[result.status] || 'grey']);
result.reports.forEach(function (report) {
console.log(' - %s', report);
});
View
19 lib/jenkins.js
@@ -2,7 +2,6 @@
var _ = require('underscore'),
async = require('async'),
bag = require('bagofholding'),
- colors = require('colors'),
dgram = require('dgram'),
request = require('request'),
xml2js = require('xml2js');
@@ -327,22 +326,20 @@ Jenkins.prototype.version = function (cb) {
Jenkins.prototype._status = function (color) {
- var colors = {
- 'blue': 'OK',
- 'green': 'OK',
- 'grey': 'ABORTED',
- 'red': 'FAIL',
- 'yellow': 'WARN'
- },
- status;
+ const STATUS = {
+ blue: 'OK',
+ green: 'OK',
+ grey: 'ABORTED',
+ red: 'FAIL',
+ yellow: 'WARN'
+ };
// Jenkins color value can contain either a color, color_anime, or status in job.color field,
// hence to get color/status value out of the mix we need to remove the postfix _anime,
// _anime postfix only exists on a job currently being built
color = color.replace(/_anime/, '');
- status = (colors[color]) ? (colors[color]) : color.toUpperCase();
- return status[color] || status.grey;
+ return (STATUS[color]) ? (STATUS[color]) : color.toUpperCase();
};
module.exports = Jenkins;
View
21 test/cli.js
@@ -191,13 +191,13 @@ buster.testCase('cli - dashboard', {
});
},
'should log jobs status and name when exec dashboard is called and Jenkins result has jobs': function () {
- this.mockConsole.expects('log').once().withExactArgs('%s - %s', 'OK', 'job1');
- this.mockConsole.expects('log').once().withExactArgs('%s - %s', 'FAIL', 'job2');
+ this.mockConsole.expects('log').once().withExactArgs('%s - %s', 'OK'.green, 'job1');
+ this.mockConsole.expects('log').once().withExactArgs('%s - %s', 'UNKNOWN'.grey, 'job2');
this.mockProcess.expects('exit').once().withExactArgs(0);
this.stub(Jenkins.prototype, 'dashboard', function (cb) {
cb(null, [
{ status: 'OK', name: 'job1' },
- { status: 'FAIL', name: 'job2' }
+ { status: 'UNKNOWN', name: 'job2' }
]);
});
cli.exec();
@@ -339,7 +339,7 @@ buster.testCase('cli - job', {
});
},
'should log job name, status, and reports when job exists': function () {
- this.mockConsole.expects('log').once().withExactArgs('%s | %s', 'job1', 'OK');
+ this.mockConsole.expects('log').once().withExactArgs('%s | %s', 'job1', 'OK'.green);
this.mockConsole.expects('log').once().withExactArgs(' - %s', 'Coverage 100%');
this.mockConsole.expects('log').once().withExactArgs(' - %s', 'All good!');
this.mockProcess.expects('exit').once().withExactArgs(0);
@@ -352,6 +352,19 @@ buster.testCase('cli - job', {
});
cli.exec();
},
+ 'should use grey colour when job status is unknown': function () {
+ this.mockConsole.expects('log').once().withExactArgs('%s | %s', 'job1', 'UNKNOWN'.grey);
+ this.mockConsole.expects('log').once().withExactArgs(' - %s', 'Coverage 100%');
+ this.mockConsole.expects('log').once().withExactArgs(' - %s', 'All good!');
+ this.mockProcess.expects('exit').once().withExactArgs(0);
+ this.stub(Jenkins.prototype, 'job', function (name, cb) {
+ cb(null, {
+ status: 'UNKNOWN',
+ reports: ['Coverage 100%', 'All good!']
+ });
+ });
+ cli.exec();
+ },
'should log job not found error when job does not exist': function () {
this.mockConsole.expects('error').once().withExactArgs('someerror');
this.mockProcess.expects('exit').once().withExactArgs(1);
View
35 test/jenkins.js
@@ -23,6 +23,7 @@ buster.testCase('jenkins - jenkins', {
assert.equals(opts.proxy, undefined);
opts.handlers['401']({ statusCode: 401 }, cb);
};
+ this.stub(process, 'env', {}); // simulate no http_proxy environment variable
this.stub(bag, 'http', { request: mockRequest });
var jenkins = new Jenkins();
jenkins.build('job1', undefined, function (err, result) {
@@ -331,15 +332,15 @@ buster.testCase('jenkins - dashboard', {
assert.isNull(err);
assert.equals(result.length, 5);
assert.equals(result[0].name, 'job1');
- assert.equals(result[0].status, 'OK'.blue);
+ assert.equals(result[0].status, 'OK');
assert.equals(result[1].name, 'job2');
- assert.equals(result[1].status, 'OK'.green);
+ assert.equals(result[1].status, 'OK');
assert.equals(result[2].name, 'job3');
- assert.equals(result[2].status, 'ABORTED'.grey);
+ assert.equals(result[2].status, 'ABORTED');
assert.equals(result[3].name, 'job4');
- assert.equals(result[3].status, 'FAIL'.red);
+ assert.equals(result[3].status, 'FAIL');
assert.equals(result[4].name, 'job5');
- assert.equals(result[4].status, 'WARN'.yellow);
+ assert.equals(result[4].status, 'WARN');
done();
});
},
@@ -360,9 +361,9 @@ buster.testCase('jenkins - dashboard', {
assert.isNull(err);
assert.equals(result.length, 2);
assert.equals(result[0].name, 'job1');
- assert.equals(result[0].status, 'ABORTED'.grey);
+ assert.equals(result[0].status, 'ABORTED');
assert.equals(result[1].name, 'job2');
- assert.equals(result[1].status, 'FAIL'.red);
+ assert.equals(result[1].status, 'FAIL');
done();
});
},
@@ -382,7 +383,7 @@ buster.testCase('jenkins - dashboard', {
assert.isNull(err);
assert.equals(result.length, 1);
assert.equals(result[0].name, 'job1');
- assert.equals(result[0].status, 'DISABLED'.grey);
+ assert.equals(result[0].status, 'DISABLED');
done();
});
}
@@ -541,7 +542,7 @@ buster.testCase('jenkins - job', {
var jenkins = new Jenkins('http://localhost:8080');
jenkins.job('job1', function (err, result) {
assert.isNull(err);
- assert.equals(result.status, 'OK'.blue);
+ assert.equals(result.status, 'OK');
assert.equals(result.reports[0], 'Coverage is 100%');
assert.equals(result.reports[1], 'All system is go!');
done();
@@ -637,18 +638,18 @@ buster.testCase('jenkins - version', {
buster.testCase('jenkins - _status', {
'should show the correct status for all supported colors': function () {
var jenkins = new Jenkins();
- assert.equals(jenkins._status('blue'), 'OK'.blue);
- assert.equals(jenkins._status('green'), 'OK'.green);
- assert.equals(jenkins._status('grey'), 'ABORTED'.grey);
- assert.equals(jenkins._status('red'), 'FAIL'.red);
- assert.equals(jenkins._status('yellow'), 'WARN'.yellow);
+ assert.equals(jenkins._status('blue'), 'OK');
+ assert.equals(jenkins._status('green'), 'OK');
+ assert.equals(jenkins._status('grey'), 'ABORTED');
+ assert.equals(jenkins._status('red'), 'FAIL');
+ assert.equals(jenkins._status('yellow'), 'WARN');
},
'should show the correct status for actively running build': function () {
var jenkins = new Jenkins();
- assert.equals(jenkins._status('blue_anime'), 'OK'.blue);
+ assert.equals(jenkins._status('blue_anime'), 'OK');
},
- 'should use grey color when status is unsupported': function () {
+ 'should uppercase status when it is unsupported': function () {
var jenkins = new Jenkins();
- assert.equals(jenkins._status('unknown'), 'UNKNOWN'.grey);
+ assert.equals(jenkins._status('unknown'), 'UNKNOWN');
}
});
Please sign in to comment.
Something went wrong with that request. Please try again.