diff options
author | Naoto Ono <onoto1998@gmail.com> | 2024-07-06 18:20:41 +0900 |
---|---|---|
committer | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2024-07-08 10:15:04 +0900 |
commit | 5b7892545542c0d451cb1e14a1d85474ac46b537 (patch) | |
tree | 5867397f0cf6362e8f78066bbc614685fec43e0e /tool | |
parent | dface4427da99561a4578a4b92a8321bc2d6c023 (diff) | |
download | ruby-5b7892545542c0d451cb1e14a1d85474ac46b537.tar.gz |
Integrate Launchable into make btest
Diffstat (limited to 'tool')
-rw-r--r-- | tool/lib/test/unit.rb | 91 | ||||
-rw-r--r-- | tool/lib/test/unit/launchable.rb | 91 | ||||
-rw-r--r-- | tool/test/testunit/test_launchable.rb | 3 |
3 files changed, 95 insertions, 90 deletions
diff --git a/tool/lib/test/unit.rb b/tool/lib/test/unit.rb index 5e5b34f5b6..0d32abd18a 100644 --- a/tool/lib/test/unit.rb +++ b/tool/lib/test/unit.rb @@ -1450,9 +1450,8 @@ module Test def setup_options(opts, options) super opts.on_tail '--launchable-test-reports=PATH', String, 'Report test results in Launchable JSON format' do |path| - require 'json' - require 'uri' - options[:launchable_test_reports] = writer = JsonStreamWriter.new(path) + require_relative '../test/unit/launchable' + options[:launchable_test_reports] = writer = Launchable::JsonStreamWriter.new(path) writer.write_array('testCases') main_pid = Process.pid at_exit { @@ -1469,92 +1468,6 @@ module Test component.to_s.gsub('%', '%25').gsub('=', '%3D').gsub('#', '%23').gsub('&', '%26') end end - - ## - # JsonStreamWriter writes a JSON file using a stream. - # By utilizing a stream, we can minimize memory usage, especially for large files. - class JsonStreamWriter - def initialize(path) - @file = File.open(path, "w") - @file.write("{") - @indent_level = 0 - @is_first_key_val = true - @is_first_obj = true - write_new_line - end - - def write_object obj - if @is_first_obj - @is_first_obj = false - else - write_comma - write_new_line - end - @indent_level += 1 - @file.write(to_json_str(obj)) - @indent_level -= 1 - @is_first_key_val = true - # Occasionally, invalid JSON will be created as shown below, especially when `--repeat-count` is specified. - # { - # "testPath": "file=test%2Ftest_timeout.rb&class=TestTimeout&testcase=test_allows_zero_seconds", - # "status": "TEST_PASSED", - # "duration": 2.7e-05, - # "createdAt": "2024-02-09 12:21:07 +0000", - # "stderr": null, - # "stdout": null - # }: null <- here - # }, - # To prevent this, IO#flush is called here. - @file.flush - end - - def write_array(key) - @indent_level += 1 - @file.write(to_json_str(key)) - write_colon - @file.write(" ", "[") - write_new_line - end - - def close - return if @file.closed? - close_array - @indent_level -= 1 - write_new_line - @file.write("}", "\n") - @file.flush - @file.close - end - - private - def to_json_str(obj) - json = JSON.pretty_generate(obj) - json.gsub(/^/, ' ' * (2 * @indent_level)) - end - - def write_indent - @file.write(" " * 2 * @indent_level) - end - - def write_new_line - @file.write("\n") - end - - def write_comma - @file.write(',') - end - - def write_colon - @file.write(":") - end - - def close_array - write_new_line - write_indent - @file.write("]") - @indent_level -= 1 - end - end end class Runner # :nodoc: all diff --git a/tool/lib/test/unit/launchable.rb b/tool/lib/test/unit/launchable.rb new file mode 100644 index 0000000000..38f4fe92b3 --- /dev/null +++ b/tool/lib/test/unit/launchable.rb @@ -0,0 +1,91 @@ +# frozen_string_literal: true +require 'json' +require 'uri' + +module Launchable + ## + # JsonStreamWriter writes a JSON file using a stream. + # By utilizing a stream, we can minimize memory usage, especially for large files. + class JsonStreamWriter + def initialize(path) + @file = File.open(path, "w") + @file.write("{") + @indent_level = 0 + @is_first_key_val = true + @is_first_obj = true + write_new_line + end + + def write_object obj + if @is_first_obj + @is_first_obj = false + else + write_comma + write_new_line + end + @indent_level += 1 + @file.write(to_json_str(obj)) + @indent_level -= 1 + @is_first_key_val = true + # Occasionally, invalid JSON will be created as shown below, especially when `--repeat-count` is specified. + # { + # "testPath": "file=test%2Ftest_timeout.rb&class=TestTimeout&testcase=test_allows_zero_seconds", + # "status": "TEST_PASSED", + # "duration": 2.7e-05, + # "createdAt": "2024-02-09 12:21:07 +0000", + # "stderr": null, + # "stdout": null + # }: null <- here + # }, + # To prevent this, IO#flush is called here. + @file.flush + end + + def write_array(key) + @indent_level += 1 + @file.write(to_json_str(key)) + write_colon + @file.write(" ", "[") + write_new_line + end + + def close + return if @file.closed? + close_array + @indent_level -= 1 + write_new_line + @file.write("}", "\n") + @file.flush + @file.close + end + + private + def to_json_str(obj) + json = JSON.pretty_generate(obj) + json.gsub(/^/, ' ' * (2 * @indent_level)) + end + + def write_indent + @file.write(" " * 2 * @indent_level) + end + + def write_new_line + @file.write("\n") + end + + def write_comma + @file.write(',') + end + + def write_colon + @file.write(":") + end + + def close_array + write_new_line + write_indent + @file.write("]") + @indent_level -= 1 + end + end +end diff --git a/tool/test/testunit/test_launchable.rb b/tool/test/testunit/test_launchable.rb index 70c371e212..3d3c5c29de 100644 --- a/tool/test/testunit/test_launchable.rb +++ b/tool/test/testunit/test_launchable.rb @@ -2,11 +2,12 @@ require 'test/unit' require 'tempfile' require 'json' +require_relative '../../lib/test/unit/launchable' class TestLaunchable < Test::Unit::TestCase def test_json_stream_writer Tempfile.create(['launchable-test-', '.json']) do |f| - json_stream_writer = Test::Unit::LaunchableOption::JsonStreamWriter.new(f.path) + json_stream_writer = Launchable::JsonStreamWriter.new(f.path) json_stream_writer.write_array('testCases') json_stream_writer.write_object( { |