Geminstaller C0 Coverage Information - RCov

spec/fixture/rubygems_dist/rubygems-trunk/lib/rubygems/uninstaller.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
spec/fixture/rubygems_dist/rubygems-trunk/lib/rubygems/uninstaller.rb 262 156
65.27%
50.00%

Key

Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.

Coverage Details

1 #--
2 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
3 # All rights reserved.
4 # See LICENSE.txt for permissions.
5 #++
6 
7 require 'fileutils'
8 require 'rubygems'
9 require 'rubygems/dependency_list'
10 require 'rubygems/doc_manager'
11 require 'rubygems/user_interaction'
12 
13 ##
14 # An Uninstaller.
15 #
16 # The uninstaller fires pre and post uninstall hooks.  Hooks can be added
17 # either through a rubygems_plugin.rb file in an installed gem or via a
18 # rubygems/defaults/#{RUBY_ENGINE}.rb or rubygems/defaults/operating_system.rb
19 # file.  See Gem.pre_uninstall and Gem.post_uninstall for details.
20 
21 class Gem::Uninstaller
22 
23   include Gem::UserInteraction
24 
25   ##
26   # The directory a gem's executables will be installed into
27 
28   attr_reader :bin_dir
29 
30   ##
31   # The gem repository the gem will be installed into
32 
33   attr_reader :gem_home
34 
35   ##
36   # The Gem::Specification for the gem being uninstalled, only set during
37   # #uninstall_gem
38 
39   attr_reader :spec
40 
41   ##
42   # Constructs an uninstaller that will uninstall +gem+
43 
44   def initialize(gem, options = {})
45     @gem = gem
46     @version = options[:version] || Gem::Requirement.default
47     gem_home = options[:install_dir] || Gem.dir
48     @gem_home = File.expand_path gem_home
49     @force_executables = options[:executables]
50     @force_all = options[:all]
51     @force_ignore = options[:ignore]
52     @bin_dir = options[:bin_dir]
53 
54     # only add user directory if install_dir is not set
55     @user_install = false
56     @user_install = options[:user_install] unless options[:install_dir]
57 
58     spec_dir = File.join @gem_home, 'specifications'
59     @source_index = Gem::SourceIndex.from_gems_in spec_dir
60 
61     if @user_install then
62       user_dir = File.join Gem.user_dir, 'specifications'
63       @user_index = Gem::SourceIndex.from_gems_in user_dir
64     end
65   end
66 
67   ##
68   # Performs the uninstall of the gem.  This removes the spec, the Gem
69   # directory, and the cached .gem file.
70 
71   def uninstall
72     list = @source_index.find_name @gem, @version
73     list += @user_index.find_name @gem, @version if @user_install
74 
75     if list.empty? then
76       raise Gem::InstallError, "cannot uninstall, check `gem list -d #{@gem}`"
77 
78     elsif list.size > 1 and @force_all then
79       remove_all list.dup
80 
81     elsif list.size > 1 then
82       gem_names = list.collect {|gem| gem.full_name} + ["All versions"]
83 
84       say
85       gem_name, index = choose_from_list "Select gem to uninstall:", gem_names
86 
87       if index == list.size then
88         remove_all list.dup
89       elsif index >= 0 && index < list.size then
90         uninstall_gem list[index], list.dup
91       else
92         say "Error: must enter a number [1-#{list.size+1}]"
93       end
94     else
95       uninstall_gem list.first, list.dup
96     end
97   end
98 
99   ##
100   # Uninstalls gem +spec+
101 
102   def uninstall_gem(spec, specs)
103     @spec = spec
104 
105     Gem.pre_uninstall_hooks.each do |hook|
106       hook.call self
107     end
108 
109     remove_executables @spec
110     remove @spec, specs
111 
112     Gem.post_uninstall_hooks.each do |hook|
113       hook.call self
114     end
115 
116     @spec = nil
117   end
118 
119   ##
120   # Removes installed executables and batch files (windows only) for
121   # +gemspec+.
122 
123   def remove_executables(spec)
124     return if spec.nil?
125 
126     unless spec.executables.empty? then
127       bindir = @bin_dir ? @bin_dir : Gem.bindir(spec.installation_path)
128 
129       list = @source_index.find_name(spec.name).delete_if { |s|
130         s.version == spec.version
131       }
132 
133       executables = spec.executables.clone
134 
135       list.each do |s|
136         s.executables.each do |exe_name|
137           executables.delete exe_name
138         end
139       end
140 
141       return if executables.empty?
142 
143       answer = if @force_executables.nil? then
144                  ask_yes_no("Remove executables:\n" \
145                             "\t#{spec.executables.join(", ")}\n\nin addition to the gem?",
146                             true) # " # appease ruby-mode - don't ask
147                else
148                  @force_executables
149                end
150 
151       unless answer then
152         say "Executables and scripts will remain installed."
153       else
154         raise Gem::FilePermissionError, bindir unless File.writable? bindir
155 
156         spec.executables.each do |exe_name|
157           say "Removing #{exe_name}"
158           FileUtils.rm_f File.join(bindir, exe_name)
159           FileUtils.rm_f File.join(bindir, "#{exe_name}.bat")
160         end
161       end
162     end
163   end
164 
165   ##
166   # Removes all gems in +list+.
167   #
168   # NOTE: removes uninstalled gems from +list+.
169 
170   def remove_all(list)
171     list.dup.each { |spec| uninstall_gem spec, list }
172   end
173 
174   ##
175   # spec:: the spec of the gem to be uninstalled
176   # list:: the list of all such gems
177   #
178   # Warning: this method modifies the +list+ parameter.  Once it has
179   # uninstalled a gem, it is removed from that list.
180 
181   def remove(spec, list)
182     unless dependencies_ok? spec then
183       raise Gem::DependencyRemovalException,
184             "Uninstallation aborted due to dependent gem(s)"
185     end
186 
187     unless path_ok?(@gem_home, spec) or
188            (@user_install and path_ok?(Gem.user_dir, spec)) then
189       e = Gem::GemNotInHomeException.new \
190             "Gem is not installed in directory #{@gem_home}"
191       e.spec = spec
192 
193       raise e
194     end
195 
196     raise Gem::FilePermissionError, spec.installation_path unless
197       File.writable?(spec.installation_path)
198 
199     FileUtils.rm_rf spec.full_gem_path
200 
201     original_platform_name = [
202       spec.name, spec.version, spec.original_platform].join '-'
203 
204     spec_dir = File.join spec.installation_path, 'specifications'
205     gemspec = File.join spec_dir, spec.spec_name
206 
207     unless File.exist? gemspec then
208       gemspec = File.join spec_dir, "#{original_platform_name}.gemspec"
209     end
210 
211     FileUtils.rm_rf gemspec
212 
213     cache_dir = File.join spec.installation_path, 'cache'
214     gem = File.join cache_dir, spec.file_name
215 
216     unless File.exist? gem then
217       gem = File.join cache_dir, "#{original_platform_name}.gem"
218     end
219 
220     FileUtils.rm_rf gem
221 
222     Gem::DocManager.new(spec).uninstall_doc
223 
224     say "Successfully uninstalled #{spec.full_name}"
225 
226     list.delete spec
227   end
228 
229   ##
230   # Is +spec+ in +gem_dir+?
231 
232   def path_ok?(gem_dir, spec)
233     full_path = File.join gem_dir, 'gems', spec.full_name
234     original_path = File.join gem_dir, 'gems', spec.original_name
235 
236     full_path == spec.full_gem_path || original_path == spec.full_gem_path
237   end
238 
239   def dependencies_ok?(spec)
240     return true if @force_ignore
241 
242     deplist = Gem::DependencyList.from_source_index @source_index
243     deplist.add(*@user_index.gems.values) if @user_install
244     deplist.ok_to_remove?(spec.full_name) || ask_if_ok(spec)
245   end
246 
247   def ask_if_ok(spec)
248     msg = ['']
249     msg << 'You have requested to uninstall the gem:'
250     msg << "\t#{spec.full_name}"
251     spec.dependent_gems.each do |gem,dep,satlist|
252       msg <<
253         ("#{gem.name}-#{gem.version} depends on " +
254         "[#{dep.name} (#{dep.requirement})]")
255     end
256     msg << 'If you remove this gems, one or more dependencies will not be met.'
257     msg << 'Continue with Uninstall?'
258     return ask_yes_no(msg.join("\n"), true)
259   end
260 
261 end
262 

Generated on Mon May 10 23:40:29 -0700 2010 with rcov 0.9.8