Skip to content

Commit

Permalink
updated some cli tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brentlintner committed Jun 14, 2011
1 parent 8228943 commit 7a8812c
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions test/cli.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,51 @@
var sys = require('sys'),
fs = require('fs'),
path = require('path'),
cli = require('./../lib/cli'),
hint = require('./../lib/hint');

describe("cli", function () {

beforeEach(function () {
spyOn(hint, "hint");
});

it("interprets --help with no args", function () {
var txt = require('fs').readFileSync(__dirname + "/../HELP", "utf-8");

spyOn(sys, "print");

cli.interpret(["node", "file.js"]);

cli.interpret(["node", "hint"]);
expect(sys.print.mostRecentCall.args[0]).toEqual(txt);
});

it("interprets --help", function () {
var txt = require('fs').readFileSync(__dirname + "/../HELP", "utf-8");

spyOn(sys, "print");

cli.interpret(["node", "file.js", "--help"]);

cli.interpret(["node", "hint", "file.js", "--help"]);
expect(sys.print.mostRecentCall.args[0]).toEqual(txt);
});

it("interprets --config", function () {
var config = {};

spyOn(fs, "readFileSync").andReturn("data");
spyOn(path, "existsSync").andCallFake(function (path) {
return path.match(/file\.json$/) ? true : false;
});
spyOn(JSON, "parse").andReturn(config);
spyOn(sys, "print");

cli.interpret(["node", "file.js", "file.js", "--config", "file.json"]);
cli.interpret(["node", "hint", "file2.js", "file.js", "--config", "file.json"]);

expect(fs.readFileSync).toHaveBeenCalledWith("file.json", "utf-8");
expect(JSON.parse).toHaveBeenCalledWith("data");
expect(hint.hint.mostRecentCall.args[0]).toContain("file.js");
expect(hint.hint.mostRecentCall.args[0]).toContain("file2.js");
expect(hint.hint.mostRecentCall.args[1]).toEqual(config);
});

it("interprets --reporter", function () {
var reporter = require("./../example/reporter").reporter;

spyOn(process, "cwd").andReturn(__dirname + "/../");

cli.interpret(["node", "file.js", "file.js", "--reporter", "example/reporter.js"]);

cli.interpret(["node", "hint", "file.js", "file.js", "--reporter", "example/reporter.js"]);
expect(hint.hint.mostRecentCall.args[2]).toEqual(reporter);
});

Expand All @@ -59,7 +54,7 @@ describe("cli", function () {
path = require('path');

spyOn(fs, "readFileSync").andReturn(JSON.stringify(config));
cli.interpret(["node", "file.js", "file.js"]);
cli.interpret(["node", "hint", "file.js", "file.js"]);
expect(fs.readFileSync).toHaveBeenCalledWith(path.join(process.env.HOME, '.jshintrc'), "utf-8");
});

Expand All @@ -68,9 +63,11 @@ describe("cli", function () {
path = require('path');

spyOn(fs, "readFileSync").andReturn(JSON.stringify(config));
spyOn(path, "existsSync").andReturn(true);
spyOn(path, "existsSync").andCallFake(function (path) {
return path.match(/\.jshintrc/) ? true : false;
});

cli.interpret(["node", "file.js", "file.js"]);
cli.interpret(["node", "hint", "file.js", "file2.js"]);

expect(fs.readFileSync.argsForCall[0]).toEqual([path.join(process.env.HOME, '.jshintrc'), "utf-8"]);
expect(fs.readFileSync.argsForCall[1]).toEqual([path.join(process.cwd(), '.jshintrc'), "utf-8"]);
Expand All @@ -79,7 +76,7 @@ describe("cli", function () {
it("overrides options from the $HOME .jshintrc file with options from the cwd .jshintrc file", function () {
var config = '{"evil": true,"predef":["Monkeys","Elephants"]}';
fs.writeFileSync('.jshintrc', config, "utf-8");
cli.interpret(["node", "file.js", "file.js"]);
cli.interpret(["node", "hint", "file.js", "file2.js"]);
expect(hint.hint.mostRecentCall.args[1].predef).toContain("Monkeys");
expect(hint.hint.mostRecentCall.args[1].predef).toContain("Elephants");
expect(hint.hint.mostRecentCall.args[1].evil).toEqual(true);
Expand All @@ -88,21 +85,15 @@ describe("cli", function () {

it("interprets --version and logs the current package version", function () {
var data = {version: 1};

spyOn(sys, "print");
spyOn(fs, "readFileSync").andReturn(JSON.stringify(data));

cli.interpret(["node", "file.js", "--version"]);

expect(sys.print.mostRecentCall.args[0]).toEqual(data.version + "\n");
});

it("interprets --jslint-reporter and uses the jslint xml reporter", function () {
var reporter = require("./../lib/jslint_reporter").reporter;

cli.interpret(["node", "file.js", "file.js", "--jslint-reporter"]);

expect(hint.hint.mostRecentCall.args[2]).toEqual(reporter);
});

});

0 comments on commit 7a8812c

Please sign in to comment.