diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2019-04-01 05:45:54 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-04-01 05:45:54 -0600 |
| commit | f90274150ffca00bef5c5c9faed48db70072ef5d (patch) | |
| tree | af2ed97ae3384ddec1dd4e7cea75400f73d50b94 | |
| parent | 581443f37d584a42aabcece4b3e725cbb5b25f94 (diff) | |
use IO.popen with env argument in test suite (PR #3229)
| -rw-r--r-- | test/invoker_test.rb | 79 | ||||
| -rw-r--r-- | test/lists_test.rb | 2 | ||||
| -rw-r--r-- | test/test_helper.rb | 45 |
3 files changed, 64 insertions, 62 deletions
diff --git a/test/invoker_test.rb b/test/invoker_test.rb index 909c9bbd..5e771a49 100644 --- a/test/invoker_test.rb +++ b/test/invoker_test.rb @@ -676,35 +676,20 @@ context 'Invoker' do end test 'should force default external encoding to UTF-8' do - ruby = File.join RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'] - executable = File.join bindir, 'asciidoctor' input_path = fixture_path 'encoding.adoc' - old_lang = ENV['LANG'] - begin - ENV['LANG'] = 'US-ASCII' - # using open3 to work around a bug in JRuby process_manager.rb, - # which tries to run a gsub on stdout prematurely breaking the test - # warnings may be issued, so don't assert on stderr - stdout_lines = Open3.popen3(%(#{ruby} #{executable} -o - --trace #{input_path})) {|_, out| out.readlines } - refute_empty stdout_lines - # NOTE Ruby on Windows runs with a IBM437 encoding by default - stdout_lines.each {|l| l.force_encoding Encoding::UTF_8 } unless Encoding.default_external == Encoding::UTF_8 - stdout_str = stdout_lines.join - assert_includes stdout_str, 'Codierungen sind verrückt auf älteren Versionen von Ruby' - ensure - if old_lang - ENV['LANG'] = old_lang - else - ENV.delete 'LANG' - end - end + # using open3 to work around a bug in JRuby process_manager.rb, + # which tries to run a gsub on stdout prematurely breaking the test + # warnings may be issued, so don't assert on stderr + stdout_lines = run_command({ 'LANG' => 'US-ASCII' }, %(#{asciidoctor_cmd} -o - --trace #{input_path})) {|out| out.readlines } + refute_empty stdout_lines + # NOTE Ruby on Windows runs with a IBM437 encoding by default + stdout_lines.each {|l| l.force_encoding Encoding::UTF_8 } unless Encoding.default_external == Encoding::UTF_8 + stdout_str = stdout_lines.join + assert_includes stdout_str, 'Codierungen sind verrückt auf älteren Versionen von Ruby' end test 'should force stdio encoding to UTF-8' do - require_script = fixture_path 'configure-stdin.rb' - ruby = File.join RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'] - executable = File.join bindir, 'asciidoctor' - result = Open3.popen3(%(#{ruby} -E IBM866:IBM866 #{executable} -r #{require_script} -s -o - -)) {|_, out| out.read } + result = run_command(%(#{asciidoctor_cmd true, '-E IBM866:IBM866'} -r #{fixture_path 'configure-stdin.rb'} -s -o - -)) {|out| out.read } # NOTE Ruby on Windows runs with a IBM437 encoding by default result.force_encoding Encoding::UTF_8 unless Encoding.default_external == Encoding::UTF_8 assert_equal Encoding::UTF_8, result.encoding @@ -726,47 +711,19 @@ context 'Invoker' do end test 'should show timezone as UTC if system TZ is set to UTC' do - ruby = File.join RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'] - executable = File.join bindir, 'asciidoctor' input_path = fixture_path 'doctime-localtime.adoc' - old_tz = ENV['TZ'] - old_source_date_epoch = ENV.delete 'SOURCE_DATE_EPOCH' - begin - ENV['TZ'] = 'UTC' - result = `#{ruby} #{executable} -d inline -o - -s #{input_path}` - doctime, localtime = result.lines.map(&:chomp) - assert doctime.end_with?(' UTC') - assert localtime.end_with?(' UTC') - ensure - if old_tz - ENV['TZ'] = old_tz - else - ENV.delete 'TZ' - end - ENV['SOURCE_DATE_EPOCH'] = old_source_date_epoch if old_source_date_epoch - end + output = run_command({ 'TZ' => 'UTC', 'SOURCE_DATE_EPOCH' => nil }, %(#{asciidoctor_cmd} -d inline -o - -s #{input_path})) {|out| out.read } + doctime, localtime = output.lines.map(&:chomp) + assert doctime.end_with?(' UTC') + assert localtime.end_with?(' UTC') end test 'should show timezone as offset if system TZ is not set to UTC' do - ruby = File.join RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'] - executable = File.join bindir, 'asciidoctor' input_path = fixture_path 'doctime-localtime.adoc' - old_tz = ENV['TZ'] - old_source_date_epoch = ENV.delete 'SOURCE_DATE_EPOCH' - begin - ENV['TZ'] = 'EST+5' - result = `#{ruby} #{executable} -d inline -o - -s #{input_path}` - doctime, localtime = result.lines.map(&:chomp) - assert doctime.end_with?(' -0500') - assert localtime.end_with?(' -0500') - ensure - if old_tz - ENV['TZ'] = old_tz - else - ENV.delete 'TZ' - end - ENV['SOURCE_DATE_EPOCH'] = old_source_date_epoch if old_source_date_epoch - end + output = run_command({ 'TZ' => 'EST+5', 'SOURCE_DATE_EPOCH' => nil }, %(#{asciidoctor_cmd} -d inline -o - -s #{input_path})) {|out| out.read } + doctime, localtime = output.lines.map(&:chomp) + assert doctime.end_with?(' -0500') + assert localtime.end_with?(' -0500') end test 'should use SOURCE_DATE_EPOCH as modified time of input file and local time' do diff --git a/test/lists_test.rb b/test/lists_test.rb index 75cfcfa1..387112ac 100644 --- a/test/lists_test.rb +++ b/test/lists_test.rb @@ -3138,7 +3138,7 @@ context "Description lists (:dlist)" do assert_css '.hdlist table colgroup', output, 0 assert_css '.hdlist table tr', output, 2 # see nokogiri#1803 for why this is necessary - tbody_path = RUBY_ENGINE == 'jruby' ? 'tbody/' : '' + tbody_path = jruby? ? 'tbody/' : '' refute_includes output, '<tbody>' assert_xpath %(/*[@class="hdlist"]/table/#{tbody_path}tr[1]/td), output, 2 assert_xpath %(/*[@class="hdlist"]/table/#{tbody_path}tr[1]/td[@class="hdlist1"]), output, 1 diff --git a/test/test_helper.rb b/test/test_helper.rb index bfb0cf6a..7ab2abf0 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -31,6 +31,10 @@ require 'minitest/autorun' Minitest::Test = MiniTest::Unit::TestCase unless defined? Minitest::Test class Minitest::Test + def jruby? + RUBY_ENGINE == 'jruby' + end + def windows? RbConfig::CONFIG['host_os'] =~ /win|ming/ end @@ -63,6 +67,17 @@ class Minitest::Test File.join Asciidoctor::ROOT_DIR, 'bin' end + def asciidoctor_cmd use_ruby = true, ruby_opts = nil + executable = File.join bindir, 'asciidoctor' + if use_ruby + ruby = File.join RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'] + ruby = %(#{ruby} #{ruby_opts}) if ruby_opts + %(#{ruby} #{executable}) + else + executable + end + end + def testdir ASCIIDOCTOR_TEST_DIR end @@ -320,6 +335,36 @@ class Minitest::Test end end + def run_command *args, &block + if Hash === (env = args[0]) + cmd = args[1] + else + cmd, env = env, nil + end + if env + if jruby? + begin + old_env, env = ENV, (ENV.merge env) + env.each {|key, val| env.delete key if val.nil? } if env.value? nil + ENV.replace env + IO.popen cmd, &block + ensure + ENV.replace old_env + end + elsif env.value? nil + env = env.inject(ENV.to_h) do |acc, (key, val)| + val.nil? ? (acc.delete key) : (acc[key] = val) + acc + end + IO.popen env, cmd, unsetenv_others: true, &block + else + IO.popen env, cmd, &block + end + else + IO.popen cmd, &block + end + end + def using_test_webserver host = resolve_localhost, port = 9876 base_dir = testdir server = TCPServer.new host, port |
