diff --git a/mrbgems/mruby-io/test/file.rb b/mrbgems/mruby-io/test/file.rb index 5746986ad..465135185 100644 --- a/mrbgems/mruby-io/test/file.rb +++ b/mrbgems/mruby-io/test/file.rb @@ -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 diff --git a/mrbgems/mruby-io/test/mruby_io_test.c b/mrbgems/mruby-io/test/mruby_io_test.c index c1ff0f8f0..1b54a4438 100644 --- a/mrbgems/mruby-io/test/mruby_io_test.c +++ b/mrbgems/mruby-io/test/mruby_io_test.c @@ -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); + } }