PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/test/externals/ruby1.9/rubygems/gemutilities.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 588 lines | 467 code | 95 blank | 26 comment | 20 complexity | d7d4d2bb4ea1b0dacd88ab0c1679690c MD5 | raw file
  1. at_exit { $SAFE = 1 }
  2. $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
  3. if RUBY_VERSION > '1.9' then
  4. Gem::QuickLoader.load_full_rubygems_library
  5. else
  6. require 'rubygems'
  7. end
  8. require 'fileutils'
  9. require 'minitest/autorun'
  10. require 'tmpdir'
  11. require 'uri'
  12. require 'rubygems/package'
  13. require 'rubygems/test_utilities'
  14. require 'pp'
  15. require 'yaml'
  16. begin
  17. YAML::ENGINE.yamler = 'psych'
  18. rescue LoadError
  19. end if YAML.const_defined? :ENGINE
  20. begin
  21. gem 'rdoc'
  22. rescue Gem::LoadError
  23. end
  24. require 'rdoc/rdoc'
  25. require_relative 'mockgemui'
  26. module Gem
  27. def self.searcher=(searcher)
  28. MUTEX.synchronize do @searcher = searcher end
  29. end
  30. def self.source_index=(si)
  31. @@source_index = si
  32. end
  33. def self.win_platform=(val)
  34. @@win_platform = val
  35. end
  36. module DefaultUserInteraction
  37. @ui = MockGemUi.new
  38. end
  39. end
  40. class RubyGemTestCase < MiniTest::Unit::TestCase
  41. include Gem::DefaultUserInteraction
  42. undef_method :default_test if instance_methods.include? 'default_test' or
  43. instance_methods.include? :default_test
  44. def setup
  45. super
  46. @ui = MockGemUi.new
  47. tmpdir = nil
  48. Dir.chdir Dir.tmpdir do tmpdir = Dir.pwd end # HACK OSX /private/tmp
  49. @tempdir = File.join tmpdir, "test_rubygems_#{$$}"
  50. @tempdir.untaint
  51. @gemhome = File.join @tempdir, "gemhome"
  52. @gemcache = File.join(@gemhome, "source_cache")
  53. @usrcache = File.join(@gemhome, ".gem", "user_cache")
  54. @latest_usrcache = File.join(@gemhome, ".gem", "latest_user_cache")
  55. @userhome = File.join @tempdir, 'userhome'
  56. Gem.ensure_gem_subdirectories @gemhome
  57. @orig_ruby = if ruby = ENV['RUBY'] then
  58. Gem.class_eval { ruby, @ruby = @ruby, ruby }
  59. ruby
  60. end
  61. Gem.ensure_gem_subdirectories @gemhome
  62. @orig_ENV_HOME = ENV['HOME']
  63. ENV['HOME'] = @userhome
  64. Gem.instance_variable_set :@user_home, nil
  65. FileUtils.mkdir_p @gemhome
  66. FileUtils.mkdir_p @userhome
  67. ENV['GEMCACHE'] = @usrcache
  68. Gem.use_paths(@gemhome)
  69. Gem.loaded_specs.clear
  70. Gem.configuration.verbose = true
  71. Gem.configuration.update_sources = true
  72. @gem_repo = "http://gems.example.com/"
  73. @uri = URI.parse @gem_repo
  74. Gem.sources.replace [@gem_repo]
  75. Gem::SpecFetcher.fetcher = nil
  76. @orig_BASERUBY = Gem::ConfigMap[:BASERUBY]
  77. Gem::ConfigMap[:BASERUBY] = Gem::ConfigMap[:ruby_install_name]
  78. @orig_arch = Gem::ConfigMap[:arch]
  79. if win_platform?
  80. util_set_arch 'i386-mswin32'
  81. else
  82. util_set_arch 'i686-darwin8.10.1'
  83. end
  84. @marshal_version = "#{Marshal::MAJOR_VERSION}.#{Marshal::MINOR_VERSION}"
  85. @private_key = File.expand_path File.join(File.dirname(__FILE__),
  86. 'private_key.pem')
  87. @public_cert = File.expand_path File.join(File.dirname(__FILE__),
  88. 'public_cert.pem')
  89. Gem.post_install_hooks.clear
  90. Gem.post_uninstall_hooks.clear
  91. Gem.pre_install_hooks.clear
  92. Gem.pre_uninstall_hooks.clear
  93. Gem.post_install do |installer|
  94. @post_install_hook_arg = installer
  95. end
  96. Gem.post_uninstall do |uninstaller|
  97. @post_uninstall_hook_arg = uninstaller
  98. end
  99. Gem.pre_install do |installer|
  100. @pre_install_hook_arg = installer
  101. end
  102. Gem.pre_uninstall do |uninstaller|
  103. @pre_uninstall_hook_arg = uninstaller
  104. end
  105. end
  106. def teardown
  107. Gem::ConfigMap[:BASERUBY] = @orig_BASERUBY
  108. Gem::ConfigMap[:arch] = @orig_arch
  109. if defined? Gem::RemoteFetcher then
  110. Gem::RemoteFetcher.fetcher = nil
  111. end
  112. FileUtils.rm_rf @tempdir
  113. ENV.delete 'GEMCACHE'
  114. ENV.delete 'GEM_HOME'
  115. ENV.delete 'GEM_PATH'
  116. Gem.clear_paths
  117. Gem.class_eval { @ruby = ruby } if ruby = @orig_ruby
  118. if @orig_ENV_HOME then
  119. ENV['HOME'] = @orig_ENV_HOME
  120. else
  121. ENV.delete 'HOME'
  122. end
  123. end
  124. def install_gem gem
  125. require 'rubygems/installer'
  126. use_ui MockGemUi.new do
  127. Dir.chdir @tempdir do
  128. Gem::Builder.new(gem).build
  129. end
  130. end
  131. gem = File.join(@tempdir, gem.file_name).untaint
  132. Gem::Installer.new(gem, :wrappers => true).install
  133. end
  134. def mu_pp(obj)
  135. s = ''
  136. s = PP.pp obj, s
  137. s = s.force_encoding(Encoding.default_external) if defined? Encoding
  138. s.chomp
  139. end
  140. def prep_cache_files(lc)
  141. @usr_si ||= Gem::SourceIndex.new
  142. @usr_sice ||= Gem::SourceInfoCacheEntry.new @usr_si, 0
  143. @sys_si ||= Gem::SourceIndex.new
  144. @sys_sice ||= Gem::SourceInfoCacheEntry.new @sys_si, 0
  145. latest_si = Gem::SourceIndex.new
  146. latest_si.add_specs(*@sys_si.latest_specs)
  147. latest_sys_sice = Gem::SourceInfoCacheEntry.new latest_si, 0
  148. latest_si = Gem::SourceIndex.new
  149. latest_si.add_specs(*@usr_si.latest_specs)
  150. latest_usr_sice = Gem::SourceInfoCacheEntry.new latest_si, 0
  151. [ [lc.system_cache_file, @sys_sice],
  152. [lc.latest_system_cache_file, latest_sys_sice],
  153. [lc.user_cache_file, @usr_sice],
  154. [lc.latest_user_cache_file, latest_usr_sice],
  155. ].each do |filename, data|
  156. FileUtils.mkdir_p File.dirname(filename).untaint
  157. open filename.dup.untaint, 'wb' do |f|
  158. f.write Marshal.dump({ @gem_repo => data })
  159. end
  160. end
  161. end
  162. def read_cache(path)
  163. open path.dup.untaint, 'rb' do |io|
  164. Marshal.load io.read
  165. end
  166. end
  167. def read_binary(path)
  168. Gem.read_binary path
  169. end
  170. def write_file(path)
  171. path = File.join(@gemhome, path)
  172. dir = File.dirname path
  173. FileUtils.mkdir_p dir
  174. open path, 'wb' do |io|
  175. yield io
  176. end
  177. path
  178. end
  179. def quick_gem(gemname, version='2')
  180. require 'rubygems/specification'
  181. spec = Gem::Specification.new do |s|
  182. s.platform = Gem::Platform::RUBY
  183. s.name = gemname
  184. s.version = version
  185. s.author = 'A User'
  186. s.email = 'example@example.com'
  187. s.homepage = 'http://example.com'
  188. s.has_rdoc = true
  189. s.summary = "this is a summary"
  190. s.description = "This is a test description"
  191. yield(s) if block_given?
  192. end
  193. path = File.join "specifications", spec.spec_name
  194. written_path = write_file path do |io|
  195. io.write(spec.to_ruby)
  196. end
  197. spec.loaded_from = written_path
  198. Gem.source_index.add_spec spec
  199. return spec
  200. end
  201. def util_build_gem(spec)
  202. dir = File.join(@gemhome, 'gems', spec.full_name)
  203. FileUtils.mkdir_p dir
  204. Dir.chdir dir do
  205. spec.files.each do |file|
  206. next if File.exist? file
  207. FileUtils.mkdir_p File.dirname(file)
  208. File.open file, 'w' do |fp| fp.puts "# #{file}" end
  209. end
  210. use_ui MockGemUi.new do
  211. Gem::Builder.new(spec).build
  212. end
  213. FileUtils.mv spec.file_name,
  214. File.join(@gemhome, 'cache', "#{spec.original_name}.gem")
  215. end
  216. end
  217. def util_clear_gems
  218. FileUtils.rm_r File.join(@gemhome, 'gems')
  219. FileUtils.rm_r File.join(@gemhome, 'specifications')
  220. Gem.source_index.refresh!
  221. end
  222. def util_gem(name, version, &block)
  223. spec = quick_gem(name, version, &block)
  224. util_build_gem spec
  225. cache_file = File.join @tempdir, 'gems', "#{spec.original_name}.gem"
  226. FileUtils.mv File.join(@gemhome, 'cache', "#{spec.original_name}.gem"),
  227. cache_file
  228. FileUtils.rm File.join(@gemhome, 'specifications', spec.spec_name)
  229. spec.loaded_from = nil
  230. spec.loaded = false
  231. [spec, cache_file]
  232. end
  233. def util_gzip(data)
  234. out = StringIO.new
  235. Zlib::GzipWriter.wrap out do |io|
  236. io.write data
  237. end
  238. out.string
  239. end
  240. def util_make_gems(prerelease = false)
  241. @a1 = quick_gem 'a', '1' do |s|
  242. s.files = %w[lib/code.rb]
  243. s.require_paths = %w[lib]
  244. s.date = Gem::Specification::TODAY - 86400
  245. s.homepage = 'http://a.example.com'
  246. s.email = %w[example@example.com example2@example.com]
  247. s.authors = %w[Example Example2]
  248. s.description = <<-DESC
  249. This line is really, really long. So long, in fact, that it is more than eighty characters long! The purpose of this line is for testing wrapping behavior because sometimes people don't wrap their text to eighty characters. Without the wrapping, the text might not look good in the RSS feed.
  250. Also, a list:
  251. * An entry that's actually kind of sort
  252. * an entry that's really long, which will probably get wrapped funny. That's ok, somebody wasn't thinking straight when they made it more than eighty characters.
  253. DESC
  254. end
  255. init = proc do |s|
  256. s.files = %w[lib/code.rb]
  257. s.require_paths = %w[lib]
  258. end
  259. @a2 = quick_gem('a', '2', &init)
  260. @a3a = quick_gem('a', '3.a', &init)
  261. @a_evil9 = quick_gem('a_evil', '9', &init)
  262. @b2 = quick_gem('b', '2', &init)
  263. @c1_2 = quick_gem('c', '1.2', &init)
  264. @pl1 = quick_gem 'pl', '1' do |s| # l for legacy
  265. s.files = %w[lib/code.rb]
  266. s.require_paths = %w[lib]
  267. s.platform = Gem::Platform.new 'i386-linux'
  268. s.instance_variable_set :@original_platform, 'i386-linux'
  269. end
  270. if prerelease
  271. @a2_pre = quick_gem('a', '2.a', &init)
  272. write_file File.join(*%W[gems #{@a2_pre.original_name} lib code.rb]) do
  273. end
  274. util_build_gem @a2_pre
  275. end
  276. write_file File.join(*%W[gems #{@a1.original_name} lib code.rb]) do end
  277. write_file File.join(*%W[gems #{@a2.original_name} lib code.rb]) do end
  278. write_file File.join(*%W[gems #{@a3a.original_name} lib code.rb]) do end
  279. write_file File.join(*%W[gems #{@b2.original_name} lib code.rb]) do end
  280. write_file File.join(*%W[gems #{@c1_2.original_name} lib code.rb]) do end
  281. write_file File.join(*%W[gems #{@pl1.original_name} lib code.rb]) do end
  282. [@a1, @a2, @a3a, @a_evil9, @b2, @c1_2, @pl1].each do |spec|
  283. util_build_gem spec
  284. end
  285. FileUtils.rm_r File.join(@gemhome, 'gems', @pl1.original_name)
  286. Gem.source_index = nil
  287. end
  288. ##
  289. # Set the platform to +arch+
  290. def util_set_arch(arch)
  291. Gem::ConfigMap[:arch] = arch
  292. platform = Gem::Platform.new arch
  293. Gem.instance_variable_set :@platforms, nil
  294. Gem::Platform.instance_variable_set :@local, nil
  295. platform
  296. end
  297. def util_setup_fake_fetcher(prerelease = false)
  298. require 'zlib'
  299. require 'socket'
  300. require 'rubygems/remote_fetcher'
  301. @fetcher = Gem::FakeFetcher.new
  302. util_make_gems(prerelease)
  303. @all_gems = [@a1, @a2, @a3a, @a_evil9, @b2, @c1_2].sort
  304. @all_gem_names = @all_gems.map { |gem| gem.full_name }
  305. gem_names = [@a1.full_name, @a2.full_name, @a3a.full_name, @b2.full_name]
  306. @gem_names = gem_names.sort.join("\n")
  307. @source_index = Gem::SourceIndex.new
  308. @source_index.add_spec @a1
  309. @source_index.add_spec @a2
  310. @source_index.add_spec @a3a
  311. @source_index.add_spec @a_evil9
  312. @source_index.add_spec @c1_2
  313. @source_index.add_spec @a2_pre if prerelease
  314. Gem::RemoteFetcher.fetcher = @fetcher
  315. end
  316. def util_setup_spec_fetcher(*specs)
  317. specs = Hash[*specs.map { |spec| [spec.full_name, spec] }.flatten]
  318. si = Gem::SourceIndex.new specs
  319. spec_fetcher = Gem::SpecFetcher.fetcher
  320. spec_fetcher.specs[@uri] = []
  321. si.gems.sort_by { |_, spec| spec }.each do |_, spec|
  322. spec_tuple = [spec.name, spec.version, spec.original_platform]
  323. spec_fetcher.specs[@uri] << spec_tuple
  324. end
  325. spec_fetcher.latest_specs[@uri] = []
  326. si.latest_specs.sort.each do |spec|
  327. spec_tuple = [spec.name, spec.version, spec.original_platform]
  328. spec_fetcher.latest_specs[@uri] << spec_tuple
  329. end
  330. spec_fetcher.prerelease_specs[@uri] = []
  331. si.prerelease_specs.sort.each do |spec|
  332. spec_tuple = [spec.name, spec.version, spec.original_platform]
  333. spec_fetcher.prerelease_specs[@uri] << spec_tuple
  334. end
  335. (si.gems.merge si.prerelease_gems).sort_by { |_,spec| spec }.each do |_, spec|
  336. path = "#{@gem_repo}quick/Marshal.#{Gem.marshal_version}/#{spec.original_name}.gemspec.rz"
  337. data = Marshal.dump spec
  338. data_deflate = Zlib::Deflate.deflate data
  339. @fetcher.data[path] = data_deflate
  340. end
  341. si
  342. end
  343. def util_zip(data)
  344. Zlib::Deflate.deflate data
  345. end
  346. def self.win_platform?
  347. Gem.win_platform?
  348. end
  349. def win_platform?
  350. Gem.win_platform?
  351. end
  352. # Returns whether or not we're on a version of Ruby built with VC++ (or
  353. # Borland) versus Cygwin, Mingw, etc.
  354. #
  355. def self.vc_windows?
  356. RUBY_PLATFORM.match('mswin')
  357. end
  358. # Returns whether or not we're on a version of Ruby built with VC++ (or
  359. # Borland) versus Cygwin, Mingw, etc.
  360. #
  361. def vc_windows?
  362. RUBY_PLATFORM.match('mswin')
  363. end
  364. # Returns the make command for the current platform. For versions of Ruby
  365. # built on MS Windows with VC++ or Borland it will return 'nmake'. On all
  366. # other platforms, including Cygwin, it will return 'make'.
  367. #
  368. def self.make_command
  369. ENV["make"] || (vc_windows? ? 'nmake' : 'make')
  370. end
  371. # Returns the make command for the current platform. For versions of Ruby
  372. # built on MS Windows with VC++ or Borland it will return 'nmake'. On all
  373. # other platforms, including Cygwin, it will return 'make'.
  374. #
  375. def make_command
  376. ENV["make"] || (vc_windows? ? 'nmake' : 'make')
  377. end
  378. # Returns whether or not the nmake command could be found.
  379. #
  380. def nmake_found?
  381. system('nmake /? 1>NUL 2>&1')
  382. end
  383. # NOTE Allow tests to use a random (but controlled) port number instead of
  384. # a hardcoded one. This helps CI tools when running parallels builds on
  385. # the same builder slave.
  386. def self.process_based_port
  387. @@process_based_port ||= 8000 + $$ % 1000
  388. end
  389. def process_based_port
  390. self.class.process_based_port
  391. end
  392. def build_rake_in
  393. gem_ruby = Gem.ruby
  394. ruby = @@ruby
  395. Gem.module_eval {@ruby = ruby}
  396. env_rake = ENV["rake"]
  397. ENV["rake"] = @@rake
  398. yield @@rake
  399. ensure
  400. Gem.module_eval {@ruby = gem_ruby}
  401. if env_rake
  402. ENV["rake"] = env_rake
  403. else
  404. ENV.delete("rake")
  405. end
  406. end
  407. def self.rubybin
  408. if ruby = ENV["RUBY"]
  409. return ruby
  410. end
  411. ruby = "ruby"
  412. rubyexe = ruby+".exe"
  413. 3.times do
  414. if File.exist? ruby and File.executable? ruby and !File.directory? ruby
  415. return File.expand_path(ruby)
  416. end
  417. if File.exist? rubyexe and File.executable? rubyexe
  418. return File.expand_path(rubyexe)
  419. end
  420. ruby = File.join("..", ruby)
  421. end
  422. begin
  423. require "rbconfig"
  424. File.join(
  425. RbConfig::CONFIG["bindir"],
  426. RbConfig::CONFIG["ruby_install_name"] + RbConfig::CONFIG["EXEEXT"]
  427. )
  428. rescue LoadError
  429. "ruby"
  430. end
  431. end
  432. @@ruby = rubybin
  433. env_rake = ENV['rake']
  434. ruby19_rake = File.expand_path("../../../bin/rake", __FILE__)
  435. @@rake = if env_rake then
  436. ENV["rake"]
  437. elsif File.exist? ruby19_rake then
  438. @@ruby + " " + ruby19_rake
  439. else
  440. 'rake'
  441. end
  442. ##
  443. # Construct a new Gem::Dependency.
  444. def dep name, *requirements
  445. Gem::Dependency.new name, *requirements
  446. end
  447. ##
  448. # Construct a new Gem::Requirement.
  449. def req *requirements
  450. return requirements.first if Gem::Requirement === requirements.first
  451. Gem::Requirement.create requirements
  452. end
  453. ##
  454. # Construct a new Gem::Specification.
  455. def spec name, version, &block
  456. Gem::Specification.new name, v(version), &block
  457. end
  458. ##
  459. # Construct a new Gem::Version.
  460. def v string
  461. Gem::Version.create string
  462. end
  463. end