mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
add File.expand_path
This commit is contained in:
@@ -63,6 +63,71 @@ class File < IO
|
||||
end
|
||||
end
|
||||
|
||||
def self.expand_path(path, default_dir = '.')
|
||||
def concat_path(path, base_path)
|
||||
if path[0] == "/"
|
||||
expanded_path = path
|
||||
elsif path[0] == "~"
|
||||
if (path[1] == "/" || path[1] == nil)
|
||||
dir = path[1, path.size]
|
||||
home_dir = _gethome
|
||||
|
||||
unless home_dir
|
||||
raise ArgumentError, "couldn't find HOME environment -- expanding '~'"
|
||||
end
|
||||
|
||||
expanded_path = home_dir
|
||||
expanded_path += dir if dir
|
||||
expanded_path += "/"
|
||||
else
|
||||
splitted_path = path.split("/")
|
||||
user = splitted_path[0][1, splitted_path[0].size]
|
||||
dir = "/" + splitted_path[1, splitted_path.size].join("/")
|
||||
|
||||
home_dir = _gethome(user)
|
||||
|
||||
unless home_dir
|
||||
raise ArgumentError, "user #{user} doesn't exist"
|
||||
end
|
||||
|
||||
expanded_path = home_dir
|
||||
expanded_path += dir if dir
|
||||
expanded_path += "/"
|
||||
end
|
||||
else
|
||||
expanded_path = concat_path(base_path, _getwd)
|
||||
expanded_path += "/" + path
|
||||
end
|
||||
|
||||
expanded_path
|
||||
end
|
||||
|
||||
expanded_path = concat_path(path, default_dir)
|
||||
expand_path_array = []
|
||||
while expanded_path.include?('//')
|
||||
expanded_path = expanded_path.gsub('//', '/')
|
||||
end
|
||||
|
||||
if expanded_path == "/"
|
||||
expanded_path
|
||||
else
|
||||
expanded_path.split('/').each do |path_token|
|
||||
if path_token == '..'
|
||||
if expand_path_array.size > 1
|
||||
expand_path_array.pop
|
||||
end
|
||||
elsif path_token == '.'
|
||||
# nothing to do.
|
||||
else
|
||||
expand_path_array << path_token
|
||||
end
|
||||
end
|
||||
|
||||
expand_path = expand_path_array.join("/")
|
||||
expand_path.empty? ? '/' : expand_path
|
||||
end
|
||||
end
|
||||
|
||||
def self.directory?(file)
|
||||
FileTest.directory?(file)
|
||||
end
|
||||
|
||||
@@ -29,5 +29,7 @@ MRuby::Build.new do |conf|
|
||||
conf.gem x unless x =~ /\/mruby-(print|sprintf)$/
|
||||
end
|
||||
|
||||
conf.gem :github => 'iij/mruby-env'
|
||||
|
||||
conf.gem File.expand_path(File.dirname(__FILE__))
|
||||
end
|
||||
|
||||
@@ -73,3 +73,26 @@ end
|
||||
assert('File TEST CLEANUP') do
|
||||
assert_nil MRubyIOTestUtil.io_test_cleanup
|
||||
end
|
||||
|
||||
assert('File.expand_path') do
|
||||
assert_equal "/", File.expand_path("..", "/tmp"), "parent path with base_dir (1)"
|
||||
assert_equal "/tmp", File.expand_path("..", "/tmp/mruby"), "parent path with base_dir (2)"
|
||||
|
||||
assert_equal "/home", File.expand_path("/home"), "absolute"
|
||||
assert_equal "/home", File.expand_path("/home", "."), "absolute with base_dir"
|
||||
|
||||
assert_equal "/hoge", File.expand_path("/tmp/..//hoge")
|
||||
assert_equal "/hoge", File.expand_path("////tmp/..///////hoge")
|
||||
|
||||
assert_equal "/", File.expand_path("../../../..", "/")
|
||||
assert_equal "/", File.expand_path(([".."] * 100).join("/"))
|
||||
end
|
||||
|
||||
assert('File.expand_path (with ENV)') do
|
||||
skip unless Object.const_defined?(:ENV) && ENV['HOME']
|
||||
|
||||
assert_equal ENV['HOME'], File.expand_path("~/"), "home"
|
||||
assert_equal ENV['HOME'], File.expand_path("~/", "/"), "home with base_dir"
|
||||
|
||||
assert_equal "#{ENV['HOME']}/user", File.expand_path("user", ENV['HOME']), "relative with base_dir"
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user