Class: Dev::Coverage::Cobertura

Inherits:
Base show all
Defined in:
lib/firespring_dev_commands/coverage/cobertura.rb

Overview

Class for checking code coverage using cobertura

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#node_options, #ruby_options

Constructor Details

#initialize(filename: File.join('coverage', 'cobertura.xml'), threshold: nil, container_path: nil, local_path: nil, exclude: nil) ⇒ Cobertura

Returns a new instance of Cobertura.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/firespring_dev_commands/coverage/cobertura.rb', line 8

def initialize(filename: File.join('coverage', 'cobertura.xml'), threshold: nil, container_path: nil, local_path: nil, exclude: nil)
  super()

  @filename = filename
  @local_filename = File.join(local_path || '.', @filename)
  @container_filename = File.join(container_path || '.', @filename)
  @threshold = threshold
  @exclude = (exclude || []).map do |it|
    next it if it.is_a?(Regex)

    Regex.new(it)
  end
end

Instance Attribute Details

#container_filenameObject (readonly)

Returns the value of attribute container_filename.



6
7
8
# File 'lib/firespring_dev_commands/coverage/cobertura.rb', line 6

def container_filename
  @container_filename
end

#excludeObject (readonly)

Returns the value of attribute exclude.



6
7
8
# File 'lib/firespring_dev_commands/coverage/cobertura.rb', line 6

def exclude
  @exclude
end

#filenameObject (readonly)

Returns the value of attribute filename.



6
7
8
# File 'lib/firespring_dev_commands/coverage/cobertura.rb', line 6

def filename
  @filename
end

#local_filenameObject (readonly)

Returns the value of attribute local_filename.



6
7
8
# File 'lib/firespring_dev_commands/coverage/cobertura.rb', line 6

def local_filename
  @local_filename
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



6
7
8
# File 'lib/firespring_dev_commands/coverage/cobertura.rb', line 6

def threshold
  @threshold
end

Instance Method Details

#check(application: nil) ⇒ Object

Parse the cobertura file and check the lines missed against the desired threshold



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/firespring_dev_commands/coverage/cobertura.rb', line 33

def check(application: nil)
  # If an application has been specified and the file does not exist locally, attempt to copy it back from the docker container
  if application && !File.exist?(local_filename)
    container = Dev::Docker::Compose.new.container_by_name(application)
    Dev::Docker.new.copy_from_container(container, container_filename, local_filename, required: true)
  end

  report = Ox.load(File.read(local_filename))
  total_missed = report.coverage.locate('packages/package').sum { |package| parse_package_missed(package) }
  puts "Lines missing coverage was #{total_missed}"
  puts "Configured threshold was #{threshold}" if threshold
  raise 'Code coverage not met' if threshold && total_missed > threshold
end

#php_optionsObject

Remove any previous versions of the local file that will be output return the phpunit options needed to regenerate the cobertura xml file



24
25
26
27
28
29
30
# File 'lib/firespring_dev_commands/coverage/cobertura.rb', line 24

def php_options
  # Remove any previous coverage info
  FileUtils.rm_f(local_filename, verbose: true)

  # Return the needed php commands to generate the cobertura report
  %W(--coverage-cobertura #{container_filename})
end