Skip to content

Commit e4907fd

Browse files
authored
Merge pull request #796 from coderjoe/respect.changed.env.no_color
Respect the updated NO_COLOR specification
2 parents 937c443 + 86e21aa commit e4907fd

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

Gemfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ group :test do
1313
gem "rspec", ">= 3.2"
1414
gem "rspec-mocks", ">= 3"
1515
gem "simplecov", ">= 0.13"
16-
gem "webmock"
16+
gem "webmock", '~> 3.14.0' if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.4.0")
17+
gem "webmock", '>= 3.14' if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.4.0")
1718
end
1819

1920
gemspec

lib/thor/shell/color.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def are_colors_supported?
105105
end
106106

107107
def are_colors_disabled?
108-
!ENV['NO_COLOR'].nil?
108+
!ENV['NO_COLOR'].nil? && !ENV['NO_COLOR'].empty?
109109
end
110110

111111
# Overwrite show_diff to show diff with colors if Diff::LCS is

spec/shell/color_spec.rb

+65-7
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,33 @@ def shell
2121
shell.ask "Is this green?", :green, :limited_to => %w(Yes No Maybe)
2222
end
2323

24-
it "does not set the color if specified and NO_COLOR is set" do
25-
allow(ENV).to receive(:[]).with("NO_COLOR").and_return("")
24+
it "does not set the color if specified and NO_COLOR is set to a non-empty value" do
25+
allow(ENV).to receive(:[]).with("NO_COLOR").and_return("non-empty value")
2626
expect(Thor::LineEditor).to receive(:readline).with("Is this green? ", anything).and_return("yes")
2727
shell.ask "Is this green?", :green
2828

2929
expect(Thor::LineEditor).to receive(:readline).with("Is this green? [Yes, No, Maybe] ", anything).and_return("Yes")
3030
shell.ask "Is this green?", :green, :limited_to => %w(Yes No Maybe)
3131
end
3232

33+
it "sets the color when NO_COLOR is ignored because the environment variable is nil" do
34+
allow(ENV).to receive(:[]).with("NO_COLOR").and_return(nil)
35+
expect(Thor::LineEditor).to receive(:readline).with("\e[32mIs this green? \e[0m", anything).and_return("yes")
36+
shell.ask "Is this green?", :green
37+
38+
expect(Thor::LineEditor).to receive(:readline).with("\e[32mIs this green? [Yes, No, Maybe] \e[0m", anything).and_return("Yes")
39+
shell.ask "Is this green?", :green, :limited_to => %w(Yes No Maybe)
40+
end
41+
42+
it "sets the color when NO_COLOR is ignored because the environment variable is an empty-string" do
43+
allow(ENV).to receive(:[]).with("NO_COLOR").and_return("")
44+
expect(Thor::LineEditor).to receive(:readline).with("\e[32mIs this green? \e[0m", anything).and_return("yes")
45+
shell.ask "Is this green?", :green
46+
47+
expect(Thor::LineEditor).to receive(:readline).with("\e[32mIs this green? [Yes, No, Maybe] \e[0m", anything).and_return("Yes")
48+
shell.ask "Is this green?", :green, :limited_to => %w(Yes No Maybe)
49+
end
50+
3351
it "handles an Array of colors" do
3452
expect(Thor::LineEditor).to receive(:readline).with("\e[32m\e[47m\e[1mIs this green on white? \e[0m", anything).and_return("yes")
3553
shell.ask "Is this green on white?", [:green, :on_white, :bold]
@@ -59,13 +77,31 @@ def shell
5977
expect(out.chomp).to eq("Wow! Now we have colors!")
6078
end
6179

62-
it "does not set the color if NO_COLOR is set" do
80+
it "does not set the color if NO_COLOR is set to any value that is not an empty string" do
81+
allow(ENV).to receive(:[]).with("NO_COLOR").and_return("non-empty string value")
82+
out = capture(:stdout) do
83+
shell.say "NO_COLOR is enforced! We should not have colors!", :green
84+
end
85+
86+
expect(out.chomp).to eq("NO_COLOR is enforced! We should not have colors!")
87+
end
88+
89+
it "colors are still used and NO_COLOR is ignored if the environment variable is nil" do
90+
allow(ENV).to receive(:[]).with("NO_COLOR").and_return(nil)
91+
out = capture(:stdout) do
92+
shell.say "NO_COLOR is ignored! We have colors!", :green
93+
end
94+
95+
expect(out.chomp).to eq("\e[32mNO_COLOR is ignored! We have colors!\e[0m")
96+
end
97+
98+
it "colors are still used and NO_COLOR is ignored if the environment variable is an empty-string" do
6399
allow(ENV).to receive(:[]).with("NO_COLOR").and_return("")
64100
out = capture(:stdout) do
65-
shell.say "Wow! Now we have colors!", :green
101+
shell.say "NO_COLOR is ignored! We have colors!", :green
66102
end
67103

68-
expect(out.chomp).to eq("Wow! Now we have colors!")
104+
expect(out.chomp).to eq("\e[32mNO_COLOR is ignored! We have colors!\e[0m")
69105
end
70106

71107
it "does not use a new line even with colors" do
@@ -145,12 +181,34 @@ def shell
145181
expect(colorless).to eq("hi!")
146182
end
147183

148-
it "does nothing when the NO_COLOR environment variable is set" do
149-
allow(ENV).to receive(:[]).with("NO_COLOR").and_return("")
184+
it "does nothing when the NO_COLOR environment variable is set to a non-empty string" do
185+
allow(ENV).to receive(:[]).with("NO_COLOR").and_return("non-empty value")
150186
allow($stdout).to receive(:tty?).and_return(true)
151187
colorless = shell.set_color "hi!", :white
152188
expect(colorless).to eq("hi!")
153189
end
190+
191+
it "sets color when the NO_COLOR environment variable is ignored for being nil" do
192+
allow(ENV).to receive(:[]).with("NO_COLOR").and_return(nil)
193+
allow($stdout).to receive(:tty?).and_return(true)
194+
195+
red = shell.set_color "hi!", :red
196+
expect(red).to eq("\e[31mhi!\e[0m")
197+
198+
on_red = shell.set_color "hi!", :white, :on_red
199+
expect(on_red).to eq("\e[37m\e[41mhi!\e[0m")
200+
end
201+
202+
it "sets color when the NO_COLOR environment variable is ignored for being an empty string" do
203+
allow(ENV).to receive(:[]).with("NO_COLOR").and_return("")
204+
allow($stdout).to receive(:tty?).and_return(true)
205+
206+
red = shell.set_color "hi!", :red
207+
expect(red).to eq("\e[31mhi!\e[0m")
208+
209+
on_red = shell.set_color "hi!", :white, :on_red
210+
expect(on_red).to eq("\e[37m\e[41mhi!\e[0m")
211+
end
154212
end
155213

156214
describe "#file_collision" do

0 commit comments

Comments
 (0)