2007-11-10 02:48:56 -05:00
|
|
|
#--
|
|
|
|
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
|
|
|
|
# All rights reserved.
|
|
|
|
# See LICENSE.txt for permissions.
|
|
|
|
#++
|
|
|
|
|
|
|
|
require 'find'
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
require 'digest'
|
2007-11-10 02:48:56 -05:00
|
|
|
require 'rubygems/format'
|
|
|
|
require 'rubygems/installer'
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
begin
|
2010-04-22 04:24:42 -04:00
|
|
|
gem 'test-unit'
|
2009-06-09 17:38:59 -04:00
|
|
|
rescue Gem::LoadError
|
2010-04-22 04:24:42 -04:00
|
|
|
# Ignore - use the test-unit library that's part of the standard library
|
2009-12-08 02:19:09 -05:00
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
|
2008-10-25 18:58:43 -04:00
|
|
|
##
|
|
|
|
# Validator performs various gem file and gem database validation
|
|
|
|
|
|
|
|
class Gem::Validator
|
|
|
|
|
|
|
|
include Gem::UserInteraction
|
2007-11-10 02:48:56 -05:00
|
|
|
|
|
|
|
##
|
2008-10-25 18:58:43 -04:00
|
|
|
# Given a gem file's contents, validates against its own MD5 checksum
|
|
|
|
# gem_data:: [String] Contents of the gem file
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-10-25 18:58:43 -04:00
|
|
|
def verify_gem(gem_data)
|
|
|
|
raise Gem::VerificationError, 'empty gem file' if gem_data.size == 0
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-10-25 18:58:43 -04:00
|
|
|
unless gem_data =~ /MD5SUM/ then
|
|
|
|
return # Don't worry about it...this sucks. Need to fix MD5 stuff for
|
|
|
|
# new format
|
|
|
|
# FIXME
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2008-10-25 18:58:43 -04:00
|
|
|
sum_data = gem_data.gsub(/MD5SUM = "([a-z0-9]+)"/,
|
|
|
|
"MD5SUM = \"#{"F" * 32}\"")
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
unless Digest::MD5.hexdigest(sum_data) == $1.to_s then
|
2008-10-25 18:58:43 -04:00
|
|
|
raise Gem::VerificationError, 'invalid checksum for gem file'
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2008-10-25 18:58:43 -04:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-10-25 18:58:43 -04:00
|
|
|
##
|
|
|
|
# Given the path to a gem file, validates against its own MD5 checksum
|
|
|
|
#
|
|
|
|
# gem_path:: [String] Path to gem file
|
|
|
|
|
|
|
|
def verify_gem_file(gem_path)
|
|
|
|
open gem_path, Gem.binary_mode do |file|
|
|
|
|
gem_data = file.read
|
|
|
|
verify_gem gem_data
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
rescue Errno::ENOENT, Errno::EINVAL
|
2008-10-25 18:58:43 -04:00
|
|
|
raise Gem::VerificationError, "missing gem file #{gem_path}"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_files_for_gem(gem_directory)
|
|
|
|
installed_files = []
|
2009-06-09 17:38:59 -04:00
|
|
|
Find.find gem_directory do |file_name|
|
|
|
|
fn = file_name[gem_directory.size..file_name.size-1].sub(/^\//, "")
|
|
|
|
installed_files << fn unless
|
|
|
|
fn =~ /CVS/ || fn.empty? || File.directory?(file_name)
|
|
|
|
end
|
2008-10-25 18:58:43 -04:00
|
|
|
installed_files
|
|
|
|
end
|
|
|
|
|
|
|
|
public
|
|
|
|
|
|
|
|
ErrorData = Struct.new :path, :problem
|
|
|
|
|
|
|
|
##
|
|
|
|
# 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.
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def alien(gems=[])
|
|
|
|
errors = Hash.new { |h,k| h[k] = {} }
|
2008-10-25 18:58:43 -04:00
|
|
|
|
|
|
|
Gem::SourceIndex.from_installed_gems.each do |gem_name, gem_spec|
|
2009-06-09 17:38:59 -04:00
|
|
|
next unless gems.include? gem_spec.name unless gems.empty?
|
|
|
|
|
|
|
|
install_dir = gem_spec.installation_path
|
2010-02-21 21:52:35 -05:00
|
|
|
gem_path = File.join install_dir, "cache", gem_spec.file_name
|
|
|
|
spec_path = File.join install_dir, "specifications", gem_spec.spec_name
|
2009-06-09 17:38:59 -04:00
|
|
|
gem_directory = gem_spec.full_gem_path
|
|
|
|
|
|
|
|
unless File.directory? gem_directory then
|
|
|
|
errors[gem_name][gem_spec.full_name] =
|
|
|
|
"Gem registered but doesn't exist at #{gem_directory}"
|
|
|
|
next
|
|
|
|
end
|
2008-10-25 18:58:43 -04:00
|
|
|
|
|
|
|
unless File.exist? spec_path then
|
2009-06-09 17:38:59 -04:00
|
|
|
errors[gem_name][spec_path] = "Spec file missing for installed gem"
|
2008-10-25 18:58:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
verify_gem_file(gem_path)
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
good, gone, unreadable = nil, nil, nil, nil
|
|
|
|
|
2008-10-25 18:58:43 -04:00
|
|
|
open gem_path, Gem.binary_mode do |file|
|
|
|
|
format = Gem::Format.from_file_by_path(gem_path)
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
good, gone = format.file_entries.partition { |entry, _|
|
|
|
|
File.exist? File.join(gem_directory, entry['path'])
|
|
|
|
}
|
|
|
|
|
|
|
|
gone.map! { |entry, _| entry['path'] }
|
|
|
|
gone.sort.each do |path|
|
|
|
|
errors[gem_name][path] = "Missing file"
|
|
|
|
end
|
|
|
|
|
|
|
|
good, unreadable = good.partition { |entry, _|
|
|
|
|
File.readable? File.join(gem_directory, entry['path'])
|
|
|
|
}
|
|
|
|
|
|
|
|
unreadable.map! { |entry, _| entry['path'] }
|
|
|
|
unreadable.sort.each do |path|
|
|
|
|
errors[gem_name][path] = "Unreadable file"
|
|
|
|
end
|
2008-10-25 18:58:43 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
good.each do |entry, data|
|
|
|
|
begin
|
|
|
|
next unless data # HACK `gem check -a mkrf`
|
|
|
|
|
|
|
|
open File.join(gem_directory, entry['path']), Gem.binary_mode do |f|
|
|
|
|
unless Digest::MD5.hexdigest(f.read).to_s ==
|
|
|
|
Digest::MD5.hexdigest(data).to_s then
|
|
|
|
errors[gem_name][entry['path']] = "Modified from original"
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
|
|
|
|
installed_files = find_files_for_gem(gem_directory)
|
|
|
|
good.map! { |entry, _| entry['path'] }
|
|
|
|
extras = installed_files - good - unreadable
|
|
|
|
|
|
|
|
extras.each do |extra|
|
|
|
|
errors[gem_name][extra] = "Extra file"
|
|
|
|
end
|
2008-10-25 18:58:43 -04:00
|
|
|
rescue Gem::VerificationError => e
|
2009-06-09 17:38:59 -04:00
|
|
|
errors[gem_name][gem_path] = e.message
|
2008-10-25 18:58:43 -04:00
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
2008-10-25 18:58:43 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
errors.each do |name, subhash|
|
|
|
|
errors[name] = subhash.map { |path, msg| ErrorData.new(path, msg) }
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2008-10-25 18:58:43 -04:00
|
|
|
errors
|
|
|
|
end
|
|
|
|
|
2009-12-30 03:45:55 -05:00
|
|
|
=begin
|
2008-10-25 18:58:43 -04:00
|
|
|
if RUBY_VERSION < '1.9' then
|
2007-11-10 02:48:56 -05:00
|
|
|
class TestRunner
|
|
|
|
def initialize(suite, ui)
|
|
|
|
@suite = suite
|
|
|
|
@ui = ui
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.run(suite, ui)
|
|
|
|
require 'test/unit/ui/testrunnermediator'
|
|
|
|
return new(suite, ui).start
|
|
|
|
end
|
|
|
|
|
|
|
|
def start
|
|
|
|
@mediator = Test::Unit::UI::TestRunnerMediator.new(@suite)
|
|
|
|
@mediator.add_listener(Test::Unit::TestResult::FAULT, &method(:add_fault))
|
|
|
|
return @mediator.run_suite
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_fault(fault)
|
|
|
|
if Gem.configuration.verbose then
|
|
|
|
@ui.say fault.long_display
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
autoload :TestRunner, 'test/unit/ui/testrunnerutilities'
|
2008-10-25 18:58:43 -04:00
|
|
|
end
|
2009-12-30 03:45:55 -05:00
|
|
|
=end
|
2008-10-25 18:58:43 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Runs unit tests for a given gem specification
|
|
|
|
|
|
|
|
def unit_test(gem_spec)
|
|
|
|
start_dir = Dir.pwd
|
|
|
|
Dir.chdir(gem_spec.full_gem_path)
|
2009-06-09 17:38:59 -04:00
|
|
|
$: << gem_spec.full_gem_path
|
2008-10-25 18:58:43 -04:00
|
|
|
# XXX: why do we need this gem_spec when we've already got 'spec'?
|
|
|
|
test_files = gem_spec.test_files
|
|
|
|
|
|
|
|
if test_files.empty? then
|
|
|
|
say "There are no unit tests to run for #{gem_spec.full_name}"
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
gem gem_spec.name, "= #{gem_spec.version.version}"
|
|
|
|
|
|
|
|
test_files.each do |f| require f end
|
|
|
|
|
2009-12-30 03:45:55 -05:00
|
|
|
=begin
|
2008-10-25 18:58:43 -04:00
|
|
|
if RUBY_VERSION < '1.9' then
|
2007-11-10 02:48:56 -05:00
|
|
|
suite = Test::Unit::TestSuite.new("#{gem_spec.name}-#{gem_spec.version}")
|
2008-10-25 18:58:43 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
ObjectSpace.each_object(Class) do |klass|
|
|
|
|
suite << klass.suite if (klass < Test::Unit::TestCase)
|
|
|
|
end
|
|
|
|
|
2008-10-25 18:58:43 -04:00
|
|
|
result = TestRunner.run suite, ui
|
|
|
|
|
|
|
|
alert_error result.to_s unless result.passed?
|
|
|
|
else
|
|
|
|
result = MiniTest::Unit.new
|
|
|
|
result.run
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2009-12-30 03:45:55 -05:00
|
|
|
=end
|
|
|
|
result = MiniTest::Unit.new
|
|
|
|
result.run
|
2008-10-25 18:58:43 -04:00
|
|
|
|
|
|
|
result
|
|
|
|
ensure
|
|
|
|
Dir.chdir(start_dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_leading_dot_dir(path)
|
|
|
|
path.sub(/^\.\//, "")
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2008-10-25 18:58:43 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2008-10-25 18:58:43 -04:00
|
|
|
|