From babd16f68f0ef1fc8539de7e93eae6520786af22 Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Wed, 12 Oct 2022 23:26:38 +0800 Subject: [PATCH] refactor(cli): exit normally when no alteration found --- .../src/commands/database/alteration/index.test.ts | 14 ++++++-------- .../src/commands/database/alteration/version.ts | 10 +++++----- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/cli/src/commands/database/alteration/index.test.ts b/packages/cli/src/commands/database/alteration/index.test.ts index c5f40dc83..5584dcf83 100644 --- a/packages/cli/src/commands/database/alteration/index.test.ts +++ b/packages/cli/src/commands/database/alteration/index.test.ts @@ -13,10 +13,6 @@ const pool = createMockPool({ }, }); -const mockExit = jest.fn((code?: number) => { - throw new Error(String(code)); -}); - describe('getUndeployedAlterations()', () => { const files = Object.freeze([ { filename: '1.0.0-1663923770-a.js', path: '/alterations/1.0.0-1663923770-a.js' }, @@ -63,14 +59,16 @@ describe('chooseAlterationsByVersion()', () => { ].map((filename) => ({ filename, path: '/alterations/' + filename })) ); - it('exits with code 1 when no alteration file available', async () => { - jest.spyOn(process, 'exit').mockImplementation(mockExit); - await expect(chooseAlterationsByVersion([], 'v1.0.0')).rejects.toThrow('1'); - mockExit.mockRestore(); + it('chooses nothing when input version is invalid', async () => { + await expect(chooseAlterationsByVersion(files, 'next1')).rejects.toThrow( + 'Invalid Version: next1' + ); + await expect(chooseAlterationsByVersion([], 'ok')).rejects.toThrow('Invalid Version: ok'); }); it('chooses correct alteration files', async () => { await Promise.all([ + expect(chooseAlterationsByVersion([], 'v1.0.0')).resolves.toEqual([]), expect(chooseAlterationsByVersion(files, 'v1.0.0')).resolves.toEqual(files.slice(0, 7)), expect(chooseAlterationsByVersion(files, 'v1.0.0-beta.10')).resolves.toEqual( files.slice(0, 3) diff --git a/packages/cli/src/commands/database/alteration/version.ts b/packages/cli/src/commands/database/alteration/version.ts index 355474a16..096ff56b1 100644 --- a/packages/cli/src/commands/database/alteration/version.ts +++ b/packages/cli/src/commands/database/alteration/version.ts @@ -27,7 +27,7 @@ export const chooseAlterationsByVersion = async ( ); if (endIndex === -1) { - log.error('No alteration script to deploy'); + return []; } log.info(`Deploy target ${chalk.green(nextTag)}`); @@ -43,10 +43,6 @@ export const chooseAlterationsByVersion = async ( .slice() .sort((i, j) => compare(j, i)); - if (!versions[0]) { - log.error('No alteration script to deploy'); - } - const { version: targetVersion } = initialVersion === latestTag ? { version: versions[0] } @@ -65,6 +61,10 @@ export const chooseAlterationsByVersion = async ( } ); + if (!targetVersion) { + return []; + } + log.info(`Deploy target ${chalk.green(targetVersion.version)}`); return alterations.filter(({ filename }) => {