PageRenderTime 22ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/lib/ruby/shared/rubygems/test_case.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 870 lines | 566 code | 162 blank | 142 comment | 31 complexity | 43afa922870972df7e5c36b1f8f61f18 MD5 | raw file
  1. at_exit { $SAFE = 1 }
  2. if defined? Gem::QuickLoader
  3. Gem::QuickLoader.load_full_rubygems_library
  4. else
  5. require 'rubygems'
  6. end
  7. begin
  8. gem 'minitest'
  9. rescue Gem::LoadError
  10. end
  11. require "rubygems/deprecate"
  12. require 'minitest/autorun'
  13. require 'fileutils'
  14. require 'tmpdir'
  15. require 'uri'
  16. require 'rubygems/package'
  17. require 'rubygems/test_utilities'
  18. require 'pp'
  19. require 'zlib'
  20. require 'pathname'
  21. Gem.load_yaml
  22. require 'rubygems/mock_gem_ui'
  23. module Gem
  24. ##
  25. # Allows setting the gem path searcher. This method is available when
  26. # requiring 'rubygems/test_case'
  27. def self.searcher=(searcher)
  28. @searcher = searcher
  29. end
  30. ##
  31. # Allows setting the default SourceIndex. This method is available when
  32. # requiring 'rubygems/test_case'
  33. def self.source_index=(si)
  34. raise "This method is not supported"
  35. Gem::Specification.reset if si # HACK
  36. @@source_index = si
  37. end
  38. ##
  39. # Allows toggling Windows behavior. This method is available when requiring
  40. # 'rubygems/test_case'
  41. def self.win_platform=(val)
  42. @@win_platform = val
  43. end
  44. ##
  45. # Allows setting path to ruby. This method is available when requiring
  46. # 'rubygems/test_case'
  47. def self.ruby= ruby
  48. @ruby = ruby
  49. end
  50. ##
  51. # When rubygems/test_case is required the default user interaction is a
  52. # MockGemUi.
  53. module DefaultUserInteraction
  54. @ui = Gem::MockGemUi.new
  55. end
  56. end
  57. ##
  58. # RubyGemTestCase provides a variety of methods for testing rubygems and
  59. # gem-related behavior in a sandbox. Through RubyGemTestCase you can install
  60. # and uninstall gems, fetch remote gems through a stub fetcher and be assured
  61. # your normal set of gems is not affected.
  62. #
  63. # Tests are always run at a safe level of 1.
  64. class Gem::TestCase < MiniTest::Unit::TestCase
  65. # TODO: move to minitest
  66. def assert_path_exists path, msg = nil
  67. msg = message(msg) { "Expected path '#{path}' to exist" }
  68. assert File.exist?(path), msg
  69. end
  70. # TODO: move to minitest
  71. def refute_path_exists path, msg = nil
  72. msg = message(msg) { "Expected path '#{path}' to not exist" }
  73. refute File.exist?(path), msg
  74. end
  75. include Gem::DefaultUserInteraction
  76. undef_method :default_test if instance_methods.include? 'default_test' or
  77. instance_methods.include? :default_test
  78. @@project_dir = Dir.pwd unless defined?(@@project_dir)
  79. ##
  80. # #setup prepares a sandboxed location to install gems. All installs are
  81. # directed to a temporary directory. All install plugins are removed.
  82. #
  83. # If the +RUBY+ environment variable is set the given path is used for
  84. # Gem::ruby. The local platform is set to <tt>i386-mswin32</tt> for Windows
  85. # or <tt>i686-darwin8.10.1</tt> otherwise.
  86. #
  87. # If the +KEEP_FILES+ environment variable is set the files will not be
  88. # removed from <tt>/tmp/test_rubygems_#{$$}.#{Time.now.to_i}</tt>.
  89. def setup
  90. super
  91. @orig_gem_home = ENV['GEM_HOME']
  92. @orig_gem_path = ENV['GEM_PATH']
  93. @current_dir = Dir.pwd
  94. @ui = Gem::MockGemUi.new
  95. tmpdir = nil
  96. Dir.chdir Dir.tmpdir do tmpdir = Dir.pwd end # HACK OSX /private/tmp
  97. if ENV['KEEP_FILES'] then
  98. @tempdir = File.join(tmpdir, "test_rubygems_#{$$}.#{Time.now.to_i}")
  99. else
  100. @tempdir = File.join(tmpdir, "test_rubygems_#{$$}")
  101. end
  102. @tempdir.untaint
  103. @gemhome = File.join @tempdir, 'gemhome'
  104. @userhome = File.join @tempdir, 'userhome'
  105. @orig_ruby = if ruby = ENV['RUBY'] then
  106. Gem.class_eval { ruby, @ruby = @ruby, ruby }
  107. ruby
  108. end
  109. Gem.ensure_gem_subdirectories @gemhome
  110. @orig_LOAD_PATH = $LOAD_PATH.dup
  111. $LOAD_PATH.map! { |s| File.expand_path s }
  112. Dir.chdir @tempdir
  113. @orig_ENV_HOME = ENV['HOME']
  114. ENV['HOME'] = @userhome
  115. Gem.instance_variable_set :@user_home, nil
  116. FileUtils.mkdir_p @gemhome
  117. FileUtils.mkdir_p @userhome
  118. Gem.use_paths(@gemhome)
  119. Gem.loaded_specs.clear
  120. Gem.unresolved_deps.clear
  121. Gem.configuration.verbose = true
  122. Gem.configuration.update_sources = true
  123. @gem_repo = "http://gems.example.com/"
  124. @uri = URI.parse @gem_repo
  125. Gem.sources.replace [@gem_repo]
  126. Gem.searcher = nil
  127. Gem::SpecFetcher.fetcher = nil
  128. @orig_BASERUBY = Gem::ConfigMap[:BASERUBY]
  129. Gem::ConfigMap[:BASERUBY] = Gem::ConfigMap[:ruby_install_name]
  130. @orig_arch = Gem::ConfigMap[:arch]
  131. if win_platform?
  132. util_set_arch 'i386-mswin32'
  133. else
  134. util_set_arch 'i686-darwin8.10.1'
  135. end
  136. @marshal_version = "#{Marshal::MAJOR_VERSION}.#{Marshal::MINOR_VERSION}"
  137. # TODO: move to installer test cases
  138. Gem.post_build_hooks.clear
  139. Gem.post_install_hooks.clear
  140. Gem.post_uninstall_hooks.clear
  141. Gem.pre_install_hooks.clear
  142. Gem.pre_uninstall_hooks.clear
  143. # TODO: move to installer test cases
  144. Gem.post_build do |installer|
  145. @post_build_hook_arg = installer
  146. true
  147. end
  148. Gem.post_install do |installer|
  149. @post_install_hook_arg = installer
  150. end
  151. Gem.post_uninstall do |uninstaller|
  152. @post_uninstall_hook_arg = uninstaller
  153. end
  154. Gem.pre_install do |installer|
  155. @pre_install_hook_arg = installer
  156. true
  157. end
  158. Gem.pre_uninstall do |uninstaller|
  159. @pre_uninstall_hook_arg = uninstaller
  160. end
  161. end
  162. ##
  163. # #teardown restores the process to its original state and removes the
  164. # tempdir unless the +KEEP_FILES+ environment variable was set.
  165. def teardown
  166. $LOAD_PATH.replace @orig_LOAD_PATH
  167. Gem::ConfigMap[:BASERUBY] = @orig_BASERUBY
  168. Gem::ConfigMap[:arch] = @orig_arch
  169. if defined? Gem::RemoteFetcher then
  170. Gem::RemoteFetcher.fetcher = nil
  171. end
  172. Dir.chdir @current_dir
  173. FileUtils.rm_rf @tempdir unless ENV['KEEP_FILES']
  174. ENV['GEM_HOME'] = @orig_gem_home
  175. ENV['GEM_PATH'] = @orig_gem_path
  176. _ = @orig_ruby
  177. Gem.class_eval { @ruby = _ } if _
  178. if @orig_ENV_HOME then
  179. ENV['HOME'] = @orig_ENV_HOME
  180. else
  181. ENV.delete 'HOME'
  182. end
  183. end
  184. ##
  185. # Builds and installs the Gem::Specification +spec+
  186. def install_gem spec, options = {}
  187. require 'rubygems/installer'
  188. use_ui Gem::MockGemUi.new do
  189. Dir.chdir @tempdir do
  190. Gem::Builder.new(spec).build
  191. end
  192. end
  193. gem = File.join(@tempdir, File.basename(spec.cache_file)).untaint
  194. Gem::Installer.new(gem, options.merge({:wrappers => true})).install
  195. end
  196. ##
  197. # Builds and installs the Gem::Specification +spec+ into the user dir
  198. def install_gem_user spec
  199. install_gem spec, :user_install => true
  200. end
  201. ##
  202. # Uninstalls the Gem::Specification +spec+
  203. def uninstall_gem spec
  204. require 'rubygems/uninstaller'
  205. Gem::Uninstaller.new(spec.name,
  206. :executables => true, :user_install => true).uninstall
  207. end
  208. ##
  209. # creates a temporary directory with hax
  210. def create_tmpdir
  211. tmpdir = nil
  212. Dir.chdir Dir.tmpdir do tmpdir = Dir.pwd end # HACK OSX /private/tmp
  213. tmpdir = File.join tmpdir, "test_rubygems_#{$$}"
  214. FileUtils.mkdir_p tmpdir
  215. return tmpdir
  216. end
  217. ##
  218. # Enables pretty-print for all tests
  219. def mu_pp(obj)
  220. s = ''
  221. s = PP.pp obj, s
  222. s = s.force_encoding(Encoding.default_external) if defined? Encoding
  223. s.chomp
  224. end
  225. ##
  226. # Reads a Marshal file at +path+
  227. def read_cache(path)
  228. open path.dup.untaint, 'rb' do |io|
  229. Marshal.load io.read
  230. end
  231. end
  232. ##
  233. # Reads a binary file at +path+
  234. def read_binary(path)
  235. Gem.read_binary path
  236. end
  237. ##
  238. # Writes a binary file to +path+ which is relative to +@gemhome+
  239. def write_file(path)
  240. path = File.join @gemhome, path unless Pathname.new(path).absolute?
  241. dir = File.dirname path
  242. FileUtils.mkdir_p dir
  243. open path, 'wb' do |io|
  244. yield io if block_given?
  245. end
  246. path
  247. end
  248. def all_spec_names
  249. Gem::Specification.map(&:full_name)
  250. end
  251. ##
  252. # Creates a Gem::Specification with a minimum of extra work. +name+ and
  253. # +version+ are the gem's name and version, platform, author, email,
  254. # homepage, summary and description are defaulted. The specification is
  255. # yielded for customization.
  256. #
  257. # The gem is added to the installed gems in +@gemhome+ and to the current
  258. # source_index.
  259. #
  260. # Use this with #write_file to build an installed gem.
  261. def quick_gem(name, version='2')
  262. require 'rubygems/specification'
  263. spec = Gem::Specification.new do |s|
  264. s.platform = Gem::Platform::RUBY
  265. s.name = name
  266. s.version = version
  267. s.author = 'A User'
  268. s.email = 'example@example.com'
  269. s.homepage = 'http://example.com'
  270. s.summary = "this is a summary"
  271. s.description = "This is a test description"
  272. yield(s) if block_given?
  273. end
  274. Gem::Specification.map # HACK: force specs to (re-)load before we write
  275. written_path = write_file spec.spec_file do |io|
  276. io.write spec.to_ruby_for_cache
  277. end
  278. spec.loaded_from = spec.loaded_from = written_path
  279. Gem::Specification.add_spec spec.for_cache
  280. return spec
  281. end
  282. def quick_spec name, version = '2'
  283. # TODO: deprecate
  284. require 'rubygems/specification'
  285. spec = Gem::Specification.new do |s|
  286. s.platform = Gem::Platform::RUBY
  287. s.name = name
  288. s.version = version
  289. s.author = 'A User'
  290. s.email = 'example@example.com'
  291. s.homepage = 'http://example.com'
  292. s.summary = "this is a summary"
  293. s.description = "This is a test description"
  294. yield(s) if block_given?
  295. end
  296. spec.loaded_from = spec.spec_file
  297. Gem::Specification.add_spec spec
  298. return spec
  299. end
  300. ##
  301. # Builds a gem from +spec+ and places it in <tt>File.join @gemhome,
  302. # 'cache'</tt>. Automatically creates files based on +spec.files+
  303. def util_build_gem(spec)
  304. dir = spec.gem_dir
  305. FileUtils.mkdir_p dir
  306. Dir.chdir dir do
  307. spec.files.each do |file|
  308. next if File.exist? file
  309. FileUtils.mkdir_p File.dirname(file)
  310. File.open file, 'w' do |fp| fp.puts "# #{file}" end
  311. end
  312. use_ui Gem::MockGemUi.new do
  313. Gem::Builder.new(spec).build
  314. end
  315. cache = spec.cache_file
  316. FileUtils.mv File.basename(cache), cache
  317. end
  318. end
  319. ##
  320. # Removes all installed gems from +@gemhome+.
  321. def util_clear_gems
  322. FileUtils.rm_rf File.join(@gemhome, "gems") # TODO: use Gem::Dirs
  323. FileUtils.rm_rf File.join(@gemhome, "specifications")
  324. Gem::Specification.reset
  325. end
  326. ##
  327. # Install the provided specs
  328. def install_specs(*specs)
  329. Gem::Specification.add_specs(*specs)
  330. Gem.searcher = nil
  331. end
  332. ##
  333. # Create a new spec (or gem if passed an array of files) and set it
  334. # up properly. Use this instead of util_spec and util_gem.
  335. def new_spec name, version, deps = nil, *files
  336. require 'rubygems/specification'
  337. spec = Gem::Specification.new do |s|
  338. s.platform = Gem::Platform::RUBY
  339. s.name = name
  340. s.version = version
  341. s.author = 'A User'
  342. s.email = 'example@example.com'
  343. s.homepage = 'http://example.com'
  344. s.summary = "this is a summary"
  345. s.description = "This is a test description"
  346. Array(deps).each do |n, req|
  347. s.add_dependency n, (req || '>= 0')
  348. end
  349. s.files.push(*files) unless files.empty?
  350. yield s if block_given?
  351. end
  352. spec.loaded_from = spec.spec_file
  353. unless files.empty? then
  354. write_file spec.spec_file do |io|
  355. io.write spec.to_ruby_for_cache
  356. end
  357. util_build_gem spec
  358. cache_file = File.join @tempdir, 'gems', "#{spec.full_name}.gem"
  359. FileUtils.mkdir_p File.dirname cache_file
  360. FileUtils.mv spec.cache_file, cache_file
  361. FileUtils.rm spec.spec_file
  362. end
  363. spec
  364. end
  365. ##
  366. # Creates a spec with +name+, +version+ and +deps+.
  367. def util_spec(name, version, deps = nil, &block)
  368. # TODO: deprecate
  369. raise "deps or block, not both" if deps and block
  370. if deps then
  371. block = proc do |s|
  372. # Since Hash#each is unordered in 1.8, sort
  373. # the keys and iterate that way so the tests are
  374. # deteriminstic on all implementations.
  375. deps.keys.sort.each do |n|
  376. s.add_dependency n, (deps[n] || '>= 0')
  377. end
  378. end
  379. end
  380. quick_spec(name, version, &block)
  381. end
  382. ##
  383. # Creates a gem with +name+, +version+ and +deps+. The specification will
  384. # be yielded before gem creation for customization. The gem will be placed
  385. # in <tt>File.join @tempdir, 'gems'</tt>. The specification and .gem file
  386. # location are returned.
  387. def util_gem(name, version, deps = nil, &block)
  388. # TODO: deprecate
  389. raise "deps or block, not both" if deps and block
  390. if deps then
  391. block = proc do |s|
  392. # Since Hash#each is unordered in 1.8, sort
  393. # the keys and iterate that way so the tests are
  394. # deteriminstic on all implementations.
  395. deps.keys.sort.each do |n|
  396. s.add_dependency n, (deps[n] || '>= 0')
  397. end
  398. end
  399. end
  400. spec = quick_gem(name, version, &block)
  401. util_build_gem spec
  402. cache_file = File.join @tempdir, 'gems', "#{spec.original_name}.gem"
  403. FileUtils.mkdir_p File.dirname cache_file
  404. FileUtils.mv spec.cache_file, cache_file
  405. FileUtils.rm spec.spec_file
  406. spec.loaded_from = nil
  407. [spec, cache_file]
  408. end
  409. ##
  410. # Gzips +data+.
  411. def util_gzip(data)
  412. out = StringIO.new
  413. Zlib::GzipWriter.wrap out do |io|
  414. io.write data
  415. end
  416. out.string
  417. end
  418. ##
  419. # Creates several default gems which all have a lib/code.rb file. The gems
  420. # are not installed but are available in the cache dir.
  421. #
  422. # +@a1+:: gem a version 1, this is the best-described gem.
  423. # +@a2+:: gem a version 2
  424. # +@a3a:: gem a version 3.a
  425. # +@a_evil9+:: gem a_evil version 9, use this to ensure similarly-named gems
  426. # don't collide with a.
  427. # +@b2+:: gem b version 2
  428. # +@c1_2+:: gem c version 1.2
  429. # +@pl1+:: gem pl version 1, this gem has a legacy platform of i386-linux.
  430. #
  431. # Additional +prerelease+ gems may also be created:
  432. #
  433. # +@a2_pre+:: gem a version 2.a
  434. # TODO: nuke this and fix tests. this should speed up a lot
  435. def util_make_gems(prerelease = false)
  436. @a1 = quick_gem 'a', '1' do |s|
  437. s.files = %w[lib/code.rb]
  438. s.require_paths = %w[lib]
  439. s.date = Gem::Specification::TODAY - 86400
  440. s.homepage = 'http://a.example.com'
  441. s.email = %w[example@example.com example2@example.com]
  442. s.authors = %w[Example Example2]
  443. s.description = <<-DESC
  444. 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.
  445. Also, a list:
  446. * An entry that\'s actually kind of sort
  447. * 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.
  448. DESC
  449. end
  450. init = proc do |s|
  451. s.files = %w[lib/code.rb]
  452. s.require_paths = %w[lib]
  453. end
  454. @a2 = quick_gem('a', '2', &init)
  455. @a3a = quick_gem('a', '3.a', &init)
  456. @a_evil9 = quick_gem('a_evil', '9', &init)
  457. @b2 = quick_gem('b', '2', &init)
  458. @c1_2 = quick_gem('c', '1.2', &init)
  459. @pl1 = quick_gem 'pl', '1' do |s| # l for legacy
  460. s.files = %w[lib/code.rb]
  461. s.require_paths = %w[lib]
  462. s.platform = Gem::Platform.new 'i386-linux'
  463. s.instance_variable_set :@original_platform, 'i386-linux'
  464. end
  465. if prerelease
  466. @a2_pre = quick_gem('a', '2.a', &init)
  467. write_file File.join(*%W[gems #{@a2_pre.original_name} lib code.rb])
  468. util_build_gem @a2_pre
  469. end
  470. write_file File.join(*%W[gems #{@a1.original_name} lib code.rb])
  471. write_file File.join(*%W[gems #{@a2.original_name} lib code.rb])
  472. write_file File.join(*%W[gems #{@a3a.original_name} lib code.rb])
  473. write_file File.join(*%W[gems #{@b2.original_name} lib code.rb])
  474. write_file File.join(*%W[gems #{@c1_2.original_name} lib code.rb])
  475. write_file File.join(*%W[gems #{@pl1.original_name} lib code.rb])
  476. [@a1, @a2, @a3a, @a_evil9, @b2, @c1_2, @pl1].each do |spec|
  477. util_build_gem spec
  478. end
  479. FileUtils.rm_r File.join(@gemhome, "gems", @pl1.original_name)
  480. end
  481. ##
  482. # Set the platform to +arch+
  483. def util_set_arch(arch)
  484. Gem::ConfigMap[:arch] = arch
  485. platform = Gem::Platform.new arch
  486. Gem.instance_variable_set :@platforms, nil
  487. Gem::Platform.instance_variable_set :@local, nil
  488. platform
  489. end
  490. ##
  491. # Sets up a fake fetcher using the gems from #util_make_gems. Optionally
  492. # additional +prerelease+ gems may be included.
  493. #
  494. # Gems created by this method may be fetched using Gem::RemoteFetcher.
  495. def util_setup_fake_fetcher(prerelease = false)
  496. require 'zlib'
  497. require 'socket'
  498. require 'rubygems/remote_fetcher'
  499. @fetcher = Gem::FakeFetcher.new
  500. util_make_gems(prerelease)
  501. Gem::Specification.reset
  502. @all_gems = [@a1, @a2, @a3a, @a_evil9, @b2, @c1_2].sort
  503. @all_gem_names = @all_gems.map { |gem| gem.full_name }
  504. gem_names = [@a1.full_name, @a2.full_name, @a3a.full_name, @b2.full_name]
  505. @gem_names = gem_names.sort.join("\n")
  506. Gem::RemoteFetcher.fetcher = @fetcher
  507. end
  508. ##
  509. # Sets up Gem::SpecFetcher to return information from the gems in +specs+.
  510. # Best used with +@all_gems+ from #util_setup_fake_fetcher.
  511. def util_setup_spec_fetcher(*specs)
  512. specs -= Gem::Specification._all
  513. Gem::Specification.add_specs(*specs)
  514. spec_fetcher = Gem::SpecFetcher.fetcher
  515. prerelease, _ = Gem::Specification.partition { |spec|
  516. spec.version.prerelease?
  517. }
  518. spec_fetcher.specs[@uri] = []
  519. Gem::Specification.each do |spec|
  520. spec_tuple = [spec.name, spec.version, spec.original_platform]
  521. spec_fetcher.specs[@uri] << spec_tuple
  522. end
  523. spec_fetcher.latest_specs[@uri] = []
  524. Gem::Specification.latest_specs.each do |spec|
  525. spec_tuple = [spec.name, spec.version, spec.original_platform]
  526. spec_fetcher.latest_specs[@uri] << spec_tuple
  527. end
  528. spec_fetcher.prerelease_specs[@uri] = []
  529. prerelease.each do |spec|
  530. spec_tuple = [spec.name, spec.version, spec.original_platform]
  531. spec_fetcher.prerelease_specs[@uri] << spec_tuple
  532. end
  533. v = Gem.marshal_version
  534. Gem::Specification.each do |spec|
  535. path = "#{@gem_repo}quick/Marshal.#{v}/#{spec.original_name}.gemspec.rz"
  536. data = Marshal.dump spec
  537. data_deflate = Zlib::Deflate.deflate data
  538. @fetcher.data[path] = data_deflate
  539. end unless Gem::RemoteFetcher === @fetcher # HACK for test_download_to_cache
  540. nil # force errors
  541. end
  542. ##
  543. # Deflates +data+
  544. def util_zip(data)
  545. Zlib::Deflate.deflate data
  546. end
  547. ##
  548. # Is this test being run on a Windows platform?
  549. def self.win_platform?
  550. Gem.win_platform?
  551. end
  552. ##
  553. # Is this test being run on a Windows platform?
  554. def win_platform?
  555. Gem.win_platform?
  556. end
  557. ##
  558. # Returns whether or not we're on a version of Ruby built with VC++ (or
  559. # Borland) versus Cygwin, Mingw, etc.
  560. def self.vc_windows?
  561. RUBY_PLATFORM.match('mswin')
  562. end
  563. ##
  564. # Returns whether or not we're on a version of Ruby built with VC++ (or
  565. # Borland) versus Cygwin, Mingw, etc.
  566. def vc_windows?
  567. RUBY_PLATFORM.match('mswin')
  568. end
  569. ##
  570. # Returns the make command for the current platform. For versions of Ruby
  571. # built on MS Windows with VC++ or Borland it will return 'nmake'. On all
  572. # other platforms, including Cygwin, it will return 'make'.
  573. def self.make_command
  574. ENV["make"] || (vc_windows? ? 'nmake' : 'make')
  575. end
  576. ##
  577. # Returns the make command for the current platform. For versions of Ruby
  578. # built on MS Windows with VC++ or Borland it will return 'nmake'. On all
  579. # other platforms, including Cygwin, it will return 'make'.
  580. def make_command
  581. ENV["make"] || (vc_windows? ? 'nmake' : 'make')
  582. end
  583. ##
  584. # Returns whether or not the nmake command could be found.
  585. def nmake_found?
  586. system('nmake /? 1>NUL 2>&1')
  587. end
  588. ##
  589. # Allows tests to use a random (but controlled) port number instead of
  590. # a hardcoded one. This helps CI tools when running parallels builds on
  591. # the same builder slave.
  592. def self.process_based_port
  593. @@process_based_port ||= 8000 + $$ % 1000
  594. end
  595. ##
  596. # See ::process_based_port
  597. def process_based_port
  598. self.class.process_based_port
  599. end
  600. ##
  601. # Allows the proper version of +rake+ to be used for the test.
  602. def build_rake_in
  603. gem_ruby = Gem.ruby
  604. Gem.ruby = @@ruby
  605. env_rake = ENV["rake"]
  606. ENV["rake"] = @@rake
  607. yield @@rake
  608. ensure
  609. Gem.ruby = gem_ruby
  610. if env_rake
  611. ENV["rake"] = env_rake
  612. else
  613. ENV.delete("rake")
  614. end
  615. end
  616. ##
  617. # Finds the path to the ruby executable
  618. def self.rubybin
  619. ruby = ENV["RUBY"]
  620. return ruby if ruby
  621. ruby = "ruby"
  622. rubyexe = "#{ruby}.exe"
  623. 3.times do
  624. if File.exist? ruby and File.executable? ruby and !File.directory? ruby
  625. return File.expand_path(ruby)
  626. end
  627. if File.exist? rubyexe and File.executable? rubyexe
  628. return File.expand_path(rubyexe)
  629. end
  630. ruby = File.join("..", ruby)
  631. end
  632. begin
  633. require "rbconfig"
  634. File.join(RbConfig::CONFIG["bindir"],
  635. RbConfig::CONFIG["ruby_install_name"] +
  636. RbConfig::CONFIG["EXEEXT"])
  637. rescue LoadError
  638. "ruby"
  639. end
  640. end
  641. @@ruby = rubybin
  642. env_rake = ENV['rake']
  643. ruby19_rake = File.expand_path("bin/rake", @@project_dir)
  644. @@rake = if env_rake then
  645. ENV["rake"]
  646. elsif File.exist? ruby19_rake then
  647. @@ruby + " " + ruby19_rake
  648. else
  649. 'rake'
  650. end
  651. ##
  652. # Construct a new Gem::Dependency.
  653. def dep name, *requirements
  654. Gem::Dependency.new name, *requirements
  655. end
  656. ##
  657. # Constructs a new Gem::Requirement.
  658. def req *requirements
  659. return requirements.first if Gem::Requirement === requirements.first
  660. Gem::Requirement.create requirements
  661. end
  662. ##
  663. # Constructs a new Gem::Specification.
  664. def spec name, version, &block
  665. Gem::Specification.new name, v(version), &block
  666. end
  667. ##
  668. # Construct a new Gem::Version.
  669. def v string
  670. Gem::Version.create string
  671. end
  672. end