Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Sep 16, 2019
1 parent be131d1 commit 2631f21
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 31 deletions.
9 changes: 2 additions & 7 deletions lib/cli.js
Expand Up @@ -20,8 +20,8 @@ const fs = require("fs"),
mkdirp = require("mkdirp"),
{ CLIEngine } = require("./cli-engine"),
options = require("./options"),
info = require("./info"),
log = require("./shared/logging");
log = require("./shared/logging"),
info = require("./shared/info");

const debug = require("debug")("eslint:cli");

Expand Down Expand Up @@ -160,7 +160,6 @@ const cli = {
}

const files = currentOptions._;

const useStdin = typeof text === "string";

if (currentOptions.version) {
Expand All @@ -184,17 +183,13 @@ const cli = {
}

const engine = new CLIEngine(translateOptions(currentOptions));

const fileConfig = engine.getConfigForFile(currentOptions.printConfig);

log.info(JSON.stringify(fileConfig, null, " "));
return 0;
} else if (currentOptions.help || (!files.length && !useStdin)) {

log.info(options.generateHelp());

} else {

debug(`Running on ${useStdin ? "text" : "files"}`);

if (currentOptions.fix && currentOptions.fixDryRun) {
Expand Down
37 changes: 21 additions & 16 deletions lib/info/index.js → lib/shared/info.js
@@ -1,5 +1,5 @@
/**
* @fileoverview Log information for debugging purposes
* @fileoverview Utility to get information about the execution environment.
* @author Kai Cataldo
*/

Expand All @@ -13,18 +13,19 @@ const path = require("path");
const spawn = require("cross-spawn");
const { isEmpty } = require("lodash");
const log = require("../shared/logging");
const packageJson = require("../../package.json");

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

/**
* Checks if a given path is in a given directory.
* Checks if a path is a child of a directory.
* @param {string} parentPath - The parent path to check.
* @param {string} childPath - The path to check.
* @returns {boolean} Whether or not the given path is in the given directory.
* @returns {boolean} Whether or not the given path is a child of a directory.
*/
function isInDirectory(parentPath, childPath) {
function isChildOfDirectory(parentPath, childPath) {
return !path.relative(parentPath, childPath).startsWith("..");
}

Expand Down Expand Up @@ -62,9 +63,9 @@ function getBinVersion(bin) {
const binArgs = ["--version"];

try {
return execCommand(bin, binArgs);
return normalizeVersionStr(execCommand(bin, binArgs));
} catch (e) {
log.error(`Error finding ${bin} version running the command ${bin} ${binArgs.join(" ")}`);
log.error(`Error finding ${bin} version running the command \`${bin} ${binArgs.join(" ")}\``);
throw e;
}
}
Expand All @@ -86,7 +87,11 @@ function getNpmPackageVersion(pkg, { global = false } = {}) {
try {
const parsedStdout = JSON.parse(execCommand("npm", npmLsArgs));

if (isEmpty(parsedStdout)) {
/*
* Checking globally returns an empty JSON object, while local checks
* include the name and version of the local project.
*/
if (isEmpty(parsedStdout) || !(parsedStdout.dependencies && parsedStdout.dependencies.eslint)) {
return "Not found";
}

Expand All @@ -96,20 +101,20 @@ function getNpmPackageVersion(pkg, { global = false } = {}) {
try {
npmBinPath = execCommand("npm", npmBinArgs);
} catch (e) {
log.error(`Error finding npm binary path when running command npm ${npmBinArgs.join(" ")}`);
log.error(`Error finding npm binary path when running command \`npm ${npmBinArgs.join(" ")}\``);
throw e;
}

const isGlobal = isInDirectory(npmBinPath, processBinPath);
const isGlobal = isChildOfDirectory(npmBinPath, processBinPath);
let pkgVersion = parsedStdout.dependencies.eslint.version;

if ((global && isGlobal) || (!global && !isGlobal)) {
pkgVersion += " (Currently used)";
}

return pkgVersion;
return normalizeVersionStr(pkgVersion);
} catch (e) {
log.error(`Error finding ${pkg} version running the command npm ${npmLsArgs.join(" ")}`);
log.error(`Error finding ${pkg} version running the command \`npm ${npmLsArgs.join(" ")}\``);
throw e;
}
}
Expand All @@ -122,10 +127,10 @@ function environment() {
return [
"Environment Info:",
"",
`Node version: ${normalizeVersionStr(getBinVersion("node"))}`,
`npm version: ${normalizeVersionStr(getBinVersion("npm"))}`,
`Local ESLint version: ${normalizeVersionStr(getNpmPackageVersion("eslint", { global: false }))}`,
`Global ESLint version: ${normalizeVersionStr(getNpmPackageVersion("eslint", { global: true }))}`
`Node version: ${getBinVersion("node")}`,
`npm version: ${getBinVersion("npm")}`,
`Local ESLint version: ${getNpmPackageVersion("eslint", { global: false })}`,
`Global ESLint version: ${getNpmPackageVersion("eslint", { global: true })}`
].join("\n");
}

Expand All @@ -134,7 +139,7 @@ function environment() {
* @returns {string} The version from the currently executing ESLint's package.json.
*/
function version() {
return `v${require("../../package.json").version}`;
return `v${packageJson.version}`;
}

//------------------------------------------------------------------------------
Expand Down
27 changes: 19 additions & 8 deletions tests/lib/cli.js
Expand Up @@ -29,14 +29,18 @@ const proxyquire = require("proxyquire").noCallThru().noPreserveCache();
//------------------------------------------------------------------------------

describe("cli", () => {

let fixtureDir;
const log = {
info: sinon.spy(),
error: sinon.spy()
};
const info = {
environment: sinon.stub(),
version: sinon.stub()
};
const cli = proxyquire("../../lib/cli", {
"./shared/logging": log
"./shared/logging": log,
"./shared/info": info
});

/**
Expand Down Expand Up @@ -324,15 +328,27 @@ describe("cli", () => {
describe("when executing with version flag", () => {
it("should print out current version", () => {
assert.strictEqual(cli.execute("-v"), 0);
assert.strictEqual(log.info.callCount, 1);
});
});

describe("when executing with info flag", () => {
it("should print out environment information", () => {
assert.strictEqual(cli.execute("--info"), 0);
assert.strictEqual(log.info.callCount, 1);
});

it("should print error message and return error code", () => {
info.environment.throws("There was an error!");

assert.strictEqual(cli.execute("--info"), 2);
assert.strictEqual(log.error.callCount, 1);
});
});

describe("when executing with help flag", () => {
it("should print out help", () => {
assert.strictEqual(cli.execute("-h"), 0);

assert.strictEqual(log.info.callCount, 1);
});
});
Expand All @@ -349,7 +365,6 @@ describe("cli", () => {
});

describe("when given a file in excluded files list", () => {

it("should not process the file", () => {
const ignorePath = getFixturePath(".eslintignore");
const filePath = getFixturePath("passing.js");
Expand Down Expand Up @@ -398,7 +413,6 @@ describe("cli", () => {
});

describe("when executing a file with a shebang", () => {

it("should execute without error", () => {
const filePath = getFixturePath("shebang.js");
const exit = cli.execute(`--no-ignore ${filePath}`);
Expand All @@ -408,7 +422,6 @@ describe("cli", () => {
});

describe("when loading a custom rule", () => {

it("should return an error when rule isn't found", () => {
const rulesPath = getFixturePath("rules", "wrong");
const configPath = getFixturePath("rules", "eslint.json");
Expand Down Expand Up @@ -551,7 +564,6 @@ describe("cli", () => {
});

describe("when supplied with report output file path", () => {

afterEach(() => {
sh.rm("-rf", "tests/output");
});
Expand Down Expand Up @@ -594,7 +606,6 @@ describe("cli", () => {
});

describe("when supplied with a plugin", () => {

it("should pass plugins to CLIEngine", () => {
const examplePluginName = "eslint-plugin-example";

Expand Down

0 comments on commit 2631f21

Please sign in to comment.