3 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
5 # See LICENSE.txt for permissions.
9 require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
10 require 'rubygems/command'
16 class TestGemCommand < RubyGemTestCase
23 Gem::Command.common_options.clear
24 Gem::Command.common_options << [
25 ['-x', '--exe', 'Execute'], lambda do |*a|
31 @cmd = Gem::Command.new @cmd_name, 'summary'
34 def test_self_add_specific_extra_args
35 added_args = %w[--all]
36 @cmd.add_option '--all' do |v,o| end
38 Gem::Command.add_specific_extra_args @cmd_name, added_args
40 assert_equal added_args, Gem::Command.specific_extra_args(@cmd_name)
42 h = @cmd.add_extra_args []
44 assert_equal added_args, h
47 def test_self_add_specific_extra_args_unknown
48 added_args = %w[--definitely_not_there]
50 Gem::Command.add_specific_extra_args @cmd_name, added_args
52 assert_equal added_args, Gem::Command.specific_extra_args(@cmd_name)
54 h = @cmd.add_extra_args []
59 def test_add_option_overlapping_common_and_local_options
60 @cmd.add_option('-x', '--zip', 'BAD!') do end
61 @cmd.add_option('-z', '--exe', 'BAD!') do end
62 @cmd.add_option('-x', '--exe', 'BAD!') do end
64 assert_match %r|-x, --zip|, @cmd.parser.to_s
65 assert_match %r|-z, --exe|, @cmd.parser.to_s
66 assert_no_match %r|-x, --exe|, @cmd.parser.to_s
69 def test_basic_accessors
70 assert_equal "doit", @cmd.command
71 assert_equal "gem doit", @cmd.program_name
72 assert_equal "summary", @cmd.summary
75 def test_common_option_in_class
76 assert Array === Gem::Command.common_options
80 @cmd.add_option('-h', '--help [COMMAND]', 'Get help on COMMAND') do |value, options|
81 options[:help] = value
84 @cmd.defaults = { :help => true }
86 @cmd.when_invoked do |options|
87 assert options[:help], "Help options should default true"
94 assert_match %r|Usage: gem doit|, @ui.output
99 @cmd.when_invoked { done = true }
108 def test_invode_with_bad_options
110 @cmd.when_invoked do true end
112 ex = assert_raise(OptionParser::InvalidOption) do
116 assert_match(/invalid option:/, ex.message)
120 def test_invoke_with_common_options
121 @cmd.when_invoked do true end
127 assert @xopt, "Should have done xopt"
130 # Returning false from the command handler invokes the usage output.
131 def test_invoke_with_help
135 @cmd.add_option('-h', '--help [COMMAND]', 'Get help on COMMAND') do |value, options|
136 options[:help] = true
140 @cmd.invoke('--help')
145 assert_match(/Usage/, @ui.output)
146 assert_match(/gem doit/, @ui.output)
147 assert_match(/\[options\]/, @ui.output)
148 assert_match(/-h/, @ui.output)
149 assert_match(/--help \[COMMAND\]/, @ui.output)
150 assert_match(/Get help on COMMAND/, @ui.output)
151 assert_match(/-x/, @ui.output)
152 assert_match(/--exe/, @ui.output)
153 assert_match(/Execute/, @ui.output)
154 assert_match(/Common Options:/, @ui.output)
157 def test_invoke_with_options
158 @cmd.add_option('-h', '--help [COMMAND]', 'Get help on COMMAND') do |value, options|
159 options[:help] = true
162 @cmd.when_invoked do |opts|
170 assert_match %r|Usage: gem doit|, @ui.output
173 def test_option_recognition
174 @cmd.add_option('-h', '--help [COMMAND]', 'Get help on COMMAND') do |value, options|
175 options[:help] = true
177 @cmd.add_option('-f', '--file FILE', 'File option') do |value, options|
178 options[:help] = true
180 assert @cmd.handles?(['-x'])
181 assert @cmd.handles?(['-h'])
182 assert @cmd.handles?(['-h', 'command'])
183 assert @cmd.handles?(['--help', 'command'])
184 assert @cmd.handles?(['-f', 'filename'])
185 assert @cmd.handles?(['--file=filename'])
186 assert ! @cmd.handles?(['-z'])
187 assert ! @cmd.handles?(['-f'])
188 assert ! @cmd.handles?(['--toothpaste'])
190 args = ['-h', 'command']
192 assert_equal ['-h', 'command'], args