rspec-ctrf 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +17 -0
  4. data/lib/ctrf/r_spec_formatter.rb +102 -0
  5. metadata +147 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9d0a53813aa0abbea29ef8568f6d15a5e8ea8b8db9a64152823218f302317eab
4
+ data.tar.gz: 4d9036e2272ff15f74cb5bd5f9e4c1cfe10e51d9e92319e8563083143e7b2d58
5
+ SHA512:
6
+ metadata.gz: bbbabb7f07561c6c484c43010f3a6c11d8b2ed1fdd380be3c09107fe7dae88b4b7c9014bb37ee0c0655c3ec20ea65bda92abaffba66f826c28f34797365a2281
7
+ data.tar.gz: 663087ca68280ad727006f0bcb0bbbac66ffd929e23582a2f6b63adcf5058ebe74511bb339a836f08b980f06ee8e531fcb3c55f44f01affd61be5435a5399f17
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Down Under Software Consulting
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # RSpec-CTRF
2
+
3
+ RSpec formatter to output test results in CTRF (https://www.ctrf.io/) JSON format.
4
+
5
+ ## Usage
6
+
7
+ Add to Gemfile:
8
+
9
+ ```ruby
10
+ gem "rspec-ctrf"
11
+ ```
12
+
13
+ Add formatter to either `.rspec` or CLI:
14
+
15
+ ```
16
+ rspec --format Ctrf::RSpecFormatter --out <path>.ctrf.json
17
+ ```
@@ -0,0 +1,102 @@
1
+ require 'json'
2
+
3
+ module Ctrf
4
+ class RSpecFormatter
5
+ RSpec::Core::Formatters.register self, :start, :stop, :example_started, :example_finished
6
+
7
+ attr_reader :output
8
+
9
+ def initialize(output)
10
+ @output = output
11
+ end
12
+
13
+ def start(_notification)
14
+ @tests = {}
15
+ @run_start = Time.now.to_i
16
+ end
17
+
18
+ def example_started(notification)
19
+ @retries = 0
20
+
21
+ # Set up the object for use if it doesn't exist already
22
+ # It might, if retries
23
+ return if @tests.key?(notification.example.hash)
24
+
25
+ @tests[notification.example.hash] = {
26
+ name: notification.example.full_description,
27
+ suite: notification.example.example_group.name,
28
+ # We use id here because it's location + test scope, e.g. what we need to rerun the test
29
+ # Otherwise, this could is the location of the `it` - even if that's in a helper file unrelated to the test
30
+ filePath: notification.example.id,
31
+ extra: {
32
+ hash: notification.example.hash
33
+ }
34
+ }
35
+ end
36
+
37
+ # Gets hit if rspec-retry is enabled, for flaky support
38
+ def retry(example)
39
+ @retries += 1
40
+
41
+ # We add the previous traces here, because we only hit example_finished once
42
+ test = @tests[example.hash]
43
+ test[:extra][:previous_traces] = [] unless test[:extra].key?(:previous_traces)
44
+ backtrace_formatter = RSpec.configuration.backtrace_formatter
45
+ return unless example.exception
46
+
47
+ test[:extra][:previous_traces].push("#{example.exception.detailed_message}\n" + backtrace_formatter.format_backtrace(
48
+ example.exception.backtrace, example.metadata
49
+ ).join("\n"))
50
+ end
51
+
52
+ def example_finished(notification)
53
+ test = @tests[notification.example.hash]
54
+
55
+ test[:duration] = notification.example.execution_result.run_time * 1000
56
+ test[:start] = notification.example.execution_result.started_at.to_i
57
+ test[:stop] = notification.example.execution_result.finished_at.to_i
58
+
59
+ test[:status] = notification.example.execution_result.status.to_s
60
+ test[:rawStatus] = notification.example.execution_result.status
61
+
62
+ # If we passed but retries is >0, the test is flaky
63
+ test[:flaky] = test[:status] == 'passed' && @retries > 0
64
+ test[:retries] = @retries
65
+
66
+ # Attach error information, if any
67
+ exception = notification.example.exception
68
+ test[:message] = exception.detailed_message if exception
69
+
70
+ # If this wasn't the first run, move the trace to extra
71
+ if test.key?(:trace)
72
+ test[:extra][:previous_traces] = [] unless test[:extra].key?(:previous_traces)
73
+ test.delete(:trace)
74
+ end
75
+ test[:trace] = notification.formatted_backtrace.join("\n") if exception
76
+ end
77
+
78
+ def stop(_notification)
79
+ # Build the final result object
80
+ result = {
81
+ results: {
82
+ tool: {
83
+ name: 'rspec'
84
+ },
85
+ summary: {
86
+ tests: @tests.count,
87
+ passed: @tests.values.select { |test| test[:status] == 'passed' }.count,
88
+ failed: @tests.values.select { |test| test[:status] == 'failed' }.count,
89
+ pending: @tests.values.select { |test| test[:status] == 'pending' }.count,
90
+ skipped: @tests.values.select { |test| test[:status] == 'skipped' }.count,
91
+ other: @tests.values.select { |test| test[:status] == 'other' }.count,
92
+ start: @run_start,
93
+ stop: Time.now.to_i
94
+ },
95
+ tests: @tests.values
96
+ }
97
+ }
98
+
99
+ @output.write(JSON.pretty_generate(result))
100
+ end
101
+ end
102
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-ctrf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - James Meneghello
8
+ - DUSC
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2025-01-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '3.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '3.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '13.2'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '13.2'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec-json_matchers
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rubocop
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: 1.61.0
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: 1.61.0
84
+ - !ruby/object:Gem::Dependency
85
+ name: rubocop-rake
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: 0.6.0
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: 0.6.0
98
+ - !ruby/object:Gem::Dependency
99
+ name: rubocop-rspec
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: 2.25.0
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: 2.25.0
112
+ description: A custom RSpec formatter that supports outputting of test results in
113
+ CTRF (https://www.ctrf.io/) JSON format, including support for flaky tests.
114
+ email: james@dusc.dev
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - LICENSE
120
+ - README.md
121
+ - lib/ctrf/r_spec_formatter.rb
122
+ homepage: https://github.com/dusc-dev/rspec-ctrf
123
+ licenses:
124
+ - MIT
125
+ metadata:
126
+ rubygems_mfa_required: 'true'
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '3.0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubygems_version: 3.5.23
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: RSpec formatter to output test results in CTRF (https://www.ctrf.io/) JSON
146
+ format.
147
+ test_files: []