2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2013-11-18 19:34:13 -05:00
|
|
|
require 'rubygems/test_case'
|
|
|
|
require 'rubygems/util'
|
|
|
|
|
|
|
|
class TestGemUtil < Gem::TestCase
|
|
|
|
|
|
|
|
def test_class_popen
|
2019-06-01 05:45:11 -04:00
|
|
|
skip "popen with a block does not behave well on jruby" if Gem.java_platform?
|
2018-10-21 20:27:02 -04:00
|
|
|
assert_equal "0\n", Gem::Util.popen(Gem.ruby, '-I', File.expand_path('../../../lib', __FILE__), '-e', 'p 0')
|
2013-11-21 18:27:30 -05:00
|
|
|
|
|
|
|
assert_raises Errno::ECHILD do
|
2013-11-25 14:14:49 -05:00
|
|
|
Process.wait(-1)
|
2013-11-21 18:27:30 -05:00
|
|
|
end
|
2013-11-18 19:34:13 -05:00
|
|
|
end
|
|
|
|
|
2013-11-25 14:14:49 -05:00
|
|
|
def test_silent_system
|
2019-06-01 05:45:11 -04:00
|
|
|
skip if Gem.java_platform?
|
2013-11-25 14:14:49 -05:00
|
|
|
assert_silent do
|
2018-10-21 20:27:02 -04:00
|
|
|
Gem::Util.silent_system Gem.ruby, '-I', File.expand_path('../../../lib', __FILE__), '-e', 'puts "hello"; warn "hello"'
|
2013-11-25 14:14:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_traverse_parents
|
|
|
|
FileUtils.mkdir_p 'a/b/c'
|
|
|
|
|
|
|
|
enum = Gem::Util.traverse_parents 'a/b/c'
|
|
|
|
|
|
|
|
assert_equal File.join(@tempdir, 'a/b/c'), enum.next
|
|
|
|
assert_equal File.join(@tempdir, 'a/b'), enum.next
|
|
|
|
assert_equal File.join(@tempdir, 'a'), enum.next
|
2017-10-07 21:32:18 -04:00
|
|
|
loop { break if enum.next.nil? } # exhaust the enumerator
|
2013-11-25 14:14:49 -05:00
|
|
|
end
|
|
|
|
|
2018-02-05 21:58:35 -05:00
|
|
|
def test_traverse_parents_does_not_crash_on_permissions_error
|
2019-06-01 05:45:11 -04:00
|
|
|
skip 'skipped on MS Windows (chmod has no effect)' if win_platform? || java_platform?
|
2018-02-05 21:58:35 -05:00
|
|
|
|
|
|
|
FileUtils.mkdir_p 'd/e/f'
|
|
|
|
# remove 'execute' permission from "e" directory and make it
|
|
|
|
# impossible to cd into it and its children
|
|
|
|
FileUtils.chmod(0666, 'd/e')
|
|
|
|
|
2018-08-27 06:05:04 -04:00
|
|
|
skip 'skipped in root privilege' if Process.uid.zero?
|
|
|
|
|
2018-02-05 21:58:35 -05:00
|
|
|
paths = Gem::Util.traverse_parents('d/e/f').to_a
|
|
|
|
|
|
|
|
assert_equal File.join(@tempdir, 'd'), paths[0]
|
|
|
|
assert_equal @tempdir, paths[1]
|
2018-05-30 09:01:35 -04:00
|
|
|
assert_equal File.realpath(Dir.tmpdir), paths[2]
|
|
|
|
assert_equal File.realpath("..", Dir.tmpdir), paths[3]
|
2018-02-05 21:58:35 -05:00
|
|
|
ensure
|
|
|
|
# restore default permissions, allow the directory to be removed
|
2019-06-01 05:45:11 -04:00
|
|
|
FileUtils.chmod(0775, 'd/e') unless win_platform? || java_platform?
|
2018-02-05 21:58:35 -05:00
|
|
|
end
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def test_linked_list_find
|
2019-02-14 07:59:03 -05:00
|
|
|
list = [1,2,3,4,5].inject(Gem::List.new(0)) do |m,o|
|
2015-07-01 17:50:14 -04:00
|
|
|
Gem::List.new o, m
|
2019-02-14 07:59:03 -05:00
|
|
|
end
|
2015-07-01 17:50:14 -04:00
|
|
|
assert_equal 5, list.find { |x| x == 5 }
|
|
|
|
assert_equal 4, list.find { |x| x == 4 }
|
|
|
|
end
|
|
|
|
|
2018-12-22 19:20:49 -05:00
|
|
|
def test_glob_files_in_dir
|
|
|
|
FileUtils.mkdir_p 'g'
|
|
|
|
FileUtils.touch File.join('g', 'h.rb')
|
|
|
|
FileUtils.touch File.join('g', 'i.rb')
|
|
|
|
|
|
|
|
expected_paths = [
|
|
|
|
File.join(@tempdir, 'g/h.rb'),
|
|
|
|
File.join(@tempdir, 'g/i.rb'),
|
|
|
|
]
|
|
|
|
|
|
|
|
files_with_absolute_base = Gem::Util.glob_files_in_dir('*.rb', File.join(@tempdir, 'g'))
|
|
|
|
assert_equal expected_paths.to_set, files_with_absolute_base.to_set
|
|
|
|
|
|
|
|
files_with_relative_base = Gem::Util.glob_files_in_dir('*.rb', 'g')
|
|
|
|
assert_equal expected_paths.to_set, files_with_relative_base.to_set
|
|
|
|
end
|
|
|
|
|
2019-06-01 05:45:11 -04:00
|
|
|
def test_correct_for_windows_path
|
|
|
|
path = "/C:/WINDOWS/Temp/gems"
|
|
|
|
assert_equal "C:/WINDOWS/Temp/gems", Gem::Util.correct_for_windows_path(path)
|
|
|
|
|
|
|
|
path = "/home/skillet"
|
|
|
|
assert_equal "/home/skillet", Gem::Util.correct_for_windows_path(path)
|
|
|
|
end
|
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
end
|