summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_doctor.rb
blob: 1cef52234e8dd96f6ad099e36aace378ac13dab8 (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
# frozen_string_literal: true
require 'rubygems/test_case'
require 'rubygems/doctor'

class TestGemDoctor < Gem::TestCase
  def gem(name)
    spec = quick_gem name 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])

    spec
  end

  def test_doctor
    a = gem 'a'
    b = gem 'b'
    c = gem 'c'

    Gem.use_paths @userhome, @gemhome

    FileUtils.rm b.spec_file

    File.open c.spec_file, 'w' do |io|
      io.write 'this will raise an exception when evaluated.'
    end

    assert_path_exists File.join(a.gem_dir, 'Rakefile')
    assert_path_exists File.join(a.gem_dir, 'lib', 'a.rb')

    assert_path_exists b.gem_dir
    refute_path_exists b.spec_file

    assert_path_exists c.gem_dir
    assert_path_exists c.spec_file

    doctor = Gem::Doctor.new @gemhome

    capture_io do
      use_ui @ui do
        doctor.doctor
      end
    end

    assert_path_exists File.join(a.gem_dir, 'Rakefile')
    assert_path_exists File.join(a.gem_dir, 'lib', 'a.rb')

    refute_path_exists b.gem_dir
    refute_path_exists b.spec_file

    refute_path_exists c.gem_dir
    refute_path_exists c.spec_file

    expected = <<-OUTPUT
Checking #{@gemhome}
Removed file specifications/c-2.gemspec
Removed directory gems/b-2
Removed directory gems/c-2

    OUTPUT

    assert_equal expected, @ui.output

    assert_equal Gem.dir,  @userhome
    assert_equal Gem.path, [@gemhome, @userhome]
  end

  def test_doctor_dry_run
    a = gem 'a'
    b = gem 'b'
    c = gem 'c'

    Gem.use_paths @userhome, @gemhome

    FileUtils.rm b.spec_file

    File.open c.spec_file, 'w' do |io|
      io.write 'this will raise an exception when evaluated.'
    end

    assert_path_exists File.join(a.gem_dir, 'Rakefile')
    assert_path_exists File.join(a.gem_dir, 'lib', 'a.rb')

    assert_path_exists b.gem_dir
    refute_path_exists b.spec_file

    assert_path_exists c.gem_dir
    assert_path_exists c.spec_file

    doctor = Gem::Doctor.new @gemhome, true

    capture_io do
      use_ui @ui do
        doctor.doctor
      end
    end

    assert_path_exists File.join(a.gem_dir, 'Rakefile')
    assert_path_exists File.join(a.gem_dir, 'lib', 'a.rb')

    assert_path_exists b.gem_dir
    refute_path_exists b.spec_file

    assert_path_exists c.gem_dir
    assert_path_exists c.spec_file

    expected = <<-OUTPUT
Checking #{@gemhome}
Extra file specifications/c-2.gemspec
Extra directory gems/b-2
Extra directory gems/c-2

    OUTPUT

    assert_equal expected, @ui.output

    assert_equal Gem.dir,  @userhome
    assert_equal Gem.path, [@gemhome, @userhome]
  end

  def test_doctor_non_gem_home
    other_dir = File.join @tempdir, 'other', 'dir'

    FileUtils.mkdir_p other_dir

    doctor = Gem::Doctor.new @tempdir

    capture_io do
      use_ui @ui do
        doctor.doctor
      end
    end

    assert_path_exists other_dir

    expected = <<-OUTPUT
Checking #{@tempdir}
This directory does not appear to be a RubyGems repository, skipping

    OUTPUT

    assert_equal expected, @ui.output
  end

  def test_doctor_child_missing
    doctor = Gem::Doctor.new @gemhome

    doctor.doctor_child 'missing', ''

    assert true # count
  end

  def test_doctor_badly_named_plugins
    gem 'a'

    Gem.use_paths @gemhome.to_s

    FileUtils.mkdir_p Gem.plugindir
    bad_plugin = File.join(Gem.plugindir, "a_badly_named_file.rb")
    write_file bad_plugin

    doctor = Gem::Doctor.new @gemhome

    capture_io do
      use_ui @ui do
        doctor.doctor
      end
    end

    # refute_path_exists bad_plugin

    expected = <<-OUTPUT
Checking #{@gemhome}
Removed file plugins/a_badly_named_file.rb

    OUTPUT

    assert_equal expected, @ui.output
  end

  def test_gem_repository_eh
    doctor = Gem::Doctor.new @gemhome

    refute doctor.gem_repository?, 'no gems installed'

    install_specs util_spec 'a'

    doctor = Gem::Doctor.new @gemhome

    assert doctor.gem_repository?, 'gems installed'
  end
end