Converted test_standarddir to pytest

related to #10
This commit is contained in:
Bruno Oliveira 2015-04-02 19:45:52 -03:00
parent 751b62e344
commit 298892a4a8

View File

@ -22,117 +22,94 @@
import os import os
import os.path import os.path
import sys import sys
import shutil
import unittest
import tempfile
from PyQt5.QtWidgets import QApplication from PyQt5.QtWidgets import QApplication
import pytest
from qutebrowser.utils import standarddir from qutebrowser.utils import standarddir
from qutebrowser.test import helpers
class GetStandardDirLinuxTests(unittest.TestCase): @pytest.yield_fixture(autouse=True)
"""Tests for standarddir under Linux. def change_qapp_name():
Attributes:
temp_dir: A temporary directory.
old_name: The old applicationName.
""" """
Change the name of the QApplication instance for all tests in this module
def setUp(self): to "qutebrowser_test".
self.temp_dir = tempfile.mkdtemp()
self.old_name = QApplication.instance().applicationName()
QApplication.instance().setApplicationName('qutebrowser')
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
def test_data_explicit(self):
"""Test data dir with XDG_DATA_HOME explicitely set."""
with helpers.environ_set_temp({'XDG_DATA_HOME': self.temp_dir}):
standarddir.init(None)
expected = os.path.join(self.temp_dir, 'qutebrowser')
self.assertEqual(standarddir.data(), expected)
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
def test_config_explicit(self):
"""Test config dir with XDG_CONFIG_HOME explicitely set."""
with helpers.environ_set_temp({'XDG_CONFIG_HOME': self.temp_dir}):
standarddir.init(None)
expected = os.path.join(self.temp_dir, 'qutebrowser')
self.assertEqual(standarddir.config(), expected)
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
def test_cache_explicit(self):
"""Test cache dir with XDG_CACHE_HOME explicitely set."""
with helpers.environ_set_temp({'XDG_CACHE_HOME': self.temp_dir}):
standarddir.init(None)
expected = os.path.join(self.temp_dir, 'qutebrowser')
self.assertEqual(standarddir.cache(), expected)
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
def test_data(self):
"""Test data dir with XDG_DATA_HOME not set."""
env = {'HOME': self.temp_dir, 'XDG_DATA_HOME': None}
with helpers.environ_set_temp(env):
standarddir.init(None)
expected = os.path.join(self.temp_dir, '.local', 'share',
'qutebrowser')
self.assertEqual(standarddir.data(), expected)
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
def test_config(self):
"""Test config dir with XDG_CONFIG_HOME not set."""
env = {'HOME': self.temp_dir, 'XDG_CONFIG_HOME': None}
with helpers.environ_set_temp(env):
standarddir.init(None)
expected = os.path.join(self.temp_dir, '.config', 'qutebrowser')
self.assertEqual(standarddir.config(), expected)
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
def test_cache(self):
"""Test cache dir with XDG_CACHE_HOME not set."""
env = {'HOME': self.temp_dir, 'XDG_CACHE_HOME': None}
with helpers.environ_set_temp(env):
standarddir.init(None)
expected = os.path.join(self.temp_dir, '.cache', 'qutebrowser')
self.assertEqual(standarddir.cache(), expected)
def tearDown(self):
QApplication.instance().setApplicationName(self.old_name)
shutil.rmtree(self.temp_dir)
class GetStandardDirWindowsTests(unittest.TestCase):
"""Tests for standarddir under Windows.
Attributes:
old_name: The old applicationName.
""" """
old_name = QApplication.instance().applicationName()
def setUp(self):
self.old_name = QApplication.instance().applicationName()
# We can't store the files in a temp dir, so we don't chose qutebrowser
QApplication.instance().setApplicationName('qutebrowser_test') QApplication.instance().setApplicationName('qutebrowser_test')
yield
QApplication.instance().setApplicationName(old_name)
@pytest.mark.skipif(not sys.platform.startswith("linux"),
reason="requires Linux")
class TestGetStandardDirLinux(object):
"""Tests for standarddir under Linux.
"""
def test_data_explicit(self, monkeypatch, tmpdir):
"""Test data dir with XDG_DATA_HOME explicitely set."""
monkeypatch.setenv('XDG_DATA_HOME', str(tmpdir))
standarddir.init(None)
assert standarddir.data() == str(tmpdir / 'qutebrowser')
def test_config_explicit(self, monkeypatch, tmpdir):
"""Test config dir with XDG_CONFIG_HOME explicitely set."""
monkeypatch.setenv('XDG_CONFIG_HOME', str(tmpdir))
standarddir.init(None)
assert standarddir.config() == str(tmpdir / 'qutebrowser')
def test_cache_explicit(self, monkeypatch, tmpdir):
"""Test cache dir with XDG_CACHE_HOME explicitely set."""
monkeypatch.setenv('XDG_CACHE_HOME', str(tmpdir))
standarddir.init(None)
assert standarddir.cache() == str(tmpdir / 'qutebrowser')
def test_data(self, monkeypatch, tmpdir):
"""Test data dir with XDG_DATA_HOME not set."""
monkeypatch.setenv('HOME', str(tmpdir))
monkeypatch.setenv('XDG_DATA_HOME', None)
standarddir.init(None)
expected = tmpdir / '.local' / 'share' / 'qutebrowser'
assert standarddir.data() == str(expected)
def test_config(self, monkeypatch, tmpdir):
"""Test config dir with XDG_CONFIG_HOME not set."""
monkeypatch.setenv('HOME', str(tmpdir))
monkeypatch.setenv('XDG_CONFIG_HOME', None)
standarddir.init(None)
expected = tmpdir / '.config' / 'qutebrowser'
assert standarddir.config() == str(expected)
def test_cache(self, monkeypatch, tmpdir):
"""Test cache dir with XDG_CACHE_HOME not set."""
monkeypatch.setenv('HOME', str(tmpdir))
monkeypatch.setenv('XDG_CACHE_HOME', None)
standarddir.init(None)
expected = tmpdir / '.cache' / 'qutebrowser'
assert standarddir.cache() == expected
@pytest.mark.skipif(not sys.platform.startswith("win"),
reason="requires Windows")
class TestGetStandardDirWindows(object):
"""Tests for standarddir under Windows.
"""
@pytest.fixture(autouse=True)
def reset_standarddir(self):
standarddir.init(None) standarddir.init(None)
def tearDown(self):
QApplication.instance().setApplicationName(self.old_name)
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
def test_data(self): def test_data(self):
"""Test data dir.""" """Test data dir."""
self.assertEqual(standarddir.data().split(os.sep)[-2:], expected = ['qutebrowser_test', 'data']
['qutebrowser_test', 'data'], standarddir.data()) assert standarddir.data().split(os.sep)[-2:] == expected
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
def test_config(self): def test_config(self):
"""Test config dir.""" """Test config dir."""
self.assertEqual(standarddir.config().split(os.sep)[-1], assert standarddir.config().split(os.sep)[-1] == 'qutebrowser_test'
'qutebrowser_test',
standarddir.config())
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
def test_cache(self): def test_cache(self):
"""Test cache dir.""" """Test cache dir."""
self.assertEqual(standarddir.cache().split(os.sep)[-2:], expected = ['qutebrowser_test', 'cache']
['qutebrowser_test', 'cache'], standarddir.cache()) assert standarddir.cache().split(os.sep)[-2:] == expected