зеркало из https://github.com/twbs/bootlint.git
Begin reworking tests based on @hnrch02's tips
Begin reworking tests based on @hnrch02's tips
This commit is contained in:
Родитель
e1a9ed39f9
Коммит
12c21b23e1
|
@ -51,6 +51,8 @@
|
|||
"load-grunt-tasks": "^3.1.0",
|
||||
"nodeunit": "^0.9.0",
|
||||
"npm-shrinkwrap": "^200.1.0",
|
||||
"rewire": "^2.3.1",
|
||||
"sinon": "^1.12.2",
|
||||
"time-grunt": "^1.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
|
|
186
src/cli.js
186
src/cli.js
|
@ -10,104 +10,98 @@ var readFile = Deferred.promisify(require('fs').readFile);
|
|||
var glob = Deferred.promisify(require('glob'));
|
||||
var bootlint = require('./bootlint');
|
||||
|
||||
program
|
||||
.version(require('../package.json').version)
|
||||
.description('Lint the HTML of Bootstrap projects')
|
||||
.usage('[options] [files...]')
|
||||
.option('-d, --disable <IDs>', 'Comma-separated list of disabled lint problem IDs', function (val) {
|
||||
return val.split(',');
|
||||
})
|
||||
.parse(process.argv);
|
||||
|
||||
var disabledIds = program.disable === undefined ? [] : program.disable;
|
||||
var totalErrCount = 0;
|
||||
var totalFileCount = 0;
|
||||
var lintedFiles = [];
|
||||
|
||||
function buildReporter(origin) {
|
||||
return function (lint) {
|
||||
var lintId = (lint.id[0] === 'E') ? chalk.bgGreen.white(lint.id) : chalk.bgRed.white(lint.id);
|
||||
var output = false;
|
||||
if (lint.elements) {
|
||||
lint.elements.each(function (_, element) {
|
||||
var loc = element.startLocation;
|
||||
console.log(origin + ":" + (loc.line + 1) + ":" + (loc.column + 1), lintId, lint.message);
|
||||
totalErrCount++;
|
||||
output = true;
|
||||
});
|
||||
}
|
||||
if (!output) {
|
||||
console.log(origin + ":", lintId, lint.message);
|
||||
totalErrCount++;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
exports.buildReporter = buildReporter;
|
||||
|
||||
function handleStdin() {
|
||||
return new Deferred(function (resolve) {
|
||||
if (process.stdin.isTTY) {
|
||||
return resolve();
|
||||
}
|
||||
|
||||
var stdInput = [];
|
||||
|
||||
process.stdin.setEncoding('utf8');
|
||||
|
||||
process.stdin.on('data', function (chunk) {
|
||||
stdInput.push(chunk);
|
||||
});
|
||||
|
||||
process.stdin.on('end', function () {
|
||||
bootlint.lintHtml(stdInput.join(''), buildReporter('<stdin>'), disabledIds);
|
||||
totalFileCount++;
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
exports.handleStdin = handleStdin;
|
||||
|
||||
function handlePath(pattern) {
|
||||
return glob(pattern)
|
||||
.map(function (name) {
|
||||
return Deferred.props({
|
||||
contents: readFile(name, {encoding: 'utf8'}),
|
||||
name: name
|
||||
});
|
||||
module.exports = function () {
|
||||
program
|
||||
.version(require('../package.json').version)
|
||||
.description('Lint the HTML of Bootstrap projects')
|
||||
.usage('[options] [files...]')
|
||||
.option('-d, --disable <IDs>', 'Comma-separated list of disabled lint problem IDs', function (val) {
|
||||
return val.split(',');
|
||||
})
|
||||
.each(function (file) {
|
||||
bootlint.lintHtml(file.contents, buildReporter(file.name), disabledIds);
|
||||
totalFileCount++;
|
||||
return Deferred.resolve();
|
||||
.parse(process.argv);
|
||||
|
||||
var disabledIds = program.disable === undefined ? [] : program.disable;
|
||||
var totalErrCount = 0;
|
||||
var totalFileCount = 0;
|
||||
var lintedFiles = [];
|
||||
|
||||
function buildReporter(origin) {
|
||||
return function (lint) {
|
||||
var lintId = (lint.id[0] === 'E') ? chalk.bgGreen.white(lint.id) : chalk.bgRed.white(lint.id);
|
||||
var output = false;
|
||||
if (lint.elements) {
|
||||
lint.elements.each(function (_, element) {
|
||||
var loc = element.startLocation;
|
||||
console.log(origin + ":" + (loc.line + 1) + ":" + (loc.column + 1), lintId, lint.message);
|
||||
totalErrCount++;
|
||||
output = true;
|
||||
});
|
||||
}
|
||||
if (!output) {
|
||||
console.log(origin + ":", lintId, lint.message);
|
||||
totalErrCount++;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function handleStdin() {
|
||||
return new Deferred(function (resolve) {
|
||||
if (process.stdin.isTTY) {
|
||||
return resolve();
|
||||
}
|
||||
|
||||
var stdInput = [];
|
||||
|
||||
process.stdin.setEncoding('utf8');
|
||||
|
||||
process.stdin.on('data', function (chunk) {
|
||||
stdInput.push(chunk);
|
||||
});
|
||||
|
||||
process.stdin.on('end', function () {
|
||||
bootlint.lintHtml(stdInput.join(''), buildReporter('<stdin>'), disabledIds);
|
||||
totalFileCount++;
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
exports.handlePath = handlePath;
|
||||
|
||||
if (!program.args.length) {
|
||||
program.args.push('-');
|
||||
}
|
||||
|
||||
program.args.forEach(function (pattern) {
|
||||
lintedFiles.push(pattern === '-' ? handleStdin() : handlePath(pattern));
|
||||
});
|
||||
|
||||
exports.program = program;
|
||||
|
||||
Deferred.all(lintedFiles).then(function () {
|
||||
console.log("");
|
||||
|
||||
if (totalErrCount > 0) {
|
||||
console.log("For details, look up the lint problem IDs in the Bootlint wiki: https://github.com/twbs/bootlint/wiki");
|
||||
}
|
||||
|
||||
console.log("" + totalErrCount + " lint error(s) found across " + totalFileCount + " file(s).");
|
||||
|
||||
if (totalErrCount) {
|
||||
process.exit(1);
|
||||
function handlePath(pattern) {
|
||||
return glob(pattern)
|
||||
.map(function (name) {
|
||||
return Deferred.props({
|
||||
contents: readFile(name, {encoding: 'utf8'}),
|
||||
name: name
|
||||
});
|
||||
})
|
||||
.each(function (file) {
|
||||
bootlint.lintHtml(file.contents, buildReporter(file.name), disabledIds);
|
||||
totalFileCount++;
|
||||
return Deferred.resolve();
|
||||
});
|
||||
}
|
||||
}, function (err) {
|
||||
console.error(err.stack);
|
||||
});
|
||||
|
||||
if (!program.args.length) {
|
||||
program.args.push('-');
|
||||
}
|
||||
|
||||
program.args.forEach(function (pattern) {
|
||||
lintedFiles.push(pattern === '-' ? handleStdin() : handlePath(pattern));
|
||||
});
|
||||
|
||||
Deferred.all(lintedFiles).then(function () {
|
||||
console.log("");
|
||||
|
||||
if (totalErrCount > 0) {
|
||||
console.log("For details, look up the lint problem IDs in the Bootlint wiki: https://github.com/twbs/bootlint/wiki");
|
||||
}
|
||||
|
||||
console.log("" + totalErrCount + " lint error(s) found across " + totalFileCount + " file(s).");
|
||||
|
||||
if (totalErrCount) {
|
||||
process.exit(1);
|
||||
}
|
||||
}, function (err) {
|
||||
console.error(err.stack);
|
||||
});
|
||||
};
|
||||
|
|
109
test/cli_test.js
109
test/cli_test.js
|
@ -3,7 +3,9 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
var cli = (process.env.BOOTLINT_COV === '1') ? require('../src-cov/cli.js') : require('../src/cli.js');
|
||||
var sinon = require('sinon');
|
||||
var rewire = require('rewire');
|
||||
var cli = (process.env.BOOTLINT_COV === '1') ? rewire('../src-cov/cli.js') : rewire('../src/cli.js');
|
||||
|
||||
/*
|
||||
======== A Handy Little Nodeunit Reference ========
|
||||
|
@ -22,67 +24,62 @@ var cli = (process.env.BOOTLINT_COV === '1') ? require('../src-cov/cli.js') : re
|
|||
test.doesNotThrow(block, [error], [message])
|
||||
test.ifError(value)
|
||||
*/
|
||||
var startingDir = process.cwd();
|
||||
var oldTTY = null;
|
||||
|
||||
function rAfter() {
|
||||
if (process.stdout.write.restore) {
|
||||
process.stdout.write.restore();
|
||||
}
|
||||
|
||||
if (process.stderr.write.restore) {
|
||||
process.stderr.write.restore();
|
||||
}
|
||||
}
|
||||
|
||||
exports.bootlint = {
|
||||
setUp: function (done) {
|
||||
// setup here
|
||||
oldTTY = process.stdin.isTTY;
|
||||
|
||||
sinon.stub(process.stdout, 'write');
|
||||
sinon.stub(process.stderr, 'write');
|
||||
done();
|
||||
},
|
||||
'Program details': function (test) {
|
||||
test.expect(3);
|
||||
test.strictEqual(cli.program.version(), '0.11.0', 'should give the current Bootlint version.');
|
||||
test.strictEqual(cli.program.description(), 'Lint the HTML of Bootstrap projects', 'should provide a description of Bootlint.');
|
||||
test.strictEqual(cli.program.usage(), '[options] [files...]', 'should tell how to use Bootlint.');
|
||||
test.done();
|
||||
tearDown: function (done) {
|
||||
process.stdin.isTTY = oldTTY;
|
||||
process.chdir(startingDir);
|
||||
|
||||
// If stdin rewrites were not used, restore them here
|
||||
rAfter();
|
||||
done();
|
||||
},
|
||||
'Disabled options': function (test) {
|
||||
test.expect(5);
|
||||
test.strictEqual(cli.program.option('')._allowUnknownOption, false, 'Don\'t allow unknown command line options.');
|
||||
test.strictEqual(cli.program.option('')._name, 'grunt', 'should list the program\'s name.');
|
||||
test.strictEqual(cli.program.option('')._version, '0.11.0', 'should list the program\'s version.');
|
||||
test.strictEqual(cli.program.option('')._description, 'Lint the HTML of Bootstrap projects',
|
||||
'should provide a description of Bootlint.');
|
||||
test.strictEqual(cli.program.option('')._usage, '[options] [files...]', 'should provide Bootlint usage options.');
|
||||
test.done();
|
||||
},
|
||||
'Version flag': function (test) {
|
||||
test.expect(7);
|
||||
test.strictEqual(cli.program.option('').options[0].flags, '-V, --version', 'should list the version flag');
|
||||
test.strictEqual(cli.program.option('').options[0].required, 0, 'should not require the version flag.');
|
||||
test.strictEqual(cli.program.option('').options[0].optional, 0, 'should return whether the version flag is optional.');
|
||||
test.strictEqual(cli.program.option('').options[0].bool, true, 'should return whether the version flag is optional.');
|
||||
test.strictEqual(cli.program.option('').options[0].short, '-V', 'should list the short version flag');
|
||||
test.strictEqual(cli.program.option('').options[0].long, '--version', 'should list the long version flag');
|
||||
test.strictEqual(cli.program.option('').options[0].description, 'output the version number',
|
||||
'should provide a description of the version flag');
|
||||
test.done();
|
||||
},
|
||||
'Disabled flag': function (test) {
|
||||
test.expect(7);
|
||||
test.strictEqual(cli.program.option('').options[1].flags, '-d, --disable <IDs>', 'should list the disable flag');
|
||||
test.strictEqual(cli.program.option('').options[1].required, -15, 'should not require the disabled flag.');
|
||||
test.strictEqual(cli.program.option('').options[1].optional, 0, 'should return whether the version flag is optional.');
|
||||
test.strictEqual(cli.program.option('').options[1].bool, true, 'should return whether the version flag is optional.');
|
||||
test.strictEqual(cli.program.option('').options[1].short, '-d', 'should list the short disable flag');
|
||||
test.strictEqual(cli.program.option('').options[1].long, '--disable', 'should list the long disable flag');
|
||||
test.strictEqual(cli.program.option('').options[1].description, 'Comma-separated list of disabled lint problem IDs',
|
||||
'should provide a description of the disable flag');
|
||||
test.done();
|
||||
},
|
||||
'Handle standard input': function (test) {
|
||||
test.expect(6);
|
||||
test.strictEqual(cli.handleStdin('-d')._bitField, 268435456, 'Should calculate bitfield of argument');
|
||||
test.strictEqual(cli.handleStdin('-d')._fulfillmentHandler0, undefined, 'Should leave fulfillment handler as undefined.');
|
||||
test.strictEqual(cli.handleStdin('-d')._rejectionHandler0, undefined, 'Should leave rejection handler as undefined');
|
||||
test.strictEqual(cli.handleStdin('-d')._promise0, undefined, 'Should leave promise undefined.');
|
||||
test.strictEqual(cli.handleStdin('-d')._receiver0, undefined, 'Should leave receiver as undefined');
|
||||
test.strictEqual(cli.handleStdin('-d')._settledValue, undefined, 'Should leave settled value as undefined.');
|
||||
test.done();
|
||||
},
|
||||
'Build reporter': function (test) {
|
||||
test.expect(1);
|
||||
var reporter = cli.buildReporter('<stdin>');
|
||||
test.strictEqual(typeof reporter, 'function', 'Reporter should be a function');
|
||||
'Disable tags': function (test) {
|
||||
var i = 0;
|
||||
|
||||
sinon.stub(console, 'log', function (message) {
|
||||
switch (i) {
|
||||
case 0: {
|
||||
test.strictEqual(message, '');
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
test.strictEqual(message, '0 lint error(s) found across 0 file(s).');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
i++;
|
||||
});
|
||||
|
||||
cli.__set__('process', {
|
||||
argv: [''],
|
||||
stdin: {
|
||||
isTTY: process.stdin.isTTY
|
||||
}
|
||||
});
|
||||
|
||||
cli();
|
||||
|
||||
test.done();
|
||||
}
|
||||
};
|
||||
|
|
Загрузка…
Ссылка в новой задаче