1require 'rubygems/test_case'
2require 'rubygems/doctor'
3
4class TestGemDoctor < Gem::TestCase
5
6  def gem name
7    spec = quick_gem name do |gem|
8      gem.files = %W[lib/#{name}.rb Rakefile]
9    end
10
11    write_file File.join(*%W[gems #{spec.full_name} lib #{name}.rb])
12    write_file File.join(*%W[gems #{spec.full_name} Rakefile])
13
14    spec
15  end
16
17  def test_doctor
18    a = gem 'a'
19    b = gem 'b'
20    c = gem 'c'
21
22    Gem.use_paths @userhome, @gemhome
23
24    FileUtils.rm b.spec_file
25
26    open c.spec_file, 'w' do |io|
27      io.write 'this will raise an exception when evaluated.'
28    end
29
30    assert_path_exists File.join(a.gem_dir, 'Rakefile')
31    assert_path_exists File.join(a.gem_dir, 'lib', 'a.rb')
32
33    assert_path_exists b.gem_dir
34    refute_path_exists b.spec_file
35
36    assert_path_exists c.gem_dir
37    assert_path_exists c.spec_file
38
39    doctor = Gem::Doctor.new @gemhome
40
41    capture_io do
42      use_ui @ui do
43        doctor.doctor
44      end
45    end
46
47    assert_path_exists File.join(a.gem_dir, 'Rakefile')
48    assert_path_exists File.join(a.gem_dir, 'lib', 'a.rb')
49
50    refute_path_exists b.gem_dir
51    refute_path_exists b.spec_file
52
53    refute_path_exists c.gem_dir
54    refute_path_exists c.spec_file
55
56    expected = <<-OUTPUT
57Checking #{@gemhome}
58Removed file specifications/c-2.gemspec
59Removed directory gems/b-2
60Removed directory gems/c-2
61
62    OUTPUT
63
64    assert_equal expected, @ui.output
65
66    assert_equal Gem.dir,  @userhome
67    assert_equal Gem.path, [@gemhome, @userhome]
68  end
69
70  def test_doctor_dry_run
71    a = gem 'a'
72    b = gem 'b'
73    c = gem 'c'
74
75    Gem.use_paths @userhome, @gemhome
76
77    FileUtils.rm b.spec_file
78
79    open c.spec_file, 'w' do |io|
80      io.write 'this will raise an exception when evaluated.'
81    end
82
83    assert_path_exists File.join(a.gem_dir, 'Rakefile')
84    assert_path_exists File.join(a.gem_dir, 'lib', 'a.rb')
85
86    assert_path_exists b.gem_dir
87    refute_path_exists b.spec_file
88
89    assert_path_exists c.gem_dir
90    assert_path_exists c.spec_file
91
92    doctor = Gem::Doctor.new @gemhome, true
93
94    capture_io do
95      use_ui @ui do
96        doctor.doctor
97      end
98    end
99
100    assert_path_exists File.join(a.gem_dir, 'Rakefile')
101    assert_path_exists File.join(a.gem_dir, 'lib', 'a.rb')
102
103    assert_path_exists b.gem_dir
104    refute_path_exists b.spec_file
105
106    assert_path_exists c.gem_dir
107    assert_path_exists c.spec_file
108
109    expected = <<-OUTPUT
110Checking #{@gemhome}
111Extra file specifications/c-2.gemspec
112Extra directory gems/b-2
113Extra directory gems/c-2
114
115    OUTPUT
116
117    assert_equal expected, @ui.output
118
119    assert_equal Gem.dir,  @userhome
120    assert_equal Gem.path, [@gemhome, @userhome]
121  end
122
123  def test_doctor_non_gem_home
124    other_dir = File.join @tempdir, 'other', 'dir'
125
126    FileUtils.mkdir_p other_dir
127
128    doctor = Gem::Doctor.new @tempdir
129
130    capture_io do
131      use_ui @ui do
132        doctor.doctor
133      end
134    end
135
136    assert_path_exists other_dir
137
138    expected = <<-OUTPUT
139Checking #{@tempdir}
140This directory does not appear to be a RubyGems repository, skipping
141
142    OUTPUT
143
144    assert_equal expected, @ui.output
145  end
146
147  def test_doctor_child_missing
148    doctor = Gem::Doctor.new @gemhome
149
150    doctor.doctor_child 'missing', ''
151
152    assert true # count
153  end
154
155  def test_gem_repository_eh
156    doctor = Gem::Doctor.new @gemhome
157
158    refute doctor.gem_repository?, 'no gems installed'
159
160    quick_spec 'a'
161
162    doctor = Gem::Doctor.new @gemhome
163
164    assert doctor.gem_repository?, 'gems installed'
165  end
166
167end
168
169