From c9592a0399775a704543bf007f0d3ec7f8ab00f9 Mon Sep 17 00:00:00 2001 From: Will Date: Tue, 29 Oct 2024 12:09:02 -0500 Subject: [PATCH] fix: updated prompt tests to work correctly in ci/cd enviroments Co-authored-by: Thomas Lane <163203257+tlane25@users.noreply.github.com> --- src/utils/scripted-commands.ts | 4 +++- .../commands/blobs/blobs-delete.test.ts | 14 ++++++++------ .../commands/blobs/blobs-set.test.ts | 18 ++++++++++-------- .../integration/commands/env/env-clone.test.ts | 14 ++++++++++---- tests/integration/commands/env/env-set.test.ts | 15 +++++++++++---- .../integration/commands/env/env-unset.test.ts | 14 ++++++++++---- tests/integration/utils/mock-api.js | 4 ++++ 7 files changed, 56 insertions(+), 27 deletions(-) diff --git a/src/utils/scripted-commands.ts b/src/utils/scripted-commands.ts index c6cf938e01e..9227dc8fcfe 100644 --- a/src/utils/scripted-commands.ts +++ b/src/utils/scripted-commands.ts @@ -4,7 +4,6 @@ import { isCI } from 'ci-info' export const shouldForceFlagBeInjected = (argv: string[]): boolean => { // Is the command run in a non-interactive shell or CI/CD environment? const scriptedCommand = Boolean(!process.stdin.isTTY || isCI || process.env.CI) - // Is the `--force` flag not already present? const noForceFlag = !argv.includes('--force') @@ -13,6 +12,9 @@ export const shouldForceFlagBeInjected = (argv: string[]): boolean => { } export const injectForceFlagIfScripted = (argv: string[]) => { + // ENV Variable used to tests prompts in CI/CD enviroment + if (process.env.TESTING_PROMPTS === 'true') return + if (shouldForceFlagBeInjected(argv)) { argv.push('--force') } diff --git a/tests/integration/commands/blobs/blobs-delete.test.ts b/tests/integration/commands/blobs/blobs-delete.test.ts index 4a3c83dc3c7..07254bfc3fc 100644 --- a/tests/integration/commands/blobs/blobs-delete.test.ts +++ b/tests/integration/commands/blobs/blobs-delete.test.ts @@ -3,13 +3,13 @@ import process from 'process' import { getStore } from '@netlify/blobs' import chalk from 'chalk' import inquirer from 'inquirer' -import { describe, expect, test, vi, beforeEach, afterEach, beforeAll } from 'vitest' +import { describe, expect, test, vi, beforeEach, afterEach, beforeAll, afterAll } from 'vitest' import { log } from '../../../../src/utils/command-helpers.js' import { destructiveCommandMessages } from '../../../../src/utils/prompts/prompt-messages.js' import { reportError } from '../../../../src/utils/telemetry/report-error.js' import { Route } from '../../utils/mock-api-vitest.js' -import { getEnvironmentVariables, withMockApi, setTTYMode, setCI } from '../../utils/mock-api.js' +import { getEnvironmentVariables, withMockApi, setTTYMode, setCI, setTestingPrompts } from '../../utils/mock-api.js' import { runMockProgram } from '../../utils/mock-program.js' const siteInfo = { @@ -60,16 +60,18 @@ describe('blobs:delete command', () => { )}` describe('user is prompted to confirm when deleting a blob key', () => { - beforeEach(() => { - setTTYMode(true) - setCI('') - vi.resetAllMocks() + beforeAll(() => { + setTestingPrompts('true') }) beforeEach(() => { vi.resetAllMocks() }) + afterAll(() => { + setTestingPrompts('false') + }) + test('should log warning message and prompt for confirmation', async () => { await withMockApi(routes, async ({ apiUrl }) => { Object.assign(process.env, getEnvironmentVariables({ apiUrl })) diff --git a/tests/integration/commands/blobs/blobs-set.test.ts b/tests/integration/commands/blobs/blobs-set.test.ts index 36a5dd2e322..a720673cf37 100644 --- a/tests/integration/commands/blobs/blobs-set.test.ts +++ b/tests/integration/commands/blobs/blobs-set.test.ts @@ -3,13 +3,13 @@ import process from 'process' import { getStore } from '@netlify/blobs' import chalk from 'chalk' import inquirer from 'inquirer' -import { describe, expect, test, vi, beforeEach, afterEach } from 'vitest' +import { describe, expect, test, vi, beforeEach, afterEach, beforeAll, afterAll } from 'vitest' import { log } from '../../../../src/utils/command-helpers.js' import { destructiveCommandMessages } from '../../../../src/utils/prompts/prompt-messages.js' import { reportError } from '../../../../src/utils/telemetry/report-error.js' import { Route } from '../../utils/mock-api-vitest.js' -import { getEnvironmentVariables, withMockApi, setTTYMode, setCI } from '../../utils/mock-api.js' +import { getEnvironmentVariables, withMockApi, setTTYMode, setCI, setTestingPrompts } from '../../utils/mock-api.js' import { runMockProgram } from '../../utils/mock-program.js' const siteInfo = { @@ -61,12 +61,18 @@ describe('blobs:set command', () => { )}` describe('user is prompted to confirm when setting a a blob key that already exists', () => { + beforeAll(() => { + setTestingPrompts('true') + }) + beforeEach(() => { - setTTYMode(true) - setCI('') vi.resetAllMocks() }) + afterAll(() => { + setTestingPrompts('false') + }) + test('should not log warnings and prompt if blob key does not exist', async () => { await withMockApi(routes, async ({ apiUrl }) => { Object.assign(process.env, getEnvironmentVariables({ apiUrl })) @@ -207,10 +213,6 @@ describe('blobs:set command', () => { }) }) - beforeEach(() => { - vi.resetAllMocks() - }) - describe('prompts should not show in a non-interactive shell or in a ci/cd enviroment', () => { afterEach(() => { setTTYMode(true) diff --git a/tests/integration/commands/env/env-clone.test.ts b/tests/integration/commands/env/env-clone.test.ts index fc4765d4c22..02b3200c5ad 100644 --- a/tests/integration/commands/env/env-clone.test.ts +++ b/tests/integration/commands/env/env-clone.test.ts @@ -2,12 +2,12 @@ import process from 'process' import chalk from 'chalk' import inquirer from 'inquirer' -import { describe, expect, test, vi, beforeEach, afterEach } from 'vitest' +import { describe, expect, test, vi, beforeEach, afterEach, beforeAll, afterAll } from 'vitest' 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, setTTYMode, setCI } from '../../utils/mock-api.js' +import { getEnvironmentVariables, withMockApi, setTTYMode, setCI, setTestingPrompts } from '../../utils/mock-api.js' import { existingVar, routes, secondSiteInfo } from './api-routes.js' import { runMockProgram } from '../../utils/mock-program.js' @@ -32,12 +32,18 @@ describe('env:clone command', () => { )}` describe('user is prompted to confirm when setting an env var that already exists', () => { + beforeAll(() => { + setTestingPrompts('true') + }) + beforeEach(() => { - setTTYMode(true) - setCI('') vi.resetAllMocks() }) + afterAll(() => { + setTestingPrompts('false') + }) + test('should log warnings and prompts if enviroment variable already exists', async () => { await withMockApi(routes, async ({ apiUrl }) => { Object.assign(process.env, getEnvironmentVariables({ apiUrl })) diff --git a/tests/integration/commands/env/env-set.test.ts b/tests/integration/commands/env/env-set.test.ts index 72d6d4106cc..948ba7d1377 100644 --- a/tests/integration/commands/env/env-set.test.ts +++ b/tests/integration/commands/env/env-set.test.ts @@ -2,12 +2,12 @@ import process from 'process' import chalk from 'chalk' import inquirer from 'inquirer' -import { describe, expect, test, vi, beforeEach, afterEach } from 'vitest' +import { describe, expect, test, vi, beforeEach, afterEach, beforeAll, afterAll } from 'vitest' import { log } from '../../../../src/utils/command-helpers.js' import { destructiveCommandMessages } from '../../../../src/utils/prompts/prompt-messages.js' import { FixtureTestContext, setupFixtureTests } from '../../utils/fixture.js' -import { getEnvironmentVariables, withMockApi, setTTYMode, setCI } from '../../utils/mock-api.js' +import { getEnvironmentVariables, withMockApi, setTTYMode, setCI, setTestingPrompts } from '../../utils/mock-api.js' import { runMockProgram } from '../../utils/mock-program.js' import { routes } from './api-routes.js' @@ -280,12 +280,18 @@ describe('env:set command', () => { }) describe('user is prompted to confirmOverwrite when setting an env var that already exists', () => { + beforeAll(() => { + setTestingPrompts('true') + }) + beforeEach(() => { - setTTYMode(true) - setCI('') vi.resetAllMocks() }) + afterAll(() => { + setTestingPrompts('false') + }) + test('should log warnings and prompts if enviroment variable already exists', async () => { await withMockApi(routes, async ({ apiUrl }) => { Object.assign(process.env, getEnvironmentVariables({ apiUrl })) @@ -369,6 +375,7 @@ describe('env:set command', () => { beforeEach(() => { vi.resetAllMocks() }) + afterEach(() => { setTTYMode(true) setCI('') diff --git a/tests/integration/commands/env/env-unset.test.ts b/tests/integration/commands/env/env-unset.test.ts index aad03cf307b..c2ea9dee8b4 100644 --- a/tests/integration/commands/env/env-unset.test.ts +++ b/tests/integration/commands/env/env-unset.test.ts @@ -2,12 +2,12 @@ import process from 'process' import chalk from 'chalk' import inquirer from 'inquirer' -import { describe, expect, test, vi, beforeEach, afterEach } from 'vitest' +import { describe, expect, test, vi, beforeEach, afterEach, beforeAll, afterAll } from 'vitest' import { log } from '../../../../src/utils/command-helpers.js' import { destructiveCommandMessages } from '../../../../src/utils/prompts/prompt-messages.js' import { FixtureTestContext, setupFixtureTests } from '../../utils/fixture.js' -import { getEnvironmentVariables, withMockApi, setTTYMode, setCI } from '../../utils/mock-api.js' +import { getEnvironmentVariables, withMockApi, setTTYMode, setCI, setTestingPrompts } from '../../utils/mock-api.js' import { routes } from './api-routes.js' import { runMockProgram } from '../../utils/mock-program.js' @@ -86,12 +86,18 @@ describe('env:unset command', () => { }) describe('user is prompted to confirm when unsetting an env var that already exists', () => { + beforeAll(() => { + setTestingPrompts('true') + }) + beforeEach(() => { - setTTYMode(true) - setCI('') vi.resetAllMocks() }) + afterAll(() => { + setTestingPrompts('false') + }) + test('should log warnings and prompts if enviroment variable already exists', async () => { await withMockApi(routes, async ({ apiUrl }) => { Object.assign(process.env, getEnvironmentVariables({ apiUrl })) diff --git a/tests/integration/utils/mock-api.js b/tests/integration/utils/mock-api.js index eaf13487cfc..624a9303ba4 100644 --- a/tests/integration/utils/mock-api.js +++ b/tests/integration/utils/mock-api.js @@ -98,6 +98,10 @@ export const setTTYMode = (bool) => { process.stdin.isTTY = bool } +export const setTestingPrompts = (value) => { + process.env.TESTING_PROMPTS = value +} + /** * Simulates a Continuous Integration environment by toggling the `CI` * environment variable. Truthy value is