nedbat / coveragepy

The code coverage tool for Python
https://coverage.readthedocs.io
Apache License 2.0
3.03k stars 435 forks source link

Unable to get coverage when subprocess uses a different Python version #1182

Closed taranlu-houzz closed 3 years ago

taranlu-houzz commented 3 years ago

Describe the bug Hello, I have a fairly unique problem: I have a new tool package that I am writing in Python 3. Within this package, there are some stand-alone scripts written in Python 2 (they use the mayapy utility script to invoke the Python interpreter in a valid environment for Autodesk Maya to run headless). In order to get coverage for these scripts, I have tried to follow the information on https://coverage.readthedocs.io/en/coverage-5.5/subprocess.html, but it does not seem to work for me. In order to get the sitecustomize.py to load in the subprocess, I pass a modified environment to the subprocess command that looks like this:

MAYAPY_ENV_HACKY = {
    **os.environ,
    "COVERAGE_PROCESS_START": str(pyproject_file_path()),
    "PYTHONPATH": os.pathsep.join([str(pyproject_file_path().parent)] + site.getsitepackages()),
}

I am doing this for the following reasons:

I have coverage configured via the pyproject.toml:

[tool.coverage.paths]
source = ["src", "*/site-packages"]

[tool.coverage.run]
branch = true
parallel = true
source = ["----"]

[tool.coverage.report]
show_missing = true
fail_under = 100

And this is my sitecustomize.py:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

print(__name__, "A")
try:
    print(__name__, "B")
    import coverage

    coverage.process_startup()

    print("STARTED COVERAGE")
except ImportError:
    print(__name__, "C")
    pass

I have tested running the subprocess with just invoking the interpreter via mayapy, and it works and gives the expected output from my sitecustomize.py. However, when I try to run the actual scripts and then combine them, they always show 0% coverage for the files.

Other information about my setup:

To Reproduce How can we reproduce the problem? Please be specific. Don't just link to a failing CI job. Answer the questions below:

Expected behavior I would expect the coverage to be logged for the code run via the subprocesses.

taranlu-houzz commented 3 years ago

I think that this issue seems like it might be related: #655

taranlu-houzz commented 3 years ago

So, I found the solution to my issue: Adding the path to the subdir in the package that contains the stand-alone scripts to the source list under tool.coverage.run in my pyproject.toml got things to work.

This was not immediately obvious as an issue, since coverage was detecting the files, it just seems that it was not collecting data on them. Initially, in order to get coverage to notice the subdir, I added __init__.py as needed along the path (Python 3 should not need them). After adding the path to the subdir to my pyproject.toml I was able to delete the extraneous __init__.py files and coverage still reported on everything as expected.