Giter VIP home page Giter VIP logo

lcov-to-cobertura-xml's Introduction

lcov to cobertura XML converter

CI Docs Security check - Bandit Release

This project does as the name implies: it converts code coverage report files in lcov format to Cobertura's XML report format so that CI servers like Jenkins can aggregate results and determine build stability etc.

Coverage metrics supported:

  • Package/folder overall line and branch coverage
  • Class/file overall line and branch coverage
  • Functions hit
  • Line and Branch hits

Quick usage

Grab it raw and run it with python:

python lcov_cobertura.py lcov-file.dat
  • -b/--base-dir - (Optional) Directory where source files are located. Defaults to the current directory
  • -e/--excludes - (Optional) Comma-separated list of regexes of packages to exclude
  • -o/--output - (Optional) Path to store cobertura xml file. Defaults to ./coverage.xml
  • -d/--demangle - (Optional) Demangle C++ function names. Requires c++filt
python lcov_cobertura.py lcov-file.dat --base-dir src/dir --excludes test.lib --output build/coverage.xml --demangle

With pip:

pip install lcov_cobertura

Command-line usage

lcov_cobertura lcov-file.dat
  • -b/--base-dir - (Optional) Directory where source files are located. Defaults to the current directory
  • -e/--excludes - (Optional) Comma-separated list of regexes of packages to exclude
  • -o/--output - (Optional) Path to store cobertura xml file. Defaults to ./coverage.xml
  • -d/--demangle - (Optional) Demangle C++ function names. Requires c++filt
lcov_cobertura lcov-file.dat --base-dir src/dir --excludes test.lib --output build/coverage.xml --demangle

Usage as a Python module

Use it anywhere in your python:

from lcov_cobertura import LcovCobertura

LCOV_INPUT = 'SF:foo/file.ext\nDA:1,1\nDA:2,0\nend_of_record\n'
converter = LcovCobertura(LCOV_INPUT)
cobertura_xml = converter.convert()
print(cobertura_xml)

Environment Support

Python 3.8+ is supported. The last release with Python 2.x support is version 1.6.

Contributions

This project is made possible due to the efforts of these fine people:

License

This project is provided under the Apache License, Version 2.0.

lcov-to-cobertura-xml's People

Contributors

aconrad avatar aglosband avatar alivenets avatar betaboon avatar bjd avatar chinloyal avatar eriwen avatar hugoarregui avatar joshkel avatar jpschewe avatar nyue avatar sarnold avatar trnl avatar zyv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lcov-to-cobertura-xml's Issues

duplicate matches in `--excludes` will lead to errors

It seems if more that one --excludes wants to match on a single item one gets uncaught exceptions.
Changing it to an error message instead:

diff --git a/lcov_cobertura/lcov_cobertura.py b/lcov_cobertura/lcov_cobertura.py
index 379e405..3faf7a9 100755
--- a/lcov_cobertura/lcov_cobertura.py
+++ b/lcov_cobertura/lcov_cobertura.py
@@ -217,7 +217,10 @@ class LcovCobertura():
         excluded = [x for x in coverage_data['packages'] for e in self.excludes
                     if re.match(e, x)]
         for package in excluded:
-            del coverage_data['packages'][package]
+            try:
+                del coverage_data['packages'][package]
+            except KeyError as ex:
+                print(f"skipping filter for {package} - not found; duplicate exclude pattern match?")
 
         # Compute line coverage rates
         for package_data in list(coverage_data['packages'].values()):
@@ -425,8 +428,8 @@ def main(argv=None):
             cobertura_xml = lcov_cobertura.convert()
         with open(options.output, mode='wt') as output_file:
             output_file.write(cobertura_xml)
-    except IOError:
-        sys.stderr.write("Unable to convert %s to Cobertura XML" % args[1])
+    except IOError as ex:
+        sys.stderr.write("Unable to convert %s to Cobertura XML %s" % (args[1], ex))
 
 
 if __name__ == '__main__':

distutils.spawn is deprecated, gonna be removed

/tmp/./lcov_cobertura.py:21: DeprecationWarning: The distutils package is deprecated and slated for removal in Python 3.12. Use setuptools or check PEP 632 for potential alternatives
  from distutils.spawn import find_executable

gitlab-ci can't read .xml file, which is conversion by lcov-to-cobertura-xml

I have a .lcov file generated by llvm-cov.

/usr/lib/llvm-8/bin/llvm-cov export -format=lcov -Xdemangler c++filt -Xdemangler -n -instr-profile=xxx.profdata -object=build/bin/xxx > worker_ut.lcov

python3 /usr/local/lib/python3.9/dist-packages/lcov_cobertura/lcov_cobertura.py xxxx.lcov

When i use it in gitlab-ci:

coverage: /^\s*lines:\s*\d+.\d+\%/
  artifacts:
    expire_in: 1 days
    reports:
      cobertura: xxx.xml
      junit: xxxx.xml

But it doesn't work, Test is always 0

Python 3 error with --demangle

When I use --demangle I get:

Traceback (most recent call last):
  File "/usr/local/bin/lcov_cobertura", line 9, in <module>
    load_entry_point('lcov-cobertura==1.6', 'console_scripts', 'lcov_cobertura')()
  File "/usr/local/lib/python3.4/dist-packages/lcov_cobertura.py", line 407, in main
    cobertura_xml = lcov_cobertura.convert()
  File "/usr/local/lib/python3.4/dist-packages/lcov_cobertura.py", line 86, in convert
    return self.generate_cobertura_xml(coverage_data)
  File "/usr/local/lib/python3.4/dist-packages/lcov_cobertura.py", line 282, in generate_cobertura_xml
    'name': self.format(method_name),
  File "/usr/local/lib/python3.4/dist-packages/lcov_cobertura.py", line 38, in demangle
    self.pipe.stdin.write(name + "\n")
TypeError: 'str' does not support the buffer interface

I installed the latest master version via pip install git+https://github.com/eriwen/lcov-to-cobertura-xml.git

Missing function & statement metrics

I ran lcov-to-cobertura against a lcov results file and while the conversion worked (i.e. coverage.xml was produced), I do not see function and statement summary metrics in this file. Note, I do see branch and line summary metrics:

<coverage branch-rate="0.6012965964343598" branches-covered="742" branches-valid="1234" complexity="0" line-rate="0.703875968992248" lines-covered="1362" lines-valid="1935" timestamp="1505846593" version="2.0.3">

Sonarqube report version 1

Hi, I'm getting this error on sonarqube server:
Unknown report version: 2.0.3. This parser only handles version 1.

Not sure about this: is "report version 2.0.3" the one generated by lcov-to-cobertura converter ?
Is there any way to bump to version 1 or I have to upgrade my sonarqube server ?

Thank you in advance !

some problems using it from command line

Hi,

I have a few problems running lcov_covertura from command line, I wrote a patch with some small changes.

diff --git a/lcov_cobertura/lcov_cobertura.py b/lcov_cobertura/lcov_cobertura.py
index f80c0a5..432cbd0 100755
--- a/lcov_cobertura/lcov_cobertura.py
+++ b/lcov_cobertura/lcov_cobertura.py
@@ -319,14 +319,14 @@ if __name__ == '__main__':
                           dest='output', default='coverage.xml')
         (options, args) = parser.parse_args(args=argv)

-        if len(argv) != 2:
-            print((main.__doc__))
-            sys.exit(1)
+        #if len(argv) != 2:
+            #print((main.__doc__))
+            #sys.exit(1)

         try:
             with open(args[1], 'r') as lcov_file:
                 lcov_data = lcov_file.read()
-                lcov_cobertura = LcovCobertura(lcov_data, options)
+                lcov_cobertura = LcovCobertura(lcov_data, options.base_dir)
                 cobertura_xml = lcov_cobertura.convert()
             with open(options.output, mode='wt') as output_file:
                 output_file.write(cobertura_xml)

I just comment the lines that checks argv len, because I don't figure out if it's a needed check. But I want to add, if it's necesary, you need to be carefoul about how the program is invoked,

./lcov_coverage
python lcov_coverage

has differents argv values.

Thanks for this amazing tool!

Cannot import name 'LcovCobertura' from 'lcov_cobertura' (unknown location)

Hi,

With the new version 2 release I am getting the following ImportError:

  File "/home/jschwender/xxx/CI/scripts/./convert_lcov_to_cobertura.py", line 5, in <module>
    from lcov_cobertura import LcovCobertura
ImportError: cannot import name 'LcovCobertura' from 'lcov_cobertura' (unknown location)

I'm using python3.9. After downgrading back to version 1.6 the import works again. Any changes I should be aware of? The Readme still recommends to use from lcov_cobertura import LcovCobertura, is this wrong now?

Index Out Of bounds

lcov: LCOV version 1.10
Running the script:

Traceback (most recent call last):
  File "lcov_cobertura.py", line 359, in <module>
    main(sys.argv)
  File "lcov_cobertura.py", line 353, in main
    cobertura_xml = lcov_cobertura.convert()
  File "lcov_cobertura.py", line 55, in convert
    coverage_data = self.parse()
  File "lcov_cobertura.py", line 162, in parse
    function_name = line_parts[-1].strip().split(',')[1]
IndexError: list index out of range

genhtml creates correct html from the coverage.info

Discrepancy with genhtml

When I generate an HTML report with genhtml I get 5623/7013 lines covered in total, which is 80%.

Looking at the xml file produced by this library the root entry has lines-covered="10233" lines-valid="23111", which is 44%.

How can such a situation occur?

Minidom giving memory error

MiniDom giving memory error. Can this script lcov-to-cobertura-xml be written using xml.etree instead of minidom?

KeyError: 'hits'

I've got this exception KeyError: 'hits'

In my coverage_data, some class_data['lines'][line_number] just like this:

{'branch-conditions-covered': 0, 'branch-conditions-total': 2, 'branch': 'true'}
This line could not be parsed correctly.
But this one is ok:
{'hits': '0', 'branch-conditions-covered': 0, 'branch-conditions-total': 0, 'branch': 'false'}

Hangs in python3 with --demangle

Works in python2.

I tried in python3 it hangs and no visible error messages. So I removed the --demangle argument and it does not hang and it produced the xml.

Thanks men.

Segmentation fault

Is there a limit on the file size or if files are combined? I get segmentation fault

The file is generated by adding two files
lcov -a file1.info -a file2.info -o file.info

$ ls -lh file.info
-rw-rw-r--. 1 centos centos 23M Oct 10 17:00 file.info

$ python lcov_cobertura.py file.info
Segmentation fault

If i use a smaller file/file1 or file2 above it works

$ python lcov_cobertura.py file1.info

$ python lcov_cobertura.py file2.info

Incorrect line hit counts with multiple files

When using lcov-to-cobertura-xml on a lcov file that contains coverage results for multiple source files, the line hit counts in the produced XML are the same for each source file. They match the line hit counts of the last source file mentioned in the lcov file.

Seen on Ubuntu 10.04 with Pyton 2.6.5 and Ubuntu 11.10 with Pyton 2.7.2

Conditionals and method coverage always shows 0/0

When I generate an xml and export it to Jenkins (Cobertura plugin) I always get 100% (0/0) coverage for methods and conditionals. All other stats are non-zero as expected. Produced xml contains methods data.
What might be the problem?

How to use lcov_cobertura file from a LICENSE perspective?

It says in the Py file itself that the license is in the accompanied LICENSE.md file. However, I can only find a LICENSE file (without md), so this is hard to accomplish.

So should I then copy the py file to some subdir and then copy the LICENSE file to LICENSE.md into the same directory or what do you need? We'd be interested in using that file for internal processes only (test reporting, not shipping this to customers).

Also: If I copy the file as that, there is no link or anything, so how can this be traced back to you / your repo?

Needs a release

Related to #23, this package needs a new release for the command line version.

CLI entry point regression

Between 1.6 and 2.0 there has been a regression w.r.t. the command line entry point. Doing python -m lcov_cobertura would work in 1.6, but produce the following error in 2.0:

../bin/python: No module named lcov_cobertura.__main__; 'lcov_cobertura' is a package and cannot be directly executed

Most probably an accidental side effect of the setup.py -> setup.cfg transition.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.