Skip to content

Commit 52431ca

Browse files
ono-maxandrykonchin
authored andcommitted
Launchable: Start recording test-spec results (#12302)
1 parent c600b8f commit 52431ca

File tree

4 files changed

+107
-1
lines changed

4 files changed

+107
-1
lines changed

lib/mspec/commands/mspec-run.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ def options(argv = ARGV)
5353
options.doc "\n When to perform it"
5454
options.action_filters
5555

56+
options.doc "\n Launchable"
57+
options.launchable
58+
5659
options.doc "\n Help!"
5760
options.debug
5861
options.version MSpec::VERSION
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
module LaunchableFormatter
2+
def self.extend_object(obj)
3+
super
4+
obj.init
5+
end
6+
7+
def self.setDir(dir)
8+
@@path = File.join(dir, "#{rand.to_s}.json")
9+
self
10+
end
11+
12+
def init
13+
@timer = nil
14+
@tests = []
15+
end
16+
17+
def before(state = nil)
18+
super
19+
@timer = TimerAction.new
20+
@timer.start
21+
end
22+
23+
def after(state = nil)
24+
super
25+
@timer.finish
26+
file = MSpec.file
27+
return if file.nil? || state&.example.nil? || exception?
28+
29+
@tests << {:test => state, :file => file, :exception => false, duration: @timer.elapsed}
30+
end
31+
32+
def exception(exception)
33+
super
34+
@timer.finish
35+
file = MSpec.file
36+
return if file.nil?
37+
38+
@tests << {:test => exception, :file => file, :exception => true, duration: @timer.elapsed}
39+
end
40+
41+
def finish
42+
super
43+
44+
require_relative '../../../../../../tool/lib/launchable'
45+
46+
@writer = writer = Launchable::JsonStreamWriter.new(@@path)
47+
@writer.write_array('testCases')
48+
at_exit {
49+
@writer.close
50+
}
51+
52+
repo_path = File.expand_path("#{__dir__}/../../../../../../")
53+
54+
@tests.each do |t|
55+
testcase = t[:test].description
56+
relative_path = t[:file].delete_prefix("#{repo_path}/")
57+
# The test path is a URL-encoded representation.
58+
# https://github.com/launchableinc/cli/blob/v1.81.0/launchable/testpath.py#L18
59+
test_path = {file: relative_path, testcase: testcase}.map{|key, val|
60+
"#{encode_test_path_component(key)}=#{encode_test_path_component(val)}"
61+
}.join('#')
62+
63+
status = 'TEST_PASSED'
64+
if t[:exception]
65+
message = t[:test].message
66+
backtrace = t[:test].backtrace
67+
e = "#{message}\n#{backtrace}"
68+
status = 'TEST_FAILED'
69+
end
70+
71+
@writer.write_object(
72+
{
73+
testPath: test_path,
74+
status: status,
75+
duration: t[:duration],
76+
createdAt: Time.now.to_s,
77+
stderr: e,
78+
stdout: nil
79+
}
80+
)
81+
end
82+
end
83+
84+
private
85+
def encode_test_path_component component
86+
component.to_s.gsub('%', '%25').gsub('=', '%3D').gsub('#', '%23').gsub('&', '%26').tr("\x00-\x08", "")
87+
end
88+
end

lib/mspec/utils/options.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,14 @@ def debug
489489
end
490490
end
491491

492+
def launchable
493+
on("--launchable-test-reports", "DIR",
494+
"DIR The directory for reporting test results in Launchable JSON format") do |o|
495+
require 'mspec/runner/formatters/launchable'
496+
config[:launchable] = LaunchableFormatter.setDir(o)
497+
end
498+
end
499+
492500
def all
493501
configure {}
494502
env
@@ -508,5 +516,6 @@ def all
508516
action_filters
509517
actions
510518
debug
519+
launchable
511520
end
512521
end

lib/mspec/utils/script.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,14 @@ def config_formatter
159159
end
160160

161161
if config[:formatter]
162-
config[:formatter].new(config[:output])
162+
config[:formatter] = config[:formatter].new(config[:output])
163163
end
164+
165+
if config[:launchable]
166+
config[:formatter].extend config[:launchable]
167+
end
168+
169+
config[:formatter]
164170
end
165171

166172
# Callback for enabling custom actions, etc. This version is a

0 commit comments

Comments
 (0)