From 6f8815fe8e37d3c558692fbdf44c76e1a1cb294b Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sat, 24 Apr 2021 14:02:27 +0530 Subject: [PATCH] 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 5c546c7977bf..532b59453a1f 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 53be1b86bb7c..ca39806de414 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" }; @@ -525,4 +448,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")); + }); + }); });