Improved File.expand_path test in `mruby-io

Since `mruby-io` does not depend on `mruby-env` even for test builds, it is impossible that `ENV` constants are defined.
Therefore, define `MRubyIOTestUtil::ENV_HOME` for alternative use.
This commit is contained in:
dearblue
2024-12-07 22:32:05 +09:00
parent 1dfd3d9043
commit 06bb54d380
2 changed files with 27 additions and 5 deletions
+5 -5
View File
@@ -210,13 +210,13 @@ assert('File.expand_path') do
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
+22
View File
@@ -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);
}
}