From 2d0f467728a4ac5e4fcdab6e897781f154751269 Mon Sep 17 00:00:00 2001 From: Eric Cornelissen Date: Fri, 7 Apr 2023 12:13:00 +0200 Subject: [PATCH] fix: support running CLI in project without config file (#80) * Test behavior when no configuration file is present Update the CLI tests to include a test for the case where a project does not have a configuration file. This test currently fails because this is currently not supported due to a bug introduced in v2.2.0. To accommodate testing this, the configuration file that was present at the root of the project has been moved and related tests updated accordingly. This is necessary because svglint automatically searches for a configuration file up the file tree, so having a configuration file at the root of the project makes it impossible to test the scenario The new tests matches against a string because unfortunately the error is silent - it doesn't exit with a non-zero exit code. Improvements to this are welcome as contributions. * Support no configuration file for the CLI Update the CLI to work if no configuration file is found. This is the simplest fix I could think of. Theoretically the problem could be addressed in the API (svglint.js line 137 and 150) by replacing the default parameter `config={}` with an implementation that defaults to an empty object for both `undefined` **and** `null`. However, it's currently not clear that's the right move. Another alternative would be for `loadConfigurationFile` to return `undefined` instead of `null`. However, that could interfere with the intended behavior of that function, making it impossible to distinguish a missing default export from no-configuration-file-found. --- bin/cli.js | 2 ++ test/cli.spec.js | 7 ++++++- .svglintrc.js => test/projects/with-config/.svglintrc.js | 0 test/projects/without-config/.keep | 0 4 files changed, 8 insertions(+), 1 deletion(-) rename .svglintrc.js => test/projects/with-config/.svglintrc.js (100%) create mode 100644 test/projects/without-config/.keep diff --git a/bin/cli.js b/bin/cli.js index 9bafdf2..1079fa0 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -83,6 +83,8 @@ process.on("exit", () => { if (cli.flags.config) { logger.error("Configuration file not found"); process.exit(EXIT_CODES.configuration); + } else { + configObj = {}; } } } catch (e) { diff --git a/test/cli.spec.js b/test/cli.spec.js index ac1f72c..bdf5efb 100644 --- a/test/cli.spec.js +++ b/test/cli.spec.js @@ -47,7 +47,7 @@ describe("CLI", function(){ }); it("should fail with an invalid SVG", async function(){ - const { failed, exitCode } = await execCliWith([INVALID_SVG]); + const { failed, exitCode } = await execCliWith([INVALID_SVG], "test/projects/with-config"); expect(failed).toBeTruthy(); expect(exitCode).toBe(1); }); @@ -101,4 +101,9 @@ describe("Configuration files", function() { const { failed } = await execCliWith([VALID_SVG], "test/projects/cjs/bar/a/b/c"); expect(failed).toBeFalsy(); }); + + it("should succeed in a project without a config file", async function () { + const { stdout } = await execCliWith([VALID_SVG], "test/projects/without-config"); + expect(stdout).toNotMatch("Failed to lint"); + }); }); diff --git a/.svglintrc.js b/test/projects/with-config/.svglintrc.js similarity index 100% rename from .svglintrc.js rename to test/projects/with-config/.svglintrc.js diff --git a/test/projects/without-config/.keep b/test/projects/without-config/.keep new file mode 100644 index 0000000..e69de29