2 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
4 # See LICENSE.txt for permissions.
9 require 'rubygems/digest/md5'
10 require 'rubygems/format'
11 require 'rubygems/installer'
14 # Validator performs various gem file and gem database validation
18 include Gem::UserInteraction
21 # Given a gem file's contents, validates against its own MD5 checksum
22 # gem_data:: [String] Contents of the gem file
24 def verify_gem(gem_data)
25 raise Gem::VerificationError, 'empty gem file' if gem_data.size == 0
27 unless gem_data =~ /MD5SUM/ then
28 return # Don't worry about it...this sucks. Need to fix MD5 stuff for
33 sum_data = gem_data.gsub(/MD5SUM = "([a-z0-9]+)"/,
34 "MD5SUM = \"#{"F" * 32}\"")
36 unless Gem::MD5.hexdigest(sum_data) == $1.to_s then
37 raise Gem::VerificationError, 'invalid checksum for gem file'
42 # Given the path to a gem file, validates against its own MD5 checksum
44 # gem_path:: [String] Path to gem file
46 def verify_gem_file(gem_path)
47 open gem_path, Gem.binary_mode do |file|
52 raise Gem::VerificationError, "missing gem file #{gem_path}"
57 def find_files_for_gem(gem_directory)
59 Find.find(gem_directory) {|file_name|
60 fn = file_name.slice((gem_directory.size)..(file_name.size-1)).sub(/^\//, "")
61 if(!(fn =~ /CVS/ || File.directory?(fn) || fn == "")) then
71 ErrorData = Struct.new :path, :problem
74 # Checks the gem directory for the following potential
75 # inconsistencies/problems:
77 # * Checksum gem itself
78 # * For each file in each gem, check consistency of installed versions
79 # * Check for files that aren't part of the gem but are in the gems directory
80 # * 1 cache - 1 spec - 1 directory.
82 # returns a hash of ErrorData objects, keyed on the problem gem's name.
87 Gem::SourceIndex.from_installed_gems.each do |gem_name, gem_spec|
88 errors[gem_name] ||= []
90 gem_path = File.join(Gem.dir, "cache", gem_spec.full_name) + ".gem"
91 spec_path = File.join(Gem.dir, "specifications", gem_spec.full_name) + ".gemspec"
92 gem_directory = File.join(Gem.dir, "gems", gem_spec.full_name)
94 installed_files = find_files_for_gem(gem_directory)
96 unless File.exist? spec_path then
97 errors[gem_name] << ErrorData.new(spec_path, "Spec file doesn't exist for installed gem")
101 verify_gem_file(gem_path)
103 open gem_path, Gem.binary_mode do |file|
104 format = Gem::Format.from_file_by_path(gem_path)
105 format.file_entries.each do |entry, data|
106 # Found this file. Delete it from list
107 installed_files.delete remove_leading_dot_dir(entry['path'])
109 next unless data # HACK `gem check -a mkrf`
111 open File.join(gem_directory, entry['path']), Gem.binary_mode do |f|
112 unless Gem::MD5.hexdigest(f.read).to_s ==
113 Gem::MD5.hexdigest(data).to_s then
114 errors[gem_name] << ErrorData.new(entry['path'], "installed file doesn't match original from gem")
119 rescue Gem::VerificationError => e
120 errors[gem_name] << ErrorData.new(gem_path, e.message)
123 # Clean out directories that weren't explicitly included in the gemspec
124 # FIXME: This still allows arbitrary incorrect directories.
125 installed_files.delete_if {|potential_directory|
126 File.directory?(File.join(gem_directory, potential_directory))
128 if(installed_files.size > 0) then
129 errors[gem_name] << ErrorData.new(gem_path, "Unmanaged files in gem: #{installed_files.inspect}")
136 if RUBY_VERSION < '1.9' then
138 def initialize(suite, ui)
143 def self.run(suite, ui)
144 require 'test/unit/ui/testrunnermediator'
145 return new(suite, ui).start
149 @mediator = Test::Unit::UI::TestRunnerMediator.new(@suite)
150 @mediator.add_listener(Test::Unit::TestResult::FAULT, &method(:add_fault))
151 return @mediator.run_suite
155 if Gem.configuration.verbose then
156 @ui.say fault.long_display
161 autoload :TestRunner, 'test/unit/ui/testrunnerutilities'
165 # Runs unit tests for a given gem specification
167 def unit_test(gem_spec)
169 Dir.chdir(gem_spec.full_gem_path)
170 $: << File.join(Gem.dir, "gems", gem_spec.full_name)
171 # XXX: why do we need this gem_spec when we've already got 'spec'?
172 test_files = gem_spec.test_files
174 if test_files.empty? then
175 say "There are no unit tests to run for #{gem_spec.full_name}"
179 gem gem_spec.name, "= #{gem_spec.version.version}"
181 test_files.each do |f| require f end
183 if RUBY_VERSION < '1.9' then
184 suite = Test::Unit::TestSuite.new("#{gem_spec.name}-#{gem_spec.version}")
186 ObjectSpace.each_object(Class) do |klass|
187 suite << klass.suite if (klass < Test::Unit::TestCase)
190 result = TestRunner.run suite, ui
192 alert_error result.to_s unless result.passed?
194 result = MiniTest::Unit.new
204 def remove_leading_dot_dir(path)
205 path.sub(/^\.\//, "")