Skip to content

Commit 3da63e6

Browse files
YO4hsbt
authored andcommitted
add test for ENV.keys encoding
1 parent 239a472 commit 3da63e6

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

test/ruby/test_env.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,37 @@ def test_keys
195195
a.each {|k| assert_kind_of(String, k) }
196196
end
197197

198+
def test_keys_encoding
199+
bug20958 = '[ruby-core:120277] [Bug #20958]'
200+
orig = ENV.to_hash
201+
ENV.clear
202+
key = "TEST20958\u{30c6 30b9 30c8}"
203+
begin
204+
ENV[key] = "x"
205+
rescue
206+
omit "platform does not support UTF-8 environment variables."
207+
end
208+
EnvUtil.with_default_internal(nil) do
209+
enc = RUBY_PLATFORM =~ /mswin|mingw/ ?
210+
Encoding::UTF_8 : Encoding.find("locale")
211+
assert_equal(enc, ENV.keys.last.encoding, bug20958)
212+
assert_equal(key.encode(enc), ENV.keys.last, bug20958)
213+
end
214+
215+
ENV.update(orig) #required to restore ENV[RbConfig::CONFIG['LIBPATHENV']]
216+
env = {key => "x", "LOCALE" => "en_US.UTF-8", "LC_ALL" => "en_US.UTF-8"}
217+
load_path = $LOAD_PATH.map {|v| "-I#{v}" }
218+
internal_enc = "Windows-31J"
219+
params = [env, *load_path, "-Eutf-8:#{internal_enc}"]
220+
assert_separately(params, <<-"EOS")
221+
ENV.keep_if {|k,| k.start_with?("TEST20958") }
222+
key = ENV.keys.last
223+
assert_equal("#{internal_enc}", key.encoding.to_s, "#{bug20958}")
224+
assert_equal("#{key.encode(Encoding.find(internal_enc)).bytes}",
225+
key.bytes.to_s, "#{bug20958}")
226+
EOS
227+
end
228+
198229
def test_each_key
199230
ENV.each_key {|k| assert_kind_of(String, k) }
200231
end

0 commit comments

Comments
 (0)