diff --git a/lib/thor/shell/color.rb b/lib/thor/shell/color.rb index aa67c5230..2cce8452e 100644 --- a/lib/thor/shell/color.rb +++ b/lib/thor/shell/color.rb @@ -97,7 +97,11 @@ def set_color(string, *colors) protected def can_display_colors? - stdout.tty? && !are_colors_disabled? + are_colors_supported? && !are_colors_disabled? + end + + def are_colors_supported? + stdout.tty? && ![nil, "dumb"].include?(ENV["TERM"]) end def are_colors_disabled? diff --git a/spec/shell/color_spec.rb b/spec/shell/color_spec.rb index 220a5fb69..48f37464c 100644 --- a/spec/shell/color_spec.rb +++ b/spec/shell/color_spec.rb @@ -7,6 +7,8 @@ def shell before do allow($stdout).to receive(:tty?).and_return(true) + allow(ENV).to receive(:[]).and_return(nil) + allow(ENV).to receive(:[]).with("TERM").and_return("ansi") allow_any_instance_of(StringIO).to receive(:tty?).and_return(true) end @@ -131,13 +133,25 @@ def shell expect(colorless).to eq("hi!") end - it "does nothing when the terminal does not support color" do + it "does nothing when stdout is not a tty" do allow($stdout).to receive(:tty?).and_return(false) colorless = shell.set_color "hi!", :white expect(colorless).to eq("hi!") end - it "does nothing when the terminal has the NO_COLOR environment variable set" do + it "does nothing when the TERM environment variable is set to 'dumb'" do + allow(ENV).to receive(:[]).with("TERM").and_return("dumb") + colorless = shell.set_color "hi!", :white + expect(colorless).to eq("hi!") + end + + it "does nothing when the TERM environment variable is not set" do + allow(ENV).to receive(:[]).with("TERM").and_return(nil) + colorless = shell.set_color "hi!", :white + expect(colorless).to eq("hi!") + end + + it "does nothing when the NO_COLOR environment variable is set" do allow(ENV).to receive(:[]).with("NO_COLOR").and_return("") allow($stdout).to receive(:tty?).and_return(true) colorless = shell.set_color "hi!", :white