thor 0.18.1 → 0.19.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +13 -7
  3. data/Thorfile +4 -5
  4. data/bin/thor +1 -1
  5. data/lib/thor.rb +78 -67
  6. data/lib/thor/actions.rb +57 -56
  7. data/lib/thor/actions/create_file.rb +33 -35
  8. data/lib/thor/actions/create_link.rb +2 -3
  9. data/lib/thor/actions/directory.rb +37 -38
  10. data/lib/thor/actions/empty_directory.rb +67 -69
  11. data/lib/thor/actions/file_manipulation.rb +17 -15
  12. data/lib/thor/actions/inject_into_file.rb +27 -29
  13. data/lib/thor/base.rb +193 -189
  14. data/lib/thor/command.rb +20 -23
  15. data/lib/thor/core_ext/hash_with_indifferent_access.rb +21 -24
  16. data/lib/thor/core_ext/io_binary_read.rb +2 -4
  17. data/lib/thor/core_ext/ordered_hash.rb +9 -11
  18. data/lib/thor/error.rb +5 -1
  19. data/lib/thor/group.rb +53 -54
  20. data/lib/thor/invocation.rb +44 -38
  21. data/lib/thor/line_editor.rb +17 -0
  22. data/lib/thor/line_editor/basic.rb +35 -0
  23. data/lib/thor/line_editor/readline.rb +88 -0
  24. data/lib/thor/parser.rb +4 -4
  25. data/lib/thor/parser/argument.rb +28 -29
  26. data/lib/thor/parser/arguments.rb +102 -98
  27. data/lib/thor/parser/option.rb +26 -22
  28. data/lib/thor/parser/options.rb +86 -86
  29. data/lib/thor/rake_compat.rb +9 -10
  30. data/lib/thor/runner.rb +141 -141
  31. data/lib/thor/shell.rb +27 -34
  32. data/lib/thor/shell/basic.rb +91 -63
  33. data/lib/thor/shell/color.rb +44 -43
  34. data/lib/thor/shell/html.rb +59 -60
  35. data/lib/thor/util.rb +24 -27
  36. data/lib/thor/version.rb +1 -1
  37. data/spec/actions/create_file_spec.rb +25 -27
  38. data/spec/actions/create_link_spec.rb +19 -18
  39. data/spec/actions/directory_spec.rb +31 -31
  40. data/spec/actions/empty_directory_spec.rb +18 -18
  41. data/spec/actions/file_manipulation_spec.rb +38 -28
  42. data/spec/actions/inject_into_file_spec.rb +13 -13
  43. data/spec/actions_spec.rb +43 -43
  44. data/spec/base_spec.rb +45 -38
  45. data/spec/command_spec.rb +13 -14
  46. data/spec/core_ext/hash_with_indifferent_access_spec.rb +19 -19
  47. data/spec/core_ext/ordered_hash_spec.rb +6 -6
  48. data/spec/exit_condition_spec.rb +4 -4
  49. data/spec/fixtures/invoke.thor +19 -0
  50. data/spec/fixtures/script.thor +1 -1
  51. data/spec/group_spec.rb +30 -24
  52. data/spec/helper.rb +28 -15
  53. data/spec/invocation_spec.rb +39 -19
  54. data/spec/line_editor/basic_spec.rb +28 -0
  55. data/spec/line_editor/readline_spec.rb +69 -0
  56. data/spec/line_editor_spec.rb +43 -0
  57. data/spec/parser/argument_spec.rb +12 -12
  58. data/spec/parser/arguments_spec.rb +11 -11
  59. data/spec/parser/option_spec.rb +33 -25
  60. data/spec/parser/options_spec.rb +66 -52
  61. data/spec/quality_spec.rb +75 -0
  62. data/spec/rake_compat_spec.rb +10 -10
  63. data/spec/register_spec.rb +60 -30
  64. data/spec/runner_spec.rb +67 -62
  65. data/spec/sandbox/application.rb +2 -0
  66. data/spec/sandbox/app{1}/README +3 -0
  67. data/spec/sandbox/bundle/execute.rb +6 -0
  68. data/spec/sandbox/bundle/main.thor +1 -0
  69. data/spec/sandbox/command.thor +10 -0
  70. data/spec/sandbox/doc/%file_name%.rb.tt +1 -0
  71. data/spec/sandbox/doc/COMMENTER +11 -0
  72. data/spec/sandbox/doc/README +3 -0
  73. data/spec/sandbox/doc/block_helper.rb +3 -0
  74. data/spec/sandbox/doc/config.rb +1 -0
  75. data/spec/sandbox/doc/config.yaml.tt +1 -0
  76. data/spec/sandbox/doc/excluding/%file_name%.rb.tt +1 -0
  77. data/spec/sandbox/enum.thor +10 -0
  78. data/spec/sandbox/group.thor +128 -0
  79. data/spec/sandbox/invoke.thor +131 -0
  80. data/spec/sandbox/path with spaces b/data/spec/sandbox/path with → spaces +0 -0
  81. data/spec/sandbox/preserve/script.sh +3 -0
  82. data/spec/sandbox/script.thor +220 -0
  83. data/spec/sandbox/subcommand.thor +17 -0
  84. data/spec/shell/basic_spec.rb +107 -86
  85. data/spec/shell/color_spec.rb +32 -8
  86. data/spec/shell/html_spec.rb +3 -4
  87. data/spec/shell_spec.rb +7 -7
  88. data/spec/subcommand_spec.rb +20 -2
  89. data/spec/thor_spec.rb +111 -97
  90. data/spec/util_spec.rb +30 -30
  91. data/thor.gemspec +14 -14
  92. metadata +69 -25
@@ -0,0 +1,17 @@
1
+ module TestSubcommands
2
+
3
+ class Subcommand < Thor
4
+ desc "print_opt", "My method"
5
+ def print_opt
6
+ print options["opt"]
7
+ end
8
+ end
9
+
10
+ class Parent < Thor
11
+ class_option "opt"
12
+
13
+ desc "sub", "My subcommand"
14
+ subcommand "sub", Subcommand
15
+ end
16
+
17
+ end
@@ -1,5 +1,5 @@
1
- # coding: UTF-8
2
- require 'helper'
1
+ # coding: utf-8
2
+ require "helper"
3
3
 
4
4
  describe Thor::Shell::Basic do
5
5
  def shell
@@ -18,92 +18,123 @@ describe Thor::Shell::Basic do
18
18
 
19
19
  describe "#ask" do
20
20
  it "prints a message to the user and gets the response" do
21
- $stdout.should_receive(:print).with("Should I overwrite it? ")
22
- $stdin.should_receive(:gets).and_return('Sure')
21
+ expect(Thor::LineEditor).to receive(:readline).with("Should I overwrite it? ", {}).and_return("Sure")
23
22
  expect(shell.ask("Should I overwrite it?")).to eq("Sure")
24
23
  end
25
24
 
26
- it "prints a message and returns nil if EOF is sent to stdin" do
27
- $stdout.should_receive(:print).with(" ")
28
- $stdin.should_receive(:gets).and_return(nil)
25
+ it "prints a message to the user prefixed with the current padding" do
26
+ expect(Thor::LineEditor).to receive(:readline).with(" Enter your name: ", {}).and_return("George")
27
+ shell.padding = 2
28
+ shell.ask("Enter your name:")
29
+ end
30
+
31
+ it "prints a message and returns nil if EOF is given as input" do
32
+ expect(Thor::LineEditor).to receive(:readline).with(" ", {}).and_return(nil)
29
33
  expect(shell.ask("")).to eq(nil)
30
34
  end
31
35
 
36
+ it "prints a message to the user and does not echo stdin if the echo option is set to false" do
37
+ expect($stdout).to receive(:print).with('What\'s your password? ')
38
+ expect($stdin).to receive(:noecho).and_return("mysecretpass")
39
+ expect(shell.ask("What's your password?", :echo => false)).to eq("mysecretpass")
40
+ end
32
41
 
33
42
  it "prints a message to the user with the available options and determines the correctness of the answer" do
34
- $stdout.should_receive(:print).with('What\'s your favorite Neopolitan flavor? ["strawberry", "chocolate", "vanilla"] ')
35
- $stdin.should_receive(:gets).and_return('chocolate')
36
- expect(shell.ask("What's your favorite Neopolitan flavor?", :limited_to => ["strawberry", "chocolate", "vanilla"])).to eq("chocolate")
43
+ flavors = %w[strawberry chocolate vanilla]
44
+ expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] ', :limited_to => flavors).and_return("chocolate")
45
+ expect(shell.ask('What\'s your favorite Neopolitan flavor?', :limited_to => flavors)).to eq("chocolate")
37
46
  end
38
47
 
39
48
  it "prints a message to the user with the available options and reasks the question after an incorrect repsonse" do
40
- $stdout.should_receive(:print).with('What\'s your favorite Neopolitan flavor? ["strawberry", "chocolate", "vanilla"] ').twice
41
- $stdout.should_receive(:puts).with('Your response must be one of: ["strawberry", "chocolate", "vanilla"]. Please try again.')
42
- $stdin.should_receive(:gets).and_return('moose tracks', 'chocolate')
43
- expect(shell.ask("What's your favorite Neopolitan flavor?", :limited_to => ["strawberry", "chocolate", "vanilla"])).to eq("chocolate")
49
+ flavors = %w[strawberry chocolate vanilla]
50
+ expect($stdout).to receive(:print).with("Your response must be one of: [strawberry, chocolate, vanilla]. Please try again.\n")
51
+ expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] ', :limited_to => flavors).and_return("moose tracks", "chocolate")
52
+ expect(shell.ask('What\'s your favorite Neopolitan flavor?', :limited_to => flavors)).to eq("chocolate")
53
+ end
54
+
55
+ it "prints a message to the user containing a default and sets the default if only enter is pressed" do
56
+ expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? (vanilla) ', :default => "vanilla").and_return("")
57
+ expect(shell.ask('What\'s your favorite Neopolitan flavor?', :default => "vanilla")).to eq("vanilla")
58
+ end
59
+
60
+ it "prints a message to the user with the available options and reasks the question after an incorrect repsonse and then returns the default" do
61
+ flavors = %w[strawberry chocolate vanilla]
62
+ expect($stdout).to receive(:print).with("Your response must be one of: [strawberry, chocolate, vanilla]. Please try again.\n")
63
+ expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] (vanilla) ', :default => "vanilla", :limited_to => flavors).and_return("moose tracks", "")
64
+ expect(shell.ask("What's your favorite Neopolitan flavor?", :default => "vanilla", :limited_to => flavors)).to eq("vanilla")
44
65
  end
45
66
  end
46
67
 
47
68
  describe "#yes?" do
48
69
  it "asks the user and returns true if the user replies yes" do
49
- $stdout.should_receive(:print).with("Should I overwrite it? ")
50
- $stdin.should_receive(:gets).and_return('y')
70
+ expect(Thor::LineEditor).to receive(:readline).with("Should I overwrite it? ", :add_to_history => false).and_return("y")
51
71
  expect(shell.yes?("Should I overwrite it?")).to be_true
72
+ end
52
73
 
53
- $stdout.should_receive(:print).with("Should I overwrite it? ")
54
- $stdin.should_receive(:gets).and_return('n')
74
+ it "asks the user and returns false if the user replies no" do
75
+ expect(Thor::LineEditor).to receive(:readline).with("Should I overwrite it? ", :add_to_history => false).and_return("n")
55
76
  expect(shell.yes?("Should I overwrite it?")).not_to be_true
56
77
  end
78
+
79
+ it "asks the user and returns false if the user replies with an answer other than yes or no" do
80
+ expect(Thor::LineEditor).to receive(:readline).with("Should I overwrite it? ", :add_to_history => false).and_return("foobar")
81
+ expect(shell.yes?("Should I overwrite it?")).to be_false
82
+ end
57
83
  end
58
84
 
59
85
  describe "#no?" do
60
86
  it "asks the user and returns true if the user replies no" do
61
- $stdout.should_receive(:print).with("Should I overwrite it? ")
62
- $stdin.should_receive(:gets).and_return('n')
87
+ expect(Thor::LineEditor).to receive(:readline).with("Should I overwrite it? ", :add_to_history => false).and_return("n")
63
88
  expect(shell.no?("Should I overwrite it?")).to be_true
89
+ end
90
+
91
+ it "asks the user and returns false if the user replies yes" do
92
+ expect(Thor::LineEditor).to receive(:readline).with("Should I overwrite it? ", :add_to_history => false).and_return("Yes")
93
+ expect(shell.no?("Should I overwrite it?")).to be_false
94
+ end
64
95
 
65
- $stdout.should_receive(:print).with("Should I overwrite it? ")
66
- $stdin.should_receive(:gets).and_return('Yes')
96
+ it "asks the user and returns false if the user replies with an answer other than yes or no" do
97
+ expect(Thor::LineEditor).to receive(:readline).with("Should I overwrite it? ", :add_to_history => false).and_return("foobar")
67
98
  expect(shell.no?("Should I overwrite it?")).to be_false
68
99
  end
69
100
  end
70
101
 
71
102
  describe "#say" do
72
103
  it "prints a message to the user" do
73
- $stdout.should_receive(:puts).with("Running...")
104
+ expect($stdout).to receive(:print).with("Running...\n")
74
105
  shell.say("Running...")
75
106
  end
76
107
 
77
108
  it "prints a message to the user without new line if it ends with a whitespace" do
78
- $stdout.should_receive(:print).with("Running... ")
109
+ expect($stdout).to receive(:print).with("Running... ")
79
110
  shell.say("Running... ")
80
111
  end
81
112
 
82
113
  it "does not use a new line with whitespace+newline embedded" do
83
- $stdout.should_receive(:puts).with("It's \nRunning...")
114
+ expect($stdout).to receive(:print).with("It's \nRunning...\n")
84
115
  shell.say("It's \nRunning...")
85
116
  end
86
117
 
87
118
  it "prints a message to the user without new line" do
88
- $stdout.should_receive(:print).with("Running...")
119
+ expect($stdout).to receive(:print).with("Running...")
89
120
  shell.say("Running...", nil, false)
90
121
  end
91
122
  end
92
123
 
93
124
  describe "#say_status" do
94
125
  it "prints a message to the user with status" do
95
- $stdout.should_receive(:puts).with(" create ~/.thor/command.thor")
126
+ expect($stdout).to receive(:print).with(" create ~/.thor/command.thor\n")
96
127
  shell.say_status(:create, "~/.thor/command.thor")
97
128
  end
98
129
 
99
- it "always use new line" do
100
- $stdout.should_receive(:puts).with(" create ")
130
+ it "always uses new line" do
131
+ expect($stdout).to receive(:print).with(" create \n")
101
132
  shell.say_status(:create, "")
102
133
  end
103
134
 
104
135
  it "does not print a message if base is muted" do
105
- shell.should_receive(:mute?).and_return(true)
106
- $stdout.should_not_receive(:puts)
136
+ expect(shell).to receive(:mute?).and_return(true)
137
+ expect($stdout).not_to receive(:print)
107
138
 
108
139
  shell.mute do
109
140
  shell.say_status(:created, "~/.thor/command.thor")
@@ -111,30 +142,30 @@ describe Thor::Shell::Basic do
111
142
  end
112
143
 
113
144
  it "does not print a message if base is set to quiet" do
114
- base = MyCounter.new [1,2]
115
- base.should_receive(:options).and_return(:quiet => true)
145
+ base = MyCounter.new [1, 2]
146
+ expect(base).to receive(:options).and_return(:quiet => true)
116
147
 
117
- $stdout.should_not_receive(:puts)
148
+ expect($stdout).not_to receive(:print)
118
149
  shell.base = base
119
150
  shell.say_status(:created, "~/.thor/command.thor")
120
151
  end
121
152
 
122
153
  it "does not print a message if log status is set to false" do
123
- $stdout.should_not_receive(:puts)
154
+ expect($stdout).not_to receive(:print)
124
155
  shell.say_status(:created, "~/.thor/command.thor", false)
125
156
  end
126
157
 
127
- it "uses padding to set messages left margin" do
158
+ it "uses padding to set message's left margin" do
128
159
  shell.padding = 2
129
- $stdout.should_receive(:puts).with(" create ~/.thor/command.thor")
160
+ expect($stdout).to receive(:print).with(" create ~/.thor/command.thor\n")
130
161
  shell.say_status(:create, "~/.thor/command.thor")
131
162
  end
132
163
  end
133
164
 
134
165
  describe "#print_in_columns" do
135
166
  before do
136
- @array = [1234567890]
137
- @array += ('a'..'e').to_a
167
+ @array = [1_234_567_890]
168
+ @array += ("a".."e").to_a
138
169
  end
139
170
 
140
171
  it "prints in columns" do
@@ -172,7 +203,7 @@ TABLE
172
203
  it "uses maximum terminal width" do
173
204
  @table << ["def", "#456", "Lançam foo bar"]
174
205
  @table << ["ghi", "#789", "بالله عليكم"]
175
- shell.should_receive(:terminal_width).and_return(20)
206
+ expect(shell).to receive(:terminal_width).and_return(20)
176
207
  content = capture(:stdout) { shell.print_table(@table, :indent => 2, :truncate => true) }
177
208
  expect(content).to eq(<<-TABLE)
178
209
  abc #123 firs...
@@ -184,7 +215,7 @@ TABLE
184
215
  end
185
216
 
186
217
  it "honors the colwidth option" do
187
- content = capture(:stdout) { shell.print_table(@table, :colwidth => 10)}
218
+ content = capture(:stdout) { shell.print_table(@table, :colwidth => 10) }
188
219
  expect(content).to eq(<<-TABLE)
189
220
  abc #123 first three
190
221
  #0 empty
@@ -204,7 +235,7 @@ TABLE
204
235
 
205
236
  it "prints a table with small numbers and right-aligns them" do
206
237
  table = [
207
- ["Name", "Number", "Color"],
238
+ ["Name", "Number", "Color"], # rubocop: disable WordArray
208
239
  ["Erik", 1, "green"]
209
240
  ]
210
241
  content = capture(:stdout) { shell.print_table(table) }
@@ -216,7 +247,7 @@ TABLE
216
247
 
217
248
  it "doesn't output extra spaces for right-aligned columns in the last column" do
218
249
  table = [
219
- ["Name", "Number"],
250
+ ["Name", "Number"], # rubocop: disable WordArray
220
251
  ["Erik", 1]
221
252
  ]
222
253
  content = capture(:stdout) { shell.print_table(table) }
@@ -228,8 +259,8 @@ TABLE
228
259
 
229
260
  it "prints a table with big numbers" do
230
261
  table = [
231
- ["Name", "Number", "Color"],
232
- ["Erik", 1234567890123, "green"]
262
+ ["Name", "Number", "Color"], # rubocop: disable WordArray
263
+ ["Erik", 1_234_567_890_123, "green"]
233
264
  ]
234
265
  content = capture(:stdout) { shell.print_table(table) }
235
266
  expect(content).to eq(<<-TABLE)
@@ -241,70 +272,60 @@ TABLE
241
272
 
242
273
  describe "#file_collision" do
243
274
  it "shows a menu with options" do
244
- $stdout.should_receive(:print).with('Overwrite foo? (enter "h" for help) [Ynaqh] ')
245
- $stdin.should_receive(:gets).and_return('n')
246
- shell.file_collision('foo')
275
+ expect(Thor::LineEditor).to receive(:readline).with('Overwrite foo? (enter "h" for help) [Ynaqh] ', :add_to_history => false).and_return("n")
276
+ shell.file_collision("foo")
247
277
  end
248
278
 
249
- it "returns true if the user choose default option" do
250
- $stdout.stub!(:print)
251
- $stdin.should_receive(:gets).and_return('')
252
- expect(shell.file_collision('foo')).to be_true
279
+ it "returns true if the user chooses default option" do
280
+ expect(Thor::LineEditor).to receive(:readline).and_return("")
281
+ expect(shell.file_collision("foo")).to be_true
253
282
  end
254
283
 
255
- it "returns false if the user choose no" do
256
- $stdout.stub!(:print)
257
- $stdin.should_receive(:gets).and_return('n')
258
- expect(shell.file_collision('foo')).to be_false
284
+ it "returns false if the user chooses no" do
285
+ expect(Thor::LineEditor).to receive(:readline).and_return("n")
286
+ expect(shell.file_collision("foo")).to be_false
259
287
  end
260
288
 
261
- it "returns true if the user choose yes" do
262
- $stdout.stub!(:print)
263
- $stdin.should_receive(:gets).and_return('y')
264
- expect(shell.file_collision('foo')).to be_true
289
+ it "returns true if the user chooses yes" do
290
+ expect(Thor::LineEditor).to receive(:readline).and_return("y")
291
+ expect(shell.file_collision("foo")).to be_true
265
292
  end
266
293
 
267
- it "shows help usage if the user choose help" do
268
- $stdout.stub!(:print)
269
- $stdin.should_receive(:gets).and_return('h')
270
- $stdin.should_receive(:gets).and_return('n')
271
- help = capture(:stdout) { shell.file_collision('foo') }
294
+ it "shows help usage if the user chooses help" do
295
+ expect(Thor::LineEditor).to receive(:readline).and_return("h", "n")
296
+ help = capture(:stdout) { shell.file_collision("foo") }
272
297
  expect(help).to match(/h \- help, show this help/)
273
298
  end
274
299
 
275
- it "quits if the user choose quit" do
276
- $stdout.stub!(:print)
277
- $stdout.should_receive(:puts).with('Aborting...')
278
- $stdin.should_receive(:gets).and_return('q')
300
+ it "quits if the user chooses quit" do
301
+ expect($stdout).to receive(:print).with("Aborting...\n")
302
+ expect(Thor::LineEditor).to receive(:readline).and_return("q")
279
303
 
280
- expect {
281
- shell.file_collision('foo')
282
- }.to raise_error(SystemExit)
304
+ expect do
305
+ shell.file_collision("foo")
306
+ end.to raise_error(SystemExit)
283
307
  end
284
308
 
285
- it "always returns true if the user choose always" do
286
- $stdout.should_receive(:print).with('Overwrite foo? (enter "h" for help) [Ynaqh] ')
287
- $stdin.should_receive(:gets).and_return('a')
309
+ it "always returns true if the user chooses always" do
310
+ expect(Thor::LineEditor).to receive(:readline).with('Overwrite foo? (enter "h" for help) [Ynaqh] ', :add_to_history => false).and_return("a")
288
311
 
289
- expect(shell.file_collision('foo')).to be_true
312
+ expect(shell.file_collision("foo")).to be true
290
313
 
291
- $stdout.should_not_receive(:print)
292
- expect(shell.file_collision('foo')).to be_true
314
+ expect($stdout).not_to receive(:print)
315
+ expect(shell.file_collision("foo")).to be true
293
316
  end
294
317
 
295
318
  describe "when a block is given" do
296
319
  it "displays diff options to the user" do
297
- $stdout.should_receive(:print).with('Overwrite foo? (enter "h" for help) [Ynaqdh] ')
298
- $stdin.should_receive(:gets).and_return('s')
299
- shell.file_collision('foo'){ }
320
+ expect(Thor::LineEditor).to receive(:readline).with('Overwrite foo? (enter "h" for help) [Ynaqdh] ', :add_to_history => false).and_return("s")
321
+ shell.file_collision("foo") {}
300
322
  end
301
323
 
302
324
  it "invokes the diff command" do
303
- $stdout.stub!(:print)
304
- $stdin.should_receive(:gets).and_return('d')
305
- $stdin.should_receive(:gets).and_return('n')
306
- shell.should_receive(:system).with(/diff -u/)
307
- capture(:stdout) { shell.file_collision('foo'){ } }
325
+ expect(Thor::LineEditor).to receive(:readline).and_return("d")
326
+ expect(Thor::LineEditor).to receive(:readline).and_return("n")
327
+ expect(shell).to receive(:system).with(/diff -u/)
328
+ capture(:stdout) { shell.file_collision("foo") {} }
308
329
  end
309
330
  end
310
331
  end
@@ -1,4 +1,4 @@
1
- require 'helper'
1
+ require "helper"
2
2
 
3
3
  describe Thor::Shell::Color do
4
4
  def shell
@@ -6,7 +6,18 @@ describe Thor::Shell::Color do
6
6
  end
7
7
 
8
8
  before do
9
- StringIO.any_instance.stub(:tty?).and_return(true)
9
+ allow($stdout).to receive(:tty?).and_return(true)
10
+ allow_any_instance_of(StringIO).to receive(:tty?).and_return(true)
11
+ end
12
+
13
+ describe "#ask" do
14
+ it "sets the color if specified and tty?" do
15
+ expect(Thor::LineEditor).to receive(:readline).with("\e[32mIs this green? \e[0m", anything).and_return("yes")
16
+ shell.ask "Is this green?", :green
17
+
18
+ expect(Thor::LineEditor).to receive(:readline).with("\e[32mIs this green? [Yes, No, Maybe] \e[0m", anything).and_return("Yes")
19
+ shell.ask "Is this green?", :green, :limited_to => %w[Yes No Maybe]
20
+ end
10
21
  end
11
22
 
12
23
  describe "#say" do
@@ -20,7 +31,7 @@ describe Thor::Shell::Color do
20
31
 
21
32
  it "does not set the color if output is not a tty" do
22
33
  out = capture(:stdout) do
23
- $stdout.should_receive(:tty?).and_return(false)
34
+ expect($stdout).to receive(:tty?).and_return(false)
24
35
  shell.say "Wow! Now we have colors!", :green
25
36
  end
26
37
 
@@ -75,17 +86,30 @@ describe Thor::Shell::Color do
75
86
  bold = shell.set_color "hi!", :white, :on_red, :bold
76
87
  expect(bold).to eq("\e[37m\e[41m\e[1mhi!\e[0m")
77
88
  end
89
+
90
+ it "does nothing when there are no colors" do
91
+ colorless = shell.set_color "hi!", nil
92
+ expect(colorless).to eq("hi!")
93
+
94
+ colorless = shell.set_color "hi!"
95
+ expect(colorless).to eq("hi!")
96
+ end
97
+
98
+ it "does nothing when the terminal does not support color" do
99
+ allow($stdout).to receive(:tty?).and_return(false)
100
+ colorless = shell.set_color "hi!", :white
101
+ expect(colorless).to eq("hi!")
102
+ end
78
103
  end
79
104
 
80
105
  describe "#file_collision" do
81
106
  describe "when a block is given" do
82
107
  it "invokes the diff command" do
83
- $stdout.stub!(:print)
84
- $stdout.stub!(:tty?).and_return(true)
85
- $stdin.should_receive(:gets).and_return('d')
86
- $stdin.should_receive(:gets).and_return('n')
108
+ allow($stdout).to receive(:print)
109
+ allow($stdout).to receive(:tty?).and_return(true)
110
+ expect(Thor::LineEditor).to receive(:readline).and_return("d", "n")
87
111
 
88
- output = capture(:stdout) { shell.file_collision('spec/fixtures/doc/README'){ "README\nEND\n" } }
112
+ output = capture(:stdout) { shell.file_collision("spec/fixtures/doc/README") { "README\nEND\n" } }
89
113
  expect(output).to match(/\e\[31m\- __start__\e\[0m/)
90
114
  expect(output).to match(/^ README/)
91
115
  expect(output).to match(/\e\[32m\+ END\e\[0m/)
@@ -1,4 +1,4 @@
1
- require 'helper'
1
+ require "helper"
2
2
 
3
3
  describe Thor::Shell::HTML do
4
4
  def shell
@@ -6,7 +6,7 @@ describe Thor::Shell::HTML do
6
6
  end
7
7
 
8
8
  describe "#say" do
9
- it "set the color if specified" do
9
+ it "sets the color if specified" do
10
10
  out = capture(:stdout) { shell.say "Wow! Now we have colors!", :green }
11
11
  expect(out.chomp).to eq('<span style="color: green;">Wow! Now we have colors!</span>')
12
12
  end
@@ -24,9 +24,8 @@ describe Thor::Shell::HTML do
24
24
 
25
25
  describe "#say_status" do
26
26
  it "uses color to say status" do
27
- $stdout.should_receive(:puts).with('<span style="color: red; font-weight: bold;"> conflict</span> README')
27
+ expect($stdout).to receive(:print).with("<span style=\"color: red; font-weight: bold;\"> conflict</span> README\n")
28
28
  shell.say_status :conflict, "README", :red
29
29
  end
30
30
  end
31
-
32
31
  end