Class: OpenC3::Ctrf

Inherits:
Object show all
Defined in:
lib/openc3/utilities/ctrf.rb

Overview

Utility class for converting COSMOS script reports to CTRF (Common Test Report Format) See ctrf.io/docs/category/specification

Class Method Summary collapse

Class Method Details

.convert_report(report_content, version: OpenC3::VERSION) ⇒ Hash

Convert a COSMOS plain text script report to CTRF JSON format

Parameters:

  • report_content (String)

    Plain text script report

  • version (String) (defaults to: OpenC3::VERSION)

    Version string to include in CTRF output (defaults to OpenC3::VERSION)

Returns:

  • (Hash)

    CTRF formatted report as a Ruby hash



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/openc3/utilities/ctrf.rb', line 25

def self.convert_report(report_content, version: OpenC3::VERSION)
  lines = report_content.split("\n")
  tests = []
  summary = {}
  settings = {}
  in_settings = false
  last_result = nil
  in_summary = false

  lines.each do |line|
    next if line.nil?
    line_clean = line.strip

    if line_clean == 'Settings:'
      in_settings = true
      next
    end

    if in_settings
      if line_clean.include?('Manual')
        parts = line.split('=')
        settings[:manual] = parts[1].strip if parts[1]
        next
      elsif line_clean.include?('Pause on Error')
        parts = line.split('=')
        settings[:pauseOnError] = parts[1].strip if parts[1]
        next
      elsif line_clean.include?('Continue After Error')
        parts = line.split('=')
        settings[:continueAfterError] = parts[1].strip if parts[1]
        next
      elsif line_clean.include?('Abort After Error')
        parts = line.split('=')
        settings[:abortAfterError] = parts[1].strip if parts[1]
        next
      elsif line_clean.include?('Loop =')
        parts = line.split('=')
        settings[:loop] = parts[1].strip if parts[1]
        next
      elsif line_clean.include?('Break Loop On Error')
        parts = line.split('=')
        settings[:breakLoopOnError] = parts[1].strip if parts[1]
        in_settings = false
        next
      end
    end

    if line_clean == 'Results:'
      last_result = line_clean
      next
    end

    if last_result
      # The first line should always have a timestamp and what it is executing
      # Format: "2026-04-02T19:45:41.228209Z: Executing MySuite:ExampleGroup:script_2"
      if last_result == 'Results:' and line_clean.include?("Executing")
        # Split on first ': ' to separate timestamp from message
        timestamp_and_msg = line_clean.split(': ', 2)
        if timestamp_and_msg.length >= 2
          timestamp = timestamp_and_msg[0]
          begin
            summary[:startTime] = DateTime.parse(timestamp).to_time.to_f * 1000
          rescue Date::Error
            # Skip malformed timestamps
          end
        end
        last_result = line_clean
        next
      end

      # Format: "2026-04-02T19:45:44.041472Z: ExampleGroup:script_2:PASS"
      # Check if line contains a test result (but not Executing or Completed)
      if !line_clean.include?("Executing") && !line_clean.include?("Completed")
        # Try to parse as a test result line
        timestamp_and_msg = line_clean.split(': ', 2)
        if timestamp_and_msg.length >= 2
          result_string = timestamp_and_msg[1]
          # Must have at least 2 colons for group:name:status format
          result_parts = result_string.split(':')
          if result_parts.length >= 3
            # Get start time from last_result - could be Executing line or previous test result
            start_time = nil
            if last_result
              last_timestamp_and_msg = last_result.split(': ', 2)
              if last_timestamp_and_msg.length >= 2
                begin
                  start_time = DateTime.parse(last_timestamp_and_msg[0]).to_time.to_f * 1000
                rescue Date::Error
                  # Skip malformed timestamps
                end
              end
            end

            # Parse current line timestamp
            timestamp = timestamp_and_msg[0]
            begin
              end_time = DateTime.parse(timestamp).to_time.to_f * 1000
            rescue Date::Error
              last_result = line_clean
              next # Skip lines with malformed timestamps
            end

            # Parse the test result: ExampleGroup:script_2:PASS
            suite_group = result_parts[0]
            name = result_parts[1]
            status = result_parts[2]

            format_status = case status
            when 'PASS'
              'passed'
            when 'SKIP'
              'skipped'
            when 'FAIL'
              'failed'
            else
              'unknown'
            end

            tests << {
              name: "#{suite_group}:#{name}",
              status: format_status,
              duration: start_time ? (end_time - start_time) : 0,
            }
            last_result = line_clean
            next
          end
        end
      end

      # Format: "2026-04-02T19:45:44.044982Z: Completed MySuite:ExampleGroup:script_2"
      if line_clean.include?("Completed")
        timestamp_and_msg = line_clean.split(': ', 2)
        if timestamp_and_msg.length >= 2
          timestamp = timestamp_and_msg[0]
          begin
            summary[:stopTime] = DateTime.parse(timestamp).to_time.to_f * 1000
          rescue Date::Error
            # Skip malformed timestamps
          end
        end
        last_result = nil
        next
      end
    end

    if line_clean == '--- Test Summary ---'
      in_summary = true
      next
    end

    if in_summary
      if line_clean.include?("Total Tests")
        parts = line_clean.split(':')
        summary[:total] = parts[1].to_i if parts[1]
      end
      if line_clean.include?("Pass:")
        parts = line_clean.split(':')
        summary[:passed] = parts[1].to_i if parts[1]
      end
      if line_clean.include?("Skip:")
        parts = line_clean.split(':')
        summary[:skipped] = parts[1].to_i if parts[1]
      end
      if line_clean.include?("Fail:")
        parts = line_clean.split(':')
        summary[:failed] = parts[1].to_i if parts[1]
      end
    end
  end

  # Build CTRF report
  # See https://ctrf.io/docs/specification/root
  return {
    reportFormat: "CTRF",
    results: {
      # See https://ctrf.io/docs/specification/tool
      tool: {
        name: "COSMOS Script Runner",
        version: version,
      },
      # See https://ctrf.io/docs/specification/summary
      summary: {
        tests: summary[:total],
        passed: summary[:passed],
        failed: summary[:failed],
        pending: 0,
        skipped: summary[:skipped],
        other: 0,
        start: summary[:startTime],
        stop: summary[:stopTime],
      },
      # See https://ctrf.io/docs/specification/tests
      tests: tests,
      # See https://ctrf.io/docs/specification/extra
      extra: {
        manual: settings[:manual],
        pauseOnError: settings[:pauseOnError],
        continueAfterError: settings[:continueAfterError],
        abortAfterError: settings[:abortAfterError],
        loop: settings[:loop],
        breakLoopOnError: settings[:breakLoopOnError],
      },
    },
  }
end