pytest-dev / pytest-testinfra

Testinfra test your infrastructures
https://testinfra.readthedocs.io
Apache License 2.0
2.38k stars 355 forks source link

Using tilde (~) #137

Open adriano-di-giovanni opened 8 years ago

adriano-di-giovanni commented 8 years ago

The following test fails

def test_gitignore_global_file(File):
    gitignore_global = File('~/.gitignore_global')
    assert gitignore_global.exists

This one succeeds

def test_gitignore_global_file(File):
    gitignore_global = File('/Users/me/.gitignore_global')
    assert gitignore_global.exists

In order to make the test succeed I have to use /Users/me instead of tilde.

Is it correct?

adriano-di-giovanni commented 8 years ago

In the meanwhile, I found this workaround

from os.path import expanduser

def test_gitignore_global_file(File):
    path = expanduser('~/.gitignore_global')
    gitignore_global = File(path)
    assert gitignore_global.exists
philpep commented 8 years ago

Hi, thanks for reporting.

I think there is a bug here and the behavior could be different given the undelying backend or options (like sudo).

mr-ice commented 7 years ago

Python does not expand ~ by default. For that matter, only shells do.

likewise by default Python will not expand '$HOME/.gitignore_global' which is valid to the shell and in many cases equivalent to the example given.

Should the File() module call both expanduser and expandvars from os.path?

philpep commented 7 years ago

Using expanduser cannot work on remote connections (ssh, docker etc). This must be done by the shell.

gbirke commented 7 years ago

I had the same problem. My current workaround that works on remote connections looks like this:

import os

def test_gitignore_global_file(File, User):
    gitignore_global = File(os.path.join(User().home, '.gitignore_global'))
    assert gitignore_global.exists

But I think it makes the tests unneccesarily verbose. It would be nice if there was a shortcut for home-local paths.