diff options
| -rw-r--r-- | mrblib/file.rb | 65 | ||||
| -rw-r--r-- | run_test.rb | 2 | ||||
| -rw-r--r-- | test/file.rb | 23 |
3 files changed, 90 insertions, 0 deletions
diff --git a/mrblib/file.rb b/mrblib/file.rb index 566085066..5b6986985 100644 --- a/mrblib/file.rb +++ b/mrblib/file.rb @@ -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 diff --git a/run_test.rb b/run_test.rb index e83b504be..d9036a575 100644 --- a/run_test.rb +++ b/run_test.rb @@ -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 diff --git a/test/file.rb b/test/file.rb index f1bef32e5..8fe7636e0 100644 --- a/test/file.rb +++ b/test/file.rb @@ -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 |
