1require 'rubygems/test_case'
2require 'rubygems/commands/pristine_command'
3
4class TestGemCommandsPristineCommand < Gem::TestCase
5
6  def setup
7    super
8    @cmd = Gem::Commands::PristineCommand.new
9  end
10
11  def test_execute
12    a = quick_spec 'a' do |s|
13      s.executables = %w[foo]
14      s.files = %w[bin/foo lib/a.rb]
15    end
16
17    write_file File.join(@tempdir, 'lib', 'a.rb') do |fp|
18      fp.puts "puts __FILE__"
19    end
20    write_file File.join(@tempdir, 'bin', 'foo') do |fp|
21      fp.puts "#!/usr/bin/ruby"
22    end
23
24    install_gem a
25
26    foo_path  = File.join @gemhome, 'gems', a.full_name, 'bin', 'foo'
27    a_rb_path = File.join @gemhome, 'gems', a.full_name, 'lib', 'a.rb'
28
29    write_file foo_path do |io|
30      io.puts 'I changed it!'
31    end
32
33    write_file a_rb_path do |io|
34      io.puts 'I changed it!'
35    end
36
37    @cmd.options[:args] = %w[a]
38
39    use_ui @ui do
40      @cmd.execute
41    end
42
43    assert_equal "#!/usr/bin/ruby\n", File.read(foo_path), foo_path
44    assert_equal "puts __FILE__\n", File.read(a_rb_path), a_rb_path
45
46    out = @ui.output.split "\n"
47
48    assert_equal "Restoring gems to pristine condition...", out.shift
49    assert_equal "Restored #{a.full_name}", out.shift
50    assert_empty out, out.inspect
51  end
52
53  def test_execute_all
54    a = quick_spec 'a' do |s| s.executables = %w[foo] end
55    write_file File.join(@tempdir, 'bin', 'foo') do |fp|
56      fp.puts "#!/usr/bin/ruby"
57    end
58
59    install_gem a
60
61    gem_bin  = File.join @gemhome, 'gems', a.full_name, 'bin', 'foo'
62    gem_stub = File.join @gemhome, 'bin', 'foo'
63
64    FileUtils.rm gem_bin
65    FileUtils.rm gem_stub
66
67    @cmd.handle_options %w[--all]
68
69    use_ui @ui do
70      @cmd.execute
71    end
72
73    assert File.exist?(gem_bin)
74    assert File.exist?(gem_stub)
75
76    out = @ui.output.split "\n"
77
78    assert_equal "Restoring gems to pristine condition...", out.shift
79    assert_equal "Restored #{a.full_name}", out.shift
80    assert_empty out, out.inspect
81  end
82
83  def test_execute_no_extension
84    a = quick_spec 'a' do |s| s.extensions << 'ext/a/extconf.rb' end
85
86    ext_path = File.join @tempdir, 'ext', 'a', 'extconf.rb'
87    write_file ext_path do |io|
88      io.write '# extconf.rb'
89    end
90
91    util_build_gem a
92
93    @cmd.options[:args] = %w[a]
94    @cmd.options[:extensions] = false
95
96    use_ui @ui do
97      @cmd.execute
98    end
99
100    out = @ui.output.split "\n"
101
102    assert_equal 'Restoring gems to pristine condition...', out.shift
103    assert_equal "Skipped #{a.full_name}, it needs to compile an extension",
104                 out.shift
105    assert_empty out, out.inspect
106  end
107
108  def test_execute_with_extension_with_build_args
109    a = quick_spec 'a' do |s| s.extensions << 'ext/a/extconf.rb' end
110
111    ext_path = File.join @tempdir, 'ext', 'a', 'extconf.rb'
112    write_file ext_path do |io|
113      io.write <<-'RUBY'
114      File.open "Makefile", "w" do |f|
115        f.puts "all:\n\techo built\n"
116        f.puts "install:\n\techo built\n"
117      end
118      RUBY
119    end
120
121    build_args = %w!--with-awesome=true --sweet!
122
123    install_gem a, :build_args => build_args
124
125    @cmd.options[:args] = %w[a]
126
127    use_ui @ui do
128      @cmd.execute
129    end
130
131    out = @ui.output.split "\n"
132
133    assert_equal 'Restoring gems to pristine condition...', out.shift
134    assert_equal "Building native extensions with: '--with-awesome=true --sweet'", out.shift
135    assert_equal "This could take a while...", out.shift
136    assert_equal "Restored #{a.full_name}", out.shift
137    assert_empty out, out.inspect
138  end
139
140  def test_execute_many
141    a = quick_spec 'a'
142    b = quick_spec 'b'
143
144    install_gem a
145    install_gem b
146
147    @cmd.options[:args] = %w[a b]
148
149    use_ui @ui do
150      @cmd.execute
151    end
152
153    out = @ui.output.split "\n"
154
155    assert_equal "Restoring gems to pristine condition...", out.shift
156    assert_equal "Restored #{a.full_name}", out.shift
157    assert_equal "Restored #{b.full_name}", out.shift
158    assert_empty out, out.inspect
159  end
160
161  def test_execute_many_multi_repo
162    a = quick_spec 'a'
163    install_gem a
164
165    Gem.clear_paths
166    gemhome2 = File.join @tempdir, 'gemhome2'
167    Gem.paths = { "GEM_PATH" => [gemhome2, @gemhome], "GEM_HOME" => gemhome2 }
168
169    b = quick_spec 'b'
170    install_gem b
171
172    @cmd.options[:args] = %w[a b]
173
174    use_ui @ui do
175      @cmd.execute
176    end
177
178    out = @ui.output.split "\n"
179
180    assert_equal "Restoring gems to pristine condition...", out.shift
181    assert_equal "Restored #{a.full_name}", out.shift
182    assert_equal "Restored #{b.full_name}", out.shift
183    assert_empty out, out.inspect
184
185    assert_path_exists File.join(@gemhome, "gems", 'a-2')
186    refute_path_exists File.join(gemhome2, "gems", 'a-2')
187    assert_path_exists File.join(gemhome2, "gems", 'b-2')
188    refute_path_exists File.join(@gemhome, "gems", 'b-2')
189  end
190
191  def test_execute_missing_cache_gem
192    a_2 = quick_spec 'a', 2
193    a_3 = quick_spec 'a', 3
194
195    install_gem a_2
196    install_gem a_3
197
198    a_2_data = nil
199    open File.join(@gemhome, 'cache', a_2.file_name), 'rb' do |fp|
200      a_2_data = fp.read
201    end
202
203    util_setup_fake_fetcher
204    util_setup_spec_fetcher a_2
205
206    url = "http://gems.example.com/gems/#{a_2.file_name}"
207    Gem::RemoteFetcher.fetcher.data[url] = a_2_data
208
209    FileUtils.rm a_2.cache_file
210
211    @cmd.options[:args] = %w[a]
212
213    use_ui @ui do
214      @cmd.execute
215    end
216
217    out = @ui.output.split "\n"
218
219    [
220      "Restoring gems to pristine condition...",
221      "Restored a-1",
222      "Cached gem for a-2 not found, attempting to fetch...",
223      "Restored a-2",
224      "Restored a-3.a",
225      "Restored a-3",
226    ].each do |line|
227      assert_equal line, out.shift
228    end
229
230    assert_empty out, out.inspect
231  end
232
233  def test_execute_no_gem
234    @cmd.options[:args] = %w[]
235
236    e = assert_raises Gem::CommandLineError do
237      use_ui @ui do
238        @cmd.execute
239      end
240    end
241
242    assert_match %r|at least one gem name|, e.message
243  end
244
245  def test_execute_only_executables
246    a = quick_spec 'a' do |s|
247      s.executables = %w[foo]
248      s.files = %w[bin/foo lib/a.rb]
249    end
250    write_file File.join(@tempdir, 'lib', 'a.rb') do |fp|
251      fp.puts "puts __FILE__"
252    end
253    write_file File.join(@tempdir, 'bin', 'foo') do |fp|
254      fp.puts "#!/usr/bin/ruby"
255    end
256
257    install_gem a
258
259    gem_lib  = File.join @gemhome, 'gems', a.full_name, 'lib', 'a.rb'
260    gem_exec = File.join @gemhome, 'bin', 'foo'
261
262    FileUtils.rm gem_exec
263    FileUtils.rm gem_lib
264
265    @cmd.handle_options %w[--all --only-executables]
266
267    use_ui @ui do
268      @cmd.execute
269    end
270
271    assert File.exist? gem_exec
272    refute File.exist? gem_lib
273  end
274
275  def test_execute_default_gem
276    default_gem_spec = new_default_spec("default", "2.0.0.0",
277                                        nil, "default/gem.rb")
278    install_default_specs(default_gem_spec)
279
280    @cmd.options[:args] = %w[default]
281
282    use_ui @ui do
283      @cmd.execute
284    end
285
286    assert_equal([
287                   "Restoring gems to pristine condition...",
288                   "Skipped default-2.0.0.0, it is a default gem",
289                 ],
290                 @ui.output.split("\n"))
291    assert_empty(@ui.error)
292  end
293end
294
295