File: color_spec.rb

package info (click to toggle)
ruby-thor 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 924 kB
  • sloc: ruby: 9,491; makefile: 8; sh: 1
file content (228 lines) | stat: -rw-r--r-- 8,614 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
require "helper"

describe Thor::Shell::Color do
  def shell
    @shell ||= Thor::Shell::Color.new
  end

  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

  describe "#ask" do
    it "sets the color if specified and tty?" do
      expect(Thor::LineEditor).to receive(:readline).with("\e[32mIs this green? \e[0m", anything).and_return("yes")
      shell.ask "Is this green?", :green

      expect(Thor::LineEditor).to receive(:readline).with("\e[32mIs this green? [Yes, No, Maybe] \e[0m", anything).and_return("Yes")
      shell.ask "Is this green?", :green, limited_to: %w(Yes No Maybe)
    end

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

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

    it "sets the color when NO_COLOR is ignored because the environment variable is nil" do
      allow(ENV).to receive(:[]).with("NO_COLOR").and_return(nil)
      expect(Thor::LineEditor).to receive(:readline).with("\e[32mIs this green? \e[0m", anything).and_return("yes")
      shell.ask "Is this green?", :green

      expect(Thor::LineEditor).to receive(:readline).with("\e[32mIs this green? [Yes, No, Maybe] \e[0m", anything).and_return("Yes")
      shell.ask "Is this green?", :green, limited_to: %w(Yes No Maybe)
    end

    it "sets the color when NO_COLOR is ignored because the environment variable is an empty-string" do
      allow(ENV).to receive(:[]).with("NO_COLOR").and_return("")
      expect(Thor::LineEditor).to receive(:readline).with("\e[32mIs this green? \e[0m", anything).and_return("yes")
      shell.ask "Is this green?", :green

      expect(Thor::LineEditor).to receive(:readline).with("\e[32mIs this green? [Yes, No, Maybe] \e[0m", anything).and_return("Yes")
      shell.ask "Is this green?", :green, limited_to: %w(Yes No Maybe)
    end

    it "handles an Array of colors" do
      expect(Thor::LineEditor).to receive(:readline).with("\e[32m\e[47m\e[1mIs this green on white? \e[0m", anything).and_return("yes")
      shell.ask "Is this green on white?", [:green, :on_white, :bold]
    end

    it "supports the legacy color syntax" do
      expect(Thor::LineEditor).to receive(:readline).with("\e[1m\e[34mIs this legacy blue? \e[0m", anything).and_return("yes")
      shell.ask "Is this legacy blue?", [:blue, true]
    end
  end

  describe "#say" do
    it "set the color if specified and tty?" do
      out = capture(:stdout) do
        shell.say "Wow! Now we have colors!", :green
      end

      expect(out.chomp).to eq("\e[32mWow! Now we have colors!\e[0m")
    end

    it "does not set the color if output is not a tty" do
      out = capture(:stdout) do
        expect($stdout).to receive(:tty?).and_return(false)
        shell.say "Wow! Now we have colors!", :green
      end

      expect(out.chomp).to eq("Wow! Now we have colors!")
    end

    it "does not set the color if NO_COLOR is set to any value that is not an empty string" do
      allow(ENV).to receive(:[]).with("NO_COLOR").and_return("non-empty string value")
      out = capture(:stdout) do
        shell.say "NO_COLOR is enforced! We should not have colors!", :green
      end

      expect(out.chomp).to eq("NO_COLOR is enforced! We should not have colors!")
    end

    it "colors are still used and NO_COLOR is ignored if the environment variable is nil" do
      allow(ENV).to receive(:[]).with("NO_COLOR").and_return(nil)
      out = capture(:stdout) do
        shell.say "NO_COLOR is ignored! We have colors!", :green
      end

      expect(out.chomp).to eq("\e[32mNO_COLOR is ignored! We have colors!\e[0m")
    end

    it "colors are still used and NO_COLOR is ignored if the environment variable is an empty-string" do
      allow(ENV).to receive(:[]).with("NO_COLOR").and_return("")
      out = capture(:stdout) do
        shell.say "NO_COLOR is ignored! We have colors!", :green
      end

      expect(out.chomp).to eq("\e[32mNO_COLOR is ignored! We have colors!\e[0m")
    end

    it "does not use a new line even with colors" do
      out = capture(:stdout) do
        shell.say "Wow! Now we have colors! ", :green
      end

      expect(out.chomp).to eq("\e[32mWow! Now we have colors! \e[0m")
    end

    it "handles an Array of colors" do
      out = capture(:stdout) do
        shell.say "Wow! Now we have colors *and* background colors", [:green, :on_red, :bold]
      end

      expect(out.chomp).to eq("\e[32m\e[41m\e[1mWow! Now we have colors *and* background colors\e[0m")
    end

    it "supports the legacy color syntax" do
      out = capture(:stdout) do
        shell.say "Wow! This still works?", [:blue, true]
      end

      expect(out.chomp).to eq("\e[1m\e[34mWow! This still works?\e[0m")
    end
  end

  describe "#say_status" do
    it "uses color to say status" do
      out = capture(:stdout) do
        shell.say_status :conflict, "README", :red
      end

      expect(out.chomp).to eq("\e[1m\e[31m    conflict\e[0m  README")
    end
  end

  describe "#set_color" do
    it "colors a string with a foreground color" do
      red = shell.set_color "hi!", :red
      expect(red).to eq("\e[31mhi!\e[0m")
    end

    it "colors a string with a background color" do
      on_red = shell.set_color "hi!", :white, :on_red
      expect(on_red).to eq("\e[37m\e[41mhi!\e[0m")
    end

    it "colors a string with a bold color" do
      bold = shell.set_color "hi!", :white, true
      expect(bold).to eq("\e[1m\e[37mhi!\e[0m")

      bold = shell.set_color "hi!", :white, :bold
      expect(bold).to eq("\e[37m\e[1mhi!\e[0m")

      bold = shell.set_color "hi!", :white, :on_red, :bold
      expect(bold).to eq("\e[37m\e[41m\e[1mhi!\e[0m")
    end

    it "does nothing when there are no colors" do
      colorless = shell.set_color "hi!", nil
      expect(colorless).to eq("hi!")

      colorless = shell.set_color "hi!"
      expect(colorless).to eq("hi!")
    end

    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 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 NO_COLOR environment variable is set to a non-empty string" do
      allow(ENV).to receive(:[]).with("NO_COLOR").and_return("non-empty value")
      allow($stdout).to receive(:tty?).and_return(true)
      colorless = shell.set_color "hi!", :white
      expect(colorless).to eq("hi!")
    end

    it "sets color when the NO_COLOR environment variable is ignored for being nil" do
      allow(ENV).to receive(:[]).with("NO_COLOR").and_return(nil)
      allow($stdout).to receive(:tty?).and_return(true)

      red = shell.set_color "hi!", :red
      expect(red).to eq("\e[31mhi!\e[0m")

      on_red = shell.set_color "hi!", :white, :on_red
      expect(on_red).to eq("\e[37m\e[41mhi!\e[0m")
    end

    it "sets color when the NO_COLOR environment variable is ignored for being an empty string" do
      allow(ENV).to receive(:[]).with("NO_COLOR").and_return("")
      allow($stdout).to receive(:tty?).and_return(true)

      red = shell.set_color "hi!", :red
      expect(red).to eq("\e[31mhi!\e[0m")

      on_red = shell.set_color "hi!", :white, :on_red
      expect(on_red).to eq("\e[37m\e[41mhi!\e[0m")
    end
  end

  describe "#file_collision" do
    describe "when a block is given" do
      it "invokes the diff command" do
        allow($stdout).to receive(:print)
        allow($stdout).to receive(:tty?).and_return(true)
        expect(Thor::LineEditor).to receive(:readline).and_return("d", "n")

        output = capture(:stdout) { shell.file_collision("spec/fixtures/doc/README") { "README\nEND\n" } }
        expect(output).to match(/\e\[31m\- __start__\e\[0m/)
        expect(output).to match(/^  README/)
        expect(output).to match(/\e\[32m\+ END\e\[0m/)
      end
    end
  end
end