class RspecSonarqubeFormatter

Public Class Methods

new(output) click to toggle source
# File lib/rspec_sonarqube_formatter.rb, line 9
def initialize(output)
  @output             = output
  @current_file       = ''
  @last_failure_index = 0
end

Public Instance Methods

clean_string(input) click to toggle source
# File lib/rspec_sonarqube_formatter.rb, line 54
def clean_string(input)
  HTMLEntities.new.encode input.to_s.gsub(/\e\[\d;*\d*m/, '').tr('"', "'")
end
duration(example) click to toggle source
# File lib/rspec_sonarqube_formatter.rb, line 58
def duration(example)
  (example.execution_result.run_time.to_f * 1000).round
end
example_failed(notification) click to toggle source
# File lib/rspec_sonarqube_formatter.rb, line 42
def example_failed(notification)
  @output.puts "    <testCase name=\"#{clean_string(notification.example.description)}\" duration=\"#{duration(notification.example)}\">"
  @output.puts "      <failure message=\"#{clean_string(notification.exception)}\" stacktrace=\"#{clean_string(notification.example.location)}\" />"
  @output.puts '    </testCase>'
end
example_group_started(notification) click to toggle source
# File lib/rspec_sonarqube_formatter.rb, line 25
def example_group_started(notification)
  return if notification.group.metadata[:file_path] == @current_file

  @output.puts '  </file>' if @current_file != ''
  @output.puts "  <file path=\"#{notification.group.metadata[:file_path]}\">"

  @current_file = notification.group.metadata[:file_path]
end
example_passed(notification) click to toggle source
# File lib/rspec_sonarqube_formatter.rb, line 38
def example_passed(notification)
  @output.puts "    <testCase name=\"#{clean_string(notification.example.description)}\" duration=\"#{duration(notification.example)}\" />"
end
example_pending(notification) click to toggle source
# File lib/rspec_sonarqube_formatter.rb, line 48
def example_pending(notification)
  @output.puts "    <testCase name=\"#{clean_string(notification.example.description)}\" duration=\"#{duration(notification.example)}\">"
  @output.puts "      <skipped message=\"#{clean_string(notification.example.execution_result.pending_message)}\" />"
  @output.puts '    </testCase>'
end
example_started(_notification) click to toggle source
# File lib/rspec_sonarqube_formatter.rb, line 34
def example_started(_notification)
  # Do nothing
end
start(_notification) click to toggle source
# File lib/rspec_sonarqube_formatter.rb, line 15
def start(_notification)
  @output.puts '<?xml version="1.0" encoding="UTF-8"?>'
  @output.puts '<testExecutions version="1">'
end
stop(_notification) click to toggle source
# File lib/rspec_sonarqube_formatter.rb, line 20
def stop(_notification)
  @output.puts '  </file>' if @current_file != ''
  @output.puts '</testExecutions>'
end