Windows: Fix encoding of Dir.home

Dir.home returns an UTF-8 string since ruby-3.0, but the actual
encoding of the bytes was CP_ACP or CP_OEMCP.
That led to invalid bytes when calling Dir.home with an unicode
username.
This commit is contained in:
Lars Kanis 2022-12-18 21:05:22 +01:00 committed by Nobuyoshi Nakada
parent 18c1ca8f4c
commit d6ce4180a5
Notes: git 2022-12-24 13:54:43 +00:00
2 changed files with 13 additions and 1 deletions

View File

@ -19,6 +19,17 @@ describe "Dir.home" do
it "returns a non-frozen string" do
Dir.home.should_not.frozen?
end
platform_is :windows do
ruby_version_is "3.0" do
it "returns the home directory with forward slashs and as UTF-8" do
ENV['HOME'] = "C:\\rubyspäc\\home"
home = Dir.home
home.should == "C:/rubyspäc/home"
home.encoding.should == Encoding::UTF_8
end
end
end
end
describe "when called with the current user name" do

View File

@ -266,7 +266,8 @@ rb_default_home_dir(VALUE result)
rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
}
append_wstr(result, dir, -1,
rb_w32_filecp(), rb_filesystem_encoding());
CP_UTF8, rb_utf8_encoding());
xfree(dir);
return result;
}