Skip to content

Commit

Permalink
Add 'py2lcov --cmd exe ..' option, so user can specify the name of th…
Browse files Browse the repository at this point in the history
…eir Coverage.py execuable.

Default is retrieved from 'COVERAGE_COMMAND' environment variable, or is set to 'coverage' otherwise.
See #347

Signed-off-by: Henry Cox <[email protected]>
  • Loading branch information
henry2cox committed Dec 18, 2024
1 parent 77d1a9b commit 5ea2780
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/run_test_suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ jobs:
sudo perl -MCPAN -e 'install(Memory::Process)' # no package in Ubuntu
sudo ln -s python3-coverage /usr/bin/coverage # until issue #347 is fixed
- name: make install
run: |-
set -x -o pipefail
Expand Down
23 changes: 19 additions & 4 deletions bin/py2lcov
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,19 @@ first line of the function has a non-zero count.
Best practice is to either always specify '--no-functions' or never specify
'--no-functions'.
Note that xml2lcov does not implement the full suite of LCOV features
(e.g., filtering, substitutions, etc.).
py2lcov uses Coverage.py to extract coverage data.
Note that the name of the Coverage.py executable my differ on your platform.
By default, py2lcov uses 'coverage' (which it expects to be in your path).
You can use a different executable, either:
- through your COVERAGE_COMMAND environment variable, or
- via the 'py2lcov --cmd exename ..' command line option.
py2lcov does not implement the full suite of LCOV features (e.g., filtering,
substitutions, etc.).
Please generate the translated LCOV format file and then read the data
back in to lcov to use any of those features.
%(usage)s
Example:
$ export PYCOV_DATA=path/to/pydata
Expand All @@ -98,7 +106,6 @@ Example:
# use differential coverage to see exactly what filtering did
$ genhtml -o html_differential --baseline-file mydata.info filtered.info ...
Deprecated feature:
For backward compatibility, py2lcov also supports translation to LCOV
format from intermediate XML:
Expand All @@ -115,6 +122,12 @@ Example:
'usage' : ProcessFile.usageNote,
}

from_env = ''
cover_cmd = 'coverage'
if 'COVERAGE_COMMAND' in os.environ:
cover_cmd = os.environ['COVERAGE_COMMAND']
from_env = ' (from your COVERAGE_COMMAND environment variable)'

parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=usageString)
Expand All @@ -141,6 +154,8 @@ Example:
help='tabsize when computing indent')
parser.add_argument('-k', "--keep-going", dest='keepGoing', default=False, action='store_true',
help="ignore errors")
parser.add_argument('--cmd', dest='cover_cmd', default=cover_cmd,
help='executable used to extract python data - e.g., "python3-coverage". Default is "%s"%s.' % (cover_cmd, from_env))
parser.add_argument('inputs', nargs='*',
help="list of python coverage data input files - expected to be XML or Python .dat format")

Expand Down Expand Up @@ -176,7 +191,7 @@ Example:
while os.path.exists(xml):
xml = base + '.xml%d' % suffix
suffix += 1
cmd = 'COVERAGE_FILE=%s coverage xml -o %s' % (f, xml)
cmd = 'COVERAGE_FILE=%s %s xml -o %s' % (f, args.cover_cmd, xml)
try:
#x = subprocess.run(cmd, capture_output=True, shell=True, check=True)
x = subprocess.run(cmd, shell=True, check=True, stdout=True, stderr=True)
Expand Down
40 changes: 23 additions & 17 deletions tests/py2lcov/py2lcov.sh
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,27 @@ if [[ 1 == $CLEAN_ONLY ]] ; then
exit 0
fi

which coverage
CMD='coverage'
which $CMD
if [ 0 != $? ] ; then
CMD='python3-coverage' # ubuntu?
fi
which $CMD
if [ 0 != $? ] ; then
echo "cannot find 'coverage' or 'python3-coverage'"
echo "unable to run py2lcov - please install python Coverage.py package"
exit 1
fi

# some corner cases:
COVERAGE_FILE=./functions.dat coverage run --branch ./test.py
COVERAGE_FILE=./functions.dat $CMD run --branch ./test.py
if [ 0 != $? ] ; then
echo "coverage functions failed"
if [ 0 == $KEEP_GOING ] ; then
exit 1
fi
fi
eval ${PYCOV} ${PY2LCOV_TOOL} -o functions.info functions.dat $VERSION
eval ${PYCOV} ${PY2LCOV_TOOL} -o functions.info --cmd $CMD functions.dat $VERSION
if [ 0 != $? ] ; then
echo "py2lcov failed function example"
if [ 0 == $KEEP_GOING ] ; then
Expand Down Expand Up @@ -208,7 +214,7 @@ fi


# legacy mode: run with intermediate XML file
COVERAGE_FILE=./functions.dat coverage xml -o functions.xml
COVERAGE_FILE=./functions.dat $CMD xml -o functions.xml
if [ 0 != $? ] ; then
echo "coverage xml failed"
exit 1
Expand All @@ -232,7 +238,7 @@ if [ 0 != $? ] ; then
fi

# run again, generating checksum data...
eval ${PYCOV} ${PY2LCOV_TOOL} -o checksum.info functions.dat $VERSION --checksum
eval ${PYCOV} ${PY2LCOV_TOOL} --cmd $CMD -o checksum.info functions.dat $VERSION --checksum
if [ 0 != $? ] ; then
echo "py2lcov failed function example"
if [ 0 == $KEEP_GOING ] ; then
Expand Down Expand Up @@ -262,7 +268,7 @@ fi


# run without generating function data:
eval ${PYCOV} ${PY2LCOV_TOOL} functions.dat -o no_functions.info $VERSION --no-function
eval ${PYCOV} ${PY2LCOV_TOOL} functions.dat --cmd $CMD -o no_functions.info $VERSION --no-function
if [ 0 != $? ] ; then
echo "coverage no_functions failed"
if [ 0 == $KEEP_GOING ] ; then
Expand All @@ -279,7 +285,7 @@ if [ 0 != $COUNT ] ; then
fi

# run without extracting version
eval ${PYCOV} ${PY2LCOV_TOOL} functions.dat -o no_version.info
eval ${PYCOV} ${PY2LCOV_TOOL} functions.dat --cmd $CMD -o no_version.info
if [ 0 != $? ] ; then
echo "coverage no_functions failed"
if [ 0 == $KEEP_GOING ] ; then
Expand All @@ -296,7 +302,7 @@ if [ 0 != $COUNT ] ; then
fi

# test exclusion
eval ${PYCOV} ${PY2LCOV_TOOL} -o excl.info --exclude test.py functions.dat
eval ${PYCOV} ${PY2LCOV_TOOL} -o excl.info --cmd $CMD --exclude test.py functions.dat
if [ 0 != $? ] ; then
echo "coverage no_functions failed"
if [ 0 == $KEEP_GOING ] ; then
Expand Down Expand Up @@ -330,7 +336,7 @@ if [ 0 != $? ] ; then
fi

# some usage errors
eval ${PYCOV} ${PY2LCOV_TOOL} functions.dat -o paramErr.info ${VERSION},-x
eval ${PYCOV} ${PY2LCOV_TOOL} functions.dat -o paramErr.info --cmd $CMD ${VERSION},-x
if [ 0 == $? ] ; then
echo "coverage version did not see error"
if [ 0 == $KEEP_GOING ] ; then
Expand All @@ -339,7 +345,7 @@ if [ 0 == $? ] ; then
fi

# run again with --keep-going flag - should generate same result as we see without version script
eval ${PYCOV} ${PY2LCOV_TOOL} functions.dat -o keepGoing.info ${VERSION},-x --keep-going --verbose
eval ${PYCOV} ${PY2LCOV_TOOL} functions.dat -o keepGoing.info --cmd $CMD ${VERSION},-x --keep-going --verbose
if [ 0 != $? ] ; then
echo "keepGoing version saw error"
if [ 0 == $KEEP_GOING ] ; then
Expand All @@ -359,9 +365,9 @@ fi
# can't run this unless we have a new enough 'coverage' version
# to support the --data-file input
if [[ "${PYCOV}" =~ "COVERAGE_FILE=" || "${PY2LCOV_TOOL}" =~ "COVERAGE_FILE=" ]] ; then
${LCOV_HOME}/bin/py2lcov -o missing.info
${LCOV_HOME}/bin/py2lcov -o missing.info --cmd $CMD
else
eval ${PYCOV} ${PY2LCOV_TOOL} -o missing.info
eval ${PYCOV} ${PY2LCOV_TOOL} -o missing.info --cmd $CMD
fi
if [ 0 == $? ] ; then
echo "did not see error with missing input data"
Expand All @@ -371,7 +377,7 @@ if [ 0 == $? ] ; then
fi

# usage error:
eval ${PYCOV} ${PY2LCOV_TOOL} -o noFile.info run.dat y.xml
eval ${PYCOV} ${PY2LCOV_TOOL} -o noFile.info run.dat y.xml --cmd $CMD
if [ 0 == $? ] ; then
echo "did not see error with missing input file"
if [ 0 == $KEEP_GOING ] ; then
Expand All @@ -380,7 +386,7 @@ if [ 0 == $? ] ; then
fi

# usage error:
eval ${PYCOV} ${PY2LCOV_TOOL} -o badArg.info --noSuchParam run_help.dat
eval ${PYCOV} ${PY2LCOV_TOOL} -o badArg.info --noSuchParam run_help.dat --cmd $CMD
if [ 0 == $? ] ; then
echo "did not see error with unsupported param"
if [ 0 == $KEEP_GOING ] ; then
Expand All @@ -392,10 +398,10 @@ fi
# to support the --data-file input
if [[ "${PYCOV}" =~ "COVERAGE_FILE=" || "${PY2LCOV_TOOL}" =~ "COVERAGE_FILE=" ]] ; then
# can't generate coverage report for this feature...
COVERAGE_FILE=functions.dat ${LCOV_HOME}/bin/py2lcov -o fromEnv.info
COVERAGE_FILE=functions.dat ${LCOV_HOME}/bin/py2lcov -o fromEnv.info --cmd $CMD
else
# get input from environment var:
eval COVERAGE_FILE=functions.dat ${PYCOV} ${PY2LCOV_TOOL} -o fromEnv.info
eval COVERAGE_FILE=functions.dat ${PYCOV} ${PY2LCOV_TOOL} -o fromEnv.info --cmd $CMD
fi

if [ 0 != $? ] ; then
Expand Down Expand Up @@ -480,6 +486,6 @@ echo "Tests passed"
if [[ "x$COVER" != "x" && $LOCAL_COVERAGE == 1 ]] ; then
cover
${LCOV_HOME}/bin/perl2lcov -o perlcov.info --testname py2lcov $VERSION ./cover_db
${PY2LCOV_TOOL} -o pycov.info --testname py2lcov $VERSION ${PYCOV_DB}
${PY2LCOV_TOOL} -o pycov.info --testname py2lcov --cmd $CMD $VERSION ${PYCOV_DB}
${GENHTML_TOOL} -o pycov pycov.info perlcov.info --flat --show-navigation --show-proportion --branch $VERSION $ANNOTATE --ignore inconsistent,version
fi

0 comments on commit 5ea2780

Please sign in to comment.