mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Merge pull request #6447 from dearblue/File.expand_path
Fixed `File.expand_path`
This commit is contained in:
@@ -149,7 +149,7 @@ class File < IO
|
||||
if drive_prefix.empty?
|
||||
expanded_path
|
||||
else
|
||||
drive_prefix + expanded_path.gsub("/", File::ALT_SEPARATOR)
|
||||
drive_prefix + expanded_path
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -345,15 +345,14 @@ mrb_file__gethome(mrb_state *mrb, mrb_value klass)
|
||||
mrb_int argc = mrb_get_args(mrb, "|S", &username);
|
||||
if (argc == 0) {
|
||||
home = getenv("HOME");
|
||||
if (home == NULL) {
|
||||
return mrb_nil_value();
|
||||
}
|
||||
#ifdef _WIN32
|
||||
home = getenv("USERPROFILE");
|
||||
if (home == NULL) {
|
||||
return mrb_nil_value();
|
||||
home = getenv("USERPROFILE");
|
||||
}
|
||||
#endif
|
||||
if (home == NULL) {
|
||||
return mrb_nil_value();
|
||||
}
|
||||
if (!mrb_file_is_absolute_path(home)) {
|
||||
mrb_raise(mrb, E_ARGUMENT_ERROR, "non-absolute home");
|
||||
}
|
||||
@@ -378,6 +377,15 @@ mrb_file__gethome(mrb_state *mrb, mrb_value klass)
|
||||
home = mrb_utf8_from_locale(home, -1);
|
||||
path = mrb_str_new_cstr(mrb, home);
|
||||
mrb_utf8_free(home);
|
||||
#ifdef _WIN32
|
||||
char *pathp = RSTRING_PTR(path);
|
||||
const char *const pathend = pathp + RSTRING_LEN(path);
|
||||
for (;;) {
|
||||
pathp = memchr(pathp, '\\', pathend - pathp);
|
||||
if (!pathp) break;
|
||||
*pathp++ = '/';
|
||||
}
|
||||
#endif
|
||||
return path;
|
||||
}
|
||||
|
||||
|
||||
@@ -204,19 +204,19 @@ assert('File.expand_path') do
|
||||
assert_equal "/", File.expand_path("../../../..", "/")
|
||||
if File._getwd[1] == ":"
|
||||
drive_letter = File._getwd[0]
|
||||
assert_equal drive_letter + ":\\", File.expand_path(([".."] * 100).join("/"))
|
||||
assert_equal drive_letter + ":/", File.expand_path(([".."] * 100).join("/"))
|
||||
else
|
||||
assert_equal "/", File.expand_path(([".."] * 100).join("/"))
|
||||
end
|
||||
end
|
||||
|
||||
assert('File.expand_path (with ENV)') do
|
||||
skip unless Object.const_defined?(:ENV) && ENV['HOME']
|
||||
assert('File.expand_path (with getenv(3))') do
|
||||
skip unless MRubyIOTestUtil.const_defined?(:ENV_HOME)
|
||||
|
||||
assert_equal ENV['HOME'], File.expand_path("~/"), "home"
|
||||
assert_equal ENV['HOME'], File.expand_path("~/", "/"), "home with base_dir"
|
||||
assert_equal MRubyIOTestUtil::ENV_HOME, File.expand_path("~/"), "home"
|
||||
assert_equal MRubyIOTestUtil::ENV_HOME, File.expand_path("~/", "/"), "home with base_dir"
|
||||
|
||||
assert_equal "#{ENV['HOME']}/user", File.expand_path("user", ENV['HOME']), "relative with base_dir"
|
||||
assert_equal "#{MRubyIOTestUtil::ENV_HOME}/user", File.expand_path("user", MRubyIOTestUtil::ENV_HOME), "relative with base_dir"
|
||||
end
|
||||
|
||||
assert('File.path') do
|
||||
|
||||
@@ -243,4 +243,26 @@ mrb_mruby_io_gem_test(mrb_state* mrb)
|
||||
mrb_define_class_method(mrb, io_test, "win?", mrb_io_win_p, MRB_ARGS_NONE());
|
||||
|
||||
mrb_define_const(mrb, io_test, "MRB_WITH_IO_PREAD_PWRITE", mrb_bool_value(MRB_WITH_IO_PREAD_PWRITE_ENABLED));
|
||||
|
||||
const char *env_home = getenv("HOME");
|
||||
#ifdef _WIN32
|
||||
if (!env_home) {
|
||||
env_home = getenv("USERPROFILE");
|
||||
}
|
||||
#endif
|
||||
if (env_home) {
|
||||
char *utf8 = mrb_utf8_from_locale(env_home, strlen(env_home));
|
||||
mrb_value path = mrb_str_new_cstr(mrb, utf8);
|
||||
#ifdef _WIN32
|
||||
char *pathp = RSTRING_PTR(path);
|
||||
const char *const pathend = pathp + RSTRING_LEN(path);
|
||||
for (;;) {
|
||||
pathp = memchr(pathp, '\\', pathend - pathp);
|
||||
if (!pathp) break;
|
||||
*pathp++ = '/';
|
||||
}
|
||||
#endif
|
||||
mrb_define_const(mrb, io_test, "ENV_HOME", path);
|
||||
mrb_utf8_free(utf8);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user