From f6aa58d7b0ac7fa7a081edcbc454368c21773ffe Mon Sep 17 00:00:00 2001 From: Will Conrad Date: Mon, 21 Oct 2024 20:20:17 -0500 Subject: [PATCH] fix: refactored prompts and tests messages Co-authored-by: Thomas Lane <163203257+tlane25@users.noreply.github.com> Co-authored-by: Thomas Lane --- src/commands/base-command.ts | 6 ++ src/commands/blobs/blobs-set.ts | 12 ++- src/commands/env/env-clone.ts | 10 ++- src/commands/env/env-set.ts | 6 ++ src/commands/env/env-unset.ts | 6 ++ src/utils/prompts/blob-set-prompt.ts | 19 ++--- src/utils/prompts/env-clone-prompt.ts | 61 ++++++--------- src/utils/prompts/env-set-prompts.ts | 35 +++------ src/utils/prompts/prompt-messages.ts | 37 ++++++++++ src/utils/prompts/unset-set-prompts.ts | 25 +++---- src/utils/types.ts | 14 ++++ .../commands/blobs/blobs-set.test.ts | 30 +++----- tests/integration/commands/env/api-routes.ts | 9 ++- .../commands/env/env-clone.test.ts | 74 +++++++++---------- .../commands/env/env-unset.test.ts | 17 +++-- 15 files changed, 206 insertions(+), 155 deletions(-) create mode 100644 src/utils/prompts/prompt-messages.ts diff --git a/src/commands/base-command.ts b/src/commands/base-command.ts index 406d8a546b2..13d7e11b95a 100644 --- a/src/commands/base-command.ts +++ b/src/commands/base-command.ts @@ -167,6 +167,12 @@ export default class BaseCommand extends Command { // eslint-disable-next-line workspace/no-process-cwd workingDir = process.cwd() + /** + * Determines if the command is scripted or not. + * If the command is scripted (SHLVL is greater than 1 or CI/CONTINUOUS_INTEGRATION is true) then some commands + * might behave differently. + */ + scriptedCommand = Boolean(process.env.SHLVL !== '1' || process.env.CI || process.env.CONTINUOUS_INTEGRATION) /** * The workspace root if inside a mono repository. * Must not be the repository root! diff --git a/src/commands/blobs/blobs-set.ts b/src/commands/blobs/blobs-set.ts index da81e5a14f3..df096000d76 100644 --- a/src/commands/blobs/blobs-set.ts +++ b/src/commands/blobs/blobs-set.ts @@ -5,12 +5,12 @@ import { getStore } from '@netlify/blobs' import { OptionValues } from 'commander' import { chalk, error as printError, isNodeError, log } from '../../utils/command-helpers.js' -import { blobSetPrompts } from '../../utils/prompts/blob-set-prompt.js' +import { promptBlobSetOverwrite } from '../../utils/prompts/blob-set-prompt.js' import BaseCommand from '../base-command.js' interface Options extends OptionValues { input?: string - force?: string + force?: string | boolean } export const blobsSet = async ( @@ -20,6 +20,12 @@ export const blobsSet = async ( options: Options, command: BaseCommand, ) => { + + // Prevents prompts from blocking scripted commands + if (command.scriptedCommand){ + options.force = true + } + const { api, siteInfo } = command.netlify const { force, input } = options const store = getStore({ @@ -61,7 +67,7 @@ export const blobsSet = async ( const existingValue = await store.get(key) if (existingValue) { - await blobSetPrompts(key, storeName) + await promptBlobSetOverwrite(key, storeName) } } diff --git a/src/commands/env/env-clone.ts b/src/commands/env/env-clone.ts index 78d14a8d05a..c8a450e78d9 100644 --- a/src/commands/env/env-clone.ts +++ b/src/commands/env/env-clone.ts @@ -1,7 +1,7 @@ import { OptionValues } from 'commander' import { chalk, log, error as logError } from '../../utils/command-helpers.js' -import { envClonePrompts } from '../../utils/prompts/env-clone-prompt.js' +import { promptEnvCloneOverwrite } from '../../utils/prompts/env-clone-prompt.js' import BaseCommand from '../base-command.js' // @ts-expect-error TS(7006) FIXME: Parameter 'api' implicitly has an 'any' type. @@ -39,7 +39,7 @@ const cloneEnvVars = async ({ api, force, siteFrom, siteTo }): Promise const envVarsToDelete = envelopeTo.filter(({ key }) => keysFrom.includes(key)) if (envVarsToDelete.length !== 0 && Boolean(force) === false) { - await envClonePrompts(siteTo.id, envVarsToDelete) + await promptEnvCloneOverwrite(siteTo.id, envVarsToDelete) } // delete marked env vars in parallel // @ts-expect-error TS(7031) FIXME: Binding element 'key' implicitly has an 'any' type... Remove this comment to see the full error message @@ -56,6 +56,12 @@ const cloneEnvVars = async ({ api, force, siteFrom, siteTo }): Promise } export const envClone = async (options: OptionValues, command: BaseCommand) => { + + // Prevents prompts from blocking scripted commands + if (command.scriptedCommand){ + options.force = true + } + const { api, site } = command.netlify const { force } = options diff --git a/src/commands/env/env-set.ts b/src/commands/env/env-set.ts index 9a7bd3a83b8..01a1460179a 100644 --- a/src/commands/env/env-set.ts +++ b/src/commands/env/env-set.ts @@ -116,6 +116,12 @@ const setInEnvelope = async ({ api, context, force, key, scope, secret, siteInfo } export const envSet = async (key: string, value: string, options: OptionValues, command: BaseCommand) => { + + // Prevents prompts from blocking scripted commands + if (command.scriptedCommand){ + options.force = true + } + const { context, force, scope, secret } = options const { api, cachedConfig, site } = command.netlify const siteId = site.id diff --git a/src/commands/env/env-unset.ts b/src/commands/env/env-unset.ts index d8d1311615e..30131287d55 100644 --- a/src/commands/env/env-unset.ts +++ b/src/commands/env/env-unset.ts @@ -68,6 +68,12 @@ const unsetInEnvelope = async ({ api, context, force, key, siteInfo }) => { } export const envUnset = async (key: string, options: OptionValues, command: BaseCommand) => { + + // Prevents prompts from blocking scripted commands + if (command.scriptedCommand){ + options.force = true + } + const { context, force } = options const { api, cachedConfig, site } = command.netlify const siteId = site.id diff --git a/src/utils/prompts/blob-set-prompt.ts b/src/utils/prompts/blob-set-prompt.ts index 677839a2762..2162d23ffe8 100644 --- a/src/utils/prompts/blob-set-prompt.ts +++ b/src/utils/prompts/blob-set-prompt.ts @@ -1,18 +1,19 @@ import { chalk, log } from '../command-helpers.js' import { confirmPrompt } from './confirm-prompt.js' +import { destructiveCommandMessages } from './prompt-messages.js' + +export const promptBlobSetOverwrite = async (key: string, storeName: string): Promise => { + const { overwriteNoticeMessage } = destructiveCommandMessages + const { generateWarningMessage, overwriteConfirmationMessage } = destructiveCommandMessages.blobSet + + const warningMessage = generateWarningMessage(storeName) -const generateBlobWarningMessage = (key: string, storeName: string): void => { log() - log(`${chalk.redBright('Warning')}: The following blob key already exists in store ${chalk.cyan(storeName)}:`) + log(warningMessage) log() log(`${chalk.bold(key)}`) log() - log(`This operation will ${chalk.redBright('overwrite')} the existing value.`) - log(`${chalk.yellowBright('Notice')}: To overwrite without this warning, you can use the --force flag.`) -} - -export const blobSetPrompts = async (key: string, storeName: string): Promise => { - generateBlobWarningMessage(key, storeName) - await confirmPrompt('Do you want to proceed with overwriting this blob key existing value?') + log(overwriteNoticeMessage) + await confirmPrompt(overwriteConfirmationMessage) } diff --git a/src/utils/prompts/env-clone-prompt.ts b/src/utils/prompts/env-clone-prompt.ts index fbcf67000e6..66cb2c3c804 100644 --- a/src/utils/prompts/env-clone-prompt.ts +++ b/src/utils/prompts/env-clone-prompt.ts @@ -1,49 +1,34 @@ -import { chalk, log } from '../command-helpers.js' +import { log } from '../command-helpers.js' +import { EnvVar } from '../types.js' import { confirmPrompt } from './confirm-prompt.js' +import { destructiveCommandMessages } from './prompt-messages.js' -type User = { - id: string - email: string - avatar_url: string - full_name: string -} +export const generateEnvVarsList = (envVarsToDelete: EnvVar[]) => envVarsToDelete.map((envVar) => envVar.key) -type EnvVar = { - key: string - scopes: string[] - values: Record[] - updated_at: string - updated_by: User - is_secret: boolean -} +/** + * Prompts the user to confirm overwriting environment variables on a site. + * + * @param {string} siteId - The ID of the site. + * @param {EnvVar[]} existingEnvVars - The environment variables that already exist on the site. + * @returns {Promise} A promise that resolves when the user has confirmed the overwriting of the variables. + */ +export async function promptEnvCloneOverwrite(siteId: string, existingEnvVars: EnvVar[]): Promise { + const { overwriteNoticeMessage } = destructiveCommandMessages + const { generateWarningMessage, noticeEnvVarsMessage, overwriteConfirmationMessage } = + destructiveCommandMessages.envClone + + const existingEnvVarKeys = generateEnvVarsList(existingEnvVars) + const warningMessage = generateWarningMessage(siteId) -const generateSetMessage = (envVarsToDelete: EnvVar[], siteId: string): void => { log() - log( - `${chalk.redBright( - 'Warning', - )}: The following environment variables are already set on the site with ID ${chalk.bgBlueBright( - siteId, - )}. They will be overwritten!`, - ) + log(warningMessage) log() - - log(`${chalk.yellowBright('Notice')}: The following variables will be overwritten:`) + log(noticeEnvVarsMessage) log() - envVarsToDelete.forEach((envVar) => { - log(envVar.key) - }) - + existingEnvVarKeys.forEach(log) log() - log( - `${chalk.yellowBright( - 'Notice', - )}: To overwrite the existing variables without confirmation prompts, pass the --force flag.`, - ) -} + log(overwriteNoticeMessage) -export const envClonePrompts = async (siteId: string, envVarsToDelete: EnvVar[]): Promise => { - generateSetMessage(envVarsToDelete, siteId) - await confirmPrompt('Do you want to proceed with overwriting these variables?') + await confirmPrompt(overwriteConfirmationMessage) } diff --git a/src/utils/prompts/env-set-prompts.ts b/src/utils/prompts/env-set-prompts.ts index f99a73cedf0..e256b333b4d 100644 --- a/src/utils/prompts/env-set-prompts.ts +++ b/src/utils/prompts/env-set-prompts.ts @@ -1,34 +1,23 @@ -import { chalk, log } from '../command-helpers.js' +import { log } from '../command-helpers.js' import { confirmPrompt } from './confirm-prompt.js' - -export const generateSetMessage = (variableName: string) => ({ - warningMessage: `${chalk.redBright('Warning')}: The environment variable ${chalk.bgBlueBright( - variableName, - )} already exists!`, - noticeMessage: `${chalk.yellowBright( - 'Notice', - )}: To overwrite the existing variable without confirmation, pass the -f or --force flag.`, - confirmMessage: 'The environment variable already exists. Do you want to overwrite it?', -}) +import { destructiveCommandMessages } from './prompt-messages.js' /** - * Generates warning, notice and confirm messages when trying to set an env variable - * that already exists. + * Prompts the user to confirm overwriting an existing environment variable. * - * @param {string} key - The key of the environment variable that already exists - * @returns {Object} An object with the following properties: - * - warning: A warning message to be displayed to the user - * - notice: A notice message to be displayed to the user - * - confirm: A confirmation prompt to ask the user if they want to overwrite the existing variable + * @param {string} existingKey - The key of the existing environment variable. + * @returns {Promise} A promise that resolves when the user confirms overwriting the variable. */ -export const promptOverwriteEnvVariable = async (key: string): Promise => { - const { confirmMessage, noticeMessage, warningMessage } = generateSetMessage(key) +export const promptOverwriteEnvVariable = async (existingKey: string): Promise => { + const { overwriteNoticeMessage } = destructiveCommandMessages + const { generateWarningMessage, overwriteConfirmationMessage } = destructiveCommandMessages.envSet + + const warningMessage = generateWarningMessage(existingKey) log() log(warningMessage) log() - log(noticeMessage) - log() - await confirmPrompt(confirmMessage) + log(overwriteNoticeMessage) + await confirmPrompt(overwriteConfirmationMessage) } diff --git a/src/utils/prompts/prompt-messages.ts b/src/utils/prompts/prompt-messages.ts new file mode 100644 index 00000000000..663706a6190 --- /dev/null +++ b/src/utils/prompts/prompt-messages.ts @@ -0,0 +1,37 @@ +import { chalk } from '../command-helpers.js' +import { EnvVar } from '../types.js' + +export const destructiveCommandMessages = { + overwriteNoticeMessage: `${chalk.yellowBright( + 'Notice', + )}: To overwrite without this warning, you can use the --force flag.`, + + blobSet: { + generateWarningMessage: (storeName: string) => + `${chalk.redBright('Warning')}: The blob key already exists in store ${chalk.cyan(storeName)}.`, + overwriteConfirmationMessage: 'Do you want to proceed with overwriting this blob key existing value?', + }, + + envSet: { + generateWarningMessage: (variableName: string) => + `${chalk.redBright('Warning')}: The environment variable ${chalk.bgBlueBright(variableName)} already exists.`, + overwriteConfirmationMessage: 'The environment variable already exists. Do you want to overwrite it?', + }, + + envUnset: { + generateWarningMessage: (variableName: string) => + `${chalk.redBright('Warning')}: The environment variable ${chalk.bgBlueBright(variableName)} already exists!`, + overwriteConfirmationMessage: 'The environment variable already exists. Do you want to overwrite it?', + }, + + envClone: { + generateWarningMessage: (siteId: string) => + `${chalk.redBright( + 'Warning', + )}: The following environment variables are already set on the site with ID ${chalk.bgBlueBright( + siteId, + )}. They will be overwritten!`, + noticeEnvVarsMessage: `${chalk.yellowBright('Notice')}: The following variables will be overwritten:`, + overwriteConfirmationMessage: 'The environment variables already exist. Do you want to overwrite them?', + }, +} diff --git a/src/utils/prompts/unset-set-prompts.ts b/src/utils/prompts/unset-set-prompts.ts index 79b89f316ec..8128922636e 100644 --- a/src/utils/prompts/unset-set-prompts.ts +++ b/src/utils/prompts/unset-set-prompts.ts @@ -1,16 +1,7 @@ -import { chalk, log } from '../command-helpers.js' +import { log } from '../command-helpers.js' import { confirmPrompt } from './confirm-prompt.js' - -export const generateUnsetMessage = (variableName: string) => ({ - warningMessage: `${chalk.redBright('Warning')}: The environment variable ${chalk.bgBlueBright( - variableName, - )} already exists!`, - noticeMessage: `${chalk.yellowBright( - 'Notice', - )}: To overwrite the existing variable without confirmation, pass the -f or --force flag.`, - confirmMessage: 'The environment variable already exists. Do you want to overwrite it?', -}) +import { destructiveCommandMessages } from './prompt-messages.js' /** * Logs a warning and prompts user to confirm overwriting an existing environment variable @@ -18,10 +9,14 @@ export const generateUnsetMessage = (variableName: string) => ({ * @param {string} key - The key of the environment variable that already exists * @returns {Promise} A promise that resolves when the user has confirmed overwriting the variable */ -export const promptOverwriteEnvVariable = async (key: string): Promise => { - const { confirmMessage, noticeMessage, warningMessage } = generateUnsetMessage(key) +export const promptOverwriteEnvVariable = async (existingKey: string): Promise => { + const { overwriteNoticeMessage } = destructiveCommandMessages + const { generateWarningMessage, overwriteConfirmationMessage } = destructiveCommandMessages.envUnset + + const warningMessage = generateWarningMessage(existingKey) + log(warningMessage) log() - log(noticeMessage) - await confirmPrompt(confirmMessage) + log(overwriteNoticeMessage) + await confirmPrompt(overwriteConfirmationMessage) } diff --git a/src/utils/types.ts b/src/utils/types.ts index 38ab8682b2b..2444f337353 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -64,3 +64,17 @@ export interface Request extends IncomingMessage { export type Rewriter = (req: Request) => Match | null export type TokenLocation = 'env' | 'flag' | 'config' | 'not found' + +export type EnvVar = { + key: string + scopes: string[] + values: EnvVarValue[] + updated_at: string + is_secret: boolean +} + +type EnvVarValue = { + id: string + context: string + value: string +} diff --git a/tests/integration/commands/blobs/blobs-set.test.ts b/tests/integration/commands/blobs/blobs-set.test.ts index ce022447f53..2a74228ef59 100644 --- a/tests/integration/commands/blobs/blobs-set.test.ts +++ b/tests/integration/commands/blobs/blobs-set.test.ts @@ -8,6 +8,7 @@ import { describe, expect, test, vi, beforeEach } from 'vitest' import BaseCommand from '../../../../src/commands/base-command.js' import { createBlobsCommand } from '../../../../src/commands/blobs/blobs.js' import { log } from '../../../../src/utils/command-helpers.js' +import { destructiveCommandMessages } from '../../../../src/utils/prompts/prompt-messages.js' import { Route } from '../../utils/mock-api-vitest.js' import { getEnvironmentVariables, withMockApi } from '../../utils/mock-api.js' @@ -46,15 +47,12 @@ describe('blob:set command', () => { const value = 'my-value' const newValue = 'my-new-value' - const warningMessage = `${chalk.redBright('Warning')}: The following blob key already exists in store ${chalk.cyan( - storeName, - )}:` + const { overwriteNoticeMessage } = destructiveCommandMessages + const { generateWarningMessage, overwriteConfirmationMessage } = destructiveCommandMessages.blobSet + + const warningMessage = generateWarningMessage(storeName) const boldKey = chalk.bold(key) - const overWriteMe = `This operation will ${chalk.redBright('overwrite')} the existing value.` - const noticeMessage = `${chalk.yellowBright( - 'Notice', - )}: To overwrite without this warning, you can use the --force flag.` const successMessage = `${chalk.greenBright('Success')}: Blob ${chalk.yellow(key)} set in store ${chalk.yellow( storeName, @@ -87,8 +85,7 @@ describe('blob:set command', () => { expect(log).toHaveBeenCalledWith(successMessage) expect(log).not.toHaveBeenCalledWith(warningMessage) expect(log).not.toHaveBeenCalledWith(boldKey) - expect(log).not.toHaveBeenCalledWith(overWriteMe) - expect(log).not.toHaveBeenCalledWith(noticeMessage) + expect(log).not.toHaveBeenCalledWith(overwriteNoticeMessage) }) }) @@ -115,15 +112,14 @@ describe('blob:set command', () => { expect(promptSpy).toHaveBeenCalledWith({ type: 'confirm', name: 'wantsToSet', - message: expect.stringContaining('Do you want to proceed with overwriting this blob key existing value?'), + message: expect.stringContaining(overwriteConfirmationMessage), default: false, }) expect(log).toHaveBeenCalledWith(successMessage) expect(log).toHaveBeenCalledWith(warningMessage) expect(log).toHaveBeenCalledWith(boldKey) - expect(log).toHaveBeenCalledWith(overWriteMe) - expect(log).toHaveBeenCalledWith(noticeMessage) + expect(log).toHaveBeenCalledWith(overwriteNoticeMessage) }) }) @@ -155,14 +151,13 @@ describe('blob:set command', () => { expect(promptSpy).toHaveBeenCalledWith({ type: 'confirm', name: 'wantsToSet', - message: expect.stringContaining('Do you want to proceed with overwriting this blob key existing value?'), + message: expect.stringContaining(overwriteConfirmationMessage), default: false, }) expect(log).toHaveBeenCalledWith(warningMessage) expect(log).toHaveBeenCalledWith(boldKey) - expect(log).toHaveBeenCalledWith(overWriteMe) - expect(log).toHaveBeenCalledWith(noticeMessage) + expect(log).toHaveBeenCalledWith(overwriteNoticeMessage) expect(log).not.toHaveBeenCalledWith(successMessage) }) }) @@ -191,8 +186,7 @@ describe('blob:set command', () => { expect(log).not.toHaveBeenCalledWith(warningMessage) expect(log).not.toHaveBeenCalledWith(boldKey) - expect(log).not.toHaveBeenCalledWith(overWriteMe) - expect(log).not.toHaveBeenCalledWith(noticeMessage) + expect(log).not.toHaveBeenCalledWith(overwriteNoticeMessage) expect(log).toHaveBeenCalledWith(successMessage) }) }) @@ -221,7 +215,7 @@ describe('blob:set command', () => { expect(promptSpy).not.toHaveBeenCalled() expect(log).not.toHaveBeenCalledWith(warningMessage) - expect(log).not.toHaveBeenCalledWith(noticeMessage) + expect(log).not.toHaveBeenCalledWith(overwriteNoticeMessage) expect(log).not.toHaveBeenCalledWith(successMessage) }) }) diff --git a/tests/integration/commands/env/api-routes.ts b/tests/integration/commands/env/api-routes.ts index b0394b5d7b8..d5913deb974 100644 --- a/tests/integration/commands/env/api-routes.ts +++ b/tests/integration/commands/env/api-routes.ts @@ -1,3 +1,4 @@ +import { EnvVar } from '../../../../src/utils/types' import { HTTPMethod } from '../../utils/mock-api-vitest' const siteInfo = { @@ -9,7 +10,7 @@ const siteInfo = { name: 'site-name', } -const secondSiteInfo = { +export const secondSiteInfo = { account_slug: 'test-account-2', build_settings: { env: {}, @@ -27,7 +28,7 @@ const thirdSiteInfo = { name: 'site-name-3', } -const existingVar = { +export const existingVar: EnvVar = { key: 'EXISTING_VAR', scopes: ['builds', 'functions'], values: [ @@ -42,6 +43,8 @@ const existingVar = { value: 'envelope-dev-value', }, ], + updated_at: '2020-01-01T00:00:00Z', + is_secret: false, } const otherVar = { @@ -59,7 +62,7 @@ const otherVar = { const response = [existingVar, otherVar] const secondSiteResponse = [existingVar] -const routes = [ +export const routes = [ { path: 'sites/site_id', response: siteInfo }, { path: 'sites/site_id_2', response: secondSiteInfo }, { path: 'sites/site_id_3', response: thirdSiteInfo }, diff --git a/tests/integration/commands/env/env-clone.test.ts b/tests/integration/commands/env/env-clone.test.ts index bf3b10c8cfc..79890018cb6 100644 --- a/tests/integration/commands/env/env-clone.test.ts +++ b/tests/integration/commands/env/env-clone.test.ts @@ -7,9 +7,11 @@ import { describe, expect, test, vi, beforeEach } from 'vitest' import BaseCommand from '../../../../src/commands/base-command.js' import { createEnvCommand } from '../../../../src/commands/env/index.js' import { log } from '../../../../src/utils/command-helpers.js' +import { generateEnvVarsList } from '../../../../src/utils/prompts/env-clone-prompt.js' +import { destructiveCommandMessages } from '../../../../src/utils/prompts/prompt-messages.js' import { getEnvironmentVariables, withMockApi } from '../../utils/mock-api.js' -import routes from './api-routes.js' +import { existingVar, routes, secondSiteInfo } from './api-routes.js' vi.mock('../../../../src/utils/command-helpers.js', async () => ({ ...(await vi.importActual('../../../../src/utils/command-helpers.js')), @@ -18,18 +20,16 @@ vi.mock('../../../../src/utils/command-helpers.js', async () => ({ describe('env:clone command', () => { describe('user is prompted to confirm when setting an env var that already exists', () => { - const sharedEnvVars = 'EXISTING_VAR' - const siteIdTwo = 'site_id_2' - const warningMessage = `${chalk.redBright( - 'Warning', - )}: The following environment variables are already set on the site with ID ${chalk.bgBlueBright( - siteIdTwo, - )}. They will be overwritten!` - const expectedNoticeMessage = `${chalk.yellowBright('Notice')}: The following variables will be overwritten:` - - const expectedSkipMessage = `${chalk.yellowBright( - 'Notice', - )}: To overwrite the existing variables without confirmation prompts, pass the --force flag.` + const sharedEnvVars = [existingVar, existingVar] + const siteIdTwo = secondSiteInfo.id + + const { overwriteNoticeMessage } = destructiveCommandMessages + const { generateWarningMessage, noticeEnvVarsMessage, overwriteConfirmationMessage } = + destructiveCommandMessages.envClone + + const envVarsList = generateEnvVarsList(sharedEnvVars) + const warningMessage = generateWarningMessage(siteIdTwo) + const expectedSuccessMessage = `Successfully cloned environment variables from ${chalk.green( 'site-name', )} to ${chalk.green('site-name-2')}` @@ -47,27 +47,21 @@ describe('env:clone command', () => { const promptSpy = vi.spyOn(inquirer, 'prompt').mockResolvedValue({ wantsToSet: true }) - await program.parseAsync(['', '', 'env:clone', '-t', 'site_id_2']) + await program.parseAsync(['', '', 'env:clone', '-t', siteIdTwo]) expect(promptSpy).toHaveBeenCalledWith({ type: 'confirm', name: 'wantsToSet', - message: expect.stringContaining('Do you want to proceed with overwriting these variables?'), + message: expect.stringContaining(overwriteConfirmationMessage), default: false, }) - expect(log).toHaveBeenCalledWith( - `${chalk.redBright( - 'Warning', - )}: The following environment variables are already set on the site with ID ${chalk.bgBlueBright( - 'site_id_2', - )}. They will be overwritten!`, - ) - expect(log).toHaveBeenCalledWith(warningMessage) - expect(log).toHaveBeenCalledWith(expectedNoticeMessage) - expect(log).toHaveBeenCalledWith(sharedEnvVars) - expect(log).toHaveBeenCalledWith(expectedSkipMessage) + expect(log).toHaveBeenCalledWith(noticeEnvVarsMessage) + envVarsList.forEach((envVar) => { + expect(log).toHaveBeenCalledWith(envVar) + }) + expect(log).toHaveBeenCalledWith(overwriteNoticeMessage) expect(log).toHaveBeenCalledWith(expectedSuccessMessage) }) }) @@ -81,14 +75,16 @@ describe('env:clone command', () => { const promptSpy = vi.spyOn(inquirer, 'prompt') - await program.parseAsync(['', '', 'env:clone', '--force', '-t', 'site_id_2']) + await program.parseAsync(['', '', 'env:clone', '--force', '-t', siteIdTwo]) expect(promptSpy).not.toHaveBeenCalled() expect(log).not.toHaveBeenCalledWith(warningMessage) - expect(log).not.toHaveBeenCalledWith(expectedNoticeMessage) - expect(log).not.toHaveBeenCalledWith(sharedEnvVars) - expect(log).not.toHaveBeenCalledWith(expectedSkipMessage) + envVarsList.forEach((envVar) => { + expect(log).not.toHaveBeenCalledWith(envVar) + }) + expect(log).not.toHaveBeenCalledWith(noticeEnvVarsMessage) + expect(log).not.toHaveBeenCalledWith(overwriteNoticeMessage) expect(log).toHaveBeenCalledWith(expectedSuccessMessage) }) }) @@ -103,7 +99,7 @@ describe('env:clone command', () => { const promptSpy = vi.spyOn(inquirer, 'prompt').mockResolvedValue({ wantsToSet: false }) try { - await program.parseAsync(['', '', 'env:clone', '-t', 'site_id_2']) + await program.parseAsync(['', '', 'env:clone', '-t', siteIdTwo]) } catch (error) { // We expect the process to exit, so this is fine expect(error.message).toContain('process.exit unexpectedly called') @@ -112,9 +108,11 @@ describe('env:clone command', () => { expect(promptSpy).toHaveBeenCalled() expect(log).toHaveBeenCalledWith(warningMessage) - expect(log).toHaveBeenCalledWith(expectedNoticeMessage) - expect(log).toHaveBeenCalledWith(sharedEnvVars) - expect(log).toHaveBeenCalledWith(expectedSkipMessage) + expect(log).toHaveBeenCalledWith(noticeEnvVarsMessage) + envVarsList.forEach((envVar) => { + expect(log).toHaveBeenCalledWith(envVar) + }) + expect(log).toHaveBeenCalledWith(overwriteNoticeMessage) expect(log).not.toHaveBeenCalledWith(expectedSuccessMessage) }) }) @@ -136,9 +134,11 @@ describe('env:clone command', () => { expect(promptSpy).not.toHaveBeenCalled() expect(log).not.toHaveBeenCalledWith(warningMessage) - expect(log).not.toHaveBeenCalledWith(expectedNoticeMessage) - expect(log).not.toHaveBeenCalledWith(sharedEnvVars) - expect(log).not.toHaveBeenCalledWith(expectedSkipMessage) + expect(log).not.toHaveBeenCalledWith(noticeEnvVarsMessage) + envVarsList.forEach((envVar) => { + expect(log).not.toHaveBeenCalledWith(envVar) + }) + expect(log).not.toHaveBeenCalledWith(overwriteNoticeMessage) expect(log).toHaveBeenCalledWith(expectedSuccessMessageSite3) }) }) diff --git a/tests/integration/commands/env/env-unset.test.ts b/tests/integration/commands/env/env-unset.test.ts index 11c7e0bfd8e..35f2837f890 100644 --- a/tests/integration/commands/env/env-unset.test.ts +++ b/tests/integration/commands/env/env-unset.test.ts @@ -7,7 +7,7 @@ import { describe, expect, test, vi, beforeEach } from 'vitest' import BaseCommand from '../../../../src/commands/base-command.js' import { createEnvCommand } from '../../../../src/commands/env/env.js' import { log } from '../../../../src/utils/command-helpers.js' -import { generateUnsetMessage } from '../../../../src/utils/prompts/unset-set-prompts.js' +import { destructiveCommandMessages } from '../../../../src/utils/prompts/prompt-messages.js' import { FixtureTestContext, setupFixtureTests } from '../../utils/fixture.js' import { getEnvironmentVariables, withMockApi } from '../../utils/mock-api.js' @@ -80,7 +80,10 @@ describe('env:unset command', () => { // already exists as value in withMockApi const existingVar = 'EXISTING_VAR' - const { confirmMessage, noticeMessage, warningMessage } = generateUnsetMessage(existingVar) + const { overwriteNoticeMessage } = destructiveCommandMessages + const { generateWarningMessage, overwriteConfirmationMessage } = destructiveCommandMessages.envUnset + + const warningMessage = generateWarningMessage(existingVar) const expectedSuccessMessage = `Unset environment variable ${chalk.yellow(`${existingVar}`)} in the ${chalk.magenta( 'all', @@ -104,12 +107,12 @@ describe('env:unset command', () => { expect(promptSpy).toHaveBeenCalledWith({ type: 'confirm', name: 'wantsToSet', - message: expect.stringContaining(confirmMessage), + message: expect.stringContaining(overwriteConfirmationMessage), default: false, }) expect(log).toHaveBeenCalledWith(warningMessage) - expect(log).toHaveBeenCalledWith(noticeMessage) + expect(log).toHaveBeenCalledWith(overwriteNoticeMessage) expect(log).toHaveBeenCalledWith(expectedSuccessMessage) }) }) @@ -128,7 +131,7 @@ describe('env:unset command', () => { expect(promptSpy).not.toHaveBeenCalled() expect(log).not.toHaveBeenCalledWith(warningMessage) - expect(log).not.toHaveBeenCalledWith(noticeMessage) + expect(log).not.toHaveBeenCalledWith(overwriteNoticeMessage) expect(log).toHaveBeenCalledWith(expectedSuccessMessage) }) }) @@ -152,7 +155,7 @@ describe('env:unset command', () => { expect(promptSpy).toHaveBeenCalled() expect(log).toHaveBeenCalledWith(warningMessage) - expect(log).toHaveBeenCalledWith(noticeMessage) + expect(log).toHaveBeenCalledWith(overwriteNoticeMessage) expect(log).not.toHaveBeenCalledWith(expectedSuccessMessage) }) }) @@ -171,7 +174,7 @@ describe('env:unset command', () => { expect(promptSpy).not.toHaveBeenCalled() expect(log).not.toHaveBeenCalledWith(warningMessage) - expect(log).not.toHaveBeenCalledWith(noticeMessage) + expect(log).not.toHaveBeenCalledWith(overwriteNoticeMessage) expect(log).not.toHaveBeenCalledWith(expectedSuccessMessage) }) })