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'
16 # Validator performs various gem file and gem database validation
18 include UserInteraction
21 # Given a gem file's contents, validates against its own MD5 checksum
22 # gem_data:: [String] Contents of the gem file
23 def verify_gem(gem_data)
24 raise VerificationError, 'empty gem file' if gem_data.size == 0
26 unless gem_data =~ /MD5SUM/ then
27 return # Don't worry about it...this sucks. Need to fix MD5 stuff for
32 sum_data = gem_data.gsub(/MD5SUM = "([a-z0-9]+)"/,
33 "MD5SUM = \"#{"F" * 32}\"")
35 unless Gem::MD5.hexdigest(sum_data) == $1.to_s then
36 raise VerificationError, 'invalid checksum for gem file'
41 # Given the path to a gem file, validates against its own MD5 checksum
43 # gem_path:: [String] Path to gem file
44 def verify_gem_file(gem_path)
45 open gem_path, Gem.binary_mode do |file|
50 raise Gem::VerificationError.new("missing gem file #{gem_path}")
54 def find_files_for_gem(gem_directory)
56 Find.find(gem_directory) {|file_name|
57 fn = file_name.slice((gem_directory.size)..(file_name.size-1)).sub(/^\//, "")
58 if(!(fn =~ /CVS/ || File.directory?(fn) || fn == "")) then
68 ErrorData = Struct.new(:path, :problem)
71 # Checks the gem directory for the following potential
72 # inconsistencies/problems:
73 # * Checksum gem itself
74 # * For each file in each gem, check consistency of installed versions
75 # * Check for files that aren't part of the gem but are in the gems directory
76 # * 1 cache - 1 spec - 1 directory.
78 # returns a hash of ErrorData objects, keyed on the problem gem's name.
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)
88 if(!File.exist?(spec_path)) then
89 errors[gem_name] << ErrorData.new(spec_path, "Spec file doesn't exist for installed gem")
93 verify_gem_file(gem_path)
94 open gem_path, Gem.binary_mode 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'])
100 next unless data # HACK `gem check -a mkrf`
102 open File.join(gem_directory, entry['path']), Gem.binary_mode 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")
110 rescue VerificationError => e
111 errors[gem_name] << ErrorData.new(gem_path, e.message)
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))
118 if(installed_files.size > 0) then
119 errors[gem_name] << ErrorData.new(gem_path, "Unmanaged files in gem: #{installed_files.inspect}")
126 def initialize(suite, ui)
131 def self.run(suite, ui)
132 require 'test/unit/ui/testrunnermediator'
133 return new(suite, ui).start
137 @mediator = Test::Unit::UI::TestRunnerMediator.new(@suite)
138 @mediator.add_listener(Test::Unit::TestResult::FAULT, &method(:add_fault))
139 return @mediator.run_suite
143 if Gem.configuration.verbose then
144 @ui.say fault.long_display
149 autoload :TestRunner, 'test/unit/ui/testrunnerutilities'
152 # Runs unit tests for a given gem specification
153 def unit_test(gem_spec)
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
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
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)
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
182 def remove_leading_dot_dir(path)
183 path.sub(/^\.\//, "")