From 9091bf0a9b7df78cccaf4c30fab07ce22f9f9ebf Mon Sep 17 00:00:00 2001 From: sakamossan Date: Thu, 5 Jun 2025 09:56:23 +0900 Subject: [PATCH 1/2] feat(cli): Set exit code when repomix fails --- src/cli/cliRun.ts | 1 + tests/cli/cliRun.test.ts | 18 +++++++----------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/cli/cliRun.ts b/src/cli/cliRun.ts index d481e357..bb3381e0 100644 --- a/src/cli/cliRun.ts +++ b/src/cli/cliRun.ts @@ -140,6 +140,7 @@ export const run = async () => { await program.parseAsync(process.argv); } catch (error) { handleError(error); + process.exit(1) } }; diff --git a/tests/cli/cliRun.test.ts b/tests/cli/cliRun.test.ts index f8da7177..41abdf7f 100644 --- a/tests/cli/cliRun.test.ts +++ b/tests/cli/cliRun.test.ts @@ -1,4 +1,5 @@ import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; +import { program } from 'commander'; import * as defaultAction from '../../src/cli/actions/defaultAction.js'; import * as initAction from '../../src/cli/actions/initAction.js'; import * as remoteAction from '../../src/cli/actions/remoteAction.js'; @@ -36,16 +37,6 @@ vi.mock('../../src/shared/logger', () => ({ setLogLevelByEnv: vi.fn(), })); -vi.mock('commander', () => ({ - program: { - description: vi.fn().mockReturnThis(), - arguments: vi.fn().mockReturnThis(), - option: vi.fn().mockReturnThis(), - action: vi.fn().mockReturnThis(), - parseAsync: vi.fn().mockResolvedValue(undefined), - }, -})); - vi.mock('../../src/cli/actions/defaultAction'); vi.mock('../../src/cli/actions/initAction'); vi.mock('../../src/cli/actions/remoteAction'); @@ -163,8 +154,13 @@ describe('cliRun', () => { vi.mocked(versionAction.runVersionAction).mockResolvedValue(); }); - test('should run without arguments', async () => { + test('should call process.exit(1) on error', async () => { + const exitSpy = vi.spyOn(process, 'exit').mockImplementationOnce(() => undefined as never); + const parseSpy = vi.spyOn(program, 'description').mockImplementationOnce(() => { throw Error() }); await expect(run()).resolves.not.toThrow(); + expect(exitSpy).toHaveBeenCalledWith(1) + exitSpy.mockReset(); + parseSpy.mockReset(); }); describe('executeAction', () => { From 97d550e946139ecc0ea8f62a1e9fff49e8cd2428 Mon Sep 17 00:00:00 2001 From: sakamossan Date: Thu, 5 Jun 2025 10:09:28 +0900 Subject: [PATCH 2/2] add test --- src/cli/cliRun.ts | 2 +- tests/cli/cliRun.test.ts | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/cli/cliRun.ts b/src/cli/cliRun.ts index bb3381e0..a07b571f 100644 --- a/src/cli/cliRun.ts +++ b/src/cli/cliRun.ts @@ -140,7 +140,7 @@ export const run = async () => { await program.parseAsync(process.argv); } catch (error) { handleError(error); - process.exit(1) + process.exit(1); } }; diff --git a/tests/cli/cliRun.test.ts b/tests/cli/cliRun.test.ts index 41abdf7f..d400df06 100644 --- a/tests/cli/cliRun.test.ts +++ b/tests/cli/cliRun.test.ts @@ -1,5 +1,5 @@ -import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; import { program } from 'commander'; +import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; import * as defaultAction from '../../src/cli/actions/defaultAction.js'; import * as initAction from '../../src/cli/actions/initAction.js'; import * as remoteAction from '../../src/cli/actions/remoteAction.js'; @@ -156,11 +156,16 @@ describe('cliRun', () => { test('should call process.exit(1) on error', async () => { const exitSpy = vi.spyOn(process, 'exit').mockImplementationOnce(() => undefined as never); - const parseSpy = vi.spyOn(program, 'description').mockImplementationOnce(() => { throw Error() }); + const parseSpy = vi.spyOn(program, 'description').mockImplementationOnce(() => { + throw Error(); + }); + const handleErrorSpy = vi.spyOn(logger, 'error'); await expect(run()).resolves.not.toThrow(); - expect(exitSpy).toHaveBeenCalledWith(1) + expect(exitSpy).toHaveBeenCalledWith(1); + expect(handleErrorSpy).toHaveBeenCalled(); exitSpy.mockReset(); parseSpy.mockReset(); + handleErrorSpy.mockReset(); }); describe('executeAction', () => {