@@ -21,15 +21,33 @@ def shell
21
21
shell . ask "Is this green?" , :green , :limited_to => %w( Yes No Maybe )
22
22
end
23
23
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 " )
26
26
expect ( Thor ::LineEditor ) . to receive ( :readline ) . with ( "Is this green? " , anything ) . and_return ( "yes" )
27
27
shell . ask "Is this green?" , :green
28
28
29
29
expect ( Thor ::LineEditor ) . to receive ( :readline ) . with ( "Is this green? [Yes, No, Maybe] " , anything ) . and_return ( "Yes" )
30
30
shell . ask "Is this green?" , :green , :limited_to => %w( Yes No Maybe )
31
31
end
32
32
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
+
33
51
it "handles an Array of colors" do
34
52
expect ( Thor ::LineEditor ) . to receive ( :readline ) . with ( "\e [32m\e [47m\e [1mIs this green on white? \e [0m" , anything ) . and_return ( "yes" )
35
53
shell . ask "Is this green on white?" , [ :green , :on_white , :bold ]
@@ -59,13 +77,31 @@ def shell
59
77
expect ( out . chomp ) . to eq ( "Wow! Now we have colors!" )
60
78
end
61
79
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
63
99
allow ( ENV ) . to receive ( :[] ) . with ( "NO_COLOR" ) . and_return ( "" )
64
100
out = capture ( :stdout ) do
65
- shell . say "Wow! Now we have colors!" , :green
101
+ shell . say "NO_COLOR is ignored! We have colors!" , :green
66
102
end
67
103
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 " )
69
105
end
70
106
71
107
it "does not use a new line even with colors" do
@@ -145,12 +181,34 @@ def shell
145
181
expect ( colorless ) . to eq ( "hi!" )
146
182
end
147
183
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 " )
150
186
allow ( $stdout) . to receive ( :tty? ) . and_return ( true )
151
187
colorless = shell . set_color "hi!" , :white
152
188
expect ( colorless ) . to eq ( "hi!" )
153
189
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
154
212
end
155
213
156
214
describe "#file_collision" do
0 commit comments