File: thor_spec.rb

package info (click to toggle)
ruby-thor 0.15.3-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 608 kB
  • sloc: ruby: 6,481; makefile: 2
file content (418 lines) | stat: -rw-r--r-- 14,337 bytes parent folder | download
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe Thor do
  describe "#method_option" do
    it "sets options to the next method to be invoked" do
      args = ["foo", "bar", "--force"]
      arg, options = MyScript.start(args)
      options.should == { "force" => true }
    end

    describe ":lazy_default" do
      it "is absent when option is not specified" do
        arg, options = MyScript.start(["with_optional"])
        options.should == {}
      end

      it "sets a default that can be overridden for strings" do
        arg, options = MyScript.start(["with_optional", "--lazy"])
        options.should == { "lazy" => "yes" }

        arg, options = MyScript.start(["with_optional", "--lazy", "yesyes!"])
        options.should == { "lazy" => "yesyes!" }
      end

      it "sets a default that can be overridden for numerics" do
        arg, options = MyScript.start(["with_optional", "--lazy-numeric"])
        options.should == { "lazy_numeric" => 42 }

        arg, options = MyScript.start(["with_optional", "--lazy-numeric", 20000])
        options.should == { "lazy_numeric" => 20000 }
      end

      it "sets a default that can be overridden for arrays" do
        arg, options = MyScript.start(["with_optional", "--lazy-array"])
        options.should == { "lazy_array" => %w[eat at joes] }

        arg, options = MyScript.start(["with_optional", "--lazy-array", "hello", "there"])
        options.should == { "lazy_array" => %w[hello there] }
      end

      it "sets a default that can be overridden for hashes" do
        arg, options = MyScript.start(["with_optional", "--lazy-hash"])
        options.should == { "lazy_hash" => {'swedish' => 'meatballs'} }

        arg, options = MyScript.start(["with_optional", "--lazy-hash", "polish:sausage"])
        options.should == { "lazy_hash" => {'polish' => 'sausage'} }
      end
    end

    describe "when :for is supplied" do
      it "updates an already defined task" do
        args, options = MyChildScript.start(["animal", "horse", "--other=fish"])
        options[:other].should == "fish"
      end

      describe "and the target is on the parent class" do
        it "updates an already defined task" do
          args = ["example_default_task", "my_param", "--new-option=verified"]
          options = Scripts::MyScript.start(args)
          options[:new_option].should == "verified"
        end

        it "adds a task to the tasks list if the updated task is on the parent class" do
          Scripts::MyScript.tasks["example_default_task"].should be
        end

        it "clones the parent task" do
          Scripts::MyScript.tasks["example_default_task"].should_not == MyChildScript.tasks["example_default_task"]
        end
      end
    end
  end

  describe "#default_task" do
    it "sets a default task" do
      MyScript.default_task.should == "example_default_task"
    end

    it "invokes the default task if no command is specified" do
      MyScript.start([]).should == "default task"
    end

    it "invokes the default task if no command is specified even if switches are given" do
      MyScript.start(["--with", "option"]).should == {"with"=>"option"}
    end

    it "inherits the default task from parent" do
      MyChildScript.default_task.should == "example_default_task"
    end
  end

  describe "#map" do
    it "calls the alias of a method if one is provided" do
      MyScript.start(["-T", "fish"]).should == ["fish"]
    end

    it "calls the alias of a method if several are provided via .map" do
      MyScript.start(["-f", "fish"]).should == ["fish", {}]
      MyScript.start(["--foo", "fish"]).should == ["fish", {}]
    end

    it "inherits all mappings from parent" do
      MyChildScript.default_task.should == "example_default_task"
    end
  end

  describe "#desc" do
    it "provides description for a task" do
      content = capture(:stdout) { MyScript.start(["help"]) }
      content.should =~ /thor my_script:zoo\s+# zoo around/m
    end

    it "provides no namespace if $thor_runner is false" do
      begin
        $thor_runner = false
        content = capture(:stdout) { MyScript.start(["help"]) }
        content.should =~ /thor zoo\s+# zoo around/m
      ensure
        $thor_runner = true
      end
    end

    describe "when :for is supplied" do
      it "overwrites a previous defined task" do
        capture(:stdout) { MyChildScript.start(["help"]) }.should =~ /animal KIND \s+# fish around/m
      end
    end

    describe "when :hide is supplied" do
      it "does not show the task in help" do
        capture(:stdout) { MyScript.start(["help"]) }.should_not =~ /this is hidden/m
      end

      it "but the task is still invokcable not show the task in help" do
        MyScript.start(["hidden", "yesyes"]).should == ["yesyes"]
      end
    end
  end

  describe "#method_options" do
    it "sets default options if called before an initializer" do
      options = MyChildScript.class_options
      options[:force].type.should == :boolean
      options[:param].type.should == :numeric
    end

    it "overwrites default options if called on the method scope" do
      args = ["zoo", "--force", "--param", "feathers"]
      options = MyChildScript.start(args)
      options.should == { "force" => true, "param" => "feathers" }
    end

    it "allows default options to be merged with method options" do
      args = ["animal", "bird", "--force", "--param", "1.0", "--other", "tweets"]
      arg, options = MyChildScript.start(args)
      arg.should == 'bird'
      options.should == { "force"=>true, "param"=>1.0, "other"=>"tweets" }
    end
  end

  describe "#start" do
    it "calls a no-param method when no params are passed" do
      MyScript.start(["zoo"]).should == true
    end

    it "calls a single-param method when a single param is passed" do
      MyScript.start(["animal", "fish"]).should == ["fish"]
    end

    it "does not set options in attributes" do
      MyScript.start(["with_optional", "--all"]).should == [nil, { "all" => true }, []]
    end

    it "raises an error if a required param is not provided" do
      capture(:stderr) { MyScript.start(["animal"]) }.strip.should == 'thor animal requires at least 1 argument: "thor my_script:animal TYPE".'
    end

    it "raises an error if the invoked task does not exist" do
      capture(:stderr) { Amazing.start(["animal"]) }.strip.should == 'Could not find task "animal" in "amazing" namespace.'
    end

    it "calls method_missing if an unknown method is passed in" do
      MyScript.start(["unk", "hello"]).should == [:unk, ["hello"]]
    end

    it "does not call a private method no matter what" do
      capture(:stderr) { MyScript.start(["what"]) }.strip.should == 'Could not find task "what" in "my_script" namespace.'
    end

    it "uses task default options" do
      options = MyChildScript.start(["animal", "fish"]).last
      options.should == { "other" => "method default" }
    end

    it "raises when an exception happens within the task call" do
      lambda { MyScript.start(["call_myself_with_wrong_arity"]) }.should raise_error(ArgumentError)
    end

    context "when the user enters an unambiguous substring of a command" do
      it "should invoke a command" do
        MyScript.start(["z"]).should == MyScript.start(["zoo"])
      end

      it "should invoke a command, even when there's an alias the resolves to the same command" do
        MyScript.start(["hi"]).should == MyScript.start(["hidden"])
      end

      it "should invoke an alias" do
        MyScript.start(["animal_pri"]).should == MyScript.start(["zoo"])
      end
    end

    context "when the user enters an ambiguous substring of a command" do
      it "should raise an exception that explains the ambiguity" do
        lambda { MyScript.start(["call"]) }.should raise_error(ArgumentError, 'Ambiguous task call matches [call_myself_with_wrong_arity, call_unexistent_method]')
      end

      it "should raise an exception when there is an alias" do
        lambda { MyScript.start(["f"]) }.should raise_error(ArgumentError, 'Ambiguous task f matches [foo, fu]')
      end
    end

  end

  describe "#subcommand" do
    it "maps a given subcommand to another Thor subclass" do
      barn_help = capture(:stdout){ Scripts::MyDefaults.start(["barn"]) }
      barn_help.should include("barn help [COMMAND]  # Describe subcommands or one specific subcommand")
    end

    it "passes commands to subcommand classes" do
      capture(:stdout){ Scripts::MyDefaults.start(["barn", "open"]) }.strip.should == "Open sesame!"
    end

    it "passes arguments to subcommand classes" do
      capture(:stdout){ Scripts::MyDefaults.start(["barn", "open", "shotgun"]) }.strip.should == "That's going to leave a mark."
    end

    it "ignores unknown options (the subcommand class will handle them)" do
      capture(:stdout){ Scripts::MyDefaults.start(["barn", "paint", "blue", "--coats", "4"])}.strip.should == "4 coats of blue paint"
    end
  end

  describe "#help" do
    def shell
      @shell ||= Thor::Base.shell.new
    end

    describe "on general" do
      before do
        @content = capture(:stdout){ MyScript.help(shell) }
      end

      it "provides useful help info for the help method itself" do
        @content.should =~ /help \[TASK\]\s+# Describe available tasks/
      end

      it "provides useful help info for a method with params" do
        @content.should =~ /animal TYPE\s+# horse around/
      end

      it "uses the maximum terminal size to show tasks" do
        @shell.should_receive(:terminal_width).and_return(80)
        content = capture(:stdout){ MyScript.help(shell) }
        content.should =~ /aaa\.\.\.$/
      end

      it "provides description for tasks from classes in the same namespace" do
        @content.should =~ /baz\s+# do some bazing/
      end

      it "shows superclass tasks" do
        content = capture(:stdout){ MyChildScript.help(shell) }
        content.should =~ /foo BAR \s+# do some fooing/
      end

      it "shows class options information" do
        content = capture(:stdout){ MyChildScript.help(shell) }
        content.should =~ /Options\:/
        content.should =~ /\[\-\-param=N\]/
      end

      it "injects class arguments into default usage" do
        content = capture(:stdout){ Scripts::MyScript.help(shell) }
        content.should =~ /zoo ACCESSOR \-\-param\=PARAM/
      end
    end

    describe "for a specific task" do
      it "provides full help info when talking about a specific task" do
        capture(:stdout) { MyScript.task_help(shell, "foo") }.should == <<-END
Usage:
  thor my_script:foo BAR

Options:
  [--force]  # Force to do some fooing

do some fooing
  This is more info!
  Everyone likes more info!
END
      end

      it "raises an error if the task can't be found" do
        lambda {
          MyScript.task_help(shell, "unknown")
        }.should raise_error(Thor::UndefinedTaskError, 'Could not find task "unknown" in "my_script" namespace.')
      end

      it "normalizes names before claiming they don't exist" do
        capture(:stdout) { MyScript.task_help(shell, "name-with-dashes") }.should =~ /thor my_script:name-with-dashes/
      end

      it "uses the long description if it exists" do
        capture(:stdout) { MyScript.task_help(shell, "long_description") }.should == <<-HELP
Usage:
  thor my_script:long_description

Description:
  This is a really really really long description. Here you go. So very long.

  It even has two paragraphs.
HELP
      end

      it "doesn't assign the long description to the next task without one" do
        capture(:stdout) do
          MyScript.task_help(shell, "name_with_dashes")
        end.should_not =~ /so very long/i
      end
    end

    describe "instance method" do
      it "calls the class method" do
        capture(:stdout){ MyScript.start(["help"]) }.should =~ /Tasks:/
      end

      it "calls the class method" do
        capture(:stdout){ MyScript.start(["help", "foo"]) }.should =~ /Usage:/
      end
    end
  end

  describe "when creating tasks" do
    it "prints a warning if a public method is created without description or usage" do
      capture(:stdout) {
        klass = Class.new(Thor)
        klass.class_eval "def hello_from_thor; end"
      }.should =~ /\[WARNING\] Attempted to create task "hello_from_thor" without usage or description/
    end

    it "does not print if overwriting a previous task" do
      capture(:stdout) {
        klass = Class.new(Thor)
        klass.class_eval "def help; end"
      }.should be_empty
    end
  end

  describe "edge-cases" do
    it "can handle boolean options followed by arguments" do
      klass = Class.new(Thor) do
        method_option :loud, :type => :boolean
        desc "hi NAME", "say hi to name"
        def hi(name)
          name.upcase! if options[:loud]
          "Hi #{name}"
        end
      end

      klass.start(["hi", "jose"]).should == "Hi jose"
      klass.start(["hi", "jose", "--loud"]).should == "Hi JOSE"
      klass.start(["hi", "--loud", "jose"]).should == "Hi JOSE"
    end

    it "passes through unknown options" do
      klass = Class.new(Thor) do
        desc "unknown", "passing unknown options"
        def unknown(*args)
          args
        end
      end

      klass.start(["unknown", "foo", "--bar", "baz", "bat", "--bam"]).should == ["foo", "--bar", "baz", "bat", "--bam"]
      klass.start(["unknown", "--bar", "baz"]).should == ["--bar", "baz"]
    end

    it "does not pass through unknown options with strict args" do
      klass = Class.new(Thor) do
        strict_args_position!

        desc "unknown", "passing unknown options"
        def unknown(*args)
          args
        end
      end

      klass.start(["unknown", "--bar", "baz"]).should == []
      klass.start(["unknown", "foo", "--bar", "baz"]).should == ["foo"]
    end

    it "strict args works in the inheritance chain" do
      parent = Class.new(Thor) do
        strict_args_position!
      end

      klass = Class.new(parent) do
        desc "unknown", "passing unknown options"
        def unknown(*args)
          args
        end
      end

      klass.start(["unknown", "--bar", "baz"]).should == []
      klass.start(["unknown", "foo", "--bar", "baz"]).should == ["foo"]
    end
  end
end