Launchable: Refactor the logic of JsonStreamWriter

This commit is contained in:
Naoto Ono 2024-03-25 13:20:04 +09:00 коммит произвёл Nobuyoshi Nakada
Родитель 5903fdf43e
Коммит 3d1d1435c1
2 изменённых файлов: 72 добавлений и 35 удалений

Просмотреть файл

@ -1409,14 +1409,16 @@ module Test
if writer && test_path && status
# Occasionally, the file writing operation may be paused, especially when `--repeat-count` is specified.
# In such cases, we proceed to execute the operation here.
writer.write_object do
writer.write_key_value('testPath', test_path)
writer.write_key_value('status', status)
writer.write_key_value('duration', time)
writer.write_key_value('createdAt', Time.now.to_s)
writer.write_key_value('stderr', e)
writer.write_key_value('stdout', nil)
end
writer.write_object(
{
testPath: test_path,
status: status,
duration: time,
createdAt: Time.now.to_s,
stderr: e,
stdout: nil
}
)
end
end
@ -1457,7 +1459,7 @@ module Test
write_new_line
end
def write_object
def write_object obj
if @is_first_obj
@is_first_obj = false
else
@ -1465,15 +1467,7 @@ module Test
write_new_line
end
@indent_level += 1
write_indent
@file.write("{")
write_new_line
@indent_level += 1
yield
@indent_level -= 1
write_new_line
write_indent
@file.write("}")
@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.
@ -1492,40 +1486,26 @@ module Test
def write_array(key)
@indent_level += 1
write_indent
@file.write(to_json_str(key))
write_colon
@file.write(" ", "[")
write_new_line
end
def write_key_value(key, value)
if @is_first_key_val
@is_first_key_val = false
else
write_comma
write_new_line
end
write_indent
@file.write(to_json_str(key))
write_colon
@file.write(" ")
@file.write(to_json_str(value))
end
def close
return if @file.closed?
close_array
@indent_level -= 1
write_new_line
@file.write("}")
@file.write("}", "\n")
@file.flush
@file.close
end
private
def to_json_str(obj)
JSON.dump(obj)
json = JSON.pretty_generate(obj)
json.gsub(/^/, ' ' * (2 * @indent_level))
end
def write_indent

Просмотреть файл

@ -0,0 +1,57 @@
# frozen_string_literal: false
require 'test/unit'
require 'tempfile'
require 'json'
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.write_array('testCases')
json_stream_writer.write_object(
{
testPath: "file=test/test_a.rb#class=class1#testcase=testcase899",
duration: 42,
status: "TEST_FAILED",
stdout: nil,
stderr: nil,
createdAt: "2021-10-05T12:34:00"
}
)
json_stream_writer.write_object(
{
testPath: "file=test/test_a.rb#class=class1#testcase=testcase899",
duration: 45,
status: "TEST_PASSED",
stdout: "This is stdout",
stderr: "This is stderr",
createdAt: "2021-10-05T12:36:00"
}
)
json_stream_writer.close()
expected = <<JSON
{
"testCases": [
{
"testPath": "file=test/test_a.rb#class=class1#testcase=testcase899",
"duration": 42,
"status": "TEST_FAILED",
"stdout": null,
"stderr": null,
"createdAt": "2021-10-05T12:34:00"
},
{
"testPath": "file=test/test_a.rb#class=class1#testcase=testcase899",
"duration": 45,
"status": "TEST_PASSED",
"stdout": "This is stdout",
"stderr": "This is stderr",
"createdAt": "2021-10-05T12:36:00"
}
]
}
JSON
assert_equal(expected, f.read)
end
end
end