Skip to content

Commit

Permalink
fixed tests, moved reporters into own sub-dir
Browse files Browse the repository at this point in the history
  • Loading branch information
brentlintner committed Aug 19, 2011
1 parent 12f327f commit 9a1c3c1
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 30 deletions.
4 changes: 2 additions & 2 deletions lib/cli.js
Expand Up @@ -73,11 +73,11 @@ module.exports = {
}

if (options["--jslint-reporter"]) {
customReporter = "./jslint_reporter.js";
customReporter = "./reporters/jslint_xml.js";
}

if (options["--show-non-errors"]) {
customReporter = "./non_error_reporter.js";
customReporter = "./reporters/non_error.js";
}

if (customConfig) {
Expand Down
27 changes: 7 additions & 20 deletions lib/hint.js
Expand Up @@ -2,6 +2,7 @@ var _fs = require('fs'),
_sys = require('sys'),
_path = require('path'),
_jshint = require('./../packages/jshint/jshint.js'),
_reporter = require('./reporters/default'),
_config;

function _lint(file, results, data) {
Expand All @@ -24,8 +25,11 @@ function _lint(file, results, data) {
}

lintdata = _jshint.JSHINT.data();
lintdata.file = file;
data.push(lintdata);

if (lintdata) {
lintdata.file = file;
data.push(lintdata);
}
}

function _collect(path, files) {
Expand All @@ -38,29 +42,12 @@ function _collect(path, files) {
}
}

function _reporter(results) {
var len = results.length,
str = '',
file, error;

results.forEach(function (result) {
file = result.file;
error = result.error;
str += file + ': line ' + error.line + ', col ' +
error.character + ', ' + error.reason + '\n';
});

_sys.puts(len > 0 ? (str + "\n" + len + ' error' + ((len === 1) ? '' : 's')) : "Lint Free!");
process.exit(len > 0 ? 1 : 0);
}

module.exports = {
hint: function (targets, config, reporter) {
var files = [],
results = [],
data = [];

if (!reporter) reporter = _reporter;
_config = config || null;

targets.forEach(function (target) {
Expand All @@ -71,6 +58,6 @@ module.exports = {
_lint(file, results, data);
});

reporter(results, data);
(reporter || _reporter)(results, data);
}
};
17 changes: 17 additions & 0 deletions lib/reporters/default.js
@@ -0,0 +1,17 @@
var _sys = require('sys');

module.exports = function (results, data) {
var len = results.length,
str = '',
file, error;

results.forEach(function (result) {
file = result.file;
error = result.error;
str += file + ': line ' + error.line + ', col ' +
error.character + ', ' + error.reason + '\n';
});

_sys.puts(len > 0 ? (str + "\n" + len + ' error' + ((len === 1) ? '' : 's')) : "Lint Free!");
process.exit(len > 0 ? 1 : 0);
};
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/non_error_reporter.js → lib/reporters/non_error.js
Expand Up @@ -45,4 +45,4 @@ module.exports =

process.exit(len > 0 ? 1 : 0);
}
};
};
2 changes: 1 addition & 1 deletion test/cli.js
Expand Up @@ -92,7 +92,7 @@ describe("cli", function () {
});

it("interprets --jslint-reporter and uses the jslint xml reporter", function () {
var reporter = require("./../lib/jslint_reporter").reporter;
var reporter = require("./../lib/reporters/jslint_xml").reporter;
cli.interpret(["node", "file.js", "file.js", "--jslint-reporter"]);
expect(hint.hint.mostRecentCall.args[2]).toEqual(reporter);
});
Expand Down
19 changes: 13 additions & 6 deletions test/hint.js
Expand Up @@ -4,6 +4,12 @@ var sys = require('sys'),
hint = require('./../lib/hint');

describe("hint", function () {
function mockJSHINT(success, data) {
spyOn(jshint, "JSHINT").andReturn(success);
jshint.JSHINT.data = function () {
return data;
};
}

beforeEach(function () {
spyOn(sys, "puts");
Expand All @@ -13,7 +19,7 @@ describe("hint", function () {
it("collects files", function () {
var targets = ["file1.js", "file2.js", ".hidden"];

spyOn(jshint, "JSHINT").andReturn(true);
mockJSHINT(true);
spyOn(fs, "readFileSync").andReturn("data");

spyOn(fs, "statSync").andReturn({
Expand All @@ -31,7 +37,7 @@ describe("hint", function () {
it("collects directory files", function () {
var targets = ["dir", "file2.js"];

spyOn(jshint, "JSHINT").andReturn(true);
mockJSHINT(true);

spyOn(fs, "readFileSync").andReturn("data");
spyOn(fs, "readdirSync").andReturn(["file2.js"]);
Expand Down Expand Up @@ -61,7 +67,7 @@ describe("hint", function () {
var targets = ["file1.js"],
config = {};

spyOn(jshint, "JSHINT").andReturn(true);
mockJSHINT(true);
spyOn(fs, "readFileSync").andReturn("data");

spyOn(fs, "statSync").andReturn({
Expand All @@ -78,7 +84,7 @@ describe("hint", function () {
config = null,
reporter = jasmine.createSpy("reporter");

spyOn(jshint, "JSHINT").andReturn(true);
mockJSHINT(true);
spyOn(fs, "readFileSync").andReturn("data");

spyOn(fs, "statSync").andReturn({
Expand All @@ -93,7 +99,7 @@ describe("hint", function () {
it("exits the process with a successful status code with no lint errors", function () {
var targets = ["file1.js"];

spyOn(jshint, "JSHINT").andReturn(true);
mockJSHINT(true);
spyOn(fs, "readFileSync").andReturn("data");

spyOn(fs, "statSync").andReturn({
Expand All @@ -116,7 +122,7 @@ describe("hint", function () {
}
}];

spyOn(jshint, "JSHINT").andReturn(false);
mockJSHINT(false);
jshint.JSHINT.errors = results;
spyOn(fs, "readFileSync").andReturn("data");

Expand All @@ -131,5 +137,6 @@ describe("hint", function () {

// TODO: handles jshint errors (will tighten custom reporter assertions)
// TODO: handles file open error
// TODO: handling of JSHINT.data()

});

0 comments on commit 9a1c3c1

Please sign in to comment.