From 43580a8cb9133d26c9ed9dd38a9004a72351b5a0 Mon Sep 17 00:00:00 2001 From: Alexey Orlenko Date: Thu, 10 Nov 2022 20:05:02 +0100 Subject: [PATCH] fix(cli): don't crash when checking for updates from integration version with a long name (#16227) Fixes: #9429 Fixes: #16126 Ref: https://prisma-company.slack.com/archives/C02FZENHD4N/p1668078372144739 --- .../src/__tests__/printUpdateMessage.test.ts | 55 +++++++++++++++++++ packages/internals/src/utils/drawBox.ts | 2 +- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 packages/cli/src/__tests__/printUpdateMessage.test.ts diff --git a/packages/cli/src/__tests__/printUpdateMessage.test.ts b/packages/cli/src/__tests__/printUpdateMessage.test.ts new file mode 100644 index 000000000000..2c5ea1600f17 --- /dev/null +++ b/packages/cli/src/__tests__/printUpdateMessage.test.ts @@ -0,0 +1,55 @@ +import { printUpdateMessage } from '../utils/printUpdateMessage' + +function printUpdateMessageFromTo(from: string, to: string): void { + printUpdateMessage({ + status: 'ok', + data: { + client_event_id: '', + previous_client_event_id: '', + product: '', + cli_path_hash: '', + local_timestamp: '', + previous_version: from, + current_version: to, + current_release_date: Date.now(), + current_download_url: '', + current_changelog_url: '', + package: 'prisma', + release_tag: to, + install_command: '', + project_website: '', + outdated: true, + alerts: [], + }, + }) +} + +const consoleErrorMock = jest.spyOn(console, 'error').mockImplementation() + +afterEach(() => { + consoleErrorMock.mockReset() +}) + +test('normal release', () => { + printUpdateMessageFromTo('4.5.0', '4.6.0') + expect(consoleErrorMock.mock.calls[0][0]).toMatchInlineSnapshot(` + ┌─────────────────────────────────────────────────────────┐ + │ Update available 4.5.0 -> 4.6.0 │ + │ Run the following to update │ + │ npm i --save-dev prisma@4.6.0 │ + │ npm i @prisma/client@4.6.0 │ + └─────────────────────────────────────────────────────────┘ + `) +}) + +test('integration version with long name', () => { + printUpdateMessageFromTo('4.5.0-integration-use-keep-alive-for-node-fetch.1', '4.6.0') + expect(consoleErrorMock.mock.calls[0][0]).toMatchInlineSnapshot(` + ┌───────────────────────────────────────────────────────────────────────────────┐ + │ Update available 4.5.0-integration-use-keep-alive-for-node-fetch.1 -> 4.6.0 │ + │ Run the following to update │ + │ npm i --save-dev prisma@4.6.0 │ + │ npm i @prisma/client@4.6.0 │ + └───────────────────────────────────────────────────────────────────────────────┘ + `) +}) diff --git a/packages/internals/src/utils/drawBox.ts b/packages/internals/src/utils/drawBox.ts index befea63b5fc2..dff1cd3328f3 100644 --- a/packages/internals/src/utils/drawBox.ts +++ b/packages/internals/src/utils/drawBox.ts @@ -26,7 +26,7 @@ function maxLineLength(str: string): number { export function drawBox({ title, width, height, str, horizontalPadding }: BoxOptions): string { horizontalPadding = horizontalPadding || 0 - width = width || maxLineLength(str) + horizontalPadding * 2 + width = Math.max(width, maxLineLength(str) + horizontalPadding * 2) const topLine = title ? chalk.grey(chars.topLeft + chars.horizontal) + ' ' +