Skip to content

Commit

Permalink
fix: updated prompt tests to work correctly in ci/cd enviroments
Browse files Browse the repository at this point in the history
Co-authored-by: Thomas Lane <[email protected]>
  • Loading branch information
wconrad265 and tlane25 committed Oct 29, 2024
1 parent cbc3b7c commit c9592a0
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 27 deletions.
4 changes: 3 additions & 1 deletion src/utils/scripted-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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')
}
Expand Down
14 changes: 8 additions & 6 deletions tests/integration/commands/blobs/blobs-delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 }))
Expand Down
18 changes: 10 additions & 8 deletions tests/integration/commands/blobs/blobs-set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 }))
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 10 additions & 4 deletions tests/integration/commands/env/env-clone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 }))
Expand Down
15 changes: 11 additions & 4 deletions tests/integration/commands/env/env-set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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 }))
Expand Down Expand Up @@ -369,6 +375,7 @@ describe('env:set command', () => {
beforeEach(() => {
vi.resetAllMocks()
})

afterEach(() => {
setTTYMode(true)
setCI('')
Expand Down
14 changes: 10 additions & 4 deletions tests/integration/commands/env/env-unset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 }))
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/utils/mock-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c9592a0

Please sign in to comment.