Esempio n. 1
0
File: cli.js Progetto: Khan/jshint
	testPrereq: function (test) {
		sinon.stub(shjs, "cat")
			.withArgs(sinon.match(/file\.js$/)).returns("a();")
			.withArgs(sinon.match(/prereq.js$/)).returns("var a = 1;")
			.withArgs(sinon.match(/config.json$/))
				.returns("{\"undef\":true,\"prereq\":[\"prereq.js\", \"prereq2.js\"]}");

		sinon.stub(shjs, "test")
			.withArgs("-e", sinon.match(/file\.js$/)).returns(true)
			.withArgs("-e", sinon.match(/prereq.js$/)).returns(true)
			.withArgs("-e", sinon.match(/config.json$/)).returns(true);

		process.exit.restore();
		sinon.stub(process, "exit")
			.withArgs(0).returns(true)
			.withArgs(1).throws("ProcessExit");

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

		shjs.cat.restore();
		shjs.test.restore();

		test.done();
	},
Esempio n. 2
0
  testPrereqBothConfigAndCLIOption: function (test) {
    this.sinon.stub(shjs, "cat")
      .withArgs(sinon.match(/file\.js$/)).returns("a(); b();")
      .withArgs(sinon.match(/prereq.js$/)).returns("var a = 1;")
      .withArgs(sinon.match(/prereq2.js$/)).returns("var b = 2;")
      .withArgs(sinon.match(/config.json$/))
        .returns("{\"undef\":true,\"prereq\":[\"prereq.js\"]}");

    this.sinon.stub(shjs, "test")
      .withArgs("-e", sinon.match(/file\.js$/)).returns(true)
      .withArgs("-e", sinon.match(/prereq.js$/)).returns(true)
      .withArgs("-e", sinon.match(/prereq2.js$/)).returns(true)
      .withArgs("-e", sinon.match(/config.json$/)).returns(true);

    cli.exit.restore();
    this.sinon.stub(cli, "exit")
      .withArgs(0).returns(true)
      .withArgs(2).throws("ProcessExit");

    cli.interpret([
      "node", "jshint", "file.js",
      "--config", "config.json",
      "--prereq", "prereq2.js,prereq3.js"
    ]);

    shjs.cat.restore();
    shjs.test.restore();

    test.done();
  },
Esempio n. 3
0
File: cli.js Progetto: Khan/jshint
	testMalformedNpmFile: function (test) {
		sinon.stub(process, "cwd").returns(__dirname);
		var localNpm = path.normalize(__dirname + "/package.json");
		var localRc = path.normalize(__dirname + "/.jshintrc");
		var testStub = sinon.stub(shjs, "test");
		var catStub = sinon.stub(shjs, "cat");

		// stub rc file
		testStub.withArgs("-e", localRc).returns(true);
		catStub.withArgs(localRc).returns('{"evil": true}');

		// stub npm file
		testStub.withArgs("-e", localNpm).returns(true);
		catStub.withArgs(localNpm).returns('{'); // malformed package.json

		// stub src file
		testStub.withArgs("-e", sinon.match(/file\.js$/)).returns(true);
		catStub.withArgs(sinon.match(/file\.js$/)).returns("eval('a=2');");

		cli.interpret([
			"node", "jshint", "file.js"
		]);
		test.equal(process.exit.args[0][0], 0); // lint with wrong package.json

		shjs.test.restore();
		shjs.cat.restore();
		process.cwd.restore();
		test.done();
	},
Esempio n. 4
0
File: cli.js Progetto: Khan/jshint
	testOneLevelRcLookup: function (test) {
		var srcDir = __dirname + "../src/";
		var parentRc = path.join(srcDir, ".jshintrc");

		var cliDir = path.join(srcDir, "cli/");
		sinon.stub(process, "cwd").returns(cliDir);

		var testStub = sinon.stub(shjs, "test");
		var catStub = sinon.stub(shjs, "cat");

		// stub rc file
		testStub.withArgs("-e", parentRc).returns(true);
		catStub.withArgs(parentRc).returns('{"evil": true}');

		// stub src file
		testStub.withArgs("-e", sinon.match(/file\.js$/)).returns(true);
		catStub.withArgs(sinon.match(/file\.js$/)).returns("eval('a=2');");

		cli.interpret([
			"node", "jshint", "file.js"
		]);
		test.equal(process.exit.args[0][0], 0); // eval allowed = rc file found

		shjs.test.restore();
		shjs.cat.restore();
		process.cwd.restore();
		test.done();
	},
Esempio n. 5
0
File: cli.js Progetto: Khan/jshint
	testTargetRelativeRcLookup: function (test) {
		// working from outside the project
		sinon.stub(process, "cwd").returns(process.env.HOME || process.env.HOMEPATH);
		var projectRc = path.normalize(__dirname + "/.jshintrc");
		var srcFile = __dirname + "/sub/file.js";
		var testStub = sinon.stub(shjs, "test");
		var catStub = sinon.stub(shjs, "cat");

		// stub rc file
		testStub.withArgs("-e", projectRc).returns(true);
		catStub.withArgs(projectRc).returns('{"evil": true}');

		// stub src file
		testStub.withArgs("-e", srcFile).returns(true);
		catStub.withArgs(srcFile).returns("eval('a=2');");

		cli.interpret([
			"node", "jshint", srcFile
		]);
		test.equal(process.exit.args[0][0], 0); // eval allowed = rc file found

		shjs.test.restore();
		shjs.cat.restore();
		process.cwd.restore();
		test.done();
	},
Esempio n. 6
0
File: cli.js Progetto: Khan/jshint
	testExcludePath: function (test) {
		var run = sinon.stub(cli, "run");
		var dir = __dirname + "/../examples/";
		sinon.stub(process, "cwd").returns(dir);

		cli.interpret([
			"node", "jshint", "file.js", "--exclude-path=../examples/.customignore"
		]);

		test.equal(run.args[0][0].ignores[0], path.resolve(dir, "exclude.js"));

		run.restore();
		process.cwd.restore();

		sinon.stub(process, "cwd").returns(__dirname + "/../");
		sinon.stub(shjs, "cat")
			.withArgs(sinon.match(/file.js$/)).returns("console.log('Hello');")
			.withArgs(sinon.match(/\.jshintrc$/)).returns("{}")
			.withArgs(sinon.match(/\.jshintignore$/)).returns("examples");

		var args = shjs.cat.args.filter(function (arg) {
			return !/\.jshintrc$/.test(arg[0]) && !/\.jshintignore$/.test(arg[0]);
		});

		test.equal(args.length, 0);

		process.cwd.restore();
		shjs.cat.restore();
		test.done();
	},
Esempio n. 7
0
	testRcFile: function (test) {
		var run = sinon.stub(cli, "run");
		var dir = __dirname + "/../examples/";
		sinon.stub(process, "cwd").returns(dir);

		cli.interpret([
			"node", "jshint", "file.js"
		]);
		test.equal(run.args[0][0].config.strict, true);
		process.cwd.restore();

		var home = path.join(process.env.HOME, ".jshintrc");
		var conf = shjs.cat(path.join(dir, ".jshintrc"));
		sinon.stub(shjs, "test").withArgs("-e", home).returns(true);
		sinon.stub(shjs, "cat").withArgs(home).returns(conf);

		cli.interpret([
			"node", "jshint", "file.js"
		]);
		test.equal(run.args[1][0].config.strict, true);

		shjs.test.restore();
		shjs.cat.restore();
		run.restore();
		test.done();
	},
Esempio n. 8
0
File: cli.js Progetto: 5long/jshint
	testConfig: function (test) {
		var _cli = require("cli");
		var out = sinon.stub(_cli, "error");
		sinon.stub(cli, "run").returns(true);

		sinon.stub(shjs, "cat")
			.withArgs(sinon.match(/file\.js$/)).returns("var a = function () {}; a();")
			.withArgs(sinon.match(/file1\.json$/)).returns("wat")
			.withArgs(sinon.match(/file2\.json$/)).returns("{\"node\":true}");

		sinon.stub(shjs, "test")
			.withArgs("-e", sinon.match(/file\.js$/)).returns(true)
			.withArgs("-e", sinon.match(/file1\.json$/)).returns(true)
			.withArgs("-e", sinon.match(/file2\.json$/)).returns(true)
			.withArgs("-e", sinon.match(/file3\.json$/)).returns(false);

		process.exit.restore();
		sinon.stub(process, "exit").throws("ProcessExit");

		// File doesn't exist.
		try {
			cli.interpret([
				"node", "jshint", "file.js", "--config", "file3.json"
			]);
		} catch (err) {
			var msg = out.args[0][0];
			test.equal(msg.slice(0, 23), "Can't find config file:");
			test.equal(msg.slice(msg.length - 10), "file3.json");
			test.equal(err, "ProcessExit");
		}

		// Invalid config
		try {
			cli.interpret([
				"node", "jshint", "file.js", "--config", "file1.json"
			]);
		} catch (err) {
			var msg = out.args[1][0];
			test.equal(msg.slice(0, 24), "Can't parse config file:");
			test.equal(msg.slice(msg.length - 10), "file1.json");
			test.equal(err, "ProcessExit");
		}

		// Valid config
		process.exit.restore();
		sinon.stub(process, "exit");

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

		_cli.error.restore();
		cli.run.restore();
		shjs.cat.restore();
		shjs.test.restore();

		test.done();
	},
Esempio n. 9
0
File: cli.js Progetto: Khan/jshint
	testReporter: function (test) {
		test.expect(5);

		var _cli = require("cli");
		var rep = require("../examples/reporter.js");
		var run = sinon.stub(cli, "run");
		var out = sinon.stub(_cli, "error");
		var dir = __dirname + "/../examples/";
		sinon.stub(process, "cwd").returns(dir);

		process.exit.restore();
		sinon.stub(process, "exit").throws("ProcessExit");

		// Test failed attempt.
		try {
			cli.interpret([
				"node", "jshint", "file.js", "--reporter", "invalid.js"
			]);
		} catch (err) {
			var msg = out.args[0][0];
			test.equal(msg.slice(0, 25), "Can't load reporter file:");
			test.equal(msg.slice(msg.length - 10), "invalid.js");
			test.equal(err, "ProcessExit");
		}

		// Test successful attempt.
		run.restore();
		sinon.stub(rep, "reporter");
		sinon.stub(shjs, "test")
			.withArgs("-e", sinon.match(/file\.js$/)).returns(true)
			.withArgs("-e", sinon.match(/\.jshintrc$/)).returns(true)
			.withArgs("-e", sinon.match(/\.jshintignore$/)).returns(true);

		sinon.stub(shjs, "cat")
			.withArgs(sinon.match(/file\.js$/)).returns("func()")
			.withArgs(sinon.match(/\.jshintrc$/)).returns("{}")
			.withArgs(sinon.match(/\.jshintignore$/)).returns("");

		try {
			cli.interpret([
				"node", "jshint", "file.js", "--reporter", "reporter.js"
			]);
		} catch (err) {
			if (err.name !== "ProcessExit") {
				throw err;
			}

			test.equal(rep.reporter.args[0][0][0].error.raw, "Missing semicolon.");
			test.ok(rep.reporter.calledOnce);
		}

		rep.reporter.restore();
		shjs.test.restore();
		shjs.cat.restore();
		process.cwd.restore();
		test.done();
	},
Esempio n. 10
0
  testExtractWithIndentReportLocationMultipleFragments: function (test) {
    var rep = require("../examples/reporter.js");
    var errors = [];
    sinon.stub(rep, "reporter", function (res) {
      errors = errors.concat(res);
    });

    var dir = __dirname + "/../examples/";
    sinon.stub(process, "cwd").returns(dir);

    var html = [
      "<html>",
      "<script type='text/javascript'>",
      "  /* jshint indent: 2*/",
      "  var a = 1;",
      "    var b = 1", // misindented on purpose
      "</script>",
      "<p>nothing</p>",
      "<script type='text/javascript'>",
      "  /* jshint indent: 2*/",
      "      var a = 1", // misindented on purpose
      "</script>",
      "</html>"
    ].join("\n");

    sinon.stub(shjs, "cat")
      .withArgs(sinon.match(/indent\.html$/)).returns(html)
      .withArgs(sinon.match(/.\jshintignore$/)).returns("");

    sinon.stub(shjs, "test")
      .withArgs("-e", sinon.match(/indent\.html$/)).returns(true)
      .withArgs("-e", sinon.match(/\.jshintignore$/)).returns(false);

    cli.exit.restore();
    sinon.stub(cli, "exit");

    cli.interpret([
      "node", "jshint", "indent.html", "--extract", "always", "--reporter=reporter.js"
    ]);
    test.equal(cli.exit.args[0][0], 1);

    rep.reporter.restore();
    process.cwd.restore();
    shjs.cat.restore();
    shjs.test.restore();

    test.equal(errors.length, 2, "found two errors");

    test.equal(errors[0].error.line, 5, "first error line");
    test.equal(errors[0].error.character, 14, "first error column");

    test.equal(errors[1].error.line, 10, "second error line");
    test.equal(errors[1].error.character, 16, "second error column");

    test.done();
  }
Esempio n. 11
0
  firstLine: function (test) {
    var rep = require("../examples/reporter.js");
    var errors = [];
    sinon.stub(rep, "reporter", function (res) {
      errors = errors.concat(res);
    });

    var dir = __dirname + "/../examples/";
    sinon.stub(process, "cwd").returns(dir);

    var html = [
      "<script>",
      "  function a() {",
      "    return 5;",
      "  }",
      "</script>"
    ].join("\n");

    sinon.stub(shjs, "cat")
      .withArgs(sinon.match(/firstLine\.html$/)).returns(html)
      .withArgs(sinon.match(/\.jshintignore$/)).returns("")
      .withArgs(sinon.match(/\.jshintrc$/)).returns("{}");

    sinon.stub(shjs, "test")
      .withArgs("-e", sinon.match(/firstLine\.html$/)).returns(true)
      .withArgs("-e", sinon.match(/\.jshintignore$/)).returns(false)
      .withArgs("-e", sinon.match(/\.jshintrc$/)).returns(true);

    cli.exit.restore();
    sinon.stub(cli, "exit");

    cli.interpret([
      "node", "jshint", "firstLine.html", "--extract", "always", "--reporter=reporter.js"
    ]);
    test.equal(cli.exit.args[0][0], 0);

    rep.reporter.restore();
    process.cwd.restore();
    shjs.cat.restore();
    shjs.test.restore();

    test.equal(errors.length, 0, "found no errors");

    var js = [
      "",
      "function a() {",
      "  return 5;",
      "}",
      ""
    ].join("\n");

    test.equal(cli.extract(html, "auto"), js);

    test.done();
  },
Esempio n. 12
0
  testOverrides: function (test) {
    var dir = __dirname + "/../examples/";
    var rep = require("../examples/reporter.js");
    var config = {
      "asi": true,
      "overrides": {
        "bar.js$": {
          "asi": false
        }
      }
    };

    sinon.stub(process, "cwd").returns(dir);
    sinon.stub(rep, "reporter");
    sinon.stub(shjs, "cat")
      .withArgs(sinon.match(/foo\.js$/)).returns("a()")
      .withArgs(sinon.match(/bar\.js$/)).returns("a()")
      .withArgs(sinon.match(/config\.json$/))
        .returns(JSON.stringify(config));

    sinon.stub(shjs, "test")
      .withArgs("-e", sinon.match(/foo\.js$/)).returns(true)
      .withArgs("-e", sinon.match(/bar\.js$/)).returns(true)
      .withArgs("-e", sinon.match(/config\.json$/)).returns(true);

    cli.exit.restore();
    sinon.stub(cli, "exit")
      .withArgs(0).returns(true)
      .withArgs(1).throws("ProcessExit");

    // Test successful file
    cli.interpret([
      "node", "jshint", "foo.js", "--config", "config.json", "--reporter", "reporter.js"
    ]);
    test.ok(rep.reporter.args[0][0].length === 0);

    // Test overriden, failed file
    cli.interpret([
      "node", "jshint", "bar.js", "--config", "config.json", "--reporter", "reporter.js"
    ]);
    test.ok(rep.reporter.args[1][0].length > 0, "Error was expected but not thrown");
    test.equal(rep.reporter.args[1][0][0].error.code, "W033");

    process.cwd.restore();
    rep.reporter.restore();
    shjs.cat.restore();
    shjs.test.restore();

    test.done();
  },
Esempio n. 13
0
  testExtractWithIndentReportLocation: function (test) {
    var rep = require("../examples/reporter.js");
    var errors = [];
    sinon.stub(rep, "reporter", function (res) {
      errors = errors.concat(res);
    });

    var dir = __dirname + "/../examples/";
    sinon.stub(process, "cwd").returns(dir);

    var html = [
      "<html>",
      "<script type='text/javascript'>",
      "  /* jshint indent: 2*/",
      "  var a = 1;",
      "    var b = 1",
      "</script>",
      "</html>"
    ].join("\n");

    sinon.stub(shjs, "cat")
      .withArgs(sinon.match(/indent\.html$/)).returns(html)
      .withArgs(sinon.match(/.\jshintignore$/)).returns("");

    sinon.stub(shjs, "test")
      .withArgs("-e", sinon.match(/indent\.html$/)).returns(true)
      .withArgs("-e", sinon.match(/\.jshintignore$/)).returns(false);

    cli.exit.restore();
    sinon.stub(cli, "exit");

    cli.interpret([
      "node", "jshint", "indent.html", "--extract", "always", "--reporter=reporter.js"
    ]);
    test.equal(cli.exit.args[0][0], 1);

    rep.reporter.restore();
    process.cwd.restore();
    shjs.cat.restore();
    shjs.test.restore();

    test.equal(errors.length, 1, "found single error");
    var lintError = errors[0].error;
    test.ok(lintError, "have error object");
    test.equal(lintError.code, "W033", "found missing semicolon warning");
    test.equal(lintError.line, 5, "misaligned line");
    test.equal(lintError.character, 14, "first misaligned character at column 5");

    test.done();
  },
Esempio n. 14
0
  testFilenameOption: function (test) {
    var rep = require("../examples/reporter.js");
    var errors = [];
    sinon.stub(rep, "reporter", function (res) {
      errors = errors.concat(res);
    });

    var dir = __dirname + "/../examples/";
    sinon.stub(process, "cwd").returns(dir);

    var jshintrc = JSON.stringify({ undef: true });

    sinon.stub(shjs, "cat")
      .withArgs(sinon.match(/\/fake\/\.jshintrc$/)).returns(jshintrc)
      .withArgs(sinon.match(/\/\.jshintrc$/)).returns("")
      .withArgs(sinon.match(/\.jshintignore$/)).returns("");

    sinon.stub(shjs, "test")
      .withArgs("-e", sinon.match(/fake\/\.jshintrc$/)).returns(true)
      .withArgs("-e", sinon.match(/\.jshintrc$/)).returns(false);

    cli.exit.restore();
    sinon.stub(cli, "exit");

    cli.interpret([
      "node", "jshint", "--filename", "fake/fakescript.js", "--reporter=reporter.js", "-"
    ]);

    var bad = [
      'function returnUndef() {',
      '  return undefinedVariable;',
      '}',
      'returnUndef();'
    ];

    this.stdin.send(bad);
    this.stdin.end();

    test.equal(errors.length, 1, "should be a single error.");
    test.equal(cli.exit.args[0][0], 2, "status code should be 2 when there is a linting error.");
    rep.reporter.restore();
    process.cwd.restore();
    shjs.cat.restore();
    shjs.test.restore();
    test.done();
  },
Esempio n. 15
0
File: cli.js Progetto: Odie/jshint
	testGatherOptionalParameters: function (test) {
		sinon.stub(shjs, "test")
			.withArgs("-e", sinon.match(/.*/)).returns(true);

		sinon.stub(shjs, "cat")
			.withArgs(sinon.match(/\.jshintignore$/)).returns(path.join("ignore", "**"));

		var files = cli.gather({
			args: ["file.js"]
		});

		test.equal(files.length, 1);
		test.equal(files[0], "file.js");

		shjs.test.restore();
		shjs.cat.restore();
		test.done();
	},
Esempio n. 16
0
  testOverridesMatchesRelativePaths: function (test) {
    var dir = __dirname + "/../examples/";
    var rep = require("../examples/reporter.js");
    var config = {
      "asi": true,
      "overrides": {
        "src/bar.js": {
          "asi": false
        }
      }
    };

    sinon.stub(process, "cwd").returns(dir);
    sinon.stub(rep, "reporter");
    sinon.stub(shjs, "cat")
      .withArgs(sinon.match(/bar\.js$/)).returns("a()")
      .withArgs(sinon.match(/config\.json$/))
        .returns(JSON.stringify(config));

    sinon.stub(shjs, "test")
      .withArgs("-e", sinon.match(/bar\.js$/)).returns(true)
      .withArgs("-e", sinon.match(/config\.json$/)).returns(true);

    cli.exit.restore();
    sinon.stub(cli, "exit")
      .withArgs(0).returns(true)
      .withArgs(1).throws("ProcessExit");

    cli.interpret([
      "node", "jshint", "./src/bar.js", "--config", "config.json", "--reporter", "reporter.js"
    ]);
    test.ok(rep.reporter.args[0][0].length === 1);

    process.cwd.restore();
    rep.reporter.restore();
    shjs.cat.restore();
    shjs.test.restore();

    test.done();
  },
Esempio n. 17
0
	testRcLookup: function (test) {
		var run = sinon.stub(cli, "run");
		var srcDir = __dirname + "../src/";
		var confPath = path.join(srcDir, ".jshintrc");
		var cliDir = path.join(srcDir, "cli/");
		var conf = shjs.cat(path.join(__dirname + "/../examples/", ".jshintrc"));

		sinon.stub(process, "cwd").returns(cliDir);
		sinon.stub(shjs, "test").withArgs("-e", confPath).returns(true);
		sinon.stub(shjs, "cat").withArgs(confPath).returns(conf);

		cli.interpret([
			"node", "jshint", "file.js"
		]);
		test.equal(run.args[0][0].config.strict, true);

		shjs.test.restore();
		shjs.cat.restore();
		process.cwd.restore();
		run.restore();
		test.done();
	},
Esempio n. 18
0
File: cli.js Progetto: Khan/jshint
	testHomeRcFile: function (test) {
		var homeRc = path.join(process.env.HOME || process.env.HOMEPATH, ".jshintrc");
		var testStub = sinon.stub(shjs, "test");
		var catStub = sinon.stub(shjs, "cat");

		// stub rc file
		testStub.withArgs("-e", homeRc).returns(true);
		catStub.withArgs(homeRc).returns('{"evil": true}');

		// stub src file (in root where we are unlikely to find a .jshintrc)
		testStub.withArgs("-e", sinon.match(/\/file\.js$/)).returns(true);
		catStub.withArgs(sinon.match(/\/file\.js$/)).returns("eval('a=2');");

		cli.interpret([
			"node", "jshint", "/file.js"
		]);
		test.equal(process.exit.args[0][0], 0); // eval allowed = rc file found

		shjs.test.restore();
		shjs.cat.restore();
		test.done();
	},
Esempio n. 19
0
File: cli.js Progetto: Khan/jshint
	testStatusCode: function (test) {
		var rep = require("../examples/reporter.js");
		var dir = __dirname + "/../examples/";
		sinon.stub(rep, "reporter");
		sinon.stub(process, "cwd").returns(dir);

		sinon.stub(shjs, "test")
			.withArgs("-e", sinon.match(/(pass\.js|fail\.js|\.jshintrc|\.jshintignore)$/)).returns(true);

		sinon.stub(shjs, "cat")
			.withArgs(sinon.match(/pass\.js$/)).returns("function test() { return 0; }")
			.withArgs(sinon.match(/fail\.js$/)).returns("console.log('Hello')")
			.withArgs(sinon.match(/\.jshintrc$/)).returns("{}")
			.withArgs(sinon.match(/\.jshintignore$/)).returns("");

		process.exit.restore();
		sinon.stub(process, "exit");

		cli.interpret([
			"node", "jshint", "pass.js", "--reporter=reporter.js"
		]);

		cli.interpret([
			"node", "jshint", "fail.js", "--reporter=reporter.js"
		]);

		test.strictEqual(process.exit.args[0][0], 0);
		test.equal(process.exit.args[1][0], 2);

		rep.reporter.restore();
		process.cwd.restore();
		shjs.test.restore();
		shjs.cat.restore();

		test.done();
	},
Esempio n. 20
0
File: cli.js Progetto: Khan/jshint
	testRcFile: function (test) {
		sinon.stub(process, "cwd").returns(__dirname);
		var localRc = path.normalize(__dirname + "/.jshintrc");
		var testStub = sinon.stub(shjs, "test");
		var catStub = sinon.stub(shjs, "cat");

		// stub rc file
		testStub.withArgs("-e", localRc).returns(true);
		catStub.withArgs(localRc).returns('{"evil": true}');

		// stub src file
		testStub.withArgs("-e", sinon.match(/file\.js$/)).returns(true);
		catStub.withArgs(sinon.match(/file\.js$/)).returns("eval('a=2');");

		cli.interpret([
			"node", "jshint", "file.js"
		]);
		test.equal(process.exit.args[0][0], 0); // eval allowed = rc file found

		shjs.test.restore();
		shjs.cat.restore();
		process.cwd.restore();
		test.done();
	},
Esempio n. 21
0
File: cli.js Progetto: Khan/jshint
	testGather: function (test) {
		var dir = __dirname + "/../examples/";
		var files = [];
		sinon.stub(process, "cwd").returns(dir);

		var demoFiles = [
			[ /file2?\.js$/, "console.log('Hello');" ],
			[ /ignore[\/\\]file\d\.js$/, "console.log('Hello, ignore me');" ],
			[ /ignore[\/\\]dir[\/\\]file\d\.js$/, "print('Ignore me');" ],
			[ /node_script$/, "console.log('Hello, ignore me');" ],
			[ /\.jshintrc$/, "{}" ],
			[ /\.jshintignore$/, path.join("ignore", "**") ],
		];

		var testStub = sinon.stub(shjs, "test");
		demoFiles.forEach(function (file) {
			testStub = testStub.withArgs("-e", sinon.match(file[0])).returns(true);
		});

		var catStub = sinon.stub(shjs, "cat");
		demoFiles.forEach(function (file) {
			catStub = catStub.withArgs(sinon.match(file[0])).returns(file[1]);
		});

		files = cli.gather({
			args: ["file.js", "file2.js", "node_script",
				path.join("ignore", "file1.js"),
				path.join("ignore", "file2.js"),
				path.join("ignore", "dir", "file1.js")
			],
			ignores: [path.join("ignore", "**")],
			extensions: ""
		});

		var args = shjs.cat.args.filter(function (arg) {
			return !/\.jshintrc$/.test(arg[0]) && !/\.jshintignore$/.test(arg[0]);
		});

		test.equal(args.length, 0);
		test.equal(files.length, 3);
		test.equal(files[0], "file.js");
		test.equal(files[1], "file2.js");
		test.equal(files[2], "node_script");

		shjs.test.restore();
		shjs.cat.restore();

		demoFiles = [
			[ /file2?\.js$/, "console.log('Hello');" ],
			[ /file3\.json$/, "{}" ],
			[ /src[\/\\]file4\.js$/, "print('Hello');" ],
			[ /src[\/\\]lib[\/\\]file5\.js$/, "print('Hello'); "],
			[ /\.jshintrc$/, "{}" ],
			[ /\.jshintignore$/, "" ]
		];

		testStub = sinon.stub(shjs, "test");
		demoFiles.forEach(function (file) {
			testStub = testStub.withArgs("-e", sinon.match(file[0])).returns(true);
		});

		testStub = testStub
			.withArgs("-e", sinon.match(/src$/)).returns(true)
			.withArgs("-e", sinon.match(/src[\/\\]lib$/)).returns(true)
			.withArgs("-d", sinon.match(/src$/)).returns(true)
			.withArgs("-d", sinon.match(/src[\/\\]lib$/)).returns(true);

		sinon.stub(shjs, "ls")
			.withArgs(sinon.match(/src$/)).returns(["lib", "file4.js"])
			.withArgs(sinon.match(/src[\/\\]lib$/)).returns(["file5.js"]);

		catStub = sinon.stub(shjs, "cat");
		demoFiles.forEach(function (file) {
			catStub = catStub.withArgs(sinon.match(file[0])).returns(file[1]);
		});

		cli.interpret([
			"node", "jshint", "file.js", "file2.js", "file3.json", "--extra-ext=json", "src"
		]);

		files = cli.gather({
			args: ["file.js", "file2.js", "file3.json", "src"],
			extensions: "json",
			ignores: []
		});

		args = shjs.cat.args.filter(function (arg) {
			return !/\.jshintrc$/.test(arg[0]) && !/\.jshintignore$/.test(arg[0]);
		});

		test.equal(args.length, 5);
		test.equal(files.length, 5);
		test.equal(files[0], "file.js");
		test.equal(files[1], "file2.js");
		test.equal(files[2], "file3.json");
		test.equal(files[3], path.join("src", "lib", "file5.js"));
		test.equal(files[4], path.join("src", "file4.js"));

		shjs.test.restore();
		shjs.ls.restore();
		shjs.cat.restore();
		process.cwd.restore();

		sinon.stub(process, "cwd").returns(__dirname + "/../");
		sinon.stub(shjs, "cat")
			.withArgs(sinon.match(/reporter\.js$/)).returns("console.log('Hello');")
			.withArgs(sinon.match(/\.jshintrc$/)).returns("{}")
			.withArgs(sinon.match(/\.jshintignore$/)).returns("");

		files = cli.gather({
			args: ["examples"],
			extensions: "json",
			ignores: []
		});

		args = shjs.cat.args.filter(function (arg) {
			return !/\.jshintrc$/.test(arg[0]) && !/\.jshintignore$/.test(arg[0]);
		});

		test.equal(args.length, 0);
		test.equal(files.length, 1);
		test.equal(files[0], path.join("examples", "reporter.js"));

		shjs.cat.restore();
		process.cwd.restore();
		test.done();
	},
Esempio n. 22
0
File: cli.js Progetto: Khan/jshint
	testCollectFiles: function (test) {
		var gather = sinon.stub(cli, "gather");
		var args = [];

		gather.returns([]);

		sinon.stub(shjs, "test")
			.withArgs("-e", sinon.match(/.*/)).returns(true);

		sinon.stub(shjs, "cat")
			.withArgs(sinon.match(/file2?\.js$/)).returns("console.log('Hello');")
			.withArgs(sinon.match(/ignore[\/\\]file\d\.js$/)).returns("console.log('Hello, ignore me');")
			.withArgs(sinon.match(/ignore[\/\\]dir[\/\\]file\d\.js$/)).returns("print('Ignore me');")
			.withArgs(sinon.match(/node_script$/)).returns("console.log('Hello, ignore me');")
			.withArgs(sinon.match(/\.jshintrc$/)).returns("{}")
			.withArgs(sinon.match(/\.jshintignore$/)).returns(path.join("ignore", "**"));

		cli.interpret([
			"node", "jshint", "file.js", "file2.js", "node_script", path.join("ignore", "file1.js"),
			path.join("ignore", "file2.js"), path.join("ignore", "dir", "file1.js")
		]);

		args = gather.args[0][0];

		test.equal(args.args[0], "file.js");
		test.equal(args.args[1], "file2.js");
		test.equal(args.args[2], "node_script");
		test.equal(args.args[3], path.join("ignore", "file1.js"));
		test.equal(args.args[4], path.join("ignore", "file2.js"));
		test.equal(args.args[5], path.join("ignore", "dir", "file1.js"));
		test.equal(args.ignores, path.resolve(path.join("ignore", "**")));

		shjs.test.restore();
		shjs.cat.restore();

		sinon.stub(shjs, "test")
			.withArgs("-e", sinon.match(/.*/)).returns(true)
			.withArgs("-d", sinon.match(/src$/)).returns(true)
			.withArgs("-d", sinon.match(/src[\/\\]lib$/)).returns(true);

		sinon.stub(shjs, "ls")
			.withArgs(sinon.match(/src$/)).returns(["lib", "file4.js"])
			.withArgs(sinon.match(/src[\/\\]lib$/)).returns(["file5.js"]);

		sinon.stub(shjs, "cat")
			.withArgs(sinon.match(/file2?\.js$/)).returns("console.log('Hello');")
			.withArgs(sinon.match(/file3\.json$/)).returns("{}")
			.withArgs(sinon.match(/src[\/\\]file4\.js$/)).returns("print('Hello');")
			.withArgs(sinon.match(/src[\/\\]lib[\/\\]file5\.js$/)).returns("print('Hello');")
			.withArgs(sinon.match(/\.jshintrc$/)).returns("{}")
			.withArgs(sinon.match(/\.jshintignore$/)).returns("");

		cli.interpret([
			"node", "jshint", "file.js", "file2.js", "file3.json", "--extra-ext=json", "src"
		]);

		args = gather.args[1][0];

		test.equal(args.args.length, 4);
		test.equal(args.args[0], "file.js");
		test.equal(args.args[1], "file2.js");
		test.equal(args.args[2], "file3.json");
		test.equal(args.args[3], "src");
		test.equal(args.ignores, false);

		shjs.test.restore();
		shjs.ls.restore();
		shjs.cat.restore();

		sinon.stub(shjs, "cat")
			.withArgs(sinon.match(/reporter\.js$/)).returns("console.log('Hello');")
			.withArgs(sinon.match(/\.jshintrc$/)).returns("{}")
			.withArgs(sinon.match(/\.jshintignore$/)).returns("");

		cli.interpret([
			"node", "jshint", "examples"
		]);

		args = gather.args[2][0];

		test.equal(args.args.length, 1);
		test.equal(args.args[0], "examples");
		test.equal(args.ignores.length, 0);

		shjs.cat.restore();

		gather.restore();
		test.done();
	},
Esempio n. 23
0
	testCollectFiles: function (test) {
		var dir = __dirname + "/../examples/";
		sinon.stub(process, "cwd").returns(dir);

		sinon.stub(shjs, "test")
			.withArgs("-e", sinon.match(/.*/)).returns(true);

		sinon.stub(shjs, "cat")
			.withArgs(sinon.match(/file2?\.js$/)).returns("console.log('Hello');")
			.withArgs(sinon.match(/ignore[\/\\]file\d\.js$/)).returns("console.log('Hello, ignore me');")
			.withArgs(sinon.match(/ignore[\/\\]dir[\/\\]file\d\.js$/)).returns("print('Ignore me');")
			.withArgs(sinon.match(/node_script$/)).returns("console.log('Hello, ignore me');")
			.withArgs(sinon.match(/\.jshintrc$/)).returns("{}")
			.withArgs(sinon.match(/\.jshintignore$/)).returns(path.join("ignore", "**"));

		cli.interpret([
			"node", "jshint", "file.js", "file2.js", "node_script", path.join("ignore", "file1.js"),
			path.join("ignore", "file2.js"), path.join("ignore", "dir", "file1.js")
		]);

		var args = shjs.cat.args.filter(function (arg) {
			return !/\.jshintrc$/.test(arg[0]) && !/\.jshintignore$/.test(arg[0]);
		});

		test.equal(args.length, 3);
		test.equal(args[0][0], "file.js");
		test.equal(args[1][0], "file2.js");
		test.equal(args[2][0], "node_script");

		shjs.test.restore();
		shjs.cat.restore();

		sinon.stub(shjs, "test")
			.withArgs("-e", sinon.match(/.*/)).returns(true)
			.withArgs("-d", sinon.match(/src$/)).returns(true)
			.withArgs("-d", sinon.match(/src[\/\\]lib$/)).returns(true);

		sinon.stub(shjs, "ls")
			.withArgs(sinon.match(/src$/)).returns(["lib", "file4.js"])
			.withArgs(sinon.match(/src[\/\\]lib$/)).returns(["file5.js"]);

		sinon.stub(shjs, "cat")
			.withArgs(sinon.match(/file2?\.js$/)).returns("console.log('Hello');")
			.withArgs(sinon.match(/file3\.json$/)).returns("{}")
			.withArgs(sinon.match(/src[\/\\]file4\.js$/)).returns("print('Hello');")
			.withArgs(sinon.match(/src[\/\\]lib[\/\\]file5\.js$/)).returns("print('Hello');")
			.withArgs(sinon.match(/\.jshintrc$/)).returns("{}")
			.withArgs(sinon.match(/\.jshintignore$/)).returns("");

		cli.interpret([
			"node", "jshint", "file.js", "file2.js", "file3.json", "--extra-ext=json", "src"
		]);

		args = shjs.cat.args.filter(function (arg) {
			return !/\.jshintrc$/.test(arg[0]) && !/\.jshintignore$/.test(arg[0]);
		});

		test.equal(args.length, 5);
		test.equal(args[0][0], "file.js");
		test.equal(args[1][0], "file2.js");
		test.equal(args[2][0], "file3.json");
		test.equal(args[3][0], path.join("src", "lib", "file5.js"));
		test.equal(args[4][0], path.join("src", "file4.js"));

		shjs.test.restore();
		shjs.ls.restore();
		shjs.cat.restore();
		process.cwd.restore();

		sinon.stub(process, "cwd").returns(__dirname + "/../");
		sinon.stub(shjs, "cat")
			.withArgs(sinon.match(/reporter\.js$/)).returns("console.log('Hello');")
			.withArgs(sinon.match(/\.jshintrc$/)).returns("{}")
			.withArgs(sinon.match(/\.jshintignore$/)).returns("");

		cli.interpret([
			"node", "jshint", "examples"
		]);

		args = shjs.cat.args.filter(function (arg) {
			return !/\.jshintrc$/.test(arg[0]) && !/\.jshintignore$/.test(arg[0]);
		});

		test.equal(args.length, 1);
		test.equal(args[0][0], path.join("examples", "reporter.js"));

		shjs.cat.restore();
		process.cwd.restore();
		test.done();
	},
Esempio n. 24
0
  testConfig: function (test) {
    var _cli = require("cli");
    var out = sinon.stub(_cli, "error");
    sinon.stub(cli, "run").returns(true);

    sinon.stub(shjs, "cat")
      .withArgs(sinon.match(/file\.js$/)).returns("var a = function () {}; a();")
      .withArgs(sinon.match(/file1\.json$/)).returns("wat")
      .withArgs(sinon.match(/file2\.json$/)).returns("{\"node\":true}")
      .withArgs(sinon.match(/file4\.json$/)).returns("{\"extends\":\"file3.json\"}")
      .withArgs(sinon.match(/file5\.json$/)).returns("{\"extends\":\"file2.json\"}")
      .withArgs(sinon.match(/file6\.json$/)).returns("{\"extends\":\"file2.json\",\"node\":false}");

    sinon.stub(shjs, "test")
      .withArgs("-e", sinon.match(/file\.js$/)).returns(true)
      .withArgs("-e", sinon.match(/file1\.json$/)).returns(true)
      .withArgs("-e", sinon.match(/file2\.json$/)).returns(true)
      .withArgs("-e", sinon.match(/file3\.json$/)).returns(false)
      .withArgs("-e", sinon.match(/file4\.json$/)).returns(true)
      .withArgs("-e", sinon.match(/file5\.json$/)).returns(true)
      .withArgs("-e", sinon.match(/file6\.json$/)).returns(true);

    cli.exit.restore();
    sinon.stub(cli, "exit").throws("ProcessExit");

    // File doesn't exist.
    try {
      cli.interpret([
        "node", "jshint", "file.js", "--config", "file3.json"
      ]);
    } catch (err) {
      var msg = out.args[0][0];
      test.equal(msg.slice(0, 23), "Can't find config file:");
      test.equal(msg.slice(msg.length - 10), "file3.json");
      test.equal(err, "ProcessExit");
    }

    // Invalid config
    try {
      cli.interpret([
        "node", "jshint", "file.js", "--config", "file1.json"
      ]);
    } catch (err) {
      var msg = out.args[1][0];
      test.equal(msg.slice(0, 24), "Can't parse config file:");
      test.equal(msg.slice(msg.length - 10), "file1.json");
      test.equal(err, "ProcessExit");
    }

    // Invalid merged filed
    try {
      cli.interpret([
        "node", "jshint", "file.js", "--config", "file4.json"
      ]);
    } catch (err) {
      var msg = out.args[2][0];
      test.equal(msg.slice(0, 23), "Can't find config file:");
      test.equal(msg.slice(msg.length - 10), "file3.json");
      test.equal(err, "ProcessExit");
    }

    cli.exit.restore();
    sinon.stub(cli, "exit");

    // Merges existing valid files
    cli.interpret([
      "node", "jshint", "file.js", "--config", "file5.json"
    ]);
    test.equal(cli.run.lastCall.args[0].config.node, true);
    test.equal(cli.run.lastCall.args[0].config['extends'], void 0);

    // Overwrites options after extending
    cli.interpret([
      "node", "jshint", "file.js", "--config", "file6.json"
    ]);
    test.equal(cli.run.lastCall.args[0].config.node, false);

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

    _cli.error.restore();
    cli.run.restore();
    shjs.cat.restore();
    shjs.test.restore();

    test.done();
  },