2014-10-08 06:19:45 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2016-01-04 07:12:39 +01:00
|
|
|
# Copyright 2014-2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
2014-10-08 06:19:45 +02:00
|
|
|
#
|
|
|
|
# This file is part of qutebrowser.
|
|
|
|
#
|
|
|
|
# qutebrowser is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# qutebrowser is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""Tests for qutebrowser.utils.standarddir."""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import os.path
|
2015-05-16 22:12:27 +02:00
|
|
|
import types
|
2015-05-16 22:30:00 +02:00
|
|
|
import collections
|
2015-05-25 01:20:37 +02:00
|
|
|
import logging
|
|
|
|
import textwrap
|
2014-10-08 06:19:45 +02:00
|
|
|
|
2015-05-25 01:20:37 +02:00
|
|
|
from PyQt5.QtCore import QStandardPaths
|
2015-04-03 00:45:52 +02:00
|
|
|
import pytest
|
2015-04-03 00:05:20 +02:00
|
|
|
|
2014-10-08 06:19:45 +02:00
|
|
|
from qutebrowser.utils import standarddir
|
|
|
|
|
|
|
|
|
2015-04-03 00:45:52 +02:00
|
|
|
@pytest.yield_fixture(autouse=True)
|
2015-08-09 20:47:50 +02:00
|
|
|
def change_qapp_name(qapp):
|
2015-04-05 20:30:31 +02:00
|
|
|
"""Change the name of the QApplication instance.
|
|
|
|
|
|
|
|
This changes the applicationName for all tests in this module to
|
2015-09-08 22:14:39 +02:00
|
|
|
"qute_test".
|
2014-10-08 06:19:45 +02:00
|
|
|
"""
|
2015-08-09 20:47:50 +02:00
|
|
|
old_name = qapp.applicationName()
|
2015-09-08 22:14:39 +02:00
|
|
|
qapp.setApplicationName('qute_test')
|
2015-04-03 00:45:52 +02:00
|
|
|
yield
|
2015-08-09 20:47:50 +02:00
|
|
|
qapp.setApplicationName(old_name)
|
2014-10-08 06:19:45 +02:00
|
|
|
|
|
|
|
|
2015-05-25 01:20:37 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def no_cachedir_tag(monkeypatch):
|
|
|
|
"""Fixture to prevent writing a CACHEDIR.TAG."""
|
|
|
|
monkeypatch.setattr('qutebrowser.utils.standarddir._init_cachedir_tag',
|
|
|
|
lambda: None)
|
|
|
|
|
|
|
|
|
2016-07-10 13:44:58 +02:00
|
|
|
@pytest.yield_fixture
|
|
|
|
def reset_standarddir(no_cachedir_tag):
|
2015-09-11 20:52:24 +02:00
|
|
|
"""Clean up standarddir arguments before and after each test."""
|
|
|
|
standarddir.init(None)
|
|
|
|
yield
|
2015-05-25 01:20:37 +02:00
|
|
|
standarddir.init(None)
|
|
|
|
|
|
|
|
|
2016-07-10 13:44:58 +02:00
|
|
|
@pytest.mark.usefixtures('reset_standarddir')
|
2015-05-25 01:20:37 +02:00
|
|
|
@pytest.mark.parametrize('data_subdir, config_subdir, expected', [
|
|
|
|
('foo', 'foo', 'foo/data'),
|
|
|
|
('foo', 'bar', 'foo'),
|
|
|
|
])
|
|
|
|
def test_get_fake_windows_equal_dir(data_subdir, config_subdir, expected,
|
|
|
|
monkeypatch, tmpdir):
|
|
|
|
"""Test _get with a fake Windows OS with equal data/config dirs."""
|
|
|
|
locations = {
|
|
|
|
QStandardPaths.DataLocation: str(tmpdir / data_subdir),
|
|
|
|
QStandardPaths.ConfigLocation: str(tmpdir / config_subdir),
|
|
|
|
}
|
|
|
|
monkeypatch.setattr('qutebrowser.utils.standarddir.os.name', 'nt')
|
|
|
|
monkeypatch.setattr(
|
|
|
|
'qutebrowser.utils.standarddir.QStandardPaths.writableLocation',
|
|
|
|
locations.get)
|
|
|
|
expected = str(tmpdir / expected)
|
|
|
|
assert standarddir.data() == expected
|
|
|
|
|
|
|
|
|
2016-07-10 13:44:58 +02:00
|
|
|
@pytest.mark.usefixtures('reset_standarddir')
|
2015-05-25 01:20:37 +02:00
|
|
|
class TestWritableLocation:
|
|
|
|
|
|
|
|
"""Tests for _writable_location."""
|
|
|
|
|
|
|
|
def test_empty(self, monkeypatch):
|
|
|
|
"""Test QStandardPaths returning an empty value."""
|
|
|
|
monkeypatch.setattr(
|
|
|
|
'qutebrowser.utils.standarddir.QStandardPaths.writableLocation',
|
|
|
|
lambda typ: '')
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
standarddir._writable_location(QStandardPaths.DataLocation)
|
|
|
|
|
|
|
|
def test_sep(self, monkeypatch):
|
|
|
|
"""Make sure the right kind of separator is used."""
|
|
|
|
monkeypatch.setattr('qutebrowser.utils.standarddir.os.sep', '\\')
|
|
|
|
loc = standarddir._writable_location(QStandardPaths.DataLocation)
|
|
|
|
assert '/' not in loc
|
|
|
|
assert '\\' in loc
|
|
|
|
|
|
|
|
|
2016-07-10 13:44:58 +02:00
|
|
|
@pytest.mark.usefixtures('reset_standarddir')
|
2015-09-11 21:46:33 +02:00
|
|
|
class TestStandardDir:
|
|
|
|
|
|
|
|
"""Tests for standarddir."""
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('func, varname', [
|
|
|
|
(standarddir.data, 'XDG_DATA_HOME'),
|
|
|
|
(standarddir.config, 'XDG_CONFIG_HOME'),
|
|
|
|
(standarddir.cache, 'XDG_CACHE_HOME'),
|
|
|
|
])
|
|
|
|
@pytest.mark.linux
|
|
|
|
def test_linux_explicit(self, monkeypatch, tmpdir, func, varname):
|
2015-09-11 21:59:39 +02:00
|
|
|
"""Test dirs with XDG environment variables explicitly set.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
func: The function to test.
|
|
|
|
varname: The environment variable which should be set.
|
|
|
|
"""
|
2015-09-11 21:46:33 +02:00
|
|
|
monkeypatch.setenv(varname, str(tmpdir))
|
|
|
|
assert func() == str(tmpdir / 'qute_test')
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('func, subdirs', [
|
2015-09-11 21:59:39 +02:00
|
|
|
(standarddir.data, ['.local', 'share', 'qute_test']),
|
|
|
|
(standarddir.config, ['.config', 'qute_test']),
|
|
|
|
(standarddir.cache, ['.cache', 'qute_test']),
|
|
|
|
(standarddir.download, ['Downloads']),
|
2015-09-11 21:46:33 +02:00
|
|
|
])
|
|
|
|
@pytest.mark.linux
|
|
|
|
def test_linux_normal(self, monkeypatch, tmpdir, func, subdirs):
|
|
|
|
"""Test dirs with XDG_*_HOME not set."""
|
2015-04-03 00:45:52 +02:00
|
|
|
monkeypatch.setenv('HOME', str(tmpdir))
|
2016-07-12 22:05:32 +02:00
|
|
|
for var in ['DATA', 'CONFIG', 'CACHE']:
|
2015-09-11 21:46:33 +02:00
|
|
|
monkeypatch.delenv('XDG_{}_HOME'.format(var), raising=False)
|
2015-09-11 21:59:39 +02:00
|
|
|
assert func() == str(tmpdir.join(*subdirs))
|
2015-09-11 21:46:33 +02:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('func, elems, expected', [
|
|
|
|
(standarddir.data, 2, ['qute_test', 'data']),
|
|
|
|
(standarddir.config, 1, ['qute_test']),
|
|
|
|
(standarddir.cache, 2, ['qute_test', 'cache']),
|
2015-09-11 21:59:39 +02:00
|
|
|
(standarddir.download, 1, ['Downloads']),
|
2015-09-11 21:46:33 +02:00
|
|
|
])
|
|
|
|
@pytest.mark.windows
|
|
|
|
def test_windows(self, func, elems, expected):
|
|
|
|
assert func().split(os.sep)[-elems:] == expected
|
2015-05-16 22:12:27 +02:00
|
|
|
|
2015-09-15 20:39:01 +02:00
|
|
|
@pytest.mark.parametrize('func, elems, expected', [
|
|
|
|
(standarddir.data, 2, ['Application Support', 'qute_test']),
|
|
|
|
(standarddir.config, 1, ['qute_test']),
|
|
|
|
(standarddir.cache, 2, ['Caches', 'qute_test']),
|
|
|
|
(standarddir.download, 1, ['Downloads']),
|
|
|
|
])
|
|
|
|
@pytest.mark.osx
|
|
|
|
def test_os_x(self, func, elems, expected):
|
|
|
|
assert func().split(os.sep)[-elems:] == expected
|
|
|
|
|
2015-05-16 22:12:27 +02:00
|
|
|
|
2015-05-16 22:30:00 +02:00
|
|
|
DirArgTest = collections.namedtuple('DirArgTest', 'arg, expected')
|
|
|
|
|
|
|
|
|
2016-07-10 13:44:58 +02:00
|
|
|
@pytest.mark.usefixtures('reset_standarddir')
|
2015-05-16 22:12:27 +02:00
|
|
|
class TestArguments:
|
|
|
|
|
|
|
|
"""Tests with confdir/cachedir/datadir arguments."""
|
|
|
|
|
2015-05-16 22:30:00 +02:00
|
|
|
@pytest.fixture(params=[DirArgTest('', None), DirArgTest('foo', 'foo')])
|
|
|
|
def testcase(self, request, tmpdir):
|
|
|
|
"""Fixture providing testcases."""
|
|
|
|
if request.param.expected is None:
|
|
|
|
return request.param
|
|
|
|
else:
|
2015-05-25 01:20:37 +02:00
|
|
|
# prepend tmpdir to both
|
2015-05-16 22:30:00 +02:00
|
|
|
arg = str(tmpdir / request.param.arg)
|
|
|
|
return DirArgTest(arg, arg)
|
|
|
|
|
|
|
|
def test_confdir(self, testcase):
|
2015-05-16 22:12:27 +02:00
|
|
|
"""Test --confdir."""
|
2015-05-16 22:30:00 +02:00
|
|
|
args = types.SimpleNamespace(confdir=testcase.arg, cachedir=None,
|
2015-11-29 00:23:39 +01:00
|
|
|
datadir=None, basedir=None)
|
2015-05-16 22:12:27 +02:00
|
|
|
standarddir.init(args)
|
2015-05-16 22:30:00 +02:00
|
|
|
assert standarddir.config() == testcase.expected
|
2015-05-16 22:12:27 +02:00
|
|
|
|
2015-05-16 23:06:33 +02:00
|
|
|
def test_cachedir(self, testcase):
|
2015-05-16 22:12:27 +02:00
|
|
|
"""Test --cachedir."""
|
2015-05-16 22:30:00 +02:00
|
|
|
args = types.SimpleNamespace(confdir=None, cachedir=testcase.arg,
|
2015-11-29 00:23:39 +01:00
|
|
|
datadir=None, basedir=None)
|
2015-05-16 22:12:27 +02:00
|
|
|
standarddir.init(args)
|
2015-05-16 22:30:00 +02:00
|
|
|
assert standarddir.cache() == testcase.expected
|
2015-05-16 22:12:27 +02:00
|
|
|
|
2015-05-16 22:30:00 +02:00
|
|
|
def test_datadir(self, testcase):
|
2015-05-16 22:12:27 +02:00
|
|
|
"""Test --datadir."""
|
2015-05-16 22:30:00 +02:00
|
|
|
args = types.SimpleNamespace(confdir=None, cachedir=None,
|
2015-11-29 00:23:39 +01:00
|
|
|
datadir=testcase.arg, basedir=None)
|
2015-05-16 22:12:27 +02:00
|
|
|
standarddir.init(args)
|
2015-05-16 22:30:00 +02:00
|
|
|
assert standarddir.data() == testcase.expected
|
2015-05-16 23:10:20 +02:00
|
|
|
|
2016-07-09 17:38:32 +02:00
|
|
|
def test_confdir_none(self, mocker):
|
2015-05-25 01:20:37 +02:00
|
|
|
"""Test --confdir with None given."""
|
2016-07-09 17:38:32 +02:00
|
|
|
# patch makedirs to a noop so we don't really create a directory
|
|
|
|
mocker.patch('qutebrowser.utils.standarddir.os.makedirs')
|
2015-11-29 00:23:39 +01:00
|
|
|
args = types.SimpleNamespace(confdir=None, cachedir=None, datadir=None,
|
|
|
|
basedir=None)
|
2015-05-25 01:20:37 +02:00
|
|
|
standarddir.init(args)
|
2015-09-08 22:14:39 +02:00
|
|
|
assert standarddir.config().split(os.sep)[-1] == 'qute_test'
|
2015-05-25 01:20:37 +02:00
|
|
|
|
|
|
|
def test_runtimedir(self, tmpdir, monkeypatch):
|
|
|
|
"""Test runtime dir (which has no args)."""
|
|
|
|
monkeypatch.setattr(
|
|
|
|
'qutebrowser.utils.standarddir.QStandardPaths.writableLocation',
|
|
|
|
lambda _typ: str(tmpdir))
|
2015-11-29 00:23:39 +01:00
|
|
|
args = types.SimpleNamespace(confdir=None, cachedir=None, datadir=None,
|
|
|
|
basedir=None)
|
2015-05-25 01:20:37 +02:00
|
|
|
standarddir.init(args)
|
2015-09-08 22:14:39 +02:00
|
|
|
assert standarddir.runtime() == str(tmpdir / 'qute_test')
|
2015-05-25 01:20:37 +02:00
|
|
|
|
2015-05-16 23:10:20 +02:00
|
|
|
@pytest.mark.parametrize('typ', ['config', 'data', 'cache', 'download',
|
2015-09-15 20:49:10 +02:00
|
|
|
pytest.mark.linux('runtime')])
|
2015-05-16 23:10:20 +02:00
|
|
|
def test_basedir(self, tmpdir, typ):
|
|
|
|
"""Test --basedir."""
|
|
|
|
expected = str(tmpdir / typ)
|
|
|
|
args = types.SimpleNamespace(basedir=str(tmpdir))
|
|
|
|
standarddir.init(args)
|
|
|
|
func = getattr(standarddir, typ)
|
|
|
|
assert func() == expected
|
2015-05-25 01:20:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestInitCacheDirTag:
|
|
|
|
|
|
|
|
"""Tests for _init_cachedir_tag."""
|
|
|
|
|
|
|
|
def test_no_cache_dir(self, mocker, monkeypatch):
|
|
|
|
"""Smoke test with cache() returning None."""
|
|
|
|
monkeypatch.setattr('qutebrowser.utils.standarddir.cache',
|
|
|
|
lambda: None)
|
|
|
|
mocker.patch('builtins.open', side_effect=AssertionError)
|
|
|
|
standarddir._init_cachedir_tag()
|
|
|
|
|
2015-10-13 23:52:13 +02:00
|
|
|
def test_existent_cache_dir_tag(self, tmpdir, mocker, monkeypatch):
|
|
|
|
"""Test with an existent CACHEDIR.TAG."""
|
2015-05-25 01:20:37 +02:00
|
|
|
monkeypatch.setattr('qutebrowser.utils.standarddir.cache',
|
|
|
|
lambda: str(tmpdir))
|
|
|
|
mocker.patch('builtins.open', side_effect=AssertionError)
|
2015-07-06 18:35:58 +02:00
|
|
|
m = mocker.patch('qutebrowser.utils.standarddir.os')
|
|
|
|
m.path.join.side_effect = os.path.join
|
|
|
|
m.path.exists.return_value = True
|
2015-05-25 01:20:37 +02:00
|
|
|
standarddir._init_cachedir_tag()
|
|
|
|
assert not tmpdir.listdir()
|
2015-07-06 18:35:58 +02:00
|
|
|
m.path.exists.assert_called_with(str(tmpdir / 'CACHEDIR.TAG'))
|
2015-05-25 01:20:37 +02:00
|
|
|
|
|
|
|
def test_new_cache_dir_tag(self, tmpdir, mocker, monkeypatch):
|
|
|
|
"""Test creating a new CACHEDIR.TAG."""
|
|
|
|
monkeypatch.setattr('qutebrowser.utils.standarddir.cache',
|
|
|
|
lambda: str(tmpdir))
|
|
|
|
standarddir._init_cachedir_tag()
|
|
|
|
assert tmpdir.listdir() == [(tmpdir / 'CACHEDIR.TAG')]
|
|
|
|
data = (tmpdir / 'CACHEDIR.TAG').read_text('utf-8')
|
|
|
|
assert data == textwrap.dedent("""
|
|
|
|
Signature: 8a477f597d28d172789f06886806bc55
|
|
|
|
# This file is a cache directory tag created by qutebrowser.
|
|
|
|
# For information about cache directory tags, see:
|
|
|
|
# http://www.brynosaurus.com/cachedir/
|
|
|
|
""").lstrip()
|
|
|
|
|
|
|
|
def test_open_oserror(self, caplog, tmpdir, mocker, monkeypatch):
|
|
|
|
"""Test creating a new CACHEDIR.TAG."""
|
|
|
|
monkeypatch.setattr('qutebrowser.utils.standarddir.cache',
|
|
|
|
lambda: str(tmpdir))
|
|
|
|
mocker.patch('builtins.open', side_effect=OSError)
|
2015-11-11 19:57:03 +01:00
|
|
|
with caplog.at_level(logging.ERROR, 'init'):
|
2015-05-25 01:20:37 +02:00
|
|
|
standarddir._init_cachedir_tag()
|
2015-11-11 19:57:03 +01:00
|
|
|
assert len(caplog.records) == 1
|
|
|
|
assert caplog.records[0].message == 'Failed to create CACHEDIR.TAG'
|
2015-05-25 01:20:37 +02:00
|
|
|
assert not tmpdir.listdir()
|
2015-09-11 17:50:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestCreatingDir:
|
|
|
|
|
2015-10-04 15:41:42 +02:00
|
|
|
"""Make sure inexistent directories are created properly."""
|
2015-09-11 17:50:17 +02:00
|
|
|
|
2015-09-11 21:16:06 +02:00
|
|
|
DIR_TYPES = ['config', 'data', 'cache', 'download', 'runtime']
|
2015-09-11 17:50:17 +02:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('typ', DIR_TYPES)
|
|
|
|
def test_basedir(self, tmpdir, typ):
|
|
|
|
"""Test --basedir."""
|
|
|
|
basedir = tmpdir / 'basedir'
|
|
|
|
assert not basedir.exists()
|
|
|
|
args = types.SimpleNamespace(basedir=str(basedir))
|
|
|
|
standarddir.init(args)
|
|
|
|
|
|
|
|
func = getattr(standarddir, typ)
|
|
|
|
func()
|
|
|
|
|
|
|
|
assert basedir.exists()
|
|
|
|
|
|
|
|
if os.name == 'posix':
|
|
|
|
assert basedir.stat().mode & 0o777 == 0o700
|
2015-09-11 18:21:20 +02:00
|
|
|
|
2016-07-11 13:10:53 +02:00
|
|
|
@pytest.mark.usefixtures('reset_standarddir')
|
2015-09-11 18:21:20 +02:00
|
|
|
@pytest.mark.parametrize('typ', DIR_TYPES)
|
|
|
|
def test_exists_race_condition(self, mocker, tmpdir, typ):
|
|
|
|
"""Make sure there can't be a TOCTOU issue when creating the file.
|
|
|
|
|
|
|
|
See https://github.com/The-Compiler/qutebrowser/issues/942.
|
|
|
|
"""
|
|
|
|
(tmpdir / typ).ensure(dir=True)
|
|
|
|
|
|
|
|
m = mocker.patch('qutebrowser.utils.standarddir.os')
|
|
|
|
m.makedirs = os.makedirs
|
|
|
|
m.sep = os.sep
|
|
|
|
m.path.join = os.path.join
|
|
|
|
m.path.exists.return_value = False
|
|
|
|
|
|
|
|
args = types.SimpleNamespace(basedir=str(tmpdir))
|
|
|
|
standarddir.init(args)
|
|
|
|
|
|
|
|
func = getattr(standarddir, typ)
|
|
|
|
func()
|
2016-03-23 00:10:28 +01:00
|
|
|
|
|
|
|
|
2016-07-10 13:44:58 +02:00
|
|
|
@pytest.mark.usefixtures('reset_standarddir')
|
2016-03-23 00:10:28 +01:00
|
|
|
class TestSystemData:
|
|
|
|
|
|
|
|
"""Test system data path."""
|
|
|
|
|
|
|
|
def test_system_datadir_exist_linux(self, monkeypatch):
|
|
|
|
"""Test that /usr/share/qutebrowser is used if path exists."""
|
|
|
|
monkeypatch.setattr('sys.platform', "linux")
|
|
|
|
monkeypatch.setattr(os.path, 'exists', lambda path: True)
|
|
|
|
assert standarddir.system_data() == "/usr/share/qutebrowser"
|
|
|
|
|
|
|
|
@pytest.mark.linux
|
2016-07-12 03:14:33 +02:00
|
|
|
def test_system_datadir_not_exist_linux(self, monkeypatch, tmpdir,
|
|
|
|
fake_args):
|
2016-03-23 00:10:28 +01:00
|
|
|
"""Test that system-wide path isn't used on linux if path not exist."""
|
2016-07-12 03:14:33 +02:00
|
|
|
fake_args.basedir = str(tmpdir)
|
|
|
|
standarddir.init(fake_args)
|
2016-03-23 00:10:28 +01:00
|
|
|
monkeypatch.setattr(os.path, 'exists', lambda path: False)
|
|
|
|
assert standarddir.system_data() == standarddir.data()
|
|
|
|
|
2016-07-12 03:14:33 +02:00
|
|
|
def test_system_datadir_unsupportedos(self, monkeypatch, tmpdir,
|
|
|
|
fake_args):
|
2016-03-23 00:10:28 +01:00
|
|
|
"""Test that system-wide path is not used on non-Linux OS."""
|
2016-07-12 03:14:33 +02:00
|
|
|
fake_args.basedir = str(tmpdir)
|
|
|
|
standarddir.init(fake_args)
|
2016-03-23 00:10:28 +01:00
|
|
|
monkeypatch.setattr('sys.platform', "potato")
|
|
|
|
assert standarddir.system_data() == standarddir.data()
|