|
| 1 | +import { stripVTControlCharacters } from 'node:util'; |
| 2 | +import { testSuite, expect } from 'manten'; |
| 3 | +import { createFixture } from 'fs-fixture'; |
| 4 | +import { node } from '../utils/node.js'; |
| 5 | +import { tempDir } from '../utils/temp-dir.js'; |
| 6 | + |
| 7 | +export default testSuite(({ describe }) => { |
| 8 | + describe('color environment variables', ({ test }) => { |
| 9 | + test('NO_COLOR disables colors', async () => { |
| 10 | + await using fixture = await createFixture({ |
| 11 | + 'test.mjs': ` |
| 12 | + import task from '#tasuku'; |
| 13 | + import { setTimeout } from 'node:timers/promises'; |
| 14 | +
|
| 15 | + await task('Success task', async () => { |
| 16 | + await setTimeout(50); |
| 17 | + }); |
| 18 | + `, |
| 19 | + }, { tempDir }); |
| 20 | + |
| 21 | + const result = await node(fixture.getPath('test.mjs'), { |
| 22 | + NO_COLOR: '1', |
| 23 | + }); |
| 24 | + |
| 25 | + const textOutput = stripVTControlCharacters(result.output); |
| 26 | + |
| 27 | + // Should contain the checkmark but without color codes |
| 28 | + expect(textOutput.includes('✔')).toBe(true); |
| 29 | + |
| 30 | + // Should NOT contain color codes (green for checkmark) |
| 31 | + expect(result.output.includes('\u001B[32m')).toBe(false); |
| 32 | + }); |
| 33 | + |
| 34 | + test('NODE_DISABLE_COLORS disables colors', async () => { |
| 35 | + await using fixture = await createFixture({ |
| 36 | + 'test.mjs': ` |
| 37 | + import task from '#tasuku'; |
| 38 | + import { setTimeout } from 'node:timers/promises'; |
| 39 | +
|
| 40 | + await task('Success task', async () => { |
| 41 | + await setTimeout(50); |
| 42 | + }); |
| 43 | + `, |
| 44 | + }, { tempDir }); |
| 45 | + |
| 46 | + const result = await node(fixture.getPath('test.mjs'), { |
| 47 | + NODE_DISABLE_COLORS: '1', |
| 48 | + }); |
| 49 | + |
| 50 | + const textOutput = stripVTControlCharacters(result.output); |
| 51 | + |
| 52 | + // Should contain the checkmark but without color codes |
| 53 | + expect(textOutput.includes('✔')).toBe(true); |
| 54 | + |
| 55 | + // Should NOT contain color codes (green for checkmark) |
| 56 | + expect(result.output.includes('\u001B[32m')).toBe(false); |
| 57 | + }); |
| 58 | + |
| 59 | + test('FORCE_COLOR=0 disables colors', async () => { |
| 60 | + await using fixture = await createFixture({ |
| 61 | + 'test.mjs': ` |
| 62 | + import task from '#tasuku'; |
| 63 | + import { setTimeout } from 'node:timers/promises'; |
| 64 | +
|
| 65 | + await task('Success task', async () => { |
| 66 | + await setTimeout(50); |
| 67 | + }); |
| 68 | + `, |
| 69 | + }, { tempDir }); |
| 70 | + |
| 71 | + const result = await node(fixture.getPath('test.mjs'), { |
| 72 | + FORCE_COLOR: '0', |
| 73 | + }); |
| 74 | + |
| 75 | + const textOutput = stripVTControlCharacters(result.output); |
| 76 | + |
| 77 | + // Should contain the checkmark but without color codes |
| 78 | + expect(textOutput.includes('✔')).toBe(true); |
| 79 | + |
| 80 | + // Should NOT contain color codes (green for checkmark) |
| 81 | + expect(result.output.includes('\u001B[32m')).toBe(false); |
| 82 | + }); |
| 83 | + |
| 84 | + test('FORCE_COLOR=1 enables colors', async () => { |
| 85 | + await using fixture = await createFixture({ |
| 86 | + 'test.mjs': ` |
| 87 | + import task from '#tasuku'; |
| 88 | + import { setTimeout } from 'node:timers/promises'; |
| 89 | +
|
| 90 | + await task('Success task', async () => { |
| 91 | + await setTimeout(50); |
| 92 | + }); |
| 93 | + `, |
| 94 | + }, { tempDir }); |
| 95 | + |
| 96 | + const result = await node(fixture.getPath('test.mjs'), { |
| 97 | + FORCE_COLOR: '1', |
| 98 | + }); |
| 99 | + |
| 100 | + const textOutput = stripVTControlCharacters(result.output); |
| 101 | + |
| 102 | + // Should contain the checkmark |
| 103 | + expect(textOutput.includes('✔')).toBe(true); |
| 104 | + |
| 105 | + // Should contain color codes (green for checkmark: \u001B[32m) |
| 106 | + expect(result.output.includes('\u001B[32m')).toBe(true); |
| 107 | + }); |
| 108 | + |
| 109 | + test('colors work with error state', async () => { |
| 110 | + await using fixture = await createFixture({ |
| 111 | + 'test.mjs': ` |
| 112 | + import task from '#tasuku'; |
| 113 | + import { setTimeout } from 'node:timers/promises'; |
| 114 | +
|
| 115 | + await task('Error task', async ({ setError }) => { |
| 116 | + await setTimeout(50); |
| 117 | + setError('Something failed'); |
| 118 | + }); |
| 119 | + `, |
| 120 | + }, { tempDir }); |
| 121 | + |
| 122 | + const result = await node(fixture.getPath('test.mjs'), { |
| 123 | + FORCE_COLOR: '1', |
| 124 | + }); |
| 125 | + |
| 126 | + const textOutput = stripVTControlCharacters(result.output); |
| 127 | + |
| 128 | + // Should contain the error icon |
| 129 | + expect(textOutput.includes('✖')).toBe(true); |
| 130 | + }); |
| 131 | + |
| 132 | + test('NO_COLOR removes all colors including warning', async () => { |
| 133 | + await using fixture = await createFixture({ |
| 134 | + 'test.mjs': ` |
| 135 | + import task from '#tasuku'; |
| 136 | + import { setTimeout } from 'node:timers/promises'; |
| 137 | +
|
| 138 | + await task('Warning task', async ({ setWarning }) => { |
| 139 | + await setTimeout(50); |
| 140 | + setWarning('A warning'); |
| 141 | + }); |
| 142 | + `, |
| 143 | + }, { tempDir }); |
| 144 | + |
| 145 | + const result = await node(fixture.getPath('test.mjs'), { |
| 146 | + NO_COLOR: '1', |
| 147 | + }); |
| 148 | + |
| 149 | + const textOutput = stripVTControlCharacters(result.output); |
| 150 | + |
| 151 | + // Should contain the warning icon |
| 152 | + expect(textOutput.includes('⚠')).toBe(true); |
| 153 | + |
| 154 | + // Should NOT contain yellow color code (\u001B[33m) |
| 155 | + expect(result.output.includes('\u001B[33m')).toBe(false); |
| 156 | + }); |
| 157 | + |
| 158 | + test('colors work with nested tasks', async () => { |
| 159 | + await using fixture = await createFixture({ |
| 160 | + 'test.mjs': ` |
| 161 | + import task from '#tasuku'; |
| 162 | + import { setTimeout } from 'node:timers/promises'; |
| 163 | +
|
| 164 | + await task('Parent task', async ({ task }) => { |
| 165 | + await setTimeout(50); |
| 166 | + await task('Child task', async () => { |
| 167 | + await setTimeout(50); |
| 168 | + }); |
| 169 | + }); |
| 170 | + `, |
| 171 | + }, { tempDir }); |
| 172 | + |
| 173 | + const result = await node(fixture.getPath('test.mjs'), { |
| 174 | + FORCE_COLOR: '1', |
| 175 | + }); |
| 176 | + |
| 177 | + const textOutput = stripVTControlCharacters(result.output); |
| 178 | + |
| 179 | + // Should contain both pointer and checkmark |
| 180 | + expect(textOutput.includes('❯')).toBe(true); |
| 181 | + expect(textOutput.includes('✔')).toBe(true); |
| 182 | + |
| 183 | + // Should contain yellow color for pointer (\u001B[33m) |
| 184 | + expect(result.output.includes('\u001B[33m')).toBe(true); |
| 185 | + |
| 186 | + // Should contain green color for checkmark (\u001B[32m) |
| 187 | + expect(result.output.includes('\u001B[32m')).toBe(true); |
| 188 | + }); |
| 189 | + |
| 190 | + test('NO_COLOR with nested tasks removes all colors', async () => { |
| 191 | + await using fixture = await createFixture({ |
| 192 | + 'test.mjs': ` |
| 193 | + import task from '#tasuku'; |
| 194 | + import { setTimeout } from 'node:timers/promises'; |
| 195 | +
|
| 196 | + await task('Parent task', async ({ task }) => { |
| 197 | + await setTimeout(50); |
| 198 | + await task('Child task', async () => { |
| 199 | + await setTimeout(50); |
| 200 | + }); |
| 201 | + }); |
| 202 | + `, |
| 203 | + }, { tempDir }); |
| 204 | + |
| 205 | + const result = await node(fixture.getPath('test.mjs'), { |
| 206 | + NO_COLOR: '1', |
| 207 | + }); |
| 208 | + |
| 209 | + const textOutput = stripVTControlCharacters(result.output); |
| 210 | + |
| 211 | + // Should contain both pointer and checkmark (text only) |
| 212 | + expect(textOutput.includes('❯')).toBe(true); |
| 213 | + expect(textOutput.includes('✔')).toBe(true); |
| 214 | + |
| 215 | + // Should NOT contain any color codes |
| 216 | + expect(result.output.includes('\u001B[32m')).toBe(false); // green |
| 217 | + expect(result.output.includes('\u001B[33m')).toBe(false); // yellow |
| 218 | + expect(result.output.includes('\u001B[31m')).toBe(false); // red |
| 219 | + expect(result.output.includes('\u001B[36m')).toBe(false); // cyan |
| 220 | + }); |
| 221 | + }); |
| 222 | +}); |
0 commit comments