File: subcommand_spec.rb

package info (click to toggle)
ruby-thor 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 904 kB
  • sloc: ruby: 9,250; makefile: 8; sh: 1
file content (70 lines) | stat: -rw-r--r-- 2,685 bytes parent folder | download | duplicates (4)
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
require "helper"

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

    it "passes commands to subcommand classes" do
      expect(capture(:stdout) { Scripts::MyDefaults.start(%w(barn open)) }.strip).to eq("Open sesame!")
    end

    it "passes arguments to subcommand classes" do
      expect(capture(:stdout) { Scripts::MyDefaults.start(%w(barn open shotgun)) }.strip).to eq("That's going to leave a mark.")
    end

    it "ignores unknown options (the subcommand class will handle them)" do
      expect(capture(:stdout) { Scripts::MyDefaults.start(%w(barn paint blue --coats 4)) }.strip).to eq("4 coats of blue paint")
    end

    it "passes parsed options to subcommands" do
      output = capture(:stdout) { TestSubcommands::Parent.start(%w(sub print_opt --opt output)) }
      expect(output).to eq("output")
    end

    it "accepts the help switch and calls the help command on the subcommand" do
      output = capture(:stdout) { TestSubcommands::Parent.start(%w(sub print_opt --help)) }
      sub_help = capture(:stdout) { TestSubcommands::Parent.start(%w(sub help print_opt)) }
      expect(output).to eq(sub_help)
    end

    it "accepts the help short switch and calls the help command on the subcommand" do
      output = capture(:stdout) { TestSubcommands::Parent.start(%w(sub print_opt -h)) }
      sub_help = capture(:stdout) { TestSubcommands::Parent.start(%w(sub help print_opt)) }
      expect(output).to eq(sub_help)
    end

    it "the help command on the subcommand and after it should result in the same output" do
      output = capture(:stdout) { TestSubcommands::Parent.start(%w(sub help)) }
      sub_help = capture(:stdout) { TestSubcommands::Parent.start(%w(help sub)) }
      expect(output).to eq(sub_help)
    end
  end

  context "subcommand with an arg" do
    module SubcommandTest1
      class Child1 < Thor
        desc "foo NAME", "Fooo"
        def foo(name)
          puts "#{name} was given"
        end
      end

      class Parent < Thor
        desc "child1", "child1 description"
        subcommand "child1", Child1

        def self.exit_on_failure?
          false
        end
      end
    end

    it "shows subcommand name and method name" do
      sub_help = capture(:stderr) { SubcommandTest1::Parent.start(%w(child1 foo)) }
      expect(sub_help).to eq ['ERROR: "thor child1 foo" was called with no arguments', 'Usage: "thor child1 foo NAME"', ""].join("\n")
    end
  end
end