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