Skip to content

Commit

Permalink
Support SimpleCov.root being "/" (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mange authored Sep 28, 2020
1 parent d9358bd commit c6fa2c2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/simplecov-cobertura.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'rexml/document'
require 'rexml/element'
require 'pathname'

require_relative 'simplecov-cobertura/version'

Expand Down Expand Up @@ -94,9 +95,8 @@ def set_package_attributes(package, name, result)

def set_class_attributes(class_, file)
filename = file.filename
path = filename[SimpleCov.root.length+1..-1]
class_.attributes['name'] = File.basename(filename, '.*')
class_.attributes['filename'] = path
class_.attributes['filename'] = resolve_filename(filename)
class_.attributes['line-rate'] = (file.covered_percent/100).round(2).to_s
class_.attributes['branch-rate'] = '0'
class_.attributes['complexity'] = '0'
Expand All @@ -116,7 +116,15 @@ def set_xml_head(lines=[])
end

def coverage_output(result)
"#{result.covered_lines} / #{result.covered_lines + result.missed_lines} LOC (#{result.covered_percent.round(2)}%) covered."
"#{result.covered_lines} / #{result.covered_lines + result.missed_lines} LOC (#{result.covered_percent.round(2)}%) covered."
end

def resolve_filename(filename)
Pathname.new(filename).relative_path_from(project_root).to_s
end

def project_root
@project_root ||= Pathname.new(SimpleCov.root)
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions test/simplecov-cobertura_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,21 @@ def test_groups
assert_equal 'false', last_line.attribute('branch').value
assert_equal '2', last_line.attribute('hits').value
end

def test_supports_root_project_path
old_root = SimpleCov.root
SimpleCov.root('/')
expected_base = old_root[1..-1] # Remove leading "/"

xml = @formatter.format(@result)
doc = Nokogiri::XML::Document.parse(xml)

classes = doc.xpath '/coverage/packages/package/classes/class'
assert_equal 1, classes.length
clazz = classes.first
assert_equal 'simplecov-cobertura_test', clazz.attribute('name').value
assert_equal "#{expected_base}/test/simplecov-cobertura_test.rb", clazz.attribute('filename').value
ensure
SimpleCov.root(old_root)
end
end

0 comments on commit c6fa2c2

Please sign in to comment.