nedbat / coveragepy

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

Ignore code run with `exec` or `compile` #1363

Closed rayzchen closed 2 years ago

rayzchen commented 2 years ago

Describe the bug A clear and concise description of the bug.

coverage xml cannot find files that were generated and compiled with compile. In ideal situations we would want coverage recorded for these files but in our case we simply don't really need it, as it's only testing a certain functionality that only defines a class. A flag that ignores exec or compile would be very helpful.

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

  1. What version of Python are you using? Python 3.9
  2. What version of coverage.py shows the problem? The output of coverage debug sys is helpful.
    -- sys -------------------------------------------------------
               coverage_version: 6.3.2
                coverage_module: c:\users\raych\appdata\local\programs\python\python39-32\lib\site-packages\coverage\__init__.py
                         tracer: -none-
                        CTracer: available
           plugins.file_tracers: -none-
            plugins.configurers: -none-
      plugins.context_switchers: -none-
              configs_attempted: .coveragerc
                                 setup.cfg
                                 tox.ini
                                 pyproject.toml
                   configs_read: -none-
                    config_file: None
                config_contents: -none-
                      data_file: -none-
                         python: 3.9.4 (tags/v3.9.4:1f2e308, Apr  6 2021, 13:22:44) [MSC v.1928 32 bit (Intel)]
                       platform: Windows-10-10.0.19041-SP0
                 implementation: CPython
                     executable: c:\users\raych\appdata\local\programs\python\python39-32\python.exe
                   def_encoding: utf-8
                    fs_encoding: utf-8
                            pid: 11268
                            cwd: C:\Ray-Data\python\pyunity
                           path: C:\Users\raych\AppData\Local\Programs\Python\Python39-32\Scripts\coverage.exe
                                 c:\users\raych\appdata\local\programs\python\python39-32\python39.zip
                                 c:\users\raych\appdata\local\programs\python\python39-32\DLLs
                                 c:\users\raych\appdata\local\programs\python\python39-32\lib
                                 c:\users\raych\appdata\local\programs\python\python39-32
                                 C:\Users\raych\AppData\Roaming\Python\Python39\site-packages
                                 c:\users\raych\appdata\local\programs\python\python39-32\lib\site-packages
                    environment: HOME = C:\Users\raych
                                 TEMP = C:\Users\raych\AppData\Local\Temp
                                 TMP = C:\Users\raych\AppData\Local\Temp
                   command_line: C:\Users\raych\AppData\Local\Programs\Python\Python39-32\Scripts\coverage debug sys
                sqlite3_version: 2.6.0
         sqlite3_sqlite_version: 3.34.0
             sqlite3_temp_store: 0
        sqlite3_compile_options: COMPILER=msvc-1928; ENABLE_FTS4; ENABLE_FTS5
                                 ENABLE_JSON1; THREADSAFE=1
  3. What versions of what packages do you have installed? The output of pip freeze is helpful.
    aenum==2.2.6
    aiohttp==3.7.4.post0
    alabaster==0.7.12
    altgraph==0.17.2
    appdirs==1.4.4
    async-timeout==3.0.1
    attrs==21.4.0
    autopep8==1.6.0
    Babel==2.9.1
    backcall==0.2.0
    beautifulsoup4==4.10.0
    bleach==4.1.0
    blessings==1.7
    bpython==0.22.1
    certifi==2021.10.8
    cffi==1.15.0
    chardet==4.0.0
    charset-normalizer==2.0.12
    click==7.1.2
    colorama==0.4.4
    coverage==6.3.2
    cryptography==36.0.1
    curses==2.2.1
    curtsies==0.3.10
    cwcwidth==0.1.6
    cx-Freeze==6.10
    cx-Logging==3.0
    Cython==3.0.0a8
    decorator==5.1.1
    discord-py-slash-command==3.0.3
    discord.py==1.7.3
    distlib==0.3.4
    docutils==0.17.1
    filelock==3.4.2
    Flask==2.0.2
    future==0.18.2
    glfw==2.5.0
    gobject==0.1.0
    greenlet==1.1.2
    gunicorn==20.1.0
    idna==3.3
    imagesize==1.3.0
    importlib-metadata==4.11.0
    ipython==7.31.1
    itsdangerous==2.0.1
    jedi==0.18.1
    Jinja2==3.0.3
    keyboard==0.13.5
    keyring==23.5.0
    lief==0.11.5
    MarkupSafe==2.0.1
    matplotlib-inline==0.1.3
    MouseInfo==0.1.3
    multidict==6.0.2
    mypy==0.942
    mypy-extensions==0.4.3
    numpy==1.22.2
    packaging==21.3
    Panda3D==1.10.11
    parso==0.8.3
    pefile==2021.9.3
    Pegl @ git+https://github.com/perey/pegl.git@1aee76c9a983850d18032a34d0381f9bd561613d
    pickleshare==0.7.5
    Pillow==9.0.1
    pkginfo==1.8.2
    platformdirs==2.5.0
    prompt-toolkit==3.0.28
    psutil==5.9.0
    PyAutoGUI==0.9.53
    pybind11==2.9.2
    pycairo==1.21.0
    pycodestyle==2.8.0
    pycparser==2.21
    pyflakes==2.4.0
    pyFltk==1.3.7
    pygame==2.1.2
    PyGetWindow==0.0.9
    PyGLM==2.5.7
    pyglm-typing==0.2.1
    Pygments==2.11.2
    pyinstaller==4.9
    pyinstaller-hooks-contrib==2022.1
    PyMsgBox==1.0.9
    PyOpenGL==3.1.5
    PyOpenGL-accelerate==3.1.6
    pyOpenSSL==22.0.0
    pyparsing==3.0.7
    pyperclip==1.8.2
    PyQt5==5.15.6
    PyQt5-Qt5==5.15.2
    PyQt5-sip==12.9.1
    PyQtWebEngine==5.15.5
    PyQtWebEngine-Qt5==5.15.2
    PyRect==0.1.4
    PyScreeze==0.1.28
    PySDL2==0.9.11
    pysdl2-dll==2.0.20
    pytweening==1.0.4
    pytz==2021.3
    pyunity-editor==0.1.1
    pyunity-stubs==0.8.3.post2
    pywin32-ctypes==0.2.0
    pyxdg==0.27
    PyYAML==6.0
    qt5-applications==5.15.2.2.2
    qt5-tools==5.15.2.1.2
    readme-renderer==32.0
    requests==2.27.1
    requests-toolbelt==0.9.1
    rfc3986==2.0.0
    rply==0.7.8
    six==1.16.0
    snowballstemmer==2.2.0
    soupsieve==2.3.1
    Sphinx==4.4.0
    sphinx-hoverxref==1.0.1
    sphinxcontrib-applehelp==1.0.2
    sphinxcontrib-devhelp==1.0.2
    sphinxcontrib-htmlhelp==2.0.0
    sphinxcontrib-jsmath==1.0.1
    sphinxcontrib-qthelp==1.0.3
    sphinxcontrib-serializinghtml==1.1.5
    toml==0.10.2
    tomli==2.0.1
    tqdm==4.62.3
    traitlets==5.1.1
    twine==3.8.0
    typing_extensions==4.1.0
    urllib3==1.26.8
    virtualenv==20.13.1
    wcwidth==0.2.5
    webencodings==0.5.1
    Werkzeug==2.0.3
    wget==3.2
    yarl==1.7.2
    zipp==3.7.0
  4. What code shows the problem? Give us a specific commit of a specific repo that we can check out. If you've already worked around the problem, please provide a commit before that fix. https://github.com/pyunity/pyunity/commit/fc8c24590f537e9bba460cddb9a7bcde98e0972e
  5. What commands did you run? coverage run -m unittest tests.py coverage xml Expected behavior Report generation success Additional context I use compile(text, "file1.py", "exec") which results in
    No source for code: '<path>/file1.py'.
nedbat commented 2 years ago

The coverage xml command already has an --ignore-errors option, isn't that what you need? If not, then I'm not understanding the problem.

rayzchen commented 2 years ago

Is there a way to disable coverage scanning for anything that is run with exec during coverage run? --ignore-errors does fix it but it isn't exactly how I imagined to fix it.

nedbat commented 2 years ago

Coverage.py doesn't have a way to do that, no.