Geminstaller C0 Coverage Information - RCov

spec/helper/test_gem_home.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
spec/helper/test_gem_home.rb 252 206
87.70%
85.44%

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 dir = File.dirname(__FILE__)
2 require File.expand_path("#{dir}/override_default_source")
3 require File.expand_path("#{dir}/rubygems_installer")
4 require File.expand_path("#{dir}/spec_utils")
5 require 'fileutils'
6 
7 module GemInstaller
8 
9   class TestGemHome
10     include FileUtils
11     include GemInstaller::SpecUtils
12     @@dirs_initialized = false
13     @@server_started = false
14 
15     def self.initialized?
16       @@dirs_initialized and @@server_started
17     end
18 
19     def self.use
20       install_rubygems
21       start_server
22       Gem.class_eval { @platforms = nil } if GemInstaller::RubyGemsVersionChecker.matches?(['>=0.9.5', '<=1.1.0'])
23       Gem.platforms.clear if GemInstaller::RubyGemsVersionChecker.matches?('>=1.1.0')
24     end
25     
26     def self.reset
27       stop_server
28       uninstall_rubygems
29     end
30     
31     def self.install_rubygems
32       return if @@dirs_initialized
33       rm_dirs
34       create_dirs
35       put_rubygems_on_load_path
36       init_gem_env_vars
37       require geminstaller_lib_dir + "/geminstaller/rubygems_version_checker"
38       install_sources if GemInstaller::RubyGemsVersionChecker.matches?('<= 0.9.4')
39       GemInstaller::OverrideDefaultSource.override_default_gem_source
40       Gem::ConfigFile.class_eval do
41         # Do not let the users config file be read
42         def config_file_name
43           GemInstaller::TestGemHome.test_rubygems_config_file
44         end
45       end
46       @@dirs_initialized = true
47     end
48 
49     def self.uninstall_rubygems
50       Gem.clear_paths
51       rm_dirs
52       @@dirs_initialized = false
53     end
54 
55     def self.start_server
56       return if @@server_started
57       rm_config
58       create_config
59       GemInstaller::EmbeddedGemServer.start
60       cmd = "#{gem_cmd} update --config-file #{config_file}"
61       puts "Updating from embedded gem server, cmd = #{cmd}"
62       output = `#{cmd}`
63       raise "Failure updating from embedded gem server, cmd = #{cmd}" unless $?.success?
64       @@server_started = true
65     end
66     
67     def self.stop_server
68       $server_was_stopped = GemInstaller::EmbeddedGemServer.stop unless $server_was_stopped
69       rm_config
70       @@server_started = false
71     end
72 
73     def self.uninstall_all_test_gems
74       if GemInstaller::RubyGemsVersionChecker.matches?('>=0.9.5')
75         rm_test_gem_home_dir
76         require 'rubygems/source_info_cache'
77         Gem::SourceInfoCache.reset if GemInstaller::RubyGemsVersionChecker.matches?('>1.0.1')
78       else
79         uninstall_all_test_gems_via_rubygems
80       end
81     end
82     
83     def self.uninstall_all_test_gems_via_rubygems
84       test_gems.each do |test_gem|
85         test_gem_name = test_gem.name
86         test_gem_platform = test_gem.platform
87         test_gem_platform = 'x86-mswin32' if test_gem_platform == 'mswin32'
88         list_output = `#{gem_cmd} list #{test_gem_name}`
89         next unless list_output =~ /#{test_gem_name} /
90         if GemInstaller::RubyGemsVersionChecker.matches?('>=0.9.5')
91           platform_option = "--platform #{test_gem_platform}"
92         end
93         
94         uninstall_command = "#{gem_cmd} uninstall #{test_gem_name} --all --ignore-dependencies --executables #{platform_option} --config-file #{config_file}"
95         `#{uninstall_command}`
96       end
97     end
98 
99     def self.put_rubygems_on_load_path
100       remove_rubygems_from_require_array
101       remove_rubygems_constants
102       $LOAD_PATH.unshift(siterubyver_dir)
103       $LOAD_PATH.unshift(rubygems_lib_dir)
104       require 'rubygems'
105       hack_in_correct_rubygems_version_because_rubygems_refuses_to_allow_it_to_be_changed_in_trunk
106       print "\nReset RubyGems version, ENV['RUBYGEMS_VERSION'] == '#{ENV['RUBYGEMS_VERSION']}', Gem::RubyGemsVersion == '#{Gem::RubyGemsVersion}'\n"
107     end
108     
109     protected
110 
111     def self.remove_rubygems_from_require_array
112       require_array_copy = $".dup
113       require_array_copy.each do |require_array_entry|
114         $".delete(require_array_entry) if require_array_entry =~ /rubygems/
115       end
116       # point "require" back at the standard Ruby "require" method
117       # if RubyGems has already aliased gem_original_require to the original require...
118       if Kernel.private_method_defined?(:gem_original_require)
119         # get an unbound method reference to the original require method
120         unbound_gem_original_require = Kernel.send(:instance_method, :gem_original_require)
121         # then remove the overridden RubyGems require
122         Kernel.send(:remove_method, :require)
123         # and define require to invoke the original require method 
124         Kernel.send(:define_method, :require) {|path| unbound_gem_original_require.bind(Kernel).call(path) }
125       end
126     end
127 
128     def self.remove_rubygems_constants
129       return unless defined? Gem
130       # TODO: ignore undefined class name constants so it works under older rubygems versions (e.g. Gem::Command)
131       constants_to_remove = [
132         {:class => Gem, :name => :RubyGemsVersion},
133         {:class => Gem, :name => :VERSION},
134         {:class => Gem, :name => :MUTEX},
135         {:class => Gem, :name => :RubyGemsPackageVersion},
136         {:class => Gem, :name => :WIN_PATTERNS},
137         {:class => Gem, :name => :MARSHAL_SPEC_DIR},
138         {:class => Gem, :name => :YAML_SPEC_DIR},
139         {:class => Gem::Version, :name => :VERSION_PATTERN},
140         {:class => Gem, :name => :Requirement},
141         {:class => Gem::Dependency, :name => :TYPES},
142         {:class => Gem::Platform, :name => :RUBY},
143         {:class => Gem::Platform, :name => :CURRENT},
144         {:class => Gem::Specification, :name => :NONEXISTENT_SPECIFICATION_VERSION},
145         {:class => Gem::Specification, :name => :CURRENT_SPECIFICATION_VERSION},
146         {:class => Gem::Specification, :name => :SPECIFICATION_VERSION_HISTORY},
147         {:class => Gem::Specification, :name => :MARSHAL_FIELDS},
148         {:class => Gem::Specification, :name => :TODAY},
149         {:class => Gem, :name => :Cache},
150         {:class => Gem::ConfigFile, :name => :DEFAULT_BACKTRACE},
151         {:class => Gem::ConfigFile, :name => :DEFAULT_BENCHMARK},
152         {:class => Gem::ConfigFile, :name => :DEFAULT_BULK_THRESHOLD},
153         {:class => Gem::ConfigFile, :name => :DEFAULT_VERBOSITY},
154         {:class => Gem::ConfigFile, :name => :DEFAULT_UPDATE_SOURCES},
155         {:class => Gem::ConfigFile, :name => :OPERATING_SYSTEM_DEFAULTS},
156         {:class => Gem::ConfigFile, :name => :PLATFORM_DEFAULTS},
157         {:class => Gem::ConfigFile, :name => :SYSTEM_WIDE_CONFIG_FILE}
158       ]
159       constants_to_remove << {:class => Gem::Command, :name => :HELP} if defined? Gem::Command
160       
161       # These things went away in Rubygems >1.3.5
162       constants_to_remove << {:class => Gem::Requirement, :name => :OP_RE}
163       constants_to_remove << {:class => Gem::Version, :name => :Requirement}
164 
165       # This one can't be removed because it is used in requirement.rb and 'things break'
166       # constants_to_remove << {:class => Gem::Requirement, :name => :OPS}
167       
168       constants_to_remove.each do |constant|
169         begin
170           constant[:class].send(:remove_const,constant[:name])
171         rescue NameError => e
172           puts "Error removing constant from installed RubyGems: #{e.message}"
173         end
174       end
175     end
176 
177     def self.hack_in_correct_rubygems_version_because_rubygems_refuses_to_allow_it_to_be_changed_in_trunk
178       # http://rubyforge.org/pipermail/rubygems-developers/2009-June/004749.html
179       # http://rubyforge.org/pipermail/rubygems-developers/2009-June/004750.html
180       return unless rubygems_dist == 'trunk'
181       correct_version = Gem::RubyGemsVersion + '.1'
182       Gem.send(:remove_const,:RubyGemsVersion)
183       Gem.send(:remove_const,:VERSION)
184       Gem.const_set(:RubyGemsVersion, correct_version)
185       Gem.const_set(:VERSION, correct_version)
186     end
187 
188     def self.init_gem_env_vars
189       ENV['GEM_HOME'] = test_gem_home_dir
190       # Copied from rubygems.rb path method
191       paths = [ENV['GEM_PATH']]
192       
193       # Copied from defaults.rb default_dir method,
194       # we need to ensure that /Library/Ruby/Gems is on the GEM_PATH in Leopard
195       require geminstaller_lib_dir + "/geminstaller/rubygems_version_checker"
196       if GemInstaller::RubyGemsVersionChecker.matches?('<=0.9.5') 
197         if defined? RUBY_FRAMEWORK_VERSION
198           paths << File.join(File.dirname(Config::CONFIG["sitedir"]), "Gems", Config::CONFIG['ruby_version'])
199         else
200           paths << File.join(Config::CONFIG['libdir'], 'ruby', 'gems', Config::CONFIG['ruby_version'])
201         end
202       else
203         if defined? RUBY_FRAMEWORK_VERSION
204           paths << File.join(File.dirname(Gem::ConfigMap[:sitedir]), 'Gems', Gem::ConfigMap[:ruby_version])
205         else
206           paths << File.join(Gem::ConfigMap[:libdir], 'ruby', 'gems', Gem::ConfigMap[:ruby_version])
207         end
208       end
209 
210       paths << APPLE_GEM_HOME if defined? APPLE_GEM_HOME
211       
212       ENV['GEM_PATH'] = paths.compact.join(File::PATH_SEPARATOR)
213     end
214 
215     def self.rm_dirs
216       rm_test_gem_home_dir
217     end
218     
219     def self.rm_test_gem_home_dir
220       FileUtils.rm_rf(test_gem_home_dir) if File.exist?(test_gem_home_dir)
221     end
222     
223     def self.create_dirs
224       FileUtils.mkdir(test_gem_home_dir) unless File.exist?(test_gem_home_dir)
225       FileUtils.mkdir(siteruby_dir) unless File.exist?(siteruby_dir)
226       FileUtils.mkdir(siterubyver_dir) unless File.exist?(siterubyver_dir)
227     end
228 
229     def self.install_sources
230       @rubygems_installer = GemInstaller::RubyGemsInstaller.new
231       @rubygems_installer.gem_home_dir = test_gem_home_dir
232       @rubygems_installer.rubygems_dist_dir = rubygems_dist_dir
233       @rubygems_installer.install_sources
234     end
235     
236     def self.config_file
237       GemInstaller::TestGemHome.test_rubygems_config_file
238     end
239 
240     def self.rm_config
241       FileUtils.rm(config_file) if File.exist?(config_file)
242     end
243     
244     def self.create_config
245       file = File.open(config_file, "w") do |f| 
246         f << "gemhome: #{test_gem_home_dir}\n"
247         f << "gempath:\n"
248         f << "  - #{test_gem_home_dir}\n"
249       end 
250     end
251   end
252 end

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