dashingrocket / simplecov-cobertura

Ruby SimpleCov Cobertura Formatter
Apache License 2.0
54 stars 38 forks source link

Generated filename attribute is invalid when project root is machine root #13

Closed Mange closed 4 years ago

Mange commented 4 years ago

Assuming SimpleCov.root is /, which could happen in some forms of container builds, then the generated filename attributes are cut off incorrectly.

Example:

Full path Expected result Actual result
/foo/bar.rb foo/bar.rb oo/bar.rb

This happens because the code that tries to strip away the project root assumes that it must be at least 1 character before the path separator, which is not true with a path like "/".

filename = file.filename

# Here is the problem; the +1 assumes that SimpleCov.root.length is just before
# the path separator, while for the case of "/" it is actually *on* the path
# separator.
path = filename[SimpleCov.root.length+1..-1]

class_.attributes['name'] = File.basename(filename, '.*')
class_.attributes['filename'] = path

The real reason I get this

I'm trying to set up code coverage reporting on GitLab, which does not support the <source> element and instead requires the filename to be the full path to the file from the repo root.

However, this repo contains several projects and so the actual project root is not at the repo root. This would not have been a problem, as I could just adjust the SimpleCov.root, but the tests run inside a container where the app is just one directory below the root (/myapp). So I have to set the root to / to get it to include the myapp/ part in the filename so Gitlab can resolve the file from <repo root>/myapp/foo/bar.rb.

You could say this is a perfect storm of edge cases, and I would agree.

jessebs commented 4 years ago

Thanks for the ticket and PR. I'll review the PR the first chance I get in the next couple of days.

jessebs commented 4 years ago

Resolved in 1.4.1 which has been released to rubygems.org

Mange commented 4 years ago

Thank you! :heart: