|
1 |
| -import { describe, expect, it } from 'vitest'; |
| 1 | +import { afterEach, describe, expect, it } from 'vitest'; |
2 | 2 |
|
3 |
| -import { initConfigs } from '../config/init'; |
| 3 | +import { detectInteractiveSession, initConfigs } from '../config/init'; |
4 | 4 | import { mergeConfigs } from '../config/merge';
|
5 | 5 |
|
6 | 6 | describe('interactive config', () => {
|
7 |
| - it('should default to false when not provided', async () => { |
| 7 | + it('should use detectInteractiveSession when not provided', async () => { |
8 | 8 | const result = await initConfigs({
|
9 | 9 | input: 'test.json',
|
10 | 10 | output: './test',
|
11 | 11 | });
|
12 | 12 |
|
| 13 | + // In test environment, TTY is typically not available, so it should be false |
13 | 14 | expect(result.results[0].config.interactive).toBe(false);
|
14 | 15 | });
|
15 | 16 |
|
@@ -67,3 +68,108 @@ describe('interactive config', () => {
|
67 | 68 | expect(mergedWithBug.interactive).toBe(true); // This was the bug - it overrode the file config
|
68 | 69 | });
|
69 | 70 | });
|
| 71 | + |
| 72 | +describe('detectInteractiveSession', () => { |
| 73 | + const originalEnv = process.env; |
| 74 | + const originalStdin = process.stdin; |
| 75 | + const originalStdout = process.stdout; |
| 76 | + |
| 77 | + afterEach(() => { |
| 78 | + process.env = originalEnv; |
| 79 | + Object.defineProperty(process, 'stdin', { value: originalStdin }); |
| 80 | + Object.defineProperty(process, 'stdout', { value: originalStdout }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should return false when CI environment variable is set', () => { |
| 84 | + process.env = { ...originalEnv, CI: 'true' }; |
| 85 | + Object.defineProperty(process.stdin, 'isTTY', { |
| 86 | + configurable: true, |
| 87 | + value: true, |
| 88 | + }); |
| 89 | + Object.defineProperty(process.stdout, 'isTTY', { |
| 90 | + configurable: true, |
| 91 | + value: true, |
| 92 | + }); |
| 93 | + |
| 94 | + expect(detectInteractiveSession()).toBe(false); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should return false when NO_INTERACTIVE environment variable is set', () => { |
| 98 | + process.env = { ...originalEnv, NO_INTERACTIVE: '1' }; |
| 99 | + Object.defineProperty(process.stdin, 'isTTY', { |
| 100 | + configurable: true, |
| 101 | + value: true, |
| 102 | + }); |
| 103 | + Object.defineProperty(process.stdout, 'isTTY', { |
| 104 | + configurable: true, |
| 105 | + value: true, |
| 106 | + }); |
| 107 | + |
| 108 | + expect(detectInteractiveSession()).toBe(false); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should return false when NO_INTERACTION environment variable is set', () => { |
| 112 | + process.env = { ...originalEnv, NO_INTERACTION: '1' }; |
| 113 | + Object.defineProperty(process.stdin, 'isTTY', { |
| 114 | + configurable: true, |
| 115 | + value: true, |
| 116 | + }); |
| 117 | + Object.defineProperty(process.stdout, 'isTTY', { |
| 118 | + configurable: true, |
| 119 | + value: true, |
| 120 | + }); |
| 121 | + |
| 122 | + expect(detectInteractiveSession()).toBe(false); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should return false when stdin is not TTY', () => { |
| 126 | + process.env = { ...originalEnv }; |
| 127 | + delete process.env.CI; |
| 128 | + delete process.env.NO_INTERACTIVE; |
| 129 | + delete process.env.NO_INTERACTION; |
| 130 | + Object.defineProperty(process.stdin, 'isTTY', { |
| 131 | + configurable: true, |
| 132 | + value: false, |
| 133 | + }); |
| 134 | + Object.defineProperty(process.stdout, 'isTTY', { |
| 135 | + configurable: true, |
| 136 | + value: true, |
| 137 | + }); |
| 138 | + |
| 139 | + expect(detectInteractiveSession()).toBe(false); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should return false when stdout is not TTY', () => { |
| 143 | + process.env = { ...originalEnv }; |
| 144 | + delete process.env.CI; |
| 145 | + delete process.env.NO_INTERACTIVE; |
| 146 | + delete process.env.NO_INTERACTION; |
| 147 | + Object.defineProperty(process.stdin, 'isTTY', { |
| 148 | + configurable: true, |
| 149 | + value: true, |
| 150 | + }); |
| 151 | + Object.defineProperty(process.stdout, 'isTTY', { |
| 152 | + configurable: true, |
| 153 | + value: false, |
| 154 | + }); |
| 155 | + |
| 156 | + expect(detectInteractiveSession()).toBe(false); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should return true when TTY is available and no blocking env vars are set', () => { |
| 160 | + process.env = { ...originalEnv }; |
| 161 | + delete process.env.CI; |
| 162 | + delete process.env.NO_INTERACTIVE; |
| 163 | + delete process.env.NO_INTERACTION; |
| 164 | + Object.defineProperty(process.stdin, 'isTTY', { |
| 165 | + configurable: true, |
| 166 | + value: true, |
| 167 | + }); |
| 168 | + Object.defineProperty(process.stdout, 'isTTY', { |
| 169 | + configurable: true, |
| 170 | + value: true, |
| 171 | + }); |
| 172 | + |
| 173 | + expect(detectInteractiveSession()).toBe(true); |
| 174 | + }); |
| 175 | +}); |
0 commit comments