Module: Polyrun::Reporting::RspecJunit

Defined in:
lib/polyrun/reporting/rspec_junit.rb

Overview

CI: emit JUnit XML from RSpec’s JSON formatter output (replaces rspec_junit_formatter).

require "polyrun/reporting/rspec_junit"
Polyrun::Reporting::RspecJunit.install!(only_if: -> { ENV["CI"] })

Ensure .rspec or CLI keeps a human formatter (e.g. documentation) in addition to JSON.

Class Method Summary collapse

Class Method Details

.install!(json_path: "rspec.json", junit_path: "coverage/junit-coverage.xml", only_if: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/polyrun/reporting/rspec_junit.rb', line 14

def install!(json_path: "rspec.json", junit_path: "coverage/junit-coverage.xml", only_if: nil)
  pred = only_if || -> { ENV["CI"] }
  return unless pred.call

  json_abs = File.expand_path(json_path)
  FileUtils.mkdir_p(File.dirname(json_abs))

  require "rspec/core"
  require "rspec/core/formatters/json_formatter"

  RSpec.configure do |config|
    config.add_formatter RSpec::Core::Formatters::JsonFormatter, json_abs
  end

  at_exit do
    next unless pred.call

    FileUtils.mkdir_p(File.dirname(File.expand_path(junit_path)))
    if File.file?(json_abs)
      Junit.write_from_json_file(json_abs, output_path: junit_path)
    end
  end
end