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

Fix NO_COLOR handling to comply with the spec #40

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions colors.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
let FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM, isTTY=true;
if (typeof process !== 'undefined') {
({ FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM } = process.env);
({ FORCE_COLOR, NODE_DISABLE_COLORS, TERM } = process.env);
FORCE_COLOR = FORCE_COLOR || '0';
NO_COLOR = 'NO_COLOR' in process.env;
isTTY = process.stdout.isTTY;
}

export const $ = {
enabled: !NODE_DISABLE_COLORS && !NO_COLOR && TERM !== 'dumb' && (
FORCE_COLOR != null && FORCE_COLOR !== '0' || isTTY
FORCE_COLOR !== '0' || isTTY
)
}

Expand Down
6 changes: 4 additions & 2 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

let FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM, isTTY=true;
if (typeof process !== 'undefined') {
({ FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM } = process.env);
({ FORCE_COLOR, NODE_DISABLE_COLORS, TERM } = process.env);
FORCE_COLOR = FORCE_COLOR || '0';
NO_COLOR = 'NO_COLOR' in process.env;
isTTY = process.stdout.isTTY;
}

const $ = {
enabled: !NODE_DISABLE_COLORS && !NO_COLOR && TERM !== 'dumb' && (
FORCE_COLOR != null && FORCE_COLOR !== '0' || isTTY
FORCE_COLOR !== '0' || isTTY
),

// modifiers
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
},
"scripts": {
"build": "node build",
"test": "uvu -r esm -i utils -i xyz"
"test": "uvu -r esm -i utils -i xyz",
"test:env": "bats ./test/env.bats"
},
"engines": {
"node": ">=6"
Expand All @@ -43,6 +44,7 @@
"terminal"
],
"devDependencies": {
"bats": "^1.2.1",
"esm": "3.2.25",
"uvu": "0.0.16"
}
Expand Down
190 changes: 190 additions & 0 deletions test/env.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
#!/usr/bin/env bats

# vim:ft=bash:

# CMD='node -r esm ./test/xyz.js'
CMD='node --trace-warnings -r esm ./test/xyz.js'
COLOR=$'\E[31mfoo\E[39m'
NO_COLOR='foo'

faketty() {
script -qfc "$(printf "%q " "$@")" /dev/null | tr -d '\r'
}

# process.stdout.isTTY = undefined;

@test "[tty=false] FORCE_COLOR=?" {
result=$($CMD)
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=false] FORCE_COLOR=" {
result=$(FORCE_COLOR= $CMD)
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=false] FORCE_COLOR=''" {
result=$(FORCE_COLOR='' $CMD)
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=false] FORCE_COLOR=0" {
result=$(FORCE_COLOR=0 $CMD)
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=false] NODE_DISABLE_COLORS=1" {
result=$(NODE_DISABLE_COLORS=1 $CMD)
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=false] NODE_DISABLE_COLORS=1; FORCE_COLOR=1" {
result=$(NODE_DISABLE_COLORS=1 FORCE_COLOR=1 $CMD)
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=false] NO_COLOR=" {
result=$(NO_COLOR= $CMD)
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=false] NO_COLOR=''" {
result=$(NO_COLOR='' $CMD)
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=false] NO_COLOR=0" {
result=$(NO_COLOR=0 $CMD)
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=false] NO_COLOR=1" {
result=$(NO_COLOR=1 $CMD)
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=false] NO_COLOR=yes" {
result=$(NO_COLOR=yes $CMD)
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=false] NO_COLOR=; FORCE_COLOR=1" {
result=$(NO_COLOR= FORCE_COLOR=1 $CMD)
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=false] NO_COLOR=0; FORCE_COLOR=1" {
result=$(NO_COLOR=0 FORCE_COLOR=1 $CMD)
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=false] NO_COLOR=1; FORCE_COLOR=1" {
result=$(NO_COLOR=1 FORCE_COLOR=1 $CMD)
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=false] FORCE_COLOR=1" {
result=$(FORCE_COLOR=1 $CMD)
[ "$result" = "$COLOR" ]
}

@test "[tty=false] FORCE_COLOR=2" {
result=$(FORCE_COLOR=2 $CMD)
[ "$result" = "$COLOR" ]
}

@test "[tty=false] TERM=dumb; FORCE_COLOR=1" {
result=$(TERM=dumb FORCE_COLOR=1 $CMD)
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=false] TERM=dumb" {
result=$(TERM=dumb $CMD)
[ "$result" = "$NO_COLOR" ]
}

# process.stdout.isTTY = true;

@test "[tty=true] FORCE_COLOR=?" {
result=$(faketty $CMD)
[ "$result" = "$COLOR" ]
}

@test "[tty=true] FORCE_COLOR=" {
result=$(FORCE_COLOR= faketty $CMD)
[ "$result" = "$COLOR" ]
}

@test "[tty=true] FORCE_COLOR=''" {
result=$(FORCE_COLOR='' faketty $CMD)
[ "$result" = "$COLOR" ]
}

@test "[tty=true] FORCE_COLOR=0" {
result=$(FORCE_COLOR=0 faketty $CMD)
[ "$result" = "$COLOR" ]
}

@test "[tty=true] NODE_DISABLE_COLORS=1" {
result=$(NODE_DISABLE_COLORS=1 faketty $CMD)
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=true] NODE_DISABLE_COLORS=1; FORCE_COLOR=1" {
result=$(NODE_DISABLE_COLORS=1 FORCE_COLOR=1 faketty $CMD)
printf "XXX result: [%q]" "$result"
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=true] NO_COLOR=" {
result=$(NO_COLOR= faketty $CMD)
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=true] NO_COLOR=''" {
result=$(NO_COLOR='' faketty $CMD)
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=true] NO_COLOR=0" {
result=$(NO_COLOR=0 faketty $CMD)
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=true] NO_COLOR=1" {
result=$(NO_COLOR=1 faketty $CMD)
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=true] NO_COLOR=; FORCE_COLOR=1" {
result=$(NO_COLOR= FORCE_COLOR=1 faketty $CMD)
printf "XXX result: [%q]" "$result"
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=true] NO_COLOR=0; FORCE_COLOR=1" {
result=$(NO_COLOR=0 FORCE_COLOR=1 faketty $CMD)
printf "XXX result: [%q]" "$result"
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=true] NO_COLOR=1; FORCE_COLOR=1" {
result=$(NO_COLOR=1 FORCE_COLOR=1 faketty $CMD)
printf "XXX result: [%q]" "$result"
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=true] TERM=dumb; FORCE_COLOR=1" {
result=$(TERM=dumb FORCE_COLOR=1 faketty $CMD)
[ "$result" = "$NO_COLOR" ]
}

@test "[tty=true] FORCE_COLOR=1" {
result=$(FORCE_COLOR=1 faketty $CMD)
[ "$result" = "$COLOR" ]
}

@test "[tty=true] FORCE_COLOR=2" {
result=$(FORCE_COLOR=2 faketty $CMD)
[ "$result" = "$COLOR" ]
}