1require 'rubygems/test_case'
2require 'rubygems/command'
3
4class Gem::Command
5  public :parser
6end
7
8class TestGemCommand < Gem::TestCase
9
10  def setup
11    super
12
13    @xopt = nil
14
15    Gem::Command.common_options.clear
16    Gem::Command.common_options <<  [
17      ['-x', '--exe', 'Execute'], lambda do |*a|
18        @xopt = true
19      end
20    ]
21
22    @cmd_name = 'doit'
23    @cmd = Gem::Command.new @cmd_name, 'summary'
24  end
25
26  def test_self_add_specific_extra_args
27    added_args = %w[--all]
28    @cmd.add_option '--all' do |v,o| end
29
30    Gem::Command.add_specific_extra_args @cmd_name, added_args
31
32    assert_equal added_args, Gem::Command.specific_extra_args(@cmd_name)
33
34    h = @cmd.add_extra_args []
35
36    assert_equal added_args, h
37  end
38
39  def test_self_add_specific_extra_args_unknown
40    added_args = %w[--definitely_not_there]
41
42    Gem::Command.add_specific_extra_args @cmd_name, added_args
43
44    assert_equal added_args, Gem::Command.specific_extra_args(@cmd_name)
45
46    h = @cmd.add_extra_args []
47
48    assert_equal [], h
49  end
50
51  def test_basic_accessors
52    assert_equal "doit", @cmd.command
53    assert_equal "gem doit", @cmd.program_name
54    assert_equal "summary", @cmd.summary
55  end
56
57  def test_common_option_in_class
58    assert Array === Gem::Command.common_options
59  end
60
61  def test_defaults
62    @cmd.add_option('-h', '--help [COMMAND]', 'Get help on COMMAND') do |value, options|
63      options[:help] = value
64    end
65
66    @cmd.defaults = { :help => true }
67
68    @cmd.when_invoked do |options|
69      assert options[:help], "Help options should default true"
70    end
71
72    use_ui @ui do
73      @cmd.invoke
74    end
75
76    assert_match %r|Usage: gem doit|, @ui.output
77  end
78
79  def test_invoke
80    done = false
81    @cmd.when_invoked { done = true }
82
83    use_ui @ui do
84      @cmd.invoke
85    end
86
87    assert done
88  end
89
90  def test_invoke_with_bad_options
91    use_ui @ui do
92      @cmd.when_invoked do true end
93
94      ex = assert_raises OptionParser::InvalidOption do
95        @cmd.invoke('-zzz')
96      end
97
98      assert_match(/invalid option:/, ex.message)
99    end
100  end
101
102  def test_invoke_with_common_options
103    @cmd.when_invoked do true end
104
105    use_ui @ui do
106      @cmd.invoke "-x"
107    end
108
109    assert @xopt, "Should have done xopt"
110  end
111
112  def test_invoke_with_build_args
113    @cmd.when_invoked { true }
114
115    use_ui @ui do
116      @cmd.invoke_with_build_args ["-x"], ["--awesome=true"]
117    end
118
119    assert_equal ["--awesome=true"], @cmd.options[:build_args]
120  end
121
122  # Returning false from the command handler invokes the usage output.
123  def test_invoke_with_help
124    done = false
125
126    use_ui @ui do
127      @cmd.add_option('-h', '--help [COMMAND]', 'Get help on COMMAND') do |value, options|
128        options[:help] = true
129        done = true
130      end
131
132      @cmd.invoke('--help')
133
134      assert done
135    end
136
137    assert_match(/Usage/, @ui.output)
138    assert_match(/gem doit/, @ui.output)
139    assert_match(/\[options\]/, @ui.output)
140    assert_match(/-h/, @ui.output)
141    assert_match(/--help \[COMMAND\]/, @ui.output)
142    assert_match(/Get help on COMMAND/, @ui.output)
143    assert_match(/-x/, @ui.output)
144    assert_match(/--exe/, @ui.output)
145    assert_match(/Execute/, @ui.output)
146    assert_match(/Common Options:/, @ui.output)
147  end
148
149  def test_invoke_with_options
150    @cmd.add_option('-h', '--help [COMMAND]', 'Get help on COMMAND') do |value, options|
151      options[:help] = true
152    end
153
154    @cmd.when_invoked do |opts|
155      assert opts[:help]
156    end
157
158    use_ui @ui do
159      @cmd.invoke '-h'
160    end
161
162    assert_match %r|Usage: gem doit|, @ui.output
163  end
164
165  def test_option_recognition
166    @cmd.add_option('-h', '--help [COMMAND]', 'Get help on COMMAND') do |value, options|
167      options[:help] = true
168    end
169    @cmd.add_option('-f', '--file FILE', 'File option') do |value, options|
170      options[:help] = true
171    end
172    assert @cmd.handles?(['-x'])
173    assert @cmd.handles?(['-h'])
174    assert @cmd.handles?(['-h', 'command'])
175    assert @cmd.handles?(['--help', 'command'])
176    assert @cmd.handles?(['-f', 'filename'])
177    assert @cmd.handles?(['--file=filename'])
178    refute @cmd.handles?(['-z'])
179    refute @cmd.handles?(['-f'])
180    refute @cmd.handles?(['--toothpaste'])
181
182    args = ['-h', 'command']
183    @cmd.handles?(args)
184    assert_equal ['-h', 'command'], args
185  end
186
187end
188
189