def test_system_datadir_unsupportedos(self, monkeypatch, tmpdir, fake_args): """Test that system-wide path is not used on non-Linux OS.""" fake_args.basedir = str(tmpdir) monkeypatch.setattr('sys.platform', "potato") standarddir._init_dirs(fake_args) assert standarddir.data(system=True) == standarddir.data()
def test_linux_normal(self, monkeypatch, tmpdir, func, subdirs): """Test dirs with XDG_*_HOME not set.""" monkeypatch.setenv('HOME', str(tmpdir)) for var in ['DATA', 'CONFIG', 'CACHE']: monkeypatch.delenv('XDG_{}_HOME'.format(var), raising=False) standarddir._init_dirs() assert func() == str(tmpdir.join(*subdirs))
def test_no_qapplication(qapp, tmpdir): """Make sure directories with/without QApplication are equal.""" sub_code = """ import sys import json sys.path = sys.argv[1:] # make sure we have the same python path from PyQt5.QtWidgets import QApplication from qutebrowser.utils import standarddir assert QApplication.instance() is None standarddir.APPNAME = 'qute_test' standarddir._init_dirs() locations = {k.name: v for k, v in standarddir._locations.items()} print(json.dumps(locations)) """ pyfile = tmpdir / 'sub.py' pyfile.write_text(textwrap.dedent(sub_code), encoding='ascii') output = subprocess.check_output([sys.executable, str(pyfile)] + sys.path, universal_newlines=True) sub_locations = json.loads(output) standarddir._init_dirs() locations = {k.name: v for k, v in standarddir._locations.items()} assert sub_locations == locations
def test_basedir(self, tmpdir, typ, args): """Test --basedir.""" expected = str(tmpdir / typ) init_args = types.SimpleNamespace(basedir=str(tmpdir)) standarddir._init_dirs(init_args) func = getattr(standarddir, typ) assert func(*args) == expected
def test_system_datadir_not_exist_linux(self, monkeypatch, tmpdir, fake_args): """Test that system-wide path isn't used on linux if path not exist.""" fake_args.basedir = str(tmpdir) monkeypatch.setattr(os.path, 'exists', lambda path: False) standarddir._init_dirs(fake_args) assert standarddir.data(system=True) == standarddir.data()
def test_basedir_relative(self, tmpdir): """Test --basedir with a relative path.""" basedir = (tmpdir / 'basedir') basedir.ensure(dir=True) with tmpdir.as_cwd(): args = types.SimpleNamespace(basedir='basedir') standarddir._init_dirs(args) assert standarddir.config() == str(basedir / 'config')
def test_linux_invalid_runtimedir(self, monkeypatch, tmpdir): """With invalid XDG_RUNTIME_DIR, fall back to TempLocation.""" tmpdir_env = tmpdir / 'temp' tmpdir_env.ensure(dir=True) monkeypatch.setenv('XDG_RUNTIME_DIR', str(tmpdir / 'does-not-exist')) monkeypatch.setenv('TMPDIR', str(tmpdir_env)) standarddir._init_dirs() assert standarddir.runtime() == str(tmpdir_env / APPNAME)
def test_downloads_dir_not_created(monkeypatch, tmpdir): """Make sure ~/Downloads is not created.""" download_dir = tmpdir / 'Downloads' monkeypatch.setenv('HOME', str(tmpdir)) # Make sure xdg-user-dirs.dirs is not picked up monkeypatch.delenv('XDG_CONFIG_HOME', raising=False) standarddir._init_dirs() assert standarddir.download() == str(download_dir) assert not download_dir.exists()
def test_linux_explicit(self, monkeypatch, tmpdir, func, varname): """Test dirs with XDG environment variables explicitly set. Args: func: The function to test. varname: The environment variable which should be set. """ monkeypatch.setenv(varname, str(tmpdir)) standarddir._init_dirs() assert func() == str(tmpdir / APPNAME)
def test_basedir(self, tmpdir, typ): """Test --basedir.""" basedir = tmpdir / 'basedir' assert not basedir.exists() args = types.SimpleNamespace(basedir=str(basedir)) standarddir._init_dirs(args) func = getattr(standarddir, typ) func() assert basedir.exists() if utils.is_posix: assert basedir.stat().mode & 0o777 == 0o700
def test_basedir(self, tmpdir, typ): """Test --basedir.""" basedir = tmpdir / 'basedir' assert not basedir.exists() args = types.SimpleNamespace(basedir=str(basedir)) standarddir._init_dirs(args) func = getattr(standarddir, typ) func() assert basedir.exists() if os.name == 'posix': assert basedir.stat().mode & 0o777 == 0o700
def test_no_qapplication(qapp, tmpdir, monkeypatch): """Make sure directories with/without QApplication are equal.""" sub_code = """ import sys import json sys.path = sys.argv[1:] # make sure we have the same python path from PyQt5.QtWidgets import QApplication from qutebrowser.utils import standarddir assert QApplication.instance() is None standarddir.APPNAME = 'qute_test' standarddir._init_dirs() locations = {k.name: v for k, v in standarddir._locations.items()} print(json.dumps(locations)) """ pyfile = tmpdir / 'sub.py' pyfile.write_text(textwrap.dedent(sub_code), encoding='ascii') for name in ['CONFIG', 'DATA', 'CACHE']: monkeypatch.delenv('XDG_{}_HOME'.format(name), raising=False) runtime_dir = tmpdir / 'runtime' runtime_dir.ensure(dir=True) runtime_dir.chmod(0o0700) monkeypatch.setenv('XDG_RUNTIME_DIR', str(runtime_dir)) home_dir = tmpdir / 'home' home_dir.ensure(dir=True) monkeypatch.setenv('HOME', str(home_dir)) proc = subprocess.run([sys.executable, str(pyfile)] + sys.path, universal_newlines=True, check=True, stdout=subprocess.PIPE) sub_locations = json.loads(proc.stdout) standarddir._init_dirs() locations = {k.name: v for k, v in standarddir._locations.items()} assert sub_locations == locations
def test_basedir(self, tmpdir, typ): """Test --basedir.""" basedir = tmpdir / 'basedir' assert not basedir.exists() args = types.SimpleNamespace(basedir=str(basedir)) standarddir._init_dirs(args) func = getattr(standarddir, typ) func() assert basedir.exists() if typ == 'download' or (typ == 'runtime' and not utils.is_linux): assert not (basedir / typ).exists() else: assert (basedir / typ).exists() if utils.is_posix: assert (basedir / typ).stat().mode & 0o777 == 0o700
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/qutebrowser/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.expanduser = os.path.expanduser m.path.exists.return_value = False m.path.abspath = lambda x: x args = types.SimpleNamespace(basedir=str(tmpdir)) standarddir._init_dirs(args) func = getattr(standarddir, typ) func()
def test_mac(self, func, elems, expected): standarddir._init_dirs() assert func().split(os.sep)[-elems:] == expected
def fake_runtime_dir(monkeypatch, short_tmpdir): monkeypatch.setenv('XDG_RUNTIME_DIR', str(short_tmpdir)) standarddir._init_dirs() return short_tmpdir
def test_system_datadir_exist_linux(self, monkeypatch): """Test that /usr/share/qute_test is used if path exists.""" monkeypatch.setattr('sys.platform', "linux") monkeypatch.setattr(os.path, 'exists', lambda path: True) standarddir._init_dirs() assert standarddir.data(system=True) == "/usr/share/qute_test"
def test_linux_normal(self, fake_home_envvar, tmp_path, func, subdirs): """Test dirs with XDG_*_HOME not set.""" standarddir._init_dirs() assert func() == str(tmp_path.joinpath(*subdirs))
def test_system_datadir_exist_linux(self, monkeypatch): """Test that /usr/share/qute_test is used if path exists.""" monkeypatch.setattr(os.path, 'exists', lambda path: True) standarddir._init_dirs() assert standarddir.data(system=True) == "/usr/share/qute_test"