From c2dbdf3067c334e22946fbda74181d3a94afbf97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Mon, 13 Dec 2021 16:52:37 +0100 Subject: [rubygems/rubygems] Extract a helper to temporarily modify internal encoding https://github.com/rubygems/rubygems/commit/93051fd2aa --- test/rubygems/helper.rb | 9 +++++++++ test/rubygems/test_gem_specification.rb | 27 ++++++++++++--------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/test/rubygems/helper.rb b/test/rubygems/helper.rb index b0ad5aafcd..f321b02e48 100644 --- a/test/rubygems/helper.rb +++ b/test/rubygems/helper.rb @@ -1300,6 +1300,15 @@ Also, a list: Gem.instance_variable_set :@ruby, orig_ruby end + def with_internal_encoding(encoding) + int_enc = Encoding.default_internal + silence_warnings { Encoding.default_internal = encoding } + + yield + ensure + silence_warnings { Encoding.default_internal = int_enc } + end + def silence_warnings old_verbose, $VERBOSE = $VERBOSE, false yield diff --git a/test/rubygems/test_gem_specification.rb b/test/rubygems/test_gem_specification.rb index 3062b66bc6..b1d925bb57 100644 --- a/test/rubygems/test_gem_specification.rb +++ b/test/rubygems/test_gem_specification.rb @@ -873,24 +873,21 @@ dependencies: [] end def test_self_load_utf8_with_ascii_encoding - int_enc = Encoding.default_internal - silence_warnings { Encoding.default_internal = 'US-ASCII' } - - spec2 = @a2.dup - bin = "\u5678".dup - spec2.authors = [bin] - full_path = spec2.spec_file - write_file full_path do |io| - io.write spec2.to_ruby_for_cache.force_encoding('BINARY').sub("\\u{5678}", bin.force_encoding('BINARY')) - end + with_internal_encoding('US-ASCII') do + spec2 = @a2.dup + bin = "\u5678".dup + spec2.authors = [bin] + full_path = spec2.spec_file + write_file full_path do |io| + io.write spec2.to_ruby_for_cache.force_encoding('BINARY').sub("\\u{5678}", bin.force_encoding('BINARY')) + end - spec = Gem::Specification.load full_path + spec = Gem::Specification.load full_path - spec2.files.clear + spec2.files.clear - assert_equal spec2, spec - ensure - silence_warnings { Encoding.default_internal = int_enc } + assert_equal spec2, spec + end end def test_self_load_legacy_ruby -- cgit v1.2.3