diff --git a/test/ruby/test_dir.rb b/test/ruby/test_dir.rb index 65803d0bc5..34c2e69dc1 100644 --- a/test/ruby/test_dir.rb +++ b/test/ruby/test_dir.rb @@ -578,6 +578,40 @@ class TestDir < Test::Unit::TestCase ENV.delete('USERPROFILE') assert_equal("C:/ruby/homepath", Dir.home) end + + def test_home_at_startup_windows + env = {'HOME' => "C:\\ruby\\home"} + args = [env] + assert_separately(args, "#{<<~"begin;"}\n#{<<~'end;'}") + begin; + assert_equal("C:/ruby/home", Dir.home) + end; + + env['USERPROFILE'] = "C:\\ruby\\userprofile" + assert_separately(args, "#{<<~"begin;"}\n#{<<~'end;'}") + begin; + assert_equal("C:/ruby/home", Dir.home) + end; + + env['HOME'] = nil + assert_separately(args, "#{<<~"begin;"}\n#{<<~'end;'}") + begin; + assert_equal("C:/ruby/userprofile", Dir.home) + end; + + env['HOMEDRIVE'] = "C:" + env['HOMEPATH'] = "\\ruby\\homepath" + assert_separately(args, "#{<<~"begin;"}\n#{<<~'end;'}") + begin; + assert_equal("C:/ruby/userprofile", Dir.home) + end; + + env['USERPROFILE'] = nil + assert_separately(args, "#{<<~"begin;"}\n#{<<~'end;'}") + begin; + assert_equal("C:/ruby/homepath", Dir.home) + end; + end end def test_home diff --git a/win32/win32.c b/win32/win32.c index bf96439c47..80fb30de43 100644 --- a/win32/win32.c +++ b/win32/win32.c @@ -622,21 +622,24 @@ init_env(void) if (!GetEnvironmentVariableW(L"HOME", env, numberof(env))) { f = FALSE; - if (GetEnvironmentVariableW(L"HOMEDRIVE", env, numberof(env))) - len = lstrlenW(env); - else - len = 0; - if (GetEnvironmentVariableW(L"HOMEPATH", env + len, numberof(env) - len) || len) { + if (GetEnvironmentVariableW(L"USERPROFILE", env, numberof(env))) { f = TRUE; } - else if (GetEnvironmentVariableW(L"USERPROFILE", env, numberof(env))) { - f = TRUE; - } - else if (get_special_folder(CSIDL_PROFILE, env, numberof(env))) { - f = TRUE; - } - else if (get_special_folder(CSIDL_PERSONAL, env, numberof(env))) { - f = TRUE; + else { + if (GetEnvironmentVariableW(L"HOMEDRIVE", env, numberof(env))) + len = lstrlenW(env); + else + len = 0; + + if (GetEnvironmentVariableW(L"HOMEPATH", env + len, numberof(env) - len) || len) { + f = TRUE; + } + else if (get_special_folder(CSIDL_PROFILE, env, numberof(env))) { + f = TRUE; + } + else if (get_special_folder(CSIDL_PERSONAL, env, numberof(env))) { + f = TRUE; + } } if (f) { regulate_path(env);