From fe4e6722f597beaa1b4dd01924b689beb42fb8f0 Mon Sep 17 00:00:00 2001 From: Leah Wasser Date: Sat, 7 Nov 2026 20:03:10 -0700 Subject: [PATCH 1/3] fix: show a friendly error when readme etc is missing test: add await so error reach CLI error handler and throw proper returns chore: lint ci: add e2e to ci test: add test and cleaner message for when json file is missing test: cleanup unit test to check for UserError and match message --- src/util/__tests__/config-file.js | 8 ++++++-- src/util/config-file.js | 9 ++++++--- test/e2e/cli.test.js | 11 +++++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/util/__tests__/config-file.js b/src/util/__tests__/config-file.js index 2325b327..1c45e35a 100644 --- a/src/util/__tests__/config-file.js +++ b/src/util/__tests__/config-file.js @@ -5,6 +5,7 @@ import path from 'path'; import { expect, test } from 'vitest'; import { readConfig, writeConfig, writeContributors } from '../config-file.js'; +import { UserError } from '../user-error.js'; const absentFile = './abc'; const absentConfigFileExpected = `Configuration file not found: ${absentFile}`; @@ -35,8 +36,11 @@ const NoFilesConfigFile = { files: [], }; -test('Reading an absent configuration file throws a helpful error', async () => { - await expect(readConfig(absentFile)).rejects.toThrow(absentConfigFileExpected); +test('Reading an absent configuration file throws a helpful UserError', async () => { + const result = readConfig(absentFile); + + await expect(result).rejects.toBeInstanceOf(UserError); + await expect(result).rejects.toThrow(absentConfigFileExpected); }); test('Writing contributors in an absent configuration file throws a helpful error', async () => { diff --git a/src/util/config-file.js b/src/util/config-file.js index 76659f39..867f5920 100644 --- a/src/util/config-file.js +++ b/src/util/config-file.js @@ -3,6 +3,7 @@ import { promises as fs } from 'fs'; import jf from 'json-fixer'; import { formatConfig } from './formatting.js'; +import { UserError } from './user-error.js'; export async function readConfig(configPath) { const config = await readConfigFile(configPath); @@ -31,9 +32,11 @@ async function readConfigFile(configPath) { } if (error.code === 'ENOENT') { - throw new Error(`Configuration file not found: ${configPath}`, { - cause: error, - }); + throw new UserError( + `Configuration file not found: ${configPath}\n` + + 'Check that the path is correct, or run `all-contributors init` ' + + 'to create a new configuration file.', + ); } throw error; diff --git a/test/e2e/cli.test.js b/test/e2e/cli.test.js index 7d12dcbd..7aad6c59 100644 --- a/test/e2e/cli.test.js +++ b/test/e2e/cli.test.js @@ -44,3 +44,14 @@ it('generate exits 1 when the README is missing', async ({ projectWithoutReadme expect(result.stderr).not.toContain('ENOENT'); expect(result.stderr).not.toContain('at readFileToUpdate'); }); + +it('exits 1 when the -c config file does not exist', async ({ tmpDir }) => { + const result = await runCli(['generate', '-c', 'missing.json'], { + cwd: tmpDir, + }); + + expect(result.code).toBe(1); + expect(result.stderr).toContain('Configuration file not found'); + expect(result.stderr).not.toContain('at readConfigFile'); + expect(result.stderr).toContain('all-contributors init'); +}); From b237ac2cee832016f90ae480474dfc24792fe12b Mon Sep 17 00:00:00 2001 From: Leah Wasser Date: Tue, 10 Nov 2026 18:53:58 -0700 Subject: [PATCH 2/3] test: missing config file --- test/e2e/cli.test.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/e2e/cli.test.js b/test/e2e/cli.test.js index 7aad6c59..b41ed74c 100644 --- a/test/e2e/cli.test.js +++ b/test/e2e/cli.test.js @@ -11,6 +11,10 @@ const it = baseIt.extend({ await rm(path.join(project, 'README.md')); await use(project); }, + projectWithoutConfig: async ({ project }, use) => { + await rm(path.join(project, '.all-contributorsrc')); + await use(project); + }, }); it('--help exits 0 and lists the commands', async ({ tmpDir }) => { @@ -45,6 +49,12 @@ it('generate exits 1 when the README is missing', async ({ projectWithoutReadme expect(result.stderr).not.toContain('at readFileToUpdate'); }); +it('generate exits 0 when the default config is missing', async ({ projectWithoutConfig }) => { + const result = await runCli(['generate'], { cwd: projectWithoutConfig }); + + expect(result.code).toBe(0); +}); + it('exits 1 when the -c config file does not exist', async ({ tmpDir }) => { const result = await runCli(['generate', '-c', 'missing.json'], { cwd: tmpDir, From 0a745803facd7390aefb42411abedb1e2db0c884 Mon Sep 17 00:00:00 2001 From: Leah Wasser Date: Tue, 10 Nov 2026 19:50:52 -0700 Subject: [PATCH 3/3] fix: show friendly errors and round out e2e tests for this --- src/cli.js | 8 ++++-- src/util/__tests__/config-file.js | 8 ++++++ src/util/config-file.js | 44 ++++++++++++++++++------------- src/util/user-error.js | 4 +-- test/e2e/cli.test.js | 27 ++++++++++++++++++- 5 files changed, 68 insertions(+), 23 deletions(-) diff --git a/src/cli.js b/src/cli.js index b24edb00..5e0e0037 100755 --- a/src/cli.js +++ b/src/cli.js @@ -145,7 +145,7 @@ async function run() { const configData = await util.configFile.readConfig(argv.config); Object.assign(argv, configData); } catch (error) { - if (error instanceof SyntaxError || argv.config !== defaultRCFile) { + if (argv.config !== defaultRCFile || error.cause?.code !== 'ENOENT') { throw error; } // If default config file doesn't exist, that's okay @@ -163,7 +163,11 @@ async function run() { case 'check': return await checkContributors(argv); default: - throw new Error(`Unknown command ${command}`); + throw new util.UserError( + `Unknown command: ${command}\n` + + 'Valid commands are init, generate, add, and check.\n' + + 'Run `all-contributors --help` for more details.', + ); } } catch (e) { if (e instanceof util.UserError) { diff --git a/src/util/__tests__/config-file.js b/src/util/__tests__/config-file.js index 1c45e35a..c7b00acf 100644 --- a/src/util/__tests__/config-file.js +++ b/src/util/__tests__/config-file.js @@ -43,6 +43,14 @@ test('Reading an absent configuration file throws a helpful UserError', async () await expect(result).rejects.toThrow(absentConfigFileExpected); }); +test('Reading a configuration file with malformed JSON throws a UserError', async () => { + const directory = await fs.mkdtemp(path.join(os.tmpdir(), 'all-contributors-')); + const configPath = path.join(directory, '.all-contributorsrc'); + await fs.writeFile(configPath, '{'); + + await expect(readConfig(configPath)).rejects.toBeInstanceOf(UserError); +}); + test('Writing contributors in an absent configuration file throws a helpful error', async () => { await expect(writeContributors(absentFile, [])).rejects.toThrow(absentConfigFileExpected); }); diff --git a/src/util/config-file.js b/src/util/config-file.js index 867f5920..e89d15ed 100644 --- a/src/util/config-file.js +++ b/src/util/config-file.js @@ -12,35 +12,43 @@ export async function readConfig(configPath) { } async function readConfigFile(configPath) { + let configFileContents; try { - const configFileContents = await fs.readFile(configPath, 'utf-8'); - const { data: config, changed } = jf(configFileContents); - - if (changed) { - const formatterConfig = await formatConfig(configPath, config); - //Updates the file with fixes - await fs.writeFile(configPath, formatterConfig); - } - - return config; + configFileContents = await fs.readFile(configPath, 'utf-8'); } catch (error) { - if (error instanceof SyntaxError) { - throw new SyntaxError( - `Configuration file has malformed JSON: ${configPath}. Error:: ${error.message}`, - { cause: error }, - ); - } - if (error.code === 'ENOENT') { throw new UserError( `Configuration file not found: ${configPath}\n` + 'Check that the path is correct, or run `all-contributors init` ' + 'to create a new configuration file.', + { cause: error }, ); } - throw error; } + + let config; + let changed; + try { + // json-fixer prints "There's still an error!" to stderr itself before + // throwing on malformed JSON message. json-fixer has no option to turn that off. + // we could do something hacky to silence this in the future + ({ data: config, changed } = jf(configFileContents)); + } catch (error) { + throw new UserError( + `Configuration file has malformed JSON: ${configPath}\n${error.message}\n` + + 'Fix the JSON in this file, or delete it and run `all-contributors init`.', + { cause: error }, + ); + } + + if (changed) { + const formatterConfig = await formatConfig(configPath, config); + //Updates the file with fixes + await fs.writeFile(configPath, formatterConfig); + } + + return config; } export async function writeConfig(configPath, content) { diff --git a/src/util/user-error.js b/src/util/user-error.js index d44fd0f1..d7f001d8 100644 --- a/src/util/user-error.js +++ b/src/util/user-error.js @@ -3,8 +3,8 @@ * The CLI prints only the message for these, without a stack trace. */ export class UserError extends Error { - constructor(message) { - super(message); + constructor(message, options) { + super(message, options); this.name = 'UserError'; } } diff --git a/test/e2e/cli.test.js b/test/e2e/cli.test.js index b41ed74c..4c686ccc 100644 --- a/test/e2e/cli.test.js +++ b/test/e2e/cli.test.js @@ -1,4 +1,4 @@ -import { readdir, readFile, rm } from 'node:fs/promises'; +import { readdir, readFile, rm, writeFile } from 'node:fs/promises'; import path from 'node:path'; import { expect } from 'vitest'; @@ -15,6 +15,10 @@ const it = baseIt.extend({ await rm(path.join(project, '.all-contributorsrc')); await use(project); }, + projectWithMalformedConfig: async ({ project }, use) => { + await writeFile(path.join(project, '.all-contributorsrc'), '{'); + await use(project); + }, }); it('--help exits 0 and lists the commands', async ({ tmpDir }) => { @@ -55,6 +59,27 @@ it('generate exits 0 when the default config is missing', async ({ projectWithou expect(result.code).toBe(0); }); +it('generate exits 1 when the default config has malformed JSON', async ({ + projectWithMalformedConfig, +}) => { + const result = await runCli(['generate'], { cwd: projectWithMalformedConfig }); + + expect(result.code).toBe(1); + expect(result.stderr).toContain('Configuration file has malformed JSON'); + expect(result.stderr).toContain('.all-contributorsrc'); + expect(result.stderr).toContain('all-contributors init'); + expect(result.stderr).not.toContain('at readConfigFile'); +}); + +it('exits 1 for an unknown command', async ({ tmpDir }) => { + const result = await runCli(['nope'], { cwd: tmpDir }); + + expect(result.code).toBe(1); + expect(result.stderr).toContain('Unknown command: nope'); + expect(result.stderr).toContain('all-contributors --help'); + expect(result.stderr).not.toContain('at run'); +}); + it('exits 1 when the -c config file does not exist', async ({ tmpDir }) => { const result = await runCli(['generate', '-c', 'missing.json'], { cwd: tmpDir,