Class: RspecSonarqubeFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_sonarqube_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ RspecSonarqubeFormatter

Returns a new instance of RspecSonarqubeFormatter.



9
10
11
12
13
# File 'lib/rspec_sonarqube_formatter.rb', line 9

def initialize(output)
  @output             = output
  @current_file       = ''
  @last_failure_index = 0
end

Instance Method Details

#clean_string(input) ⇒ Object



54
55
56
# 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) ⇒ Object



58
59
60
# File 'lib/rspec_sonarqube_formatter.rb', line 58

def duration(example)
  (example.execution_result.run_time.to_f * 1000).round
end

#example_failed(notification) ⇒ Object



42
43
44
45
46
# 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) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/rspec_sonarqube_formatter.rb', line 25

def example_group_started(notification)
  return if notification.group.[:file_path] == @current_file

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

  @current_file = notification.group.[:file_path]
end

#example_passed(notification) ⇒ Object



38
39
40
# 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) ⇒ Object



48
49
50
51
52
# 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) ⇒ Object



34
35
36
# File 'lib/rspec_sonarqube_formatter.rb', line 34

def example_started(_notification)
  # Do nothing
end

#start(_notification) ⇒ Object



15
16
17
18
# 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) ⇒ Object



20
21
22
23
# File 'lib/rspec_sonarqube_formatter.rb', line 20

def stop(_notification)
  @output.puts '  </file>' if @current_file != ''
  @output.puts '</testExecutions>'
end