`FORCE_COLOR` testcases failing (fixed 2) #155
pr4j3sh posted onGitHub
Problem
Two test cases are failing, present at line 44
and line 52
in test.js
(although 4 test cases are failing but I'm considering 2 since I was able to fix only 2, solution is provided below):
test('return true if `FORCE_COLOR` is in env, but honor 256', async t => {
process.argv = ['--color=256'];
process.env.FORCE_COLOR = 'true';
const result = await importMain();
t.truthy(result.stdout);
t.is(result.stdout.level, 2);
});
test('return true if `FORCE_COLOR` is in env, but honor 256 #2', async t => {
process.argv = ['--color=256'];
process.env.FORCE_COLOR = '1';
const result = await importMain();
t.truthy(result.stdout);
t.is(result.stdout.level, 2);
});
- Failed Tests
supports-color@9.4.0 test xo && ava && tsd
index.js:72:1 âš 72:1 Function _supportsColor has a complexity of 36. Maximum allowed is 20. complexity
1 warning
✔ return true if FORCE_COLOR
is in env
✘ [fail]: return true if FORCE_COLOR
is in env, but honor 256
✘ [fail]: return true if FORCE_COLOR
is in env, but honor 256 #2
✔ CLI color flags precede other color support checks
✔ FORCE_COLOR
environment variable precedes other color support checks
✔ return false if FORCE_COLOR
is in env and is 0
✔ do not cache FORCE_COLOR
✔ return false if not TTY
✔ return false if --no-color flag is used
✔ return false if --no-colors flag is used
✔ return true if --color flag is used
✔ return true if --colors flag is used
✔ return true if COLORTERM
is in env
✔ support --color=true
flag
✔ support --color=always
flag
✔ support --color=false
flag
✔ support --color=256
flag
✔ level should be 2 if --color=256
flag is used
✔ support --color=16m
flag
✔ support --color=full
flag
✔ support --color=truecolor
flag
✔ level should be 3 if --color=16m
flag is used
✔ ignore post-terminator flags
✔ allow tests of the properties on false
✔ return false if CI
is in env
✔ return true if TRAVIS
is in env
✔ return true if CIRCLECI
is in env
✔ return true if APPVEYOR
is in env
✔ return true if GITLAB_CI
is in env
✔ return true if BUILDKITE
is in env
✔ return true if DRONE
is in env
✔ return level 3 if GITEA_ACTIONS
is in env
✔ return true if Codeship is in env
✔ return false if TEAMCITY_VERSION
is in env and is < 9.1
✔ return level 1 if TEAMCITY_VERSION
is in env and is >= 9.1
✔ support rxvt
✔ prefer level 2/xterm over COLORTERM
✔ support screen-256color
✔ support putty-256color
✔ level should be 3 when using iTerm 3.0
✔ level should be 2 when using iTerm 2.9
✔ return level 1 if on Windows earlier than 10 build 10586
✔ return level 2 if on Windows 10 build 10586 or later
✔ return level 3 if on Windows 10 build 14931 or later
✘ [fail]: return level 2 when FORCE_COLOR is set when not TTY in xterm256
✔ supports setting a color level using FORCE_COLOR
✔ FORCE_COLOR maxes out at a value of 3
✘ [fail]: FORCE_COLOR works when set via command line (all values are strings)
✔ return false when TERM
is set to dumb
✔ return false when TERM
is set to dumb when TERM_PROGRAM
is set
✔ return false when TERM
is set to dumb when run on Windows
✔ return level 1 when TERM
is set to dumb when FORCE_COLOR
is set
✔ ignore flags when sniffFlags=false
─
return true if FORCE_COLOR
is in env, but honor 256
test.js:44
43: t.truthy(result.stdout); 44: t.is(result.stdout.level, 2); 45: });
Difference (- actual, + expected):
1
2
› file://test.js:44:4
return true if FORCE_COLOR
is in env, but honor 256 #2
test.js:52
51: t.truthy(result.stdout); 52: t.is(result.stdout.level, 2); 53: });
Difference (- actual, + expected):
1
2
› file://test.js:52:4
return level 2 when FORCE_COLOR is set when not TTY in xterm256
test.js:354
353: t.truthy(result.stdout); 354: t.is(result.stdout.level, 2); 355: });
Difference (- actual, + expected):
1
2
› file://test.js:354:4
FORCE_COLOR works when set via command line (all values are strings)
test.js:398
397: t.truthy(result.stdout); 398: t.is(result.stdout.level, 2); 399:
Difference (- actual, + expected):
1
2
› file://test.js:398:4
─
4 tests failed
Solution
In
test.js
,process.argv
is set as--color=256
andprocess.env.FORCE_COLOR
is set to'true'
and1
.However, in
index.js
, atline 80
, we have anif
statement that checks value offorceColor
and returns it,if (forceColor !== undefined) { return forceColor; }
Then we have another
if
condition atline 84
that checks forcolor=256
flag,if (sniffFlags) { if (hasFlag('color=16m') || hasFlag('color=full') || hasFlag('color=truecolor')) { return 3; } if (hasFlag('color=256')) { return 2; } }
Since the condition for
forceColor
is defined abovecolor=256
inindex.js
. Hence, the value offorceColor
, i.e.,1
is returned whereas expected is2
.To fix this, issue we can simply define
forceColor
condition belowcolor=256
condition.if (sniffFlags) { if (hasFlag('color=16m') || hasFlag('color=full') || hasFlag('color=truecolor')) { return 3; } if (hasFlag('color=256')) { return 2; } }
if (forceColor !== undefined) { return forceColor; }
- failed test cases reduced to two after the solution is applied
```bash
> supports-color@9.4.0 test
> xo && ava && tsd
index.js:72:1
âš 72:1 Function _supportsColor has a complexity of 36. Maximum allowed is 20. complexity
1 warning
✔ return true if `FORCE_COLOR` is in env
✔ return true if `FORCE_COLOR` is in env, but honor 256
✔ return true if `FORCE_COLOR` is in env, but honor 256 #2
✔ CLI color flags precede other color support checks
✔ `FORCE_COLOR` environment variable precedes other color support checks
✔ return false if `FORCE_COLOR` is in env and is 0
✔ do not cache `FORCE_COLOR`
✔ return false if not TTY
✔ return false if --no-color flag is used
✔ return false if --no-colors flag is used
✔ return true if --color flag is used
✔ return true if --colors flag is used
✔ return true if `COLORTERM` is in env
✔ support `--color=true` flag
✔ support `--color=always` flag
✔ support `--color=false` flag
✔ support `--color=256` flag
✔ level should be 2 if `--color=256` flag is used
✔ support `--color=16m` flag
✔ support `--color=full` flag
✔ support `--color=truecolor` flag
✔ level should be 3 if `--color=16m` flag is used
✔ ignore post-terminator flags
✔ allow tests of the properties on false
✔ return false if `CI` is in env
✔ return true if `TRAVIS` is in env
✔ return true if `CIRCLECI` is in env
✔ return true if `APPVEYOR` is in env
✔ return true if `GITLAB_CI` is in env
✔ return true if `BUILDKITE` is in env
✔ return true if `DRONE` is in env
✔ return level 3 if `GITEA_ACTIONS` is in env
✔ return true if Codeship is in env
✔ return false if `TEAMCITY_VERSION` is in env and is < 9.1
✔ return level 1 if `TEAMCITY_VERSION` is in env and is >= 9.1
✔ support rxvt
✔ prefer level 2/xterm over COLORTERM
✔ support screen-256color
✔ support putty-256color
✔ level should be 3 when using iTerm 3.0
✔ level should be 2 when using iTerm 2.9
✔ return level 1 if on Windows earlier than 10 build 10586
✔ return level 2 if on Windows 10 build 10586 or later
✔ return level 3 if on Windows 10 build 14931 or later
✘ [fail]: return level 2 when FORCE_COLOR is set when not TTY in xterm256
✔ supports setting a color level using FORCE_COLOR
✔ FORCE_COLOR maxes out at a value of 3
✘ [fail]: FORCE_COLOR works when set via command line (all values are strings)
✔ return false when `TERM` is set to dumb
✔ return false when `TERM` is set to dumb when `TERM_PROGRAM` is set
✔ return false when `TERM` is set to dumb when run on Windows
✔ return level 1 when `TERM` is set to dumb when `FORCE_COLOR` is set
✔ ignore flags when sniffFlags=false
─
return level 2 when FORCE_COLOR is set when not TTY in xterm256
test.js:354
353: t.truthy(result.stdout);
354: t.is(result.stdout.level, 2);
355: });
Difference (- actual, + expected):
- 1
+ 2
› file://test.js:354:4
FORCE_COLOR works when set via command line (all values are strings)
test.js:398
397: t.truthy(result.stdout);
398: t.is(result.stdout.level, 2);
399:
Difference (- actual, + expected):
- 1
+ 2
› file://test.js:398:4
─
2 tests failed
- Since, it is a very small change, I didn't bother to make a pull request. However, if you want I can create a pull request. I have the corrected code here.
Miscellaneous
- System Information
uname -r # 6.9.3-arch1-1
I use arch btw
node -v # v20.14.0
npm -v # 10.7.0