summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_commands_contents_command.rb
blob: ae8e49fdc318b789abccc3af12bc377aa162ce97 (plain)
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
require 'rubygems/test_case'
require 'rubygems/commands/contents_command'

class TestGemCommandsContentsCommand < Gem::TestCase

  def setup
    super

    @cmd = Gem::Commands::ContentsCommand.new
  end

  def gem name, version = 2
    spec = quick_gem name, version do |gem|
      gem.files = %W[lib/#{name}.rb Rakefile]
    end
    write_file File.join(*%W[gems #{spec.full_name} lib #{name}.rb])
    write_file File.join(*%W[gems #{spec.full_name} Rakefile])
  end

  def test_execute
    @cmd.options[:args] = %w[foo]

    gem 'foo'

    use_ui @ui do
      @cmd.execute
    end

    assert_match %r|lib/foo\.rb|, @ui.output
    assert_match %r|Rakefile|, @ui.output
    assert_equal "", @ui.error
  end

  def test_execute_all
    @cmd.options[:all] = true

    gem 'foo'
    gem 'bar'

    use_ui @ui do
      @cmd.execute
    end

    assert_match %r|lib/foo\.rb|, @ui.output
    assert_match %r|lib/bar\.rb|, @ui.output
    assert_match %r|Rakefile|, @ui.output
    assert_equal "", @ui.error
  end

  def test_execute_bad_gem
    @cmd.options[:args] = %w[foo]

    assert_raises Gem::MockGemUi::TermError do
      use_ui @ui do
        @cmd.execute
      end
    end

    assert_match %r|Unable to find gem 'foo' in default gem paths|, @ui.output
    assert_match %r|Directories searched:|, @ui.output
    assert_equal "", @ui.error
  end

  def test_execute_exact_match
    @cmd.options[:args] = %w[foo]
    gem 'foo'
    gem 'bar'

    use_ui @ui do
      @cmd.execute
    end

    assert_match %r|lib/foo\.rb|, @ui.output
    assert_match %r|Rakefile|, @ui.output
    assert_equal "", @ui.error
  end

  def test_execute_lib_only
    @cmd.options[:args] = %w[foo]
    @cmd.options[:lib_only] = true

    gem 'foo'

    use_ui @ui do
      @cmd.execute
    end

    assert_match %r|lib/foo\.rb|, @ui.output
    refute_match %r|Rakefile|, @ui.output

    assert_equal "", @ui.error
  end

  def test_execute_missing_single
    @cmd.options[:args] = %w[foo]

    assert_raises Gem::MockGemUi::TermError do
      use_ui @ui do
        @cmd.execute
      end
    end

    assert_match "Unable to find gem 'foo'", @ui.output
    assert_empty @ui.error
  end

  def test_execute_missing_multiple
    @cmd.options[:args] = %w[foo bar]

    gem 'foo'

    use_ui @ui do
      @cmd.execute
    end

    assert_match "lib/foo.rb",               @ui.output
    assert_match "Unable to find gem 'bar'", @ui.output

    assert_empty @ui.error
  end

  def test_execute_multiple
    @cmd.options[:args] = %w[foo bar]

    gem 'foo'
    gem 'bar'

    use_ui @ui do
      @cmd.execute
    end

    assert_match %r|lib/foo\.rb|, @ui.output
    assert_match %r|lib/bar\.rb|, @ui.output
    assert_match %r|Rakefile|, @ui.output
    assert_equal "", @ui.error
  end

  def test_execute_show_install_dir
    @cmd.options[:args] = %w[foo]
    @cmd.options[:show_install_dir] = true

    gem 'foo'

    use_ui @ui do
      @cmd.execute
    end

    expected = File.join @gemhome, 'gems', 'foo-2'

    assert_equal "#{expected}\n", @ui.output
    assert_equal "", @ui.error
  end

  def test_execute_show_install_dir_version
    @cmd.options[:args] = %w[foo]
    @cmd.options[:show_install_dir] = true
    @cmd.options[:version] = Gem::Requirement.new '= 1'

    gem 'foo', 1
    gem 'foo', 2

    use_ui @ui do
      @cmd.execute
    end

    expected = File.join @gemhome, 'gems', 'foo-1'

    assert_equal "#{expected}\n", @ui.output
    assert_equal "", @ui.error
  end

  def test_execute_no_prefix
    @cmd.options[:args] = %w[foo]
    @cmd.options[:prefix] = false

    gem 'foo'

    use_ui @ui do
      @cmd.execute
    end

    expected = <<-EOF
Rakefile
lib/foo.rb
    EOF

    assert_equal expected, @ui.output

    assert_equal "", @ui.error
  end

  def test_execute_default_gem
    default_gem_spec = new_default_spec("default", "2.0.0.0",
                                        nil, "default/gem.rb")
    default_gem_spec.executables = ["default_command"]
    default_gem_spec.files += ["default_gem.so"]
    install_default_specs(default_gem_spec)

    @cmd.options[:args] = %w[default]

    use_ui @ui do
      @cmd.execute
    end

    expected = [
      [RbConfig::CONFIG['bindir'], 'default_command'],
      [RbConfig::CONFIG['rubylibdir'], 'default/gem.rb'],
      [RbConfig::CONFIG['archdir'], 'default_gem.so']
    ].sort.map{|a|File.join a}.join "\n"

    assert_equal expected, @ui.output.chomp
    assert_equal "", @ui.error
  end

  def test_handle_options
    refute @cmd.options[:lib_only]
    assert @cmd.options[:prefix]
    assert_empty @cmd.options[:specdirs]
    assert_nil @cmd.options[:version]
    refute @cmd.options[:show_install_dir]

    @cmd.send :handle_options, %w[
      -l
      -s
      foo
      --version 0.0.2
      --no-prefix
      --show-install-dir
    ]

    assert @cmd.options[:lib_only]
    refute @cmd.options[:prefix]
    assert_equal %w[foo], @cmd.options[:specdirs]
    assert_equal Gem::Requirement.new('0.0.2'), @cmd.options[:version]
    assert @cmd.options[:show_install_dir]
  end

end