Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FORCE_COLOR testcases failing (fixed 2) #155

Open
pr4j3sh opened this issue Jun 12, 2024 · 1 comment
Open

FORCE_COLOR testcases failing (fixed 2) #155

pr4j3sh opened this issue Jun 12, 2024 · 1 comment

Comments

@pr4j3sh
Copy link

pr4j3sh commented Jun 12, 2024

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
> [email protected] 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 falsereturn 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 setreturn 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 and process.env.FORCE_COLOR is set to 'true' and 1.
  • However, in index.js, at line 80, we have an if statement that checks value of forceColor and returns it,
if (forceColor !== undefined) {
   return forceColor;
}
  • Then we have another if condition at line 84 that checks for color=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 above color=256 in index.js. Hence, the value of forceColor, i.e., 1 is returned whereas expected is 2.
  • To fix this, issue we can simply define forceColor condition below color=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
> [email protected] 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 falsereturn 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 setreturn 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
@sabiomarkanthony44

This comment was marked as spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants