Skip to content

Commit 2b56d7d

Browse files
kikonenlukastaegert
authored andcommitted
FIX support either NO_COLOR or FORCE_COLOR=0 to turn off color (#3272)
* FIX support either NO_COLOR or FORCE_COLOR=0 to turn off color * Deactivate colors in CLI tests, test NO_COLOR option
1 parent 27201d8 commit 2b56d7d

File tree

10 files changed

+127
-112
lines changed

10 files changed

+127
-112
lines changed

cli/logging.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import tc from 'turbocolor';
22
import { RollupError } from '../src/rollup/types';
33
import relativeId from '../src/utils/relativeId';
44

5+
// @see https://no-color.org
6+
// @see https://www.npmjs.com/package/chalk
7+
if (process.env.FORCE_COLOR === '0' || process.env.NO_COLOR) {
8+
tc.enabled = false;
9+
}
10+
511
// log to stderr to keep `rollup main.js > bundle.js` from breaking
612
export const stderr = console.error.bind(console);
713

test/cli/index.js

Lines changed: 79 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -26,92 +26,99 @@ runTestSuiteWithSamples(
2626
const command =
2727
'node ' + path.resolve(__dirname, '../../dist/bin') + path.sep + config.command;
2828

29-
const childProcess = exec(command, { timeout: 40000 }, (err, code, stderr) => {
30-
if (err && !err.killed) {
31-
if (config.error) {
32-
const shouldContinue = config.error(err);
29+
const childProcess = exec(
30+
command,
31+
{ timeout: 40000, env: Object.assign({ FORCE_COLOR: '0' }, process.env, config.env) },
32+
(err, code, stderr) => {
33+
if (err && !err.killed) {
34+
if (config.error) {
35+
const shouldContinue = config.error(err);
36+
if (!shouldContinue) return done();
37+
} else {
38+
throw err;
39+
}
40+
}
41+
42+
if ('stderr' in config) {
43+
const shouldContinue = config.stderr(stderr.trim());
3344
if (!shouldContinue) return done();
34-
} else {
35-
throw err;
45+
} else if (stderr) {
46+
console.error(stderr);
3647
}
37-
}
3848

39-
if ('stderr' in config) {
40-
const shouldContinue = config.stderr(stderr.trim());
41-
if (!shouldContinue) return done();
42-
} else if (stderr) {
43-
console.error(stderr);
44-
}
49+
let unintendedError;
4550

46-
let unintendedError;
51+
if (config.execute) {
52+
try {
53+
if (config.buble) {
54+
code = buble.transform(code, {
55+
transforms: { modules: false }
56+
}).code;
57+
}
4758

48-
if (config.execute) {
49-
try {
50-
if (config.buble) {
51-
code = buble.transform(code, {
52-
transforms: { modules: false }
53-
}).code;
54-
}
59+
const fn = new Function('require', 'module', 'exports', 'assert', code);
60+
const module = {
61+
exports: {}
62+
};
63+
fn(require, module, module.exports, assert);
5564

56-
const fn = new Function('require', 'module', 'exports', 'assert', code);
57-
const module = {
58-
exports: {}
59-
};
60-
fn(require, module, module.exports, assert);
65+
if (config.error) {
66+
unintendedError = new Error('Expected an error while executing output');
67+
}
6168

62-
if (config.error) {
63-
unintendedError = new Error('Expected an error while executing output');
69+
if (config.exports) {
70+
config.exports(module.exports);
71+
}
72+
} catch (err) {
73+
if (config.error) {
74+
config.error(err);
75+
} else {
76+
unintendedError = err;
77+
}
6478
}
6579

66-
if (config.exports) {
67-
config.exports(module.exports);
68-
}
69-
} catch (err) {
70-
if (config.error) {
71-
config.error(err);
72-
} else {
73-
unintendedError = err;
80+
if (config.show || unintendedError) {
81+
console.log(code + '\n\n\n');
7482
}
75-
}
76-
77-
if (config.show || unintendedError) {
78-
console.log(code + '\n\n\n');
79-
}
8083

81-
if (config.solo) console.groupEnd();
84+
if (config.solo) console.groupEnd();
8285

83-
unintendedError ? done(unintendedError) : done();
84-
} else if (config.result) {
85-
try {
86-
config.result(code);
87-
done();
88-
} catch (err) {
89-
done(err);
90-
}
91-
} else if (config.test) {
92-
try {
93-
config.test();
94-
done();
95-
} catch (err) {
96-
done(err);
97-
}
98-
} else if (sander.existsSync('_expected') && sander.statSync('_expected').isDirectory()) {
99-
try {
100-
assertDirectoriesAreEqual('_actual', '_expected');
101-
done();
102-
} catch (err) {
103-
done(err);
104-
}
105-
} else {
106-
const expected = sander.readFileSync('_expected.js').toString();
107-
try {
108-
assert.equal(normaliseOutput(code), normaliseOutput(expected));
109-
done();
110-
} catch (err) {
111-
done(err);
86+
unintendedError ? done(unintendedError) : done();
87+
} else if (config.result) {
88+
try {
89+
config.result(code);
90+
done();
91+
} catch (err) {
92+
done(err);
93+
}
94+
} else if (config.test) {
95+
try {
96+
config.test();
97+
done();
98+
} catch (err) {
99+
done(err);
100+
}
101+
} else if (
102+
sander.existsSync('_expected') &&
103+
sander.statSync('_expected').isDirectory()
104+
) {
105+
try {
106+
assertDirectoriesAreEqual('_actual', '_expected');
107+
done();
108+
} catch (err) {
109+
done(err);
110+
}
111+
} else {
112+
const expected = sander.readFileSync('_expected.js').toString();
113+
try {
114+
assert.equal(normaliseOutput(code), normaliseOutput(expected));
115+
done();
116+
} catch (err) {
117+
done(err);
118+
}
112119
}
113120
}
114-
});
121+
);
115122

116123
childProcess.stderr.on('data', data => {
117124
if (config.abortOnStderr && config.abortOnStderr(data)) {

test/cli/samples/code-splitting-named-default-inputs/_config.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
11
const assert = require('assert');
22

3-
const COLOR = '\u001b[36m\u001b[1m';
4-
const STANDARD = '\u001b[22m\u001b[39m';
5-
63
module.exports = {
74
description: 'allows defining names via CLI',
85
command:
96
'rollup entry1=main1.js "Entry 2"="main 2.js" "main3.js" --entryFileNames [name]-[hash].js -f es',
107
result(code) {
11-
let color = '';
12-
let standard = '';
13-
if (code[1] === '\u001b') {
14-
color = COLOR;
15-
standard = STANDARD;
16-
}
178
assert.equal(
189
code,
1910
'\n' +
20-
`${color}//→ entry1-b70571c1.js:${standard}\n` +
11+
`//→ entry1-b70571c1.js:\n` +
2112
"console.log('main1');\n" +
2213
'\n' +
23-
`${color}//→ Entry 2-cc781491.js:${standard}\n` +
14+
`//→ Entry 2-cc781491.js:\n` +
2415
"console.log('main2');\n" +
2516
'\n' +
26-
`${color}//→ main3-5e259623.js:${standard}\n` +
17+
`//→ main3-5e259623.js:\n` +
2718
"console.log('main3');\n"
2819
);
2920
}

test/cli/samples/code-splitting-named-inputs/_config.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
11
const assert = require('assert');
22

3-
const COLOR = '\u001b[36m\u001b[1m';
4-
const STANDARD = '\u001b[22m\u001b[39m';
5-
63
module.exports = {
74
description: 'allows defining names via CLI',
85
command:
96
'rollup --entryFileNames [name]-[hash].js --input entry1=main1.js -i "Entry 2"="main 2.js" -i "main3.js" -f es',
107
result(code) {
11-
let color = '';
12-
let standard = '';
13-
if (code[1] === '\u001b') {
14-
color = COLOR;
15-
standard = STANDARD;
16-
}
178
assert.equal(
189
code,
1910
'\n' +
20-
`${color}//→ entry1-b70571c1.js:${standard}\n` +
11+
`//→ entry1-b70571c1.js:\n` +
2112
"console.log('main1');\n" +
2213
'\n' +
23-
`${color}//→ Entry 2-cc781491.js:${standard}\n` +
14+
`//→ Entry 2-cc781491.js:\n` +
2415
"console.log('main2');\n" +
2516
'\n' +
26-
`${color}//→ main3-5e259623.js:${standard}\n` +
17+
`//→ main3-5e259623.js:\n` +
2718
"console.log('main3');\n"
2819
);
2920
}

test/cli/samples/no-color/_config.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const assert = require('assert');
2+
3+
module.exports = {
4+
description: 'respects the NO_COLOR environment variable',
5+
command: 'rollup -i main1.js -i main2.js -f es',
6+
env: { FORCE_COLOR: undefined, NO_COLOR: true },
7+
result(code) {
8+
assert.equal(
9+
code,
10+
'\n' +
11+
'//→ main1.js:\n' +
12+
"console.log('main1');\n" +
13+
'\n' +
14+
'//→ main2.js:\n' +
15+
"console.log('main2');\n"
16+
);
17+
}
18+
};

test/cli/samples/no-color/main1.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('main1');

test/cli/samples/no-color/main2.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('main2');

test/cli/samples/stdout-code-splitting/_config.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,15 @@ const STANDARD = '\u001b[22m\u001b[39m';
66
module.exports = {
77
description: 'bundles multiple files to stdout while adding file names',
88
command: 'rollup -i main1.js -i main2.js -f es',
9+
env: { FORCE_COLOR: '1' },
910
result(code) {
10-
let color = '';
11-
let standard = '';
12-
if (code[1] === '\u001b') {
13-
color = COLOR;
14-
standard = STANDARD;
15-
}
1611
assert.equal(
1712
code,
1813
'\n' +
19-
`${color}//→ main1.js:${standard}\n` +
14+
`${COLOR}//→ main1.js:${STANDARD}\n` +
2015
"console.log('main1');\n" +
2116
'\n' +
22-
`${color}//→ main2.js:${standard}\n` +
17+
`${COLOR}//→ main2.js:${STANDARD}\n` +
2318
"console.log('main2');" +
2419
'\n'
2520
);

test/cli/samples/warn-circular/_config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { assertStderrIncludes } = require('../../../utils.js');
33
module.exports = {
44
description: 'warns for circular dependencies',
55
command: 'rollup -c',
6-
stderr: stderr =>
7-
assertStderrIncludes(stderr, '(!) Circular dependency\n' + 'main.js -> dep.js -> main.js\n')
6+
stderr(stderr) {
7+
assertStderrIncludes(stderr, '(!) Circular dependency\nmain.js -> dep.js -> main.js\n');
8+
}
89
};

test/utils.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,14 @@ function assertFilesAreEqual(actualFiles, expectedFiles, dirs = []) {
227227
}
228228

229229
function assertStderrIncludes(stderr, expected) {
230-
// eslint-disable-next-line no-control-regex
231-
const output = stderr.replace(/\x1b\[[^m]*m/g, '');
232-
assert.ok(
233-
output.includes(expected),
234-
`Could not find ${JSON.stringify(expected)} in ${JSON.stringify(output)}`
235-
);
230+
try {
231+
assert.ok(
232+
stderr.includes(expected),
233+
`Could not find ${JSON.stringify(expected)} in ${JSON.stringify(stderr)}`
234+
);
235+
} catch (err) {
236+
err.actual = stderr;
237+
err.expected = expected;
238+
throw err;
239+
}
236240
}

0 commit comments

Comments
 (0)