From 5dfdb82576f24f44f97bcc00b71c3b12caa60e33 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 7 Oct 2026 00:40:59 +0000 Subject: [PATCH] [Tests] Isolate configureCLIEnvironment environment and color state Replace the whole-process.env swap and afterAll restore with per-test vi.stubEnv, and restore chalk's module-level level around each test that changes it. Also assert the behavior the previous no-color tests named but never exercised. Co-Authored-By: Claude Opus 4.8 --- .../src/cli/utilities/cli-config.test.ts | 95 +++++++++++++++---- 1 file changed, 74 insertions(+), 21 deletions(-) diff --git a/packages/theme/src/cli/utilities/cli-config.test.ts b/packages/theme/src/cli/utilities/cli-config.test.ts index cc08f0d1c3f..00e1156a7f7 100644 --- a/packages/theme/src/cli/utilities/cli-config.test.ts +++ b/packages/theme/src/cli/utilities/cli-config.test.ts @@ -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') })