|
| 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 |
0 commit comments