misbehavior in run --source (?) · Issue #328 · nedbat/coveragepy (original) (raw)

The below script reproduces the problem.
In summary, I expect that when the argument to -m and --source are equal, I should never see the code run, and get a warning "module x was never imported", but this is not the current behavior.

#!/usr/bin/env python
# pylint:disable=missing-docstring
'''
$ python repro.py
in example:
true
exiting example...

Coverage.py warning: Module example was never imported.
Coverage.py warning: No data was collected.
Name    Stmts   Miss  Cover
---------------------------

$ pip freeze | grep coverage
coverage==3.7.1
$ python -V
Python 2.7.8
'''


def main():
    from shutil import rmtree
    rmtree('repro', ignore_errors=True)

    from os import mkdir
    mkdir('repro')
    mkdir('repro/code')
    with open('repro/code/example.py', 'w') as examplepy:
        examplepy.write(r'''\
print('in example:')
if True:
    print('true')
else:
    print('false')
print('exiting example...\n')''')

    mkdir('repro/workdir')

    from os import environ
    from os.path import realpath
    env = environ.copy()
    env['PYTHONPATH'] = realpath('repro/code')

    from subprocess import Popen
    Popen(
        ('coverage', 'run', '--source=example', '-m', 'example'),
        cwd='repro/workdir',
        env=env,
    ).wait()

    Popen(
        ('coverage', 'report'),
        cwd='repro/workdir',
    ).wait()


if __name__ == '__main__':
    exit(main())