From 09dee060b2c2c4a7757cb72a46a6422c34af8ce2 Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Tue, 17 Mar 2020 23:45:59 +0100 Subject: [PATCH] Fix: Newline before eof when creating config via --init (#12952) * Fix: Newline before eof when creating config via --init * Add test * Make a dedicated test (with a bit of duplication) --- lib/init/config-file.js | 4 ++-- tests/lib/init/config-file.js | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/init/config-file.js b/lib/init/config-file.js index 960b572cddb3..fc62b81525e6 100644 --- a/lib/init/config-file.js +++ b/lib/init/config-file.js @@ -45,7 +45,7 @@ function sortByKey(a, b) { function writeJSONConfigFile(config, filePath) { debug(`Writing JSON config file: ${filePath}`); - const content = stringify(config, { cmp: sortByKey, space: 4 }); + const content = `${stringify(config, { cmp: sortByKey, space: 4 })}\n`; fs.writeFileSync(filePath, content, "utf8"); } @@ -80,7 +80,7 @@ function writeJSConfigFile(config, filePath) { debug(`Writing JS config file: ${filePath}`); let contentToWrite; - const stringifiedContent = `module.exports = ${stringify(config, { cmp: sortByKey, space: 4 })};`; + const stringifiedContent = `module.exports = ${stringify(config, { cmp: sortByKey, space: 4 })};\n`; try { const { CLIEngine } = require("../cli-engine"); diff --git a/tests/lib/init/config-file.js b/tests/lib/init/config-file.js index 45cca55f9336..69556b4b5b61 100644 --- a/tests/lib/init/config-file.js +++ b/tests/lib/init/config-file.js @@ -82,6 +82,21 @@ describe("ConfigFile", () => { StubbedConfigFile.write(config, filename); }); + it("should include a newline character at EOF", () => { + const fakeFS = leche.fake(fs); + + sinon.mock(fakeFS).expects("writeFileSync").withExactArgs( + filename, + sinon.match(value => value.endsWith("\n")), + "utf8" + ); + + const StubbedConfigFile = proxyquire("../../../lib/init/config-file", { + fs: fakeFS + }); + + StubbedConfigFile.write(config, filename); + }); }); it("should make sure js config files match linting rules", () => {