Fix os.path patching in tests.

Fixes #802.
This commit is contained in:
Florian Bruhin 2015-07-06 18:35:58 +02:00
parent 20ff7e702a
commit e010d3dabc
4 changed files with 100 additions and 94 deletions

View File

@ -70,10 +70,10 @@ class NetworkProxy(QNetworkProxy):
@pytest.fixture
def os_path(mocker):
"""Fixture that mocks and returns os.path from the configtypes module."""
return mocker.patch('qutebrowser.config.configtypes.os.path',
autospec=True)
def os_mock(mocker):
"""Fixture that mocks and returns os from the configtypes module."""
m = mocker.patch('qutebrowser.config.configtypes.os', autospec=True)
return m
class TestValidValues:
@ -1359,74 +1359,79 @@ class TestFile:
t = configtypes.File(none_ok=True)
t.validate("")
def test_validate_does_not_exist(self, os_path):
def test_validate_does_not_exist(self, os_mock):
"""Test validate with a file which does not exist."""
os_path.expanduser.side_effect = lambda x: x
os_path.expandvars.side_effect = lambda x: x
os_path.isfile.return_value = False
os_mock.path.expanduser.side_effect = lambda x: x
os_mock.path.expandvars.side_effect = lambda x: x
os_mock.path.isfile.return_value = False
with pytest.raises(configexc.ValidationError):
self.t.validate('foobar')
def test_validate_exists_abs(self, os_path):
def test_validate_exists_abs(self, os_mock):
"""Test validate with a file which does exist."""
os_path.expanduser.side_effect = lambda x: x
os_path.expandvars.side_effect = lambda x: x
os_path.isfile.return_value = True
os_path.isabs.return_value = True
os_mock.path.expanduser.side_effect = lambda x: x
os_mock.path.expandvars.side_effect = lambda x: x
os_mock.path.isfile.return_value = True
os_mock.path.isabs.return_value = True
self.t.validate('foobar')
def test_validate_exists_rel(self, os_path, monkeypatch):
def test_validate_exists_rel(self, os_mock, monkeypatch):
"""Test validate with a relative path to an existing file."""
monkeypatch.setattr(
'qutebrowser.config.configtypes.standarddir.config',
lambda: '/home/foo/.config/')
os_path.expanduser.side_effect = lambda x: x
os_path.expandvars.side_effect = lambda x: x
os_path.isfile.return_value = True
os_path.isabs.return_value = False
os_mock.path.expanduser.side_effect = lambda x: x
os_mock.path.expandvars.side_effect = lambda x: x
os_mock.path.isfile.return_value = True
os_mock.path.isabs.return_value = False
self.t.validate('foobar')
os_path.join.assert_called_once_with('/home/foo/.config/', 'foobar')
os_mock.path.join.assert_called_once_with('/home/foo/.config/',
'foobar')
def test_validate_rel_config_none(self, os_path, monkeypatch):
def test_validate_rel_config_none(self, os_mock, monkeypatch):
"""Test with a relative path and standarddir.config returning None."""
monkeypatch.setattr(
'qutebrowser.config.configtypes.standarddir.config', lambda: None)
os_path.isabs.return_value = False
os_mock.path.isabs.return_value = False
with pytest.raises(configexc.ValidationError):
self.t.validate('foobar')
def test_validate_expanduser(self, os_path):
def test_validate_expanduser(self, os_mock):
"""Test if validate expands the user correctly."""
os_path.expanduser.side_effect = lambda x: x.replace('~', '/home/foo')
os_path.expandvars.side_effect = lambda x: x
os_path.isfile.side_effect = lambda path: path == '/home/foo/foobar'
os_path.isabs.return_value = True
os_mock.path.expanduser.side_effect = (lambda x:
x.replace('~', '/home/foo'))
os_mock.path.expandvars.side_effect = lambda x: x
os_mock.path.isfile.side_effect = (lambda path:
path == '/home/foo/foobar')
os_mock.path.isabs.return_value = True
self.t.validate('~/foobar')
os_path.expanduser.assert_called_once_with('~/foobar')
os_mock.path.expanduser.assert_called_once_with('~/foobar')
def test_validate_expandvars(self, os_path):
def test_validate_expandvars(self, os_mock):
"""Test if validate expands the environment vars correctly."""
os_path.expanduser.side_effect = lambda x: x
os_path.expandvars.side_effect = lambda x: x.replace(
os_mock.path.expanduser.side_effect = lambda x: x
os_mock.path.expandvars.side_effect = lambda x: x.replace(
'$HOME', '/home/foo')
os_path.isfile.side_effect = lambda path: path == '/home/foo/foobar'
os_path.isabs.return_value = True
os_mock.path.isfile.side_effect = (lambda path:
path == '/home/foo/foobar')
os_mock.path.isabs.return_value = True
self.t.validate('$HOME/foobar')
os_path.expandvars.assert_called_once_with('$HOME/foobar')
os_mock.path.expandvars.assert_called_once_with('$HOME/foobar')
def test_validate_invalid_encoding(self, os_path, unicode_encode_err):
def test_validate_invalid_encoding(self, os_mock, unicode_encode_err):
"""Test validate with an invalid encoding, e.g. LC_ALL=C."""
os_path.isfile.side_effect = unicode_encode_err
os_path.isabs.side_effect = unicode_encode_err
os_mock.path.isfile.side_effect = unicode_encode_err
os_mock.path.isabs.side_effect = unicode_encode_err
with pytest.raises(configexc.ValidationError):
self.t.validate('foobar')
def test_transform(self, os_path):
def test_transform(self, os_mock):
"""Test transform."""
os_path.expanduser.side_effect = lambda x: x.replace('~', '/home/foo')
os_path.expandvars.side_effect = lambda x: x
os_mock.path.expanduser.side_effect = (lambda x:
x.replace('~', '/home/foo'))
os_mock.path.expandvars.side_effect = lambda x: x
assert self.t.transform('~/foobar') == '/home/foo/foobar'
os_path.expanduser.assert_called_once_with('~/foobar')
os_mock.path.expanduser.assert_called_once_with('~/foobar')
def test_transform_empty(self):
"""Test transform with none_ok = False and an empty value."""
@ -1451,61 +1456,65 @@ class TestDirectory:
t = configtypes.Directory(none_ok=True)
t.validate("")
def test_validate_does_not_exist(self, os_path):
def test_validate_does_not_exist(self, os_mock):
"""Test validate with a directory which does not exist."""
os_path.expanduser.side_effect = lambda x: x
os_path.isdir.return_value = False
os_mock.path.expanduser.side_effect = lambda x: x
os_mock.path.isdir.return_value = False
with pytest.raises(configexc.ValidationError):
self.t.validate('foobar')
def test_validate_exists_abs(self, os_path):
def test_validate_exists_abs(self, os_mock):
"""Test validate with a directory which does exist."""
os_path.expanduser.side_effect = lambda x: x
os_path.isdir.return_value = True
os_path.isabs.return_value = True
os_mock.path.expanduser.side_effect = lambda x: x
os_mock.path.isdir.return_value = True
os_mock.path.isabs.return_value = True
self.t.validate('foobar')
def test_validate_exists_not_abs(self, os_path):
def test_validate_exists_not_abs(self, os_mock):
"""Test validate with a dir which does exist but is not absolute."""
os_path.expanduser.side_effect = lambda x: x
os_path.isdir.return_value = True
os_path.isabs.return_value = False
os_mock.path.expanduser.side_effect = lambda x: x
os_mock.path.isdir.return_value = True
os_mock.path.isabs.return_value = False
with pytest.raises(configexc.ValidationError):
self.t.validate('foobar')
def test_validate_expanduser(self, os_path):
def test_validate_expanduser(self, os_mock):
"""Test if validate expands the user correctly."""
os_path.expandvars.side_effect = lambda x: x
os_path.expanduser.side_effect = lambda x: x.replace('~', '/home/foo')
os_path.isdir.side_effect = lambda path: path == '/home/foo/foobar'
os_path.isabs.return_value = True
os_mock.path.expandvars.side_effect = lambda x: x
os_mock.path.expanduser.side_effect = (lambda x:
x.replace('~', '/home/foo'))
os_mock.path.isdir.side_effect = (lambda path:
path == '/home/foo/foobar')
os_mock.path.isabs.return_value = True
self.t.validate('~/foobar')
os_path.expanduser.assert_called_once_with('~/foobar')
os_mock.path.expanduser.assert_called_once_with('~/foobar')
def test_validate_expandvars(self, os_path, monkeypatch):
def test_validate_expandvars(self, os_mock, monkeypatch):
"""Test if validate expands the user correctly."""
os_path.expandvars.side_effect = lambda x: x.replace('$BAR',
os_mock.path.expandvars.side_effect = lambda x: x.replace('$BAR',
'/home/foo/bar')
os_path.expanduser.side_effect = lambda x: x
os_path.isdir.side_effect = lambda path: path == '/home/foo/bar/foobar'
os_path.isabs.return_value = True
os_mock.path.expanduser.side_effect = lambda x: x
os_mock.path.isdir.side_effect = (lambda path:
path == '/home/foo/bar/foobar')
os_mock.path.isabs.return_value = True
monkeypatch.setenv('BAR', '/home/foo/bar')
self.t.validate('$BAR/foobar')
os_path.expandvars.assert_called_once_with('$BAR/foobar')
os_mock.path.expandvars.assert_called_once_with('$BAR/foobar')
def test_validate_invalid_encoding(self, os_path, unicode_encode_err):
def test_validate_invalid_encoding(self, os_mock, unicode_encode_err):
"""Test validate with an invalid encoding, e.g. LC_ALL=C."""
os_path.isdir.side_effect = unicode_encode_err
os_path.isabs.side_effect = unicode_encode_err
os_mock.path.isdir.side_effect = unicode_encode_err
os_mock.path.isabs.side_effect = unicode_encode_err
with pytest.raises(configexc.ValidationError):
self.t.validate('foobar')
def test_transform(self, os_path):
def test_transform(self, os_mock):
"""Test transform."""
os_path.expandvars.side_effect = lambda x: x
os_path.expanduser.side_effect = lambda x: x.replace('~', '/home/foo')
os_mock.path.expandvars.side_effect = lambda x: x
os_mock.path.expanduser.side_effect = (lambda x:
x.replace('~', '/home/foo'))
assert self.t.transform('~/foobar') == '/home/foo/foobar'
os_path.expanduser.assert_called_once_with('~/foobar')
os_mock.path.expanduser.assert_called_once_with('~/foobar')
def test_transform_empty(self):
"""Test transform with none_ok = False and an empty value."""
@ -1950,10 +1959,10 @@ class TestUserStyleSheet:
def test_validate_invalid_encoding(self, mocker, unicode_encode_err):
"""Test validate with an invalid encoding, e.g. LC_ALL=C."""
os_path = mocker.patch('qutebrowser.config.configtypes.os.path',
os_mock = mocker.patch('qutebrowser.config.configtypes.os',
autospec=True)
os_path.isfile.side_effect = unicode_encode_err
os_path.isabs.side_effect = unicode_encode_err
os_mock.path.isfile.side_effect = unicode_encode_err
os_mock.path.isabs.side_effect = unicode_encode_err
with pytest.raises(configexc.ValidationError):
self.t.validate('foobar')
@ -1961,21 +1970,21 @@ class TestUserStyleSheet:
"""Test transform with an empty value."""
assert self.t.transform('') is None
def test_transform_file(self, os_path, mocker):
def test_transform_file(self, os_mock, mocker):
"""Test transform with a filename."""
qurl = mocker.patch('qutebrowser.config.configtypes.QUrl',
autospec=True)
qurl.fromLocalFile.return_value = QUrl("file:///foo/bar")
os_path.exists.return_value = True
os_mock.path.exists.return_value = True
path = os.path.join(os.path.sep, 'foo', 'bar')
assert self.t.transform(path) == QUrl("file:///foo/bar")
def test_transform_file_expandvars(self, os_path, monkeypatch, mocker):
def test_transform_file_expandvars(self, os_mock, monkeypatch, mocker):
"""Test transform with a filename (expandvars)."""
qurl = mocker.patch('qutebrowser.config.configtypes.QUrl',
autospec=True)
qurl.fromLocalFile.return_value = QUrl("file:///foo/bar")
os_path.exists.return_value = True
os_mock.path.exists.return_value = True
monkeypatch.setenv('FOO', 'foo')
path = os.path.join(os.path.sep, '$FOO', 'bar')
assert self.t.transform(path) == QUrl("file:///foo/bar")

View File

@ -109,23 +109,19 @@ class TestBaseLineParser:
def test_prepare_save_existing(self, mocker, lineparser):
"""Test if _prepare_save does what it's supposed to do."""
exists_mock = mocker.patch(
'qutebrowser.misc.lineparser.os.path.exists')
makedirs_mock = mocker.patch('qutebrowser.misc.lineparser.os.makedirs')
exists_mock.return_value = True
os_mock = mocker.patch('qutebrowser.misc.lineparser.os')
os_mock.path.exists.return_value = True
lineparser._prepare_save()
assert not makedirs_mock.called
assert not os_mock.makedirs.called
def test_prepare_save_missing(self, mocker, lineparser):
"""Test if _prepare_save does what it's supposed to do."""
exists_mock = mocker.patch(
'qutebrowser.misc.lineparser.os.path.exists')
exists_mock.return_value = False
makedirs_mock = mocker.patch('qutebrowser.misc.lineparser.os.makedirs')
os_mock = mocker.patch('qutebrowser.misc.lineparser.os')
os_mock.path.exists.return_value = False
lineparser._prepare_save()
makedirs_mock.assert_called_with(self.CONFDIR, 0o755)
os_mock.makedirs.assert_called_with(self.CONFDIR, 0o755)
class TestAppendLineParser:

View File

@ -245,11 +245,12 @@ class TestInitCacheDirTag:
monkeypatch.setattr('qutebrowser.utils.standarddir.cache',
lambda: str(tmpdir))
mocker.patch('builtins.open', side_effect=AssertionError)
m = mocker.patch('qutebrowser.utils.standarddir.os.path.exists',
return_value=True)
m = mocker.patch('qutebrowser.utils.standarddir.os')
m.path.join.side_effect = os.path.join
m.path.exists.return_value = True
standarddir._init_cachedir_tag()
assert not tmpdir.listdir()
m.assert_called_with(str(tmpdir / 'CACHEDIR.TAG'))
m.path.exists.assert_called_with(str(tmpdir / 'CACHEDIR.TAG'))
def test_new_cache_dir_tag(self, tmpdir, mocker, monkeypatch):
"""Test creating a new CACHEDIR.TAG."""

View File

@ -132,8 +132,8 @@ class TestGitStr:
def test_normal_path_oserror(self, mocker, git_str_subprocess_fake):
"""Test with things raising OSError."""
mocker.patch('qutebrowser.utils.version.os.path.join',
side_effect=OSError)
m = mocker.patch('qutebrowser.utils.version.os')
m.path.join.side_effect = OSError
mocker.patch('qutebrowser.utils.version.utils.read_file',
side_effect=OSError)
assert version._git_str() is None
@ -218,8 +218,8 @@ class TestGitStrSubprocess:
Args:
exc: The exception to raise.
"""
mocker.patch('qutebrowser.utils.version.subprocess.os.path.isdir',
return_value=True)
m = mocker.patch('qutebrowser.utils.version.subprocess.os')
m.path.isdir.return_value = True
mocker.patch('qutebrowser.utils.version.subprocess.check_output',
side_effect=exc)
ret = version._git_str_subprocess(str(tmpdir))