From 4cc3233e70dacfbe6e5a5f50cce7288c57c1cb57 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Fri, 9 Apr 2021 07:32:40 +0530 Subject: [PATCH 1/7] Fix: create `.eslintrc.cjs` for `module` type --- lib/init/config-file.js | 1 + lib/init/config-initializer.js | 11 +++++++++++ lib/init/npm-utils.js | 1 + 3 files changed, 13 insertions(+) diff --git a/lib/init/config-file.js b/lib/init/config-file.js index fc62b81525e..4c648ac0551 100644 --- a/lib/init/config-file.js +++ b/lib/init/config-file.js @@ -117,6 +117,7 @@ function writeJSConfigFile(config, filePath) { function write(config, filePath) { switch (path.extname(filePath)) { case ".js": + case ".cjs": writeJSConfigFile(config, filePath); break; diff --git a/lib/init/config-initializer.js b/lib/init/config-initializer.js index 8cc09b6a960..212e9f7a20b 100644 --- a/lib/init/config-initializer.js +++ b/lib/init/config-initializer.js @@ -12,6 +12,7 @@ const util = require("util"), path = require("path"), + fs = require("fs"), enquirer = require("enquirer"), ProgressBar = require("progress"), semver = require("semver"), @@ -48,6 +49,16 @@ function writeFile(config, format) { extname = ".yml"; } else if (format === "JSON") { extname = ".json"; + } else if (format === "JavaScript") { + const pkgJSONPath = npmUtils.findPackageJson(); + + if (pkgJSONPath) { + const pkgJSONContents = JSON.parse(fs.readFileSync(pkgJSONPath, "utf8")); + + if (pkgJSONContents.type === "module") { + extname = ".cjs"; + } + } } const installedESLint = config.installedESLint; diff --git a/lib/init/npm-utils.js b/lib/init/npm-utils.js index 555ea2b2b28..35191cc0876 100644 --- a/lib/init/npm-utils.js +++ b/lib/init/npm-utils.js @@ -172,6 +172,7 @@ function checkPackageJson(startDir) { module.exports = { installSyncSaveDev, fetchPeerDependencies, + findPackageJson, checkDeps, checkDevDeps, checkPackageJson From 77f759bc7f3100eb1ed9fea68805c5c1d7cb9669 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Tue, 20 Apr 2021 18:18:37 +0530 Subject: [PATCH 2/7] Chore: add tests --- lib/init/config-initializer.js | 10 +++-- tests/lib/init/config-initializer.js | 58 ++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/lib/init/config-initializer.js b/lib/init/config-initializer.js index 212e9f7a20b..ea16896229c 100644 --- a/lib/init/config-initializer.js +++ b/lib/init/config-initializer.js @@ -38,9 +38,10 @@ const debug = require("debug")("eslint:config-initializer"); * Create .eslintrc file in the current working directory * @param {Object} config object that contains user's answers * @param {string} format The file format to write to. + * @param {Object} options filePath: write file here, startDir: Starting directory for searching pkgJSON. Handy for writing tests to avoid reading/writing root files. * @returns {void} */ -function writeFile(config, format) { +function writeFile(config, format, options = {}) { // default is .js let extname = ".js"; @@ -50,7 +51,7 @@ function writeFile(config, format) { } else if (format === "JSON") { extname = ".json"; } else if (format === "JavaScript") { - const pkgJSONPath = npmUtils.findPackageJson(); + const pkgJSONPath = npmUtils.findPackageJson(options.startDir || process.cwd()); if (pkgJSONPath) { const pkgJSONContents = JSON.parse(fs.readFileSync(pkgJSONPath, "utf8")); @@ -65,8 +66,8 @@ function writeFile(config, format) { delete config.installedESLint; - ConfigFile.write(config, `./.eslintrc${extname}`); - log.info(`Successfully created .eslintrc${extname} file in ${process.cwd()}`); + ConfigFile.write(config, options.filePath ? path.resolve(options.filePath, `.eslintrc${extname}`) : `./.eslintrc${extname}`); + log.info(`Successfully created .eslintrc${extname} file in ${options.filePath || process.cwd()}`); if (installedESLint) { log.info("ESLint was installed locally. We recommend using this local copy instead of your globally-installed copy."); @@ -695,6 +696,7 @@ const init = { hasESLintVersionConflict, installModules, processAnswers, + writeFile, /* istanbul ignore next */initializeConfig() { return promptUser(); } diff --git a/tests/lib/init/config-initializer.js b/tests/lib/init/config-initializer.js index 10a67fe0ab9..2940e7ea6f0 100644 --- a/tests/lib/init/config-initializer.js +++ b/tests/lib/init/config-initializer.js @@ -207,6 +207,64 @@ describe("configInitializer", () => { }); }); + describe("writeFile()", () => { + it("should create eslintrc.json", () => { + const config = init.processAnswers(answers); + const filePath = path.resolve(__dirname, ".eslintrc.json"); + + init.writeFile(config, answers.format, { filePath: __dirname }); + + assert.isTrue(fs.existsSync(filePath)); + + fs.unlinkSync(filePath); + }); + + it("should create eslintrc.js", () => { + answers.format = "JavaScript"; + + const config = init.processAnswers(answers); + const filePath = path.resolve(__dirname, ".eslintrc.js"); + + init.writeFile(config, answers.format, { filePath: __dirname }); + + assert.isTrue(fs.existsSync(filePath)); + + fs.unlinkSync(filePath); + }); + + it("should create eslintrc.yml", () => { + answers.format = "YAML"; + + const config = init.processAnswers(answers); + const filePath = path.resolve(__dirname, ".eslintrc.yml"); + + init.writeFile(config, answers.format, { filePath: __dirname }); + + assert.isTrue(fs.existsSync(filePath)); + + fs.unlinkSync(filePath); + }); + + it("should create eslintrc.cjs", () => { + answers.format = "JavaScript"; + + // create package.json with "type": "module" + const pkgJSONContens = { type: "module" }; + + fs.writeFileSync(path.resolve(__dirname, "package.json"), JSON.stringify(pkgJSONContens)); + + const config = init.processAnswers(answers); + const filePath = path.resolve(__dirname, ".eslintrc.cjs"); + + init.writeFile(config, answers.format, { filePath: __dirname, startDir: __dirname }); + + assert.isTrue(fs.existsSync(filePath)); + + fs.unlinkSync(filePath); + fs.unlinkSync(path.resolve(__dirname, "package.json")); + }); + }); + describe("guide", () => { it("should support the google style guide", () => { const config = { extends: "google" }; From 6c833ae81f805c29b42a4574dbfdade229d5e6f7 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Wed, 21 Apr 2021 07:51:11 +0530 Subject: [PATCH 3/7] Chore: more test --- tests/lib/init/config-initializer.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/lib/init/config-initializer.js b/tests/lib/init/config-initializer.js index 2940e7ea6f0..0b834d95822 100644 --- a/tests/lib/init/config-initializer.js +++ b/tests/lib/init/config-initializer.js @@ -245,6 +245,7 @@ describe("configInitializer", () => { fs.unlinkSync(filePath); }); + // For https://github.com/eslint/eslint/issues/14137 it("should create eslintrc.cjs", () => { answers.format = "JavaScript"; @@ -263,6 +264,25 @@ describe("configInitializer", () => { fs.unlinkSync(filePath); fs.unlinkSync(path.resolve(__dirname, "package.json")); }); + + it("should create eslintrc.json even with type: 'module'", () => { + answers.format = "JSON"; + + // create package.json with "type": "module" + const pkgJSONContens = { type: "module" }; + + fs.writeFileSync(path.resolve(__dirname, "package.json"), JSON.stringify(pkgJSONContens)); + + const config = init.processAnswers(answers); + const filePath = path.resolve(__dirname, ".eslintrc.json"); + + init.writeFile(config, answers.format, { filePath: __dirname, startDir: __dirname }); + + assert.isTrue(fs.existsSync(filePath)); + + fs.unlinkSync(filePath); + fs.unlinkSync(path.resolve(__dirname, "package.json")); + }); }); describe("guide", () => { From ecc8daa996a86276dae2594a1eec2da80be52040 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sat, 24 Apr 2021 14:02:27 +0530 Subject: [PATCH 4/7] Chore: refactor tests --- lib/init/config-initializer.js | 9 +- tests/lib/init/config-initializer.js | 199 ++++++++++++++++----------- 2 files changed, 125 insertions(+), 83 deletions(-) diff --git a/lib/init/config-initializer.js b/lib/init/config-initializer.js index ea16896229c..3c7f2ba0944 100644 --- a/lib/init/config-initializer.js +++ b/lib/init/config-initializer.js @@ -38,10 +38,9 @@ const debug = require("debug")("eslint:config-initializer"); * Create .eslintrc file in the current working directory * @param {Object} config object that contains user's answers * @param {string} format The file format to write to. - * @param {Object} options filePath: write file here, startDir: Starting directory for searching pkgJSON. Handy for writing tests to avoid reading/writing root files. * @returns {void} */ -function writeFile(config, format, options = {}) { +function writeFile(config, format) { // default is .js let extname = ".js"; @@ -51,7 +50,7 @@ function writeFile(config, format, options = {}) { } else if (format === "JSON") { extname = ".json"; } else if (format === "JavaScript") { - const pkgJSONPath = npmUtils.findPackageJson(options.startDir || process.cwd()); + const pkgJSONPath = npmUtils.findPackageJson(); if (pkgJSONPath) { const pkgJSONContents = JSON.parse(fs.readFileSync(pkgJSONPath, "utf8")); @@ -66,8 +65,8 @@ function writeFile(config, format, options = {}) { delete config.installedESLint; - ConfigFile.write(config, options.filePath ? path.resolve(options.filePath, `.eslintrc${extname}`) : `./.eslintrc${extname}`); - log.info(`Successfully created .eslintrc${extname} file in ${options.filePath || process.cwd()}`); + ConfigFile.write(config, `./.eslintrc${extname}`); + log.info(`Successfully created .eslintrc${extname} file in ${process.cwd()}`); if (installedESLint) { log.info("ESLint was installed locally. We recommend using this local copy instead of your globally-installed copy."); diff --git a/tests/lib/init/config-initializer.js b/tests/lib/init/config-initializer.js index 0b834d95822..be736f73cd6 100644 --- a/tests/lib/init/config-initializer.js +++ b/tests/lib/init/config-initializer.js @@ -27,6 +27,7 @@ const proxyquire = require("proxyquire").noPreserveCache(); //------------------------------------------------------------------------------ let answers = {}; +let pkgJSONContens = {}; describe("configInitializer", () => { @@ -207,84 +208,6 @@ describe("configInitializer", () => { }); }); - describe("writeFile()", () => { - it("should create eslintrc.json", () => { - const config = init.processAnswers(answers); - const filePath = path.resolve(__dirname, ".eslintrc.json"); - - init.writeFile(config, answers.format, { filePath: __dirname }); - - assert.isTrue(fs.existsSync(filePath)); - - fs.unlinkSync(filePath); - }); - - it("should create eslintrc.js", () => { - answers.format = "JavaScript"; - - const config = init.processAnswers(answers); - const filePath = path.resolve(__dirname, ".eslintrc.js"); - - init.writeFile(config, answers.format, { filePath: __dirname }); - - assert.isTrue(fs.existsSync(filePath)); - - fs.unlinkSync(filePath); - }); - - it("should create eslintrc.yml", () => { - answers.format = "YAML"; - - const config = init.processAnswers(answers); - const filePath = path.resolve(__dirname, ".eslintrc.yml"); - - init.writeFile(config, answers.format, { filePath: __dirname }); - - assert.isTrue(fs.existsSync(filePath)); - - fs.unlinkSync(filePath); - }); - - // For https://github.com/eslint/eslint/issues/14137 - it("should create eslintrc.cjs", () => { - answers.format = "JavaScript"; - - // create package.json with "type": "module" - const pkgJSONContens = { type: "module" }; - - fs.writeFileSync(path.resolve(__dirname, "package.json"), JSON.stringify(pkgJSONContens)); - - const config = init.processAnswers(answers); - const filePath = path.resolve(__dirname, ".eslintrc.cjs"); - - init.writeFile(config, answers.format, { filePath: __dirname, startDir: __dirname }); - - assert.isTrue(fs.existsSync(filePath)); - - fs.unlinkSync(filePath); - fs.unlinkSync(path.resolve(__dirname, "package.json")); - }); - - it("should create eslintrc.json even with type: 'module'", () => { - answers.format = "JSON"; - - // create package.json with "type": "module" - const pkgJSONContens = { type: "module" }; - - fs.writeFileSync(path.resolve(__dirname, "package.json"), JSON.stringify(pkgJSONContens)); - - const config = init.processAnswers(answers); - const filePath = path.resolve(__dirname, ".eslintrc.json"); - - init.writeFile(config, answers.format, { filePath: __dirname, startDir: __dirname }); - - assert.isTrue(fs.existsSync(filePath)); - - fs.unlinkSync(filePath); - fs.unlinkSync(path.resolve(__dirname, "package.json")); - }); - }); - describe("guide", () => { it("should support the google style guide", () => { const config = { extends: "google" }; @@ -533,4 +456,124 @@ describe("configInitializer", () => { }); }); }); + + describe("writeFile()", () => { + + beforeEach(() => { + answers = { + purpose: "style", + source: "prompt", + extendDefault: true, + indent: 2, + quotes: "single", + linebreak: "unix", + semi: true, + moduleType: "esm", + es6Globals: true, + env: ["browser"], + format: "JSON" + }; + + pkgJSONContens = { + name: "config-initializer", + version: "1.0.0" + }; + }); + + afterEach(() => { + process.chdir(originalDir); + }); + + it("should create eslintrc.json", () => { + process.chdir(fixtureDir); + + const config = init.processAnswers(answers); + const filePath = path.resolve(process.cwd(), ".eslintrc.json"); + + fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContens)); + + init.writeFile(config, answers.format); + + assert.isTrue(fs.existsSync(filePath)); + + fs.unlinkSync(filePath); + }); + + it("should create eslintrc.js", () => { + answers.format = "JavaScript"; + + process.chdir(fixtureDir); + + const config = init.processAnswers(answers); + const filePath = path.resolve(process.cwd(), ".eslintrc.js"); + + fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContens)); + + init.writeFile(config, answers.format); + + assert.isTrue(fs.existsSync(filePath)); + + fs.unlinkSync(filePath); + }); + + it("should create eslintrc.yml", () => { + answers.format = "YAML"; + + process.chdir(fixtureDir); + + const config = init.processAnswers(answers); + const filePath = path.resolve(process.cwd(), ".eslintrc.yml"); + + fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContens)); + + init.writeFile(config, answers.format); + + assert.isTrue(fs.existsSync(filePath)); + + fs.unlinkSync(filePath); + }); + + // For https://github.com/eslint/eslint/issues/14137 + it("should create eslintrc.cjs", () => { + answers.format = "JavaScript"; + + process.chdir(fixtureDir); + + // create package.json with "type": "module" + pkgJSONContens.type = "module"; + + fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContens)); + + const config = init.processAnswers(answers); + const filePath = path.resolve(process.cwd(), ".eslintrc.cjs"); + + init.writeFile(config, answers.format); + + assert.isTrue(fs.existsSync(filePath)); + + fs.unlinkSync(filePath); + fs.unlinkSync(path.resolve(process.cwd(), "package.json")); + }); + + it("should create eslintrc.json even with type: 'module'", () => { + answers.format = "JSON"; + + process.chdir(fixtureDir); + + // create package.json with "type": "module" + pkgJSONContens.type = "module"; + + fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContens)); + + const config = init.processAnswers(answers); + const filePath = path.resolve(process.cwd(), ".eslintrc.json"); + + init.writeFile(config, answers.format); + + assert.isTrue(fs.existsSync(filePath)); + + fs.unlinkSync(filePath); + fs.unlinkSync(path.resolve(process.cwd(), "package.json")); + }); + }); }); From 66cd2eef60d9e4164e10e81a709f7b9a79ca6e08 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Wed, 5 May 2021 07:16:58 +0530 Subject: [PATCH 5/7] Chore: refactor tests --- tests/lib/init/config-initializer.js | 31 +++++++++++++++------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/tests/lib/init/config-initializer.js b/tests/lib/init/config-initializer.js index be736f73cd6..0d054542105 100644 --- a/tests/lib/init/config-initializer.js +++ b/tests/lib/init/config-initializer.js @@ -27,7 +27,7 @@ const proxyquire = require("proxyquire").noPreserveCache(); //------------------------------------------------------------------------------ let answers = {}; -let pkgJSONContens = {}; +let pkgJSONContents = {}; describe("configInitializer", () => { @@ -474,7 +474,7 @@ describe("configInitializer", () => { format: "JSON" }; - pkgJSONContens = { + pkgJSONContents = { name: "config-initializer", version: "1.0.0" }; @@ -484,22 +484,23 @@ describe("configInitializer", () => { process.chdir(originalDir); }); - it("should create eslintrc.json", () => { + it("should create .eslintrc.json", () => { process.chdir(fixtureDir); const config = init.processAnswers(answers); const filePath = path.resolve(process.cwd(), ".eslintrc.json"); - fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContens)); + fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContents)); init.writeFile(config, answers.format); assert.isTrue(fs.existsSync(filePath)); fs.unlinkSync(filePath); + fs.unlinkSync(path.resolve(process.cwd(), "package.json")); }); - it("should create eslintrc.js", () => { + it("should create .eslintrc.js", () => { answers.format = "JavaScript"; process.chdir(fixtureDir); @@ -507,16 +508,17 @@ describe("configInitializer", () => { const config = init.processAnswers(answers); const filePath = path.resolve(process.cwd(), ".eslintrc.js"); - fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContens)); + fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContents)); init.writeFile(config, answers.format); assert.isTrue(fs.existsSync(filePath)); fs.unlinkSync(filePath); + fs.unlinkSync(path.resolve(process.cwd(), "package.json")); }); - it("should create eslintrc.yml", () => { + it("should create .eslintrc.yml", () => { answers.format = "YAML"; process.chdir(fixtureDir); @@ -524,25 +526,26 @@ describe("configInitializer", () => { const config = init.processAnswers(answers); const filePath = path.resolve(process.cwd(), ".eslintrc.yml"); - fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContens)); + fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContents)); init.writeFile(config, answers.format); assert.isTrue(fs.existsSync(filePath)); fs.unlinkSync(filePath); + fs.unlinkSync(path.resolve(process.cwd(), "package.json")); }); // For https://github.com/eslint/eslint/issues/14137 - it("should create eslintrc.cjs", () => { + it("should create .eslintrc.cjs", () => { answers.format = "JavaScript"; process.chdir(fixtureDir); // create package.json with "type": "module" - pkgJSONContens.type = "module"; + pkgJSONContents.type = "module"; - fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContens)); + fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContents)); const config = init.processAnswers(answers); const filePath = path.resolve(process.cwd(), ".eslintrc.cjs"); @@ -555,15 +558,15 @@ describe("configInitializer", () => { fs.unlinkSync(path.resolve(process.cwd(), "package.json")); }); - it("should create eslintrc.json even with type: 'module'", () => { + it("should create .eslintrc.json even with type: 'module'", () => { answers.format = "JSON"; process.chdir(fixtureDir); // create package.json with "type": "module" - pkgJSONContens.type = "module"; + pkgJSONContents.type = "module"; - fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContens)); + fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContents)); const config = init.processAnswers(answers); const filePath = path.resolve(process.cwd(), ".eslintrc.json"); From 14c6ebe314cbdc4264d72eb8ada3a76d133eea9d Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Wed, 5 May 2021 07:25:37 +0530 Subject: [PATCH 6/7] Chore: refactor test --- tests/lib/init/config-initializer.js | 30 ++++++++++++++-------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/lib/init/config-initializer.js b/tests/lib/init/config-initializer.js index 0d054542105..ad2c75fdad0 100644 --- a/tests/lib/init/config-initializer.js +++ b/tests/lib/init/config-initializer.js @@ -488,16 +488,16 @@ describe("configInitializer", () => { process.chdir(fixtureDir); const config = init.processAnswers(answers); - const filePath = path.resolve(process.cwd(), ".eslintrc.json"); + const filePath = path.resolve(fixtureDir, ".eslintrc.json"); - fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContents)); + fs.writeFileSync(path.resolve(fixtureDir, "package.json"), JSON.stringify(pkgJSONContents)); init.writeFile(config, answers.format); assert.isTrue(fs.existsSync(filePath)); fs.unlinkSync(filePath); - fs.unlinkSync(path.resolve(process.cwd(), "package.json")); + fs.unlinkSync(path.resolve(fixtureDir, "package.json")); }); it("should create .eslintrc.js", () => { @@ -506,16 +506,16 @@ describe("configInitializer", () => { process.chdir(fixtureDir); const config = init.processAnswers(answers); - const filePath = path.resolve(process.cwd(), ".eslintrc.js"); + const filePath = path.resolve(fixtureDir, ".eslintrc.js"); - fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContents)); + fs.writeFileSync(path.resolve(fixtureDir, "package.json"), JSON.stringify(pkgJSONContents)); init.writeFile(config, answers.format); assert.isTrue(fs.existsSync(filePath)); fs.unlinkSync(filePath); - fs.unlinkSync(path.resolve(process.cwd(), "package.json")); + fs.unlinkSync(path.resolve(fixtureDir, "package.json")); }); it("should create .eslintrc.yml", () => { @@ -524,16 +524,16 @@ describe("configInitializer", () => { process.chdir(fixtureDir); const config = init.processAnswers(answers); - const filePath = path.resolve(process.cwd(), ".eslintrc.yml"); + const filePath = path.resolve(fixtureDir, ".eslintrc.yml"); - fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContents)); + fs.writeFileSync(path.resolve(fixtureDir, "package.json"), JSON.stringify(pkgJSONContents)); init.writeFile(config, answers.format); assert.isTrue(fs.existsSync(filePath)); fs.unlinkSync(filePath); - fs.unlinkSync(path.resolve(process.cwd(), "package.json")); + fs.unlinkSync(path.resolve(fixtureDir, "package.json")); }); // For https://github.com/eslint/eslint/issues/14137 @@ -545,17 +545,17 @@ describe("configInitializer", () => { // create package.json with "type": "module" pkgJSONContents.type = "module"; - fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContents)); + fs.writeFileSync(path.resolve(fixtureDir, "package.json"), JSON.stringify(pkgJSONContents)); const config = init.processAnswers(answers); - const filePath = path.resolve(process.cwd(), ".eslintrc.cjs"); + const filePath = path.resolve(fixtureDir, ".eslintrc.cjs"); init.writeFile(config, answers.format); assert.isTrue(fs.existsSync(filePath)); fs.unlinkSync(filePath); - fs.unlinkSync(path.resolve(process.cwd(), "package.json")); + fs.unlinkSync(path.resolve(fixtureDir, "package.json")); }); it("should create .eslintrc.json even with type: 'module'", () => { @@ -566,17 +566,17 @@ describe("configInitializer", () => { // create package.json with "type": "module" pkgJSONContents.type = "module"; - fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContents)); + fs.writeFileSync(path.resolve(fixtureDir, "package.json"), JSON.stringify(pkgJSONContents)); const config = init.processAnswers(answers); - const filePath = path.resolve(process.cwd(), ".eslintrc.json"); + const filePath = path.resolve(fixtureDir, ".eslintrc.json"); init.writeFile(config, answers.format); assert.isTrue(fs.existsSync(filePath)); fs.unlinkSync(filePath); - fs.unlinkSync(path.resolve(process.cwd(), "package.json")); + fs.unlinkSync(path.resolve(fixtureDir, "package.json")); }); }); }); From 85326fb780ffb6dae8d1fa593248f92c8a86d49a Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Wed, 5 May 2021 07:53:02 +0530 Subject: [PATCH 7/7] Chore: refactor tests --- tests/lib/init/config-initializer.js | 35 ++++++++++++---------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/tests/lib/init/config-initializer.js b/tests/lib/init/config-initializer.js index ad2c75fdad0..81e4e52faaa 100644 --- a/tests/lib/init/config-initializer.js +++ b/tests/lib/init/config-initializer.js @@ -28,6 +28,7 @@ const proxyquire = require("proxyquire").noPreserveCache(); let answers = {}; let pkgJSONContents = {}; +let pkgJSONPath = ""; describe("configInitializer", () => { @@ -478,6 +479,10 @@ describe("configInitializer", () => { name: "config-initializer", version: "1.0.0" }; + + process.chdir(fixtureDir); + + pkgJSONPath = path.resolve(fixtureDir, "package.json"); }); afterEach(() => { @@ -485,67 +490,59 @@ describe("configInitializer", () => { }); it("should create .eslintrc.json", () => { - process.chdir(fixtureDir); - const config = init.processAnswers(answers); const filePath = path.resolve(fixtureDir, ".eslintrc.json"); - fs.writeFileSync(path.resolve(fixtureDir, "package.json"), JSON.stringify(pkgJSONContents)); + fs.writeFileSync(pkgJSONPath, JSON.stringify(pkgJSONContents)); init.writeFile(config, answers.format); assert.isTrue(fs.existsSync(filePath)); fs.unlinkSync(filePath); - fs.unlinkSync(path.resolve(fixtureDir, "package.json")); + fs.unlinkSync(pkgJSONPath); }); it("should create .eslintrc.js", () => { answers.format = "JavaScript"; - process.chdir(fixtureDir); - const config = init.processAnswers(answers); const filePath = path.resolve(fixtureDir, ".eslintrc.js"); - fs.writeFileSync(path.resolve(fixtureDir, "package.json"), JSON.stringify(pkgJSONContents)); + fs.writeFileSync(pkgJSONPath, JSON.stringify(pkgJSONContents)); init.writeFile(config, answers.format); assert.isTrue(fs.existsSync(filePath)); fs.unlinkSync(filePath); - fs.unlinkSync(path.resolve(fixtureDir, "package.json")); + fs.unlinkSync(pkgJSONPath); }); it("should create .eslintrc.yml", () => { answers.format = "YAML"; - process.chdir(fixtureDir); - const config = init.processAnswers(answers); const filePath = path.resolve(fixtureDir, ".eslintrc.yml"); - fs.writeFileSync(path.resolve(fixtureDir, "package.json"), JSON.stringify(pkgJSONContents)); + fs.writeFileSync(pkgJSONPath, JSON.stringify(pkgJSONContents)); init.writeFile(config, answers.format); assert.isTrue(fs.existsSync(filePath)); fs.unlinkSync(filePath); - fs.unlinkSync(path.resolve(fixtureDir, "package.json")); + fs.unlinkSync(pkgJSONPath); }); // For https://github.com/eslint/eslint/issues/14137 it("should create .eslintrc.cjs", () => { answers.format = "JavaScript"; - process.chdir(fixtureDir); - // create package.json with "type": "module" pkgJSONContents.type = "module"; - fs.writeFileSync(path.resolve(fixtureDir, "package.json"), JSON.stringify(pkgJSONContents)); + fs.writeFileSync(pkgJSONPath, JSON.stringify(pkgJSONContents)); const config = init.processAnswers(answers); const filePath = path.resolve(fixtureDir, ".eslintrc.cjs"); @@ -555,18 +552,16 @@ describe("configInitializer", () => { assert.isTrue(fs.existsSync(filePath)); fs.unlinkSync(filePath); - fs.unlinkSync(path.resolve(fixtureDir, "package.json")); + fs.unlinkSync(pkgJSONPath); }); it("should create .eslintrc.json even with type: 'module'", () => { answers.format = "JSON"; - process.chdir(fixtureDir); - // create package.json with "type": "module" pkgJSONContents.type = "module"; - fs.writeFileSync(path.resolve(fixtureDir, "package.json"), JSON.stringify(pkgJSONContents)); + fs.writeFileSync(pkgJSONPath, JSON.stringify(pkgJSONContents)); const config = init.processAnswers(answers); const filePath = path.resolve(fixtureDir, ".eslintrc.json"); @@ -576,7 +571,7 @@ describe("configInitializer", () => { assert.isTrue(fs.existsSync(filePath)); fs.unlinkSync(filePath); - fs.unlinkSync(path.resolve(fixtureDir, "package.json")); + fs.unlinkSync(pkgJSONPath); }); }); });