Launchable: Refactor the logic of JsonStreamWriter
This commit is contained in:
parent
5903fdf43e
commit
3d1d1435c1
@ -1409,14 +1409,16 @@ module Test
|
|||||||
if writer && test_path && status
|
if writer && test_path && status
|
||||||
# Occasionally, the file writing operation may be paused, especially when `--repeat-count` is specified.
|
# Occasionally, the file writing operation may be paused, especially when `--repeat-count` is specified.
|
||||||
# In such cases, we proceed to execute the operation here.
|
# In such cases, we proceed to execute the operation here.
|
||||||
writer.write_object do
|
writer.write_object(
|
||||||
writer.write_key_value('testPath', test_path)
|
{
|
||||||
writer.write_key_value('status', status)
|
testPath: test_path,
|
||||||
writer.write_key_value('duration', time)
|
status: status,
|
||||||
writer.write_key_value('createdAt', Time.now.to_s)
|
duration: time,
|
||||||
writer.write_key_value('stderr', e)
|
createdAt: Time.now.to_s,
|
||||||
writer.write_key_value('stdout', nil)
|
stderr: e,
|
||||||
end
|
stdout: nil
|
||||||
|
}
|
||||||
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -1457,7 +1459,7 @@ module Test
|
|||||||
write_new_line
|
write_new_line
|
||||||
end
|
end
|
||||||
|
|
||||||
def write_object
|
def write_object obj
|
||||||
if @is_first_obj
|
if @is_first_obj
|
||||||
@is_first_obj = false
|
@is_first_obj = false
|
||||||
else
|
else
|
||||||
@ -1465,15 +1467,7 @@ module Test
|
|||||||
write_new_line
|
write_new_line
|
||||||
end
|
end
|
||||||
@indent_level += 1
|
@indent_level += 1
|
||||||
write_indent
|
@file.write(to_json_str(obj))
|
||||||
@file.write("{")
|
|
||||||
write_new_line
|
|
||||||
@indent_level += 1
|
|
||||||
yield
|
|
||||||
@indent_level -= 1
|
|
||||||
write_new_line
|
|
||||||
write_indent
|
|
||||||
@file.write("}")
|
|
||||||
@indent_level -= 1
|
@indent_level -= 1
|
||||||
@is_first_key_val = true
|
@is_first_key_val = true
|
||||||
# Occasionally, invalid JSON will be created as shown below, especially when `--repeat-count` is specified.
|
# 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)
|
def write_array(key)
|
||||||
@indent_level += 1
|
@indent_level += 1
|
||||||
write_indent
|
|
||||||
@file.write(to_json_str(key))
|
@file.write(to_json_str(key))
|
||||||
write_colon
|
write_colon
|
||||||
@file.write(" ", "[")
|
@file.write(" ", "[")
|
||||||
write_new_line
|
write_new_line
|
||||||
end
|
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
|
def close
|
||||||
return if @file.closed?
|
return if @file.closed?
|
||||||
close_array
|
close_array
|
||||||
@indent_level -= 1
|
@indent_level -= 1
|
||||||
write_new_line
|
write_new_line
|
||||||
@file.write("}")
|
@file.write("}", "\n")
|
||||||
@file.flush
|
@file.flush
|
||||||
@file.close
|
@file.close
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def to_json_str(obj)
|
def to_json_str(obj)
|
||||||
JSON.dump(obj)
|
json = JSON.pretty_generate(obj)
|
||||||
|
json.gsub(/^/, ' ' * (2 * @indent_level))
|
||||||
end
|
end
|
||||||
|
|
||||||
def write_indent
|
def write_indent
|
||||||
|
57
tool/test/testunit/test_launchable.rb
Normal file
57
tool/test/testunit/test_launchable.rb
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user