Skip to content

Commit

Permalink
[rubygems/rubygems] Refactor ruby command line building for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
deivid-rodriguez authored and hsbt committed May 8, 2020
1 parent 1635394 commit 676d816
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 17 deletions.
4 changes: 4 additions & 0 deletions lib/rubygems/test_case.rb
Expand Up @@ -1260,6 +1260,10 @@ def self.rubybin
end
end

def ruby_with_rubygems_in_load_path
[Gem.ruby, "-I", File.expand_path("..", __dir__)]
end

def with_clean_path_to_ruby
orig_ruby = Gem.ruby

Expand Down
7 changes: 3 additions & 4 deletions test/rubygems/test_gem.rb
Expand Up @@ -311,7 +311,7 @@ def test_activate_bin_path_in_debug_mode

output, status = Open3.capture2e(
{ "GEM_HOME" => Gem.paths.home, "DEBUG_RESOLVER" => "1" },
Gem.ruby, "-I", File.expand_path("../../lib", __dir__), "-e", "\"Gem.activate_bin_path('a', 'exec', '>= 0')\""
*ruby_with_rubygems_in_load_path, "-e", "\"Gem.activate_bin_path('a', 'exec', '>= 0')\""
)

assert status.success?, output
Expand Down Expand Up @@ -1637,7 +1637,6 @@ def test_auto_activation_of_used_gemdeps_file
assert_equal expected_specs, Gem.use_gemdeps.sort_by { |s| s.name }
end

LIB_PATH = File.expand_path "../../../lib", __FILE__
BUNDLER_LIB_PATH = File.expand_path $LOAD_PATH.find {|lp| File.file?(File.join(lp, "bundler.rb")) }
BUNDLER_FULL_NAME = "bundler-#{Bundler::VERSION}".freeze

Expand Down Expand Up @@ -1665,7 +1664,7 @@ def test_looks_for_gemdeps_files_automatically_on_start
ENV['RUBYGEMS_GEMDEPS'] = "-"

path = File.join @tempdir, "gem.deps.rb"
cmd = [Gem.ruby, "-I#{LIB_PATH}",
cmd = [*ruby_with_rubygems_in_load_path,
"-I#{BUNDLER_LIB_PATH}"]
cmd << "-eputs Gem.loaded_specs.values.map(&:full_name).sort"

Expand Down Expand Up @@ -1703,7 +1702,7 @@ def test_looks_for_gemdeps_files_automatically_on_start_in_parent_dir
Dir.mkdir "sub1"

path = File.join @tempdir, "gem.deps.rb"
cmd = [Gem.ruby, "-Csub1", "-I#{LIB_PATH}",
cmd = [*ruby_with_rubygems_in_load_path, "-Csub1",
"-I#{BUNDLER_LIB_PATH}"]
cmd << "-eputs Gem.loaded_specs.values.map(&:full_name).sort"

Expand Down
4 changes: 2 additions & 2 deletions test/rubygems/test_gem_util.rb
Expand Up @@ -6,7 +6,7 @@ class TestGemUtil < Gem::TestCase

def test_class_popen
skip "popen with a block does not behave well on jruby" if Gem.java_platform?
assert_equal "0\n", Gem::Util.popen(Gem.ruby, '-I', File.expand_path('../../../lib', __FILE__), '-e', 'p 0')
assert_equal "0\n", Gem::Util.popen(*ruby_with_rubygems_in_load_path, '-e', 'p 0')

assert_raises Errno::ECHILD do
Process.wait(-1)
Expand All @@ -16,7 +16,7 @@ def test_class_popen
def test_silent_system
skip if Gem.java_platform?
assert_silent do
Gem::Util.silent_system Gem.ruby, '-I', File.expand_path('../../../lib', __FILE__), '-e', 'puts "hello"; warn "hello"'
Gem::Util.silent_system(*ruby_with_rubygems_in_load_path, '-e', 'puts "hello"; warn "hello"')
end
end

Expand Down
4 changes: 1 addition & 3 deletions test/rubygems/test_kernel.rb
Expand Up @@ -98,9 +98,7 @@ def test_gem_failing_inside_require_doesnt_cause_double_exceptions

output, _ = Open3.capture2e(
{ "GEM_HOME" => Gem.paths.home },
Gem.ruby,
"-I",
File.expand_path("../../lib", __dir__),
*ruby_with_rubygems_in_load_path,
"-r",
"./activate.rb"
)
Expand Down
13 changes: 5 additions & 8 deletions test/rubygems/test_require.rb
Expand Up @@ -391,8 +391,7 @@ def test_realworld_default_gem
require "json"
puts Gem.loaded_specs["json"]
RUBY
rubygems_libdir = File.expand_path('../../../lib', __FILE__)
output = Gem::Util.popen(Gem.ruby, "-I#{rubygems_libdir}", "-e", cmd).strip
output = Gem::Util.popen(*ruby_with_rubygems_in_load_path, "-e", cmd).strip
refute_empty output
end

Expand Down Expand Up @@ -519,31 +518,29 @@ def test_require_bundler_with_bundler_version
if RUBY_VERSION >= "2.5"
["", "Kernel."].each do |prefix|
define_method "test_no_kernel_require_in_#{prefix.tr(".", "_")}warn_with_uplevel" do
lib = File.realpath("../../../lib", __FILE__)
Dir.mktmpdir("warn_test") do |dir|
File.write(dir + "/sub.rb", "#{prefix}warn 'uplevel', 'test', uplevel: 1\n")
File.write(dir + "/main.rb", "require 'sub'\n")
_, err = capture_subprocess_io do
system(Gem.ruby, "-w", "--disable=gems", "-I", lib, "-C", dir, "-I.", "main.rb")
system(*ruby_with_rubygems_in_load_path, "-w", "--disable=gems", "-C", dir, "-I.", "main.rb")
end
assert_match(/main\.rb:1: warning: uplevel\ntest\n$/, err)
_, err = capture_subprocess_io do
system(Gem.ruby, "-w", "--enable=gems", "-I", lib, "-C", dir, "-I.", "main.rb")
system(*ruby_with_rubygems_in_load_path, "-w", "--enable=gems", "-C", dir, "-I.", "main.rb")
end
assert_match(/main\.rb:1: warning: uplevel\ntest\n$/, err)
end
end

define_method "test_no_other_behavioral_changes_with_#{prefix.tr(".", "_")}warn" do
lib = File.realpath("../../../lib", __FILE__)
Dir.mktmpdir("warn_test") do |dir|
File.write(dir + "/main.rb", "#{prefix}warn({x:1}, {y:2}, [])\n")
_, err = capture_subprocess_io do
system(Gem.ruby, "-w", "--disable=gems", "-I", lib, "-C", dir, "main.rb")
system(*ruby_with_rubygems_in_load_path, "-w", "--disable=gems", "-C", dir, "main.rb")
end
assert_match(/{:x=>1}\n{:y=>2}\n$/, err)
_, err = capture_subprocess_io do
system(Gem.ruby, "-w", "--enable=gems", "-I", lib, "-C", dir, "main.rb")
system(*ruby_with_rubygems_in_load_path, "-w", "--enable=gems", "-C", dir, "main.rb")
end
assert_match(/{:x=>1}\n{:y=>2}\n$/, err)
end
Expand Down

0 comments on commit 676d816

Please sign in to comment.