Skip to content

Commit

Permalink
test: ensure that CLI tests run with FlatESLint (#17884)
Browse files Browse the repository at this point in the history
* test: ensure that CLI tests run with FlatESLint

* mock `process.cwd()` in `beforeEach`/`afterEach` hooks
  • Loading branch information
fasttime committed Dec 21, 2023
1 parent 12be307 commit 595a1f6
Showing 1 changed file with 49 additions and 13 deletions.
62 changes: 49 additions & 13 deletions tests/lib/cli.js
Expand Up @@ -16,7 +16,7 @@

const assert = require("chai").assert,
stdAssert = require("assert"),
{ ESLint } = require("../../lib/eslint"),
{ ESLint, FlatESLint } = require("../../lib/eslint"),
BuiltinRules = require("../../lib/rules"),
path = require("path"),
sinon = require("sinon"),
Expand Down Expand Up @@ -54,10 +54,12 @@ describe("cli", () => {
*/
async function verifyESLintOpts(cmd, opts, configType) {

const ActiveESLint = configType === "flat" ? FlatESLint : ESLint;

// create a fake ESLint class to test with
const fakeESLint = sinon.mock().withExactArgs(sinon.match(opts));

Object.defineProperties(fakeESLint.prototype, Object.getOwnPropertyDescriptors(ESLint.prototype));
Object.defineProperties(fakeESLint.prototype, Object.getOwnPropertyDescriptors(ActiveESLint.prototype));
sinon.stub(fakeESLint.prototype, "lintFiles").returns([]);
sinon.stub(fakeESLint.prototype, "loadFormatter").returns({ format: sinon.spy() });

Expand Down Expand Up @@ -110,6 +112,7 @@ describe("cli", () => {
["eslintrc", "flat"].forEach(configType => {

const useFlatConfig = configType === "flat";
const ActiveESLint = configType === "flat" ? FlatESLint : ESLint;

describe("execute()", () => {

Expand Down Expand Up @@ -337,6 +340,17 @@ describe("cli", () => {
});

describe("when given an invalid built-in formatter name", () => {

const originalCwd = process.cwd;

beforeEach(() => {
process.cwd = () => getFixturePath();
});

afterEach(() => {
process.cwd = originalCwd;
});

it(`should execute with error: with configType:${configType}`, async () => {
const filePath = getFixturePath("passing.js");
const exit = await cli.execute(`-f fakeformatter ${filePath} ${flag}`, null, useFlatConfig);
Expand All @@ -346,6 +360,17 @@ describe("cli", () => {
});

describe("when given a valid formatter path", () => {

const originalCwd = process.cwd;

beforeEach(() => {
process.cwd = () => getFixturePath();
});

afterEach(() => {
process.cwd = originalCwd;
});

it(`should execute without any errors with configType:${configType}`, async () => {
const formatterPath = getFixturePath("formatters", "simple.js");
const filePath = getFixturePath("passing.js");
Expand Down Expand Up @@ -377,6 +402,17 @@ describe("cli", () => {
});

describe("when given an async formatter path", () => {

const originalCwd = process.cwd;

beforeEach(() => {
process.cwd = () => getFixturePath();
});

afterEach(() => {
process.cwd = originalCwd;
});

it(`should execute without any errors with configType:${configType}`, async () => {
const formatterPath = getFixturePath("formatters", "async.js");
const filePath = getFixturePath("passing.js");
Expand Down Expand Up @@ -555,7 +591,7 @@ describe("cli", () => {

it(`should allow defining variables with multiple flags with configType:${configType}`, async () => {
const filePath = getFixturePath("undef.js");
const exit = await cli.execute(`--global baz --global bat:true --no-ignore ${filePath}`);
const exit = await cli.execute(`--global baz --global bat:true --no-ignore ${filePath}`, null, useFlatConfig);

assert.isTrue(log.info.notCalled);
assert.strictEqual(exit, 0);
Expand Down Expand Up @@ -987,7 +1023,7 @@ describe("cli", () => {
// create a fake ESLint class to test with
const fakeESLint = sinon.mock().withExactArgs(sinon.match({ allowInlineConfig: false }));

Object.defineProperties(fakeESLint.prototype, Object.getOwnPropertyDescriptors(ESLint.prototype));
Object.defineProperties(fakeESLint.prototype, Object.getOwnPropertyDescriptors(ActiveESLint.prototype));
sinon.stub(fakeESLint.prototype, "lintFiles").returns([{
filePath: "./foo.js",
output: "bar",
Expand Down Expand Up @@ -1017,7 +1053,7 @@ describe("cli", () => {
// create a fake ESLint class to test with
const fakeESLint = sinon.mock().withExactArgs(sinon.match({ allowInlineConfig: true }));

Object.defineProperties(fakeESLint.prototype, Object.getOwnPropertyDescriptors(ESLint.prototype));
Object.defineProperties(fakeESLint.prototype, Object.getOwnPropertyDescriptors(ActiveESLint.prototype));
sinon.stub(fakeESLint.prototype, "lintFiles").returns([]);
sinon.stub(fakeESLint.prototype, "loadFormatter").returns({ format: () => "done" });
fakeESLint.outputFixes = sinon.stub();
Expand Down Expand Up @@ -1048,7 +1084,7 @@ describe("cli", () => {
// create a fake ESLint class to test with
const fakeESLint = sinon.mock().withExactArgs(sinon.match({ fix: true }));

Object.defineProperties(fakeESLint.prototype, Object.getOwnPropertyDescriptors(ESLint.prototype));
Object.defineProperties(fakeESLint.prototype, Object.getOwnPropertyDescriptors(ActiveESLint.prototype));
sinon.stub(fakeESLint.prototype, "lintFiles").returns([]);
sinon.stub(fakeESLint.prototype, "loadFormatter").returns({ format: () => "done" });
fakeESLint.outputFixes = sinon.mock().once();
Expand Down Expand Up @@ -1084,7 +1120,7 @@ describe("cli", () => {
// create a fake ESLint class to test with
const fakeESLint = sinon.mock().withExactArgs(sinon.match({ fix: true }));

Object.defineProperties(fakeESLint.prototype, Object.getOwnPropertyDescriptors(ESLint.prototype));
Object.defineProperties(fakeESLint.prototype, Object.getOwnPropertyDescriptors(ActiveESLint.prototype));
sinon.stub(fakeESLint.prototype, "lintFiles").returns(report);
sinon.stub(fakeESLint.prototype, "loadFormatter").returns({ format: () => "done" });
fakeESLint.outputFixes = sinon.mock().withExactArgs(report);
Expand Down Expand Up @@ -1120,7 +1156,7 @@ describe("cli", () => {
// create a fake ESLint class to test with
const fakeESLint = sinon.mock().withExactArgs(sinon.match({ fix: sinon.match.func }));

Object.defineProperties(fakeESLint.prototype, Object.getOwnPropertyDescriptors(ESLint.prototype));
Object.defineProperties(fakeESLint.prototype, Object.getOwnPropertyDescriptors(ActiveESLint.prototype));
sinon.stub(fakeESLint.prototype, "lintFiles").returns(report);
sinon.stub(fakeESLint.prototype, "loadFormatter").returns({ format: () => "done" });
fakeESLint.getErrorResults = sinon.stub().returns([]);
Expand Down Expand Up @@ -1170,7 +1206,7 @@ describe("cli", () => {
// create a fake ESLint class to test with
const fakeESLint = sinon.mock().withExactArgs(sinon.match({ fix: true }));

Object.defineProperties(fakeESLint.prototype, Object.getOwnPropertyDescriptors(ESLint.prototype));
Object.defineProperties(fakeESLint.prototype, Object.getOwnPropertyDescriptors(ActiveESLint.prototype));
sinon.stub(fakeESLint.prototype, "lintFiles").returns([]);
sinon.stub(fakeESLint.prototype, "loadFormatter").returns({ format: () => "done" });
fakeESLint.outputFixes = sinon.mock().never();
Expand Down Expand Up @@ -1198,7 +1234,7 @@ describe("cli", () => {
// create a fake ESLint class to test with
const fakeESLint = sinon.mock().withExactArgs(sinon.match(expectedESLintOptions));

Object.defineProperties(fakeESLint.prototype, Object.getOwnPropertyDescriptors(ESLint.prototype));
Object.defineProperties(fakeESLint.prototype, Object.getOwnPropertyDescriptors(ActiveESLint.prototype));
sinon.stub(fakeESLint.prototype, "lintFiles").returns([]);
sinon.stub(fakeESLint.prototype, "loadFormatter").returns({ format: () => "done" });
fakeESLint.outputFixes = sinon.stub();
Expand Down Expand Up @@ -1233,7 +1269,7 @@ describe("cli", () => {
// create a fake ESLint class to test with
const fakeESLint = sinon.mock().withExactArgs(sinon.match({ fix: true }));

Object.defineProperties(fakeESLint.prototype, Object.getOwnPropertyDescriptors(ESLint.prototype));
Object.defineProperties(fakeESLint.prototype, Object.getOwnPropertyDescriptors(ActiveESLint.prototype));
sinon.stub(fakeESLint.prototype, "lintFiles").returns(report);
sinon.stub(fakeESLint.prototype, "loadFormatter").returns({ format: () => "done" });
fakeESLint.outputFixes = sinon.mock().never();
Expand Down Expand Up @@ -1269,7 +1305,7 @@ describe("cli", () => {
// create a fake ESLint class to test with
const fakeESLint = sinon.mock().withExactArgs(sinon.match({ fix: sinon.match.func }));

Object.defineProperties(fakeESLint.prototype, Object.getOwnPropertyDescriptors(ESLint.prototype));
Object.defineProperties(fakeESLint.prototype, Object.getOwnPropertyDescriptors(ActiveESLint.prototype));
sinon.stub(fakeESLint.prototype, "lintFiles").returns(report);
sinon.stub(fakeESLint.prototype, "loadFormatter").returns({ format: () => "done" });
fakeESLint.getErrorResults = sinon.stub().returns([]);
Expand Down Expand Up @@ -1306,7 +1342,7 @@ describe("cli", () => {
// create a fake ESLint class to test with
const fakeESLint = sinon.mock().withExactArgs(sinon.match({ fix: true }));

Object.defineProperties(fakeESLint.prototype, Object.getOwnPropertyDescriptors(ESLint.prototype));
Object.defineProperties(fakeESLint.prototype, Object.getOwnPropertyDescriptors(ActiveESLint.prototype));
sinon.stub(fakeESLint.prototype, "lintText").returns(report);
sinon.stub(fakeESLint.prototype, "loadFormatter").returns({ format: () => "done" });
fakeESLint.outputFixes = sinon.mock().never();
Expand Down

0 comments on commit 595a1f6

Please sign in to comment.