Class Gem::Validator
In: lib/rubygems/validator.rb
Parent: Object

Validator performs various gem file and gem database validation

Methods

Included Modules

UserInteraction

Classes and Modules

Class Gem::Validator::TestRunner

Constants

ErrorData = Struct.new(:path, :problem)

Public Instance methods

Checks the gem directory for the following potential inconsistencies/problems:

  • Checksum gem itself
  • For each file in each gem, check consistency of installed versions
  • Check for files that aren‘t part of the gem but are in the gems directory
  • 1 cache - 1 spec - 1 directory.

returns a hash of ErrorData objects, keyed on the problem gem‘s name.

[Source]

     # File lib/rubygems/validator.rb, line 79
 79:     def alien
 80:       errors = {}
 81:       Gem::SourceIndex.from_installed_gems.each do |gem_name, gem_spec|
 82:         errors[gem_name] ||= []
 83:         gem_path = File.join(Gem.dir, "cache", gem_spec.full_name) + ".gem"
 84:         spec_path = File.join(Gem.dir, "specifications", gem_spec.full_name) + ".gemspec"
 85:         gem_directory = File.join(Gem.dir, "gems", gem_spec.full_name)
 86:         installed_files = find_files_for_gem(gem_directory)
 87:     
 88:         if(!File.exist?(spec_path)) then
 89:           errors[gem_name] << ErrorData.new(spec_path, "Spec file doesn't exist for installed gem")
 90:         end
 91:     
 92:         begin
 93:           verify_gem_file(gem_path)
 94:           File.open(gem_path, 'rb') do |file|
 95:             format = Gem::Format.from_file_by_path(gem_path)
 96:             format.file_entries.each do |entry, data|
 97:               # Found this file.  Delete it from list
 98:               installed_files.delete remove_leading_dot_dir(entry['path'])
 99: 
100:               next unless data # HACK `gem check -a mkrf`
101: 
102:               File.open(File.join(gem_directory, entry['path']), 'rb') do |f|
103:                 unless Gem::MD5.hexdigest(f.read).to_s ==
104:                        Gem::MD5.hexdigest(data).to_s then
105:                   errors[gem_name] << ErrorData.new(entry['path'], "installed file doesn't match original from gem")
106:                 end
107:               end
108:             end
109:           end
110:         rescue VerificationError => e
111:           errors[gem_name] << ErrorData.new(gem_path, e.message)
112:         end
113:         # Clean out directories that weren't explicitly included in the gemspec
114:         # FIXME: This still allows arbitrary incorrect directories.
115:         installed_files.delete_if {|potential_directory|        
116:           File.directory?(File.join(gem_directory, potential_directory))
117:         }
118:         if(installed_files.size > 0) then
119:           errors[gem_name] << ErrorData.new(gem_path, "Unmanaged files in gem: #{installed_files.inspect}")
120:         end
121:       end
122:       errors
123:     end

[Source]

     # File lib/rubygems/validator.rb, line 182
182:     def remove_leading_dot_dir(path)
183:       path.sub(/^\.\//, "")
184:     end

Runs unit tests for a given gem specification

[Source]

     # File lib/rubygems/validator.rb, line 153
153:     def unit_test(gem_spec)
154:      start_dir = Dir.pwd
155:      Dir.chdir(gem_spec.full_gem_path)
156:       $: << File.join(Gem.dir, "gems", gem_spec.full_name)
157:         # XXX: why do we need this gem_spec when we've already got 'spec'?
158:       test_files = gem_spec.test_files
159:       if test_files.empty?
160:         say "There are no unit tests to run for #{gem_spec.full_name}"
161:         require 'test/unit/ui/console/testrunner'
162:         return Test::Unit::TestResult.new
163:       end
164:       gem gem_spec.name, "= #{gem_spec.version.version}"
165:       test_files.each do |f| require f end
166:       suite = Test::Unit::TestSuite.new("#{gem_spec.name}-#{gem_spec.version}")
167:       ObjectSpace.each_object(Class) do |klass|
168:         suite << klass.suite if (klass < Test::Unit::TestCase)
169:       end
170:       result = TestRunner.run(suite, ui())
171:       unless result.passed?
172:         alert_error(result.to_s)
173:         #unless ask_yes_no(result.to_s + "...keep Gem?", true) then
174:           #Gem::Uninstaller.new(gem_spec.name, gem_spec.version.version).uninstall
175:         #end
176:       end
177:       result
178:     ensure
179:       Dir.chdir(start_dir)
180:     end

Given a gem file‘s contents, validates against its own MD5 checksum

gem_data:[String] Contents of the gem file

[Source]

    # File lib/rubygems/validator.rb, line 23
23:     def verify_gem(gem_data)
24:       raise VerificationError, 'empty gem file' if gem_data.size == 0
25: 
26:       unless gem_data =~ /MD5SUM/ then
27:         return # Don't worry about it...this sucks.  Need to fix MD5 stuff for
28:                # new format
29:                # FIXME
30:       end
31: 
32:       sum_data = gem_data.gsub(/MD5SUM = "([a-z0-9]+)"/,
33:                                "MD5SUM = \"#{"F" * 32}\"")
34: 
35:       unless Gem::MD5.hexdigest(sum_data) == $1.to_s then
36:         raise VerificationError, 'invalid checksum for gem file'
37:       end
38:     end

Given the path to a gem file, validates against its own MD5 checksum

gem_path:[String] Path to gem file

[Source]

    # File lib/rubygems/validator.rb, line 44
44:     def verify_gem_file(gem_path)
45:       File.open gem_path, 'rb' do |file|
46:         gem_data = file.read
47:         verify_gem gem_data
48:       end
49:     rescue Errno::ENOENT
50:       raise Gem::VerificationError.new("missing gem file #{gem_path}")
51:     end

[Validate]