Better support for cross platform $TMPDIR

Issue #592 invalid
Barry Warsaw created an issue

Along the lines of #429 I'd like to request better support for omitting files under $TMPDIR. In the Mailman 3 project, we also create some files under $TMPDIR and import them, but because that's part of the test suite, we don't care about coverage for those files.

Most of the existing development work for Mailman 3 was under Linux, so I had the following line in my coverage.ini:

[run]
omit =
    /tmp/*

and that worked great. Now that I'm also doing development on macOS, this isn't good enough, because $TMPDIR actually points to /private/var/folders. Thus when we run python -m coverage xml {[coverage]rc} in our tox.ini, we get the following error on macOS:

No source for code: '/private/var/folders/80/lfs_4mys5f78wrr3jjcnl7wh0000gn/T/tmp0dtr_3qs/mypackage/__init__.py'.

I could add the -i option, but I'd prefer not to ignore errors. Instead, I think I'd like a way to omit whatever the platform's $TMPDIR is.

I'm not aware of a way to omit paths based on environment variables, but that would be one option. E.g.

[run]
omit =
    $TMPDIR/*

Of course, I could add /private/var/folders/* to the omit along with /tmp/* and that does work, but it's a bit ugly and unpredictable (e.g. what if $TMPDIR gets moved in some future version?).

Comments (7)

  1. Ned Batchelder repo owner

    Environment variables are expanded in config files. I would think your example of omit = $TMPDIR/* would work just as it is. Does it not?

  2. Barry Warsaw reporter

    Ah, now I know why that doesn't work, and I'm not sure what to suggest. For whatever reason, macOS is "weird" about its $TMPDIR. While that envar points into /var/folders/ what actually ends up in the coverage data, and what you have to add to omit is /private/var/folders/*.

    % ls -l /var
    lrwxr-xr-x@ 1 root  wheel  11 May 31 19:34 /var@ -> private/var
    

    Thanks Apple!

  3. Ned Batchelder repo owner

    Is there some combination of abspath, normpath, etc, that would get me /private/var/folders/etc from $TMPDIR?

  4. Barry Warsaw reporter

    It looks like os.path.realpath() will do the trick:

    >>> os.environ['TMPDIR']
    '/var/folders/80/lfs_4mys5f78wrr3jjcnl7wh0000gn/T/'
    >>> os.path.realpath(os.environ['TMPDIR'])
    '/private/var/folders/80/lfs_4mys5f78wrr3jjcnl7wh0000gn/T'
    
  5. Ned Batchelder repo owner

    Hmm, I'm already using realpath... When I try omit = $TMPDIR/*, I end up with a self.omit list in coverage/control.py of [u'/private/var/folders/j2/gr3cj3jn63s5q8g3bjvw57hm0000gp/T/*']. Is there something else preventing this from working?

  6. Barry Warsaw reporter

    It looks like you're right. I'm not sure why that was happening when I originally filed the bug, but it does seem to be working correctly now. Unfortunately, I think I still can't use $TMPDIR/*. That envar is empty on Linux so that ends up omitting everything.

    I'll experiment some more.

  7. Log in to comment