Geminstaller C0 Coverage Information - RCov

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

Name Total Lines Lines of Code Total Coverage Code Coverage
spec/fixture/rubygems_dist/rubygems-trunk/lib/rubygems/validator.rb 240 156
31.67%
16.03%

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 'find'
8 
9 require 'digest'
10 require 'rubygems/format'
11 require 'rubygems/installer'
12 
13 begin
14   gem 'test-unit'
15 rescue Gem::LoadError
16   # Ignore - use the test-unit library that's part of the standard library
17 end
18 
19 ##
20 # Validator performs various gem file and gem database validation
21 
22 class Gem::Validator
23 
24   include Gem::UserInteraction
25 
26   ##
27   # Given a gem file's contents, validates against its own MD5 checksum
28   # gem_data:: [String] Contents of the gem file
29 
30   def verify_gem(gem_data)
31     raise Gem::VerificationError, 'empty gem file' if gem_data.size == 0
32 
33     unless gem_data =~ /MD5SUM/ then
34       return # Don't worry about it...this sucks.  Need to fix MD5 stuff for
35       # new format
36       # FIXME
37     end
38 
39     sum_data = gem_data.gsub(/MD5SUM = "([a-z0-9]+)"/,
40                              "MD5SUM = \"#{"F" * 32}\"")
41 
42     unless Digest::MD5.hexdigest(sum_data) == $1.to_s then
43       raise Gem::VerificationError, 'invalid checksum for gem file'
44     end
45   end
46 
47   ##
48   # Given the path to a gem file, validates against its own MD5 checksum
49   #
50   # gem_path:: [String] Path to gem file
51 
52   def verify_gem_file(gem_path)
53     open gem_path, Gem.binary_mode do |file|
54       gem_data = file.read
55       verify_gem gem_data
56     end
57   rescue Errno::ENOENT, Errno::EINVAL
58     raise Gem::VerificationError, "missing gem file #{gem_path}"
59   end
60 
61   private
62 
63   def find_files_for_gem(gem_directory)
64     installed_files = []
65     Find.find gem_directory do |file_name|
66       fn = file_name[gem_directory.size..file_name.size-1].sub(/^\//, "")
67       installed_files << fn unless
68         fn =~ /CVS/ || fn.empty? || File.directory?(file_name)
69     end
70     installed_files
71   end
72 
73   public
74 
75   ErrorData = Struct.new :path, :problem
76 
77   ##
78   # Checks the gem directory for the following potential
79   # inconsistencies/problems:
80   #
81   # * Checksum gem itself
82   # * For each file in each gem, check consistency of installed versions
83   # * Check for files that aren't part of the gem but are in the gems directory
84   # * 1 cache - 1 spec - 1 directory.
85   #
86   # returns a hash of ErrorData objects, keyed on the problem gem's name.
87 
88   def alien(gems=[])
89     errors = Hash.new { |h,k| h[k] = {} }
90 
91     Gem::SourceIndex.from_installed_gems.each do |gem_name, gem_spec|
92       next unless gems.include? gem_spec.name unless gems.empty?
93 
94       install_dir = gem_spec.installation_path
95       gem_path = File.join install_dir, "cache", gem_spec.file_name
96       spec_path = File.join install_dir, "specifications", gem_spec.spec_name
97       gem_directory = gem_spec.full_gem_path
98 
99       unless File.directory? gem_directory then
100         errors[gem_name][gem_spec.full_name] =
101           "Gem registered but doesn't exist at #{gem_directory}"
102         next
103       end
104 
105       unless File.exist? spec_path then
106         errors[gem_name][spec_path] = "Spec file missing for installed gem"
107       end
108 
109       begin
110         verify_gem_file(gem_path)
111 
112         good, gone, unreadable = nil, nil, nil, nil
113 
114         open gem_path, Gem.binary_mode do |file|
115           format = Gem::Format.from_file_by_path(gem_path)
116 
117           good, gone = format.file_entries.partition { |entry, _|
118             File.exist? File.join(gem_directory, entry['path'])
119           }
120 
121           gone.map! { |entry, _| entry['path'] }
122           gone.sort.each do |path|
123             errors[gem_name][path] = "Missing file"
124           end
125 
126           good, unreadable = good.partition { |entry, _|
127             File.readable? File.join(gem_directory, entry['path'])
128           }
129 
130           unreadable.map! { |entry, _| entry['path'] }
131           unreadable.sort.each do |path|
132             errors[gem_name][path] = "Unreadable file"
133           end
134 
135           good.each do |entry, data|
136             begin
137               next unless data # HACK `gem check -a mkrf`
138 
139               open File.join(gem_directory, entry['path']), Gem.binary_mode do |f|
140                 unless Digest::MD5.hexdigest(f.read).to_s ==
141                     Digest::MD5.hexdigest(data).to_s then
142                   errors[gem_name][entry['path']] = "Modified from original"
143                 end
144               end
145             end
146           end
147         end
148 
149         installed_files = find_files_for_gem(gem_directory)
150         good.map! { |entry, _| entry['path'] }
151         extras = installed_files - good - unreadable
152 
153         extras.each do |extra|
154           errors[gem_name][extra] = "Extra file"
155         end
156       rescue Gem::VerificationError => e
157         errors[gem_name][gem_path] = e.message
158       end
159     end
160 
161     errors.each do |name, subhash|
162       errors[name] = subhash.map { |path, msg| ErrorData.new(path, msg) }
163     end
164 
165     errors
166   end
167 
168   if RUBY_VERSION < '1.9' then
169     class TestRunner
170       def initialize(suite, ui)
171         @suite = suite
172         @ui = ui
173       end
174 
175       def self.run(suite, ui)
176         require 'test/unit/ui/testrunnermediator'
177         return new(suite, ui).start
178       end
179 
180       def start
181         @mediator = Test::Unit::UI::TestRunnerMediator.new(@suite)
182         @mediator.add_listener(Test::Unit::TestResult::FAULT, &method(:add_fault))
183         return @mediator.run_suite
184       end
185 
186       def add_fault(fault)
187         if Gem.configuration.verbose then
188           @ui.say fault.long_display
189         end
190       end
191     end
192 
193     autoload :TestRunner, 'test/unit/ui/testrunnerutilities'
194   end
195 
196   ##
197   # Runs unit tests for a given gem specification
198 
199   def unit_test(gem_spec)
200     start_dir = Dir.pwd
201     Dir.chdir(gem_spec.full_gem_path)
202     $: << gem_spec.full_gem_path
203     # XXX: why do we need this gem_spec when we've already got 'spec'?
204     test_files = gem_spec.test_files
205 
206     if test_files.empty? then
207       say "There are no unit tests to run for #{gem_spec.full_name}"
208       return nil
209     end
210 
211     gem gem_spec.name, "= #{gem_spec.version.version}"
212 
213     test_files.each do |f| require f end
214 
215     if RUBY_VERSION < '1.9' then
216       suite = Test::Unit::TestSuite.new("#{gem_spec.name}-#{gem_spec.version}")
217 
218       ObjectSpace.each_object(Class) do |klass|
219         suite << klass.suite if (klass < Test::Unit::TestCase)
220       end
221 
222       result = TestRunner.run suite, ui
223 
224       alert_error result.to_s unless result.passed?
225     else
226       result = MiniTest::Unit.new
227       result.run
228     end
229 
230     result
231   ensure
232     Dir.chdir(start_dir)
233   end
234 
235   def remove_leading_dot_dir(path)
236     path.sub(/^\.\//, "")
237   end
238 
239 end
240 

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