Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 74 additions & 21 deletions packages/theme/src/cli/utilities/cli-config.test.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,118 @@
import {configureCLIEnvironment} from './cli-config.js'
import {globalFlags} from '@shopify/cli-kit/node/cli'
import colors from '@shopify/cli-kit/node/colors'
import {describe, expect, beforeEach, afterAll, test} from 'vitest'
import {afterEach, describe, expect, test, vi} from 'vitest'

describe('configureCLIEnvironment', () => {
const originalEnv = process.env
const verboseEnvironmentVariable = globalFlags.verbose.env!

beforeEach(() => {
process.env = {...originalEnv}
colors.level = 1
})
afterEach(() => {
vi.unstubAllEnvs()
})

afterAll(() => {
process.env = originalEnv
})
// chalk's level is a module-level singleton, so each test starts from colors enabled and restores it.
function withColorsEnabled(run: () => void): void {
const originalLevel = colors.level
colors.level = 1
try {
run()
} finally {
colors.level = originalLevel
}
}

describe('configureCLIEnvironment', () => {
describe('verbose', () => {
test('sets verbose environment variable when verbose is true', () => {
// Given
delete process.env[globalFlags.verbose.env!]
vi.stubEnv(verboseEnvironmentVariable, undefined)

// When
configureCLIEnvironment({verbose: true})

// Then
expect(process.env[globalFlags.verbose.env!]).toBe('true')
expect(process.env[verboseEnvironmentVariable]).toBe('true')
})

test('does not set verbose environment variable when verbose is false', () => {
// Given
delete process.env[globalFlags.verbose.env!]
vi.stubEnv(verboseEnvironmentVariable, undefined)

// When
configureCLIEnvironment({verbose: false})

// Then
expect(process.env[verboseEnvironmentVariable]).toBeUndefined()
})

test('leaves an existing verbose environment variable untouched when verbose is false', () => {
// Given
vi.stubEnv(verboseEnvironmentVariable, 'true')

// When
configureCLIEnvironment({verbose: false})

// Then
expect(process.env[globalFlags.verbose.env!]).toBeUndefined()
expect(process.env[verboseEnvironmentVariable]).toBe('true')
})
})

describe('noColor', () => {
test('sets no-color environment variable when noColor is true', () => {
test('disables colors when noColor is true', () => {
// Given
delete process.env[globalFlags['no-color'].env!]
vi.stubEnv('FORCE_COLOR', '1')

withColorsEnabled(() => {
// When
configureCLIEnvironment({noColor: true})

// Then
expect(colors.level).toBe(0)
expect(process.env.FORCE_COLOR).toBe('0')
})
})

test('leaves colors enabled when noColor is false', () => {
// Given
vi.stubEnv('FORCE_COLOR', '1')

withColorsEnabled(() => {
// When
configureCLIEnvironment({noColor: false})

// Then
expect(colors.level).toBe(1)
expect(process.env.FORCE_COLOR).toBe('1')
})
})
})

test('applies verbose and noColor together', () => {
// Given
vi.stubEnv(verboseEnvironmentVariable, undefined)
vi.stubEnv('FORCE_COLOR', '1')

withColorsEnabled(() => {
// When
configureCLIEnvironment({noColor: true})
configureCLIEnvironment({verbose: true, noColor: true})

// Then
expect(process.env[verboseEnvironmentVariable]).toBe('true')
expect(colors.level).toBe(0)
expect(process.env.FORCE_COLOR).toBe('0')
})
})

test('does not set no-color environment variable when noColor is false', () => {
// Given
delete process.env[globalFlags['no-color'].env!]
test('changes nothing when no options are set', () => {
// Given
vi.stubEnv(verboseEnvironmentVariable, undefined)
vi.stubEnv('FORCE_COLOR', '1')

withColorsEnabled(() => {
// When
configureCLIEnvironment({noColor: false})
configureCLIEnvironment({})

// Then
expect(process.env[verboseEnvironmentVariable]).toBeUndefined()
expect(colors.level).toBe(1)
expect(process.env.FORCE_COLOR).toBe('1')
})
Expand Down