1require 'rubygems/test_case'
2require 'rubygems/local_remote_options'
3require 'rubygems/command'
4
5class TestGemLocalRemoteOptions < Gem::TestCase
6
7  def setup
8    super
9
10    @cmd = Gem::Command.new 'dummy', 'dummy'
11    @cmd.extend Gem::LocalRemoteOptions
12  end
13
14  def test_add_local_remote_options
15    @cmd.add_local_remote_options
16
17    args = %w[-l -r -b -B 10 --source http://gems.example.com -p --update-sources]
18    assert @cmd.handles?(args)
19  end
20
21  def test_both_eh
22    assert_equal false, @cmd.both?
23
24    @cmd.options[:domain] = :local
25
26    assert_equal false, @cmd.both?
27
28    @cmd.options[:domain] = :both
29
30    assert_equal true, @cmd.both?
31  end
32
33  def test_clear_sources_option
34    @cmd.add_local_remote_options
35
36    s = URI.parse "http://only-gems.example.com/"
37
38    @cmd.handle_options %W[--clear-sources --source #{s}]
39    assert_equal [s.to_s], Gem.sources
40  end
41
42  def test_clear_sources_option_idiot_proof
43    util_setup_fake_fetcher
44
45    @cmd.add_local_remote_options
46    @cmd.handle_options %W[--clear-sources]
47    assert_equal Gem.default_sources, Gem.sources
48  end
49
50  def test_local_eh
51    assert_equal false, @cmd.local?
52
53    @cmd.options[:domain] = :local
54
55    assert_equal true, @cmd.local?
56
57    @cmd.options[:domain] = :both
58
59    assert_equal true, @cmd.local?
60  end
61
62  def test_remote_eh
63    assert_equal false, @cmd.remote?
64
65    @cmd.options[:domain] = :remote
66
67    assert_equal true, @cmd.remote?
68
69    @cmd.options[:domain] = :both
70
71    assert_equal true, @cmd.remote?
72  end
73
74  def test_source_option
75    @cmd.add_source_option
76
77    s1 = URI.parse 'http://more-gems.example.com/'
78    s2 = URI.parse 'http://even-more-gems.example.com/'
79    s3 = URI.parse 'http://other-gems.example.com/some_subdir'
80    s4 = URI.parse 'http://more-gems.example.com/' # Intentional duplicate
81
82    original_sources = Gem.sources.dup
83
84    @cmd.handle_options %W[--source #{s1} --source #{s2} --source #{s3} --source #{s4}]
85
86    original_sources << s1.to_s
87    original_sources << s2.to_s
88    original_sources << "#{s3}/"
89
90    assert_equal original_sources, Gem.sources
91  end
92
93  def test_update_sources_option
94    @cmd.add_update_sources_option
95
96    Gem.configuration.update_sources = false
97
98    @cmd.handle_options %W[--update-sources]
99
100    assert_equal true, Gem.configuration.update_sources
101
102    @cmd.handle_options %W[--no-update-sources]
103
104    assert_equal false, Gem.configuration.update_sources
105  end
106
107  def test_source_option_bad
108    @cmd.add_source_option
109
110    s1 = 'htp://more-gems.example.com'
111
112    assert_raises OptionParser::InvalidArgument do
113      @cmd.handle_options %W[--source #{s1}]
114    end
115
116    assert_equal [@gem_repo], Gem.sources
117  end
118
119end
120
121