PageRenderTime 94ms CodeModel.GetById 37ms RepoModel.GetById 6ms app.codeStats 0ms

/vendor/jruby-1.1.6RC1/lib/ruby/site_ruby/1.8/rubygems/validator.rb

https://bitbucket.org/nicksieger/advent-jruby
Ruby | 208 lines | 134 code | 43 blank | 31 comment | 14 complexity | 3f43dc2c18eb6cf37e5bdfa1b490db37 MD5 | raw file
Possible License(s): CPL-1.0, AGPL-1.0, LGPL-2.1, JSON
  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. require 'find'
  7. require 'rubygems/digest/md5'
  8. require 'rubygems/format'
  9. require 'rubygems/installer'
  10. ##
  11. # Validator performs various gem file and gem database validation
  12. class Gem::Validator
  13. include Gem::UserInteraction
  14. ##
  15. # Given a gem file's contents, validates against its own MD5 checksum
  16. # gem_data:: [String] Contents of the gem file
  17. def verify_gem(gem_data)
  18. raise Gem::VerificationError, 'empty gem file' if gem_data.size == 0
  19. unless gem_data =~ /MD5SUM/ then
  20. return # Don't worry about it...this sucks. Need to fix MD5 stuff for
  21. # new format
  22. # FIXME
  23. end
  24. sum_data = gem_data.gsub(/MD5SUM = "([a-z0-9]+)"/,
  25. "MD5SUM = \"#{"F" * 32}\"")
  26. unless Gem::MD5.hexdigest(sum_data) == $1.to_s then
  27. raise Gem::VerificationError, 'invalid checksum for gem file'
  28. end
  29. end
  30. ##
  31. # Given the path to a gem file, validates against its own MD5 checksum
  32. #
  33. # gem_path:: [String] Path to gem file
  34. def verify_gem_file(gem_path)
  35. open gem_path, Gem.binary_mode do |file|
  36. gem_data = file.read
  37. verify_gem gem_data
  38. end
  39. rescue Errno::ENOENT
  40. raise Gem::VerificationError, "missing gem file #{gem_path}"
  41. end
  42. private
  43. def find_files_for_gem(gem_directory)
  44. installed_files = []
  45. Find.find(gem_directory) {|file_name|
  46. fn = file_name.slice((gem_directory.size)..(file_name.size-1)).sub(/^\//, "")
  47. if(!(fn =~ /CVS/ || File.directory?(fn) || fn == "")) then
  48. installed_files << fn
  49. end
  50. }
  51. installed_files
  52. end
  53. public
  54. ErrorData = Struct.new :path, :problem
  55. ##
  56. # Checks the gem directory for the following potential
  57. # inconsistencies/problems:
  58. #
  59. # * Checksum gem itself
  60. # * For each file in each gem, check consistency of installed versions
  61. # * Check for files that aren't part of the gem but are in the gems directory
  62. # * 1 cache - 1 spec - 1 directory.
  63. #
  64. # returns a hash of ErrorData objects, keyed on the problem gem's name.
  65. def alien
  66. errors = {}
  67. Gem::SourceIndex.from_installed_gems.each do |gem_name, gem_spec|
  68. errors[gem_name] ||= []
  69. gem_path = File.join(Gem.dir, "cache", gem_spec.full_name) + ".gem"
  70. spec_path = File.join(Gem.dir, "specifications", gem_spec.full_name) + ".gemspec"
  71. gem_directory = File.join(Gem.dir, "gems", gem_spec.full_name)
  72. installed_files = find_files_for_gem(gem_directory)
  73. unless File.exist? spec_path then
  74. errors[gem_name] << ErrorData.new(spec_path, "Spec file doesn't exist for installed gem")
  75. end
  76. begin
  77. verify_gem_file(gem_path)
  78. open gem_path, Gem.binary_mode do |file|
  79. format = Gem::Format.from_file_by_path(gem_path)
  80. format.file_entries.each do |entry, data|
  81. # Found this file. Delete it from list
  82. installed_files.delete remove_leading_dot_dir(entry['path'])
  83. next unless data # HACK `gem check -a mkrf`
  84. open File.join(gem_directory, entry['path']), Gem.binary_mode do |f|
  85. unless Gem::MD5.hexdigest(f.read).to_s ==
  86. Gem::MD5.hexdigest(data).to_s then
  87. errors[gem_name] << ErrorData.new(entry['path'], "installed file doesn't match original from gem")
  88. end
  89. end
  90. end
  91. end
  92. rescue Gem::VerificationError => e
  93. errors[gem_name] << ErrorData.new(gem_path, e.message)
  94. end
  95. # Clean out directories that weren't explicitly included in the gemspec
  96. # FIXME: This still allows arbitrary incorrect directories.
  97. installed_files.delete_if {|potential_directory|
  98. File.directory?(File.join(gem_directory, potential_directory))
  99. }
  100. if(installed_files.size > 0) then
  101. errors[gem_name] << ErrorData.new(gem_path, "Unmanaged files in gem: #{installed_files.inspect}")
  102. end
  103. end
  104. errors
  105. end
  106. if RUBY_VERSION < '1.9' then
  107. class TestRunner
  108. def initialize(suite, ui)
  109. @suite = suite
  110. @ui = ui
  111. end
  112. def self.run(suite, ui)
  113. require 'test/unit/ui/testrunnermediator'
  114. return new(suite, ui).start
  115. end
  116. def start
  117. @mediator = Test::Unit::UI::TestRunnerMediator.new(@suite)
  118. @mediator.add_listener(Test::Unit::TestResult::FAULT, &method(:add_fault))
  119. return @mediator.run_suite
  120. end
  121. def add_fault(fault)
  122. if Gem.configuration.verbose then
  123. @ui.say fault.long_display
  124. end
  125. end
  126. end
  127. autoload :TestRunner, 'test/unit/ui/testrunnerutilities'
  128. end
  129. ##
  130. # Runs unit tests for a given gem specification
  131. def unit_test(gem_spec)
  132. start_dir = Dir.pwd
  133. Dir.chdir(gem_spec.full_gem_path)
  134. $: << File.join(Gem.dir, "gems", gem_spec.full_name)
  135. # XXX: why do we need this gem_spec when we've already got 'spec'?
  136. test_files = gem_spec.test_files
  137. if test_files.empty? then
  138. say "There are no unit tests to run for #{gem_spec.full_name}"
  139. return nil
  140. end
  141. gem gem_spec.name, "= #{gem_spec.version.version}"
  142. test_files.each do |f| require f end
  143. if RUBY_VERSION < '1.9' then
  144. suite = Test::Unit::TestSuite.new("#{gem_spec.name}-#{gem_spec.version}")
  145. ObjectSpace.each_object(Class) do |klass|
  146. suite << klass.suite if (klass < Test::Unit::TestCase)
  147. end
  148. result = TestRunner.run suite, ui
  149. alert_error result.to_s unless result.passed?
  150. else
  151. result = MiniTest::Unit.new
  152. result.run
  153. end
  154. result
  155. ensure
  156. Dir.chdir(start_dir)
  157. end
  158. def remove_leading_dot_dir(path)
  159. path.sub(/^\.\//, "")
  160. end
  161. end