chalk/supports-color

`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 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
```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

Apache project logo

body { background-color: #EEE; color: #222; line-height: 1.25 !important; }

#wrapper > div { width: auto; margin: 0 auto; }

@media (min-width: 425px) { #wrapper > div { width: 420px; margin: 0 auto; } }

@media (min-width: 850px) { #wrapper > div { width: 850px; margin: 0 auto; } }

@media (min-width: 1280px) { #wrapper > div { width: 1270px; margin: 0 auto; } }

@media (min-width: 1730px) { #wrapper > div { width: 1700px; margin: 0 auto; } }

@media (min-width: 2160px) { #wrapper > div { width: auto; margin: 0 auto; } }

.project_rect { color: #000; width: 400px; height: 340px; text-align: center; font-family: sans-serif; border: 1.5px solid #3338; border-radius: 5px; background-color: #FFF; display: inline-block; margin: 10px; font-size: 12px; position: relative; overflow: hidden; }

#intro { margin: 10px auto; color: #000; width: 1000px; height: 80px; text-align: center; font-family: sans-serif; font-size: 12px; border: 1.5px solid #3338; border-radius: 5px; background-color: #FFFC; padding: 4px; position: relative; overflow: hidden; }

.project_logo { width: 400px; height: 170px; position: relative; vertical-align: middle; line-height: 180px; text-align: center; }

.project_logo { background: url(../images/transparent.png); background-size: 28px; }

.project_logo > img { position: absolute; top: 0; bottom: 0; right: 0; left: 0; margin:auto; } .project_rect > p { font-size: 11px; padding: 2px; }

form { font-family: sans-serif; font-size: 16px; text-align: center; }

h3 { text-align: center; font-family: sans-serif; color: #333; }

.img_download { border: 1px solid #3332; float: left; margin-left: 10px; padding: 2px; font-size: 10px; font-family: sans-serif; display: inline-block; overflow: hidden; width: 85px; line-height: 1.1; height: 50px; text-align: center; }

.img_download:hover { background: #FFC; }

posted by sabiomarkanthony44 8 months ago

Fund this Issue

$0.00
Funded

Pull requests