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

/test/jruby/test_helper.rb

http://github.com/jruby/jruby
Ruby | 149 lines | 131 code | 16 blank | 2 comment | 11 complexity | 79ccc3bb43153d7d3799fe9973bb37f7 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, GPL-2.0, JSON, LGPL-2.1
  1. require 'rbconfig'
  2. require 'jruby' if defined?(JRUBY_VERSION)
  3. require 'tempfile'
  4. module TestHelper
  5. # TODO: Consider how this should work if we have --windows or similiar
  6. WINDOWS = RbConfig::CONFIG['host_os'] =~ /Windows|mswin/
  7. SEPARATOR = WINDOWS ? '\\' : '/'
  8. # using the classloader setup to determine whether it runs inside
  9. # ScriptingContainer or via commandline
  10. if defined?(JRUBY_VERSION)
  11. IS_COMMAND_LINE_EXECUTION = JRuby.runtime.jruby_class_loader == java.lang.Thread.current_thread.context_class_loader
  12. else
  13. IS_COMMAND_LINE_EXECUTION = true
  14. end
  15. IS_JAR_EXECUTION = RbConfig::CONFIG['bindir'].match( /!\//) || RbConfig::CONFIG['bindir'].match( /:\//)
  16. RUBY = if IS_JAR_EXECUTION
  17. exe = 'java'
  18. exe += RbConfig::CONFIG['EXEEXT'] if RbConfig::CONFIG['EXEEXT']
  19. # assume the parent CL of jruby-classloader has a getUrls method
  20. urls = JRuby.runtime.getJRubyClassLoader.parent.get_ur_ls.collect do |u|
  21. u.path
  22. end
  23. urls.unshift '.'
  24. exe += " -cp #{urls.join(File::PATH_SEPARATOR)} org.jruby.Main"
  25. exe
  26. else
  27. exe = '"' + File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['RUBY_INSTALL_NAME'])
  28. exe += RbConfig::CONFIG['EXEEXT'] if RbConfig::CONFIG['EXEEXT']
  29. exe += '"'
  30. exe
  31. end
  32. if (WINDOWS)
  33. RUBY.gsub!('/', '\\')
  34. DEVNULL = 'NUL:'
  35. else
  36. DEVNULL = '/dev/null'
  37. end
  38. if defined? JRUBY_VERSION
  39. arch = java.lang.System.getProperty('sun.arch.data.model')
  40. WINDOWS_JVM_64 = (WINDOWS && arch == '64')
  41. end
  42. IBM_JVM = RbConfig::CONFIG['host_vendor'] =~ /IBM Corporation/
  43. JAVA_8 = ENV_JAVA['java.specification.version'] >= '1.8'
  44. def q
  45. WINDOWS ? '"' : '\''
  46. end
  47. def interpreter( options = {} )
  48. options = options.collect { |k,v| "-D#{k}=\"#{v}\"" }
  49. if RUBY =~ /-cp /
  50. RUBY.sub(/-cp [.]/, "-cp .#{File::PATH_SEPARATOR}#{ENV['CLASSPATH']}").sub(/-cp /, options.join(' ') + ' -cp ')
  51. else
  52. RUBY
  53. end
  54. end
  55. def jruby(*args)
  56. options = []
  57. if args.last.is_a? Hash
  58. options = args.last
  59. args = args[0..-2]
  60. end
  61. options.each { |k,v| args.unshift "-J-D#{k}=\"#{v}\"" } unless RUBY =~ /-cp /
  62. with_jruby_shell_spawning { sh "#{interpreter(options)} #{args.join(' ')}" }
  63. end
  64. def jruby_with_pipe(pipe, *args)
  65. options = []
  66. if args.last.is_a? Hash
  67. options = args.last
  68. args = args[0..-2]
  69. end
  70. options.each { |k,v| args.unshift "-J-D#{k}=\"#{v}\"" } unless RUBY =~ /-cp /
  71. with_jruby_shell_spawning { sh "#{pipe} | #{interpreter(options)} #{args.join(' ')}" }
  72. end
  73. def sh(cmd)
  74. puts cmd if $DEBUG
  75. return `#{cmd}`
  76. end
  77. private :sh
  78. def with_temp_script(script, filename="test-script")
  79. Tempfile.open([filename, ".rb"]) do |f|
  80. begin
  81. # we ignore errors writing to the tempfile to ensure the test tries to run
  82. f.syswrite(script) rescue 1
  83. return yield f
  84. ensure
  85. f.close!
  86. end
  87. end
  88. end
  89. def with_jruby_shell_spawning
  90. prev_in_process = JRuby.runtime.instance_config.run_ruby_in_process
  91. JRuby.runtime.instance_config.run_ruby_in_process = false
  92. yield
  93. ensure
  94. JRuby.runtime.instance_config.run_ruby_in_process = prev_in_process
  95. end
  96. def quiet(&block)
  97. io = [STDOUT.dup, STDERR.dup]
  98. STDOUT.reopen DEVNULL
  99. STDERR.reopen DEVNULL
  100. block.call
  101. ensure
  102. STDOUT.reopen io.first
  103. STDERR.reopen io.last
  104. end
  105. def run_in_sub_runtime(script)
  106. container = org.jruby.embed.ScriptingContainer.new(org.jruby.embed.LocalContextScope::SINGLETHREAD)
  107. container.setLoadPaths(['.'])
  108. container.runScriptlet("require 'java'")
  109. container.runScriptlet(script)
  110. end
  111. def assert_in_sub_runtime(script)
  112. assert run_in_sub_runtime(script)
  113. end
  114. def self.included(base)
  115. if defined? Test::Unit::TestCase
  116. if base < Test::Unit::TestCase
  117. Test::Unit::TestCase.class_eval do
  118. unless method_defined?(:skip)
  119. if method_defined?(:omit)
  120. alias skip omit
  121. else
  122. def skip(msg = nil)
  123. warn "Skipped: #{caller[0]} #{msg}"
  124. end
  125. end
  126. end
  127. end
  128. end
  129. end
  130. end
  131. end