19
19
"""The test suite for the control panel utilities."""
23
24
from twisted.trial.unittest import TestCase
25
26
from ubuntuone.controlpanel import utils
28
class GetDataTestCase(TestCase):
29
"""Test case for get_data_dir and get_data_file."""
27
from ubuntuone.controlpanel.tests import testcase
28
# XXX: to be replaced when MementoHandler is there
29
#from ubuntuone.devtools import testcase
32
CONSTANTS_MODULE = 'ubuntuone.controlpanel.constants'
33
NOT_DEFINED = object()
36
class FakedConstantsModule(object):
37
"""FAke the 'ubuntuone.controlpanel.constants' module."""
39
DATA_DIR = '/tmp/foo/bar'
42
class GetDataDirNoConstantsTestCase(TestCase):
43
"""Test case for get_data_dir when constants module is not defined."""
45
# pylint: disable=E1101
48
self._constants = sys.modules.get(CONSTANTS_MODULE, NOT_DEFINED)
49
sys.modules[CONSTANTS_MODULE] = None # force ImportError
51
module = utils.os.path.dirname(utils.__file__)
52
rel_data = utils.os.path.join(module, utils.os.path.pardir,
53
utils.os.path.pardir, utils.DATA_SUFFIX)
54
self.rel_data_path = utils.os.path.abspath(rel_data)
59
fdir = utils.os.path.join(ddir, 'ubuntuone-control-panel')
60
setattr(self, 'data_dir_%i' % i, fdir)
63
self.patch(utils.xdg.BaseDirectory, 'xdg_data_dirs', dirs)
65
self.memento = testcase.MementoHandler()
66
self.memento.setLevel(logging.DEBUG)
67
utils.logger.addHandler(self.memento)
70
if self._constants is not NOT_DEFINED:
71
sys.modules[CONSTANTS_MODULE] = self._constants
73
sys.modules.pop(CONSTANTS_MODULE)
75
def assert_correct_dir(self, expected_dir):
76
"""Check that utils.get_data_dir returned expected_dir."""
77
# ensure expected_path exists at os level
78
self.patch(utils.os.path, 'exists', lambda path: path == expected_dir)
80
result = utils.get_data_dir()
81
self.assertEqual(expected_dir, result)
31
83
def test_get_data_dir_relative(self):
32
84
"""The relative path for the data directory is correctly retrieved."""
33
module = os.path.dirname(utils.__file__)
34
expected = os.path.abspath(os.path.join(module, os.path.pardir,
35
os.path.pardir, 'data'))
36
result = utils.get_data_dir()
37
self.assertEqual(expected, result)
85
self.assert_correct_dir(self.rel_data_path)
87
def test_get_data_dir_from_xdg_first_in_the_list(self):
88
"""The system data directory is correctly retrieved."""
89
self.assert_correct_dir(self.data_dir_0)
91
def test_get_data_dir_from_xdg_middle_in_the_list(self):
92
"""The system data directory is correctly retrieved."""
93
self.assert_correct_dir(self.data_dir_1)
95
def test_get_data_dir_from_xdg_last_in_the_list(self):
96
"""The system data directory is correctly retrieved."""
97
self.assert_correct_dir(self.data_dir_2)
99
def test_get_data_dir_none_exists(self):
100
"""No data directory exists, return None and log as error."""
101
self.patch(utils.os.path, 'exists', lambda path: False)
102
sys.modules[CONSTANTS_MODULE] = None
104
result = utils.get_data_dir()
105
self.assertTrue(result is None)
106
msg = 'get_data_dir: can not build a valid data path'
107
self.assertTrue(self.memento.check_error(msg))
110
class GetDataDirWithConstantsTestCase(GetDataDirNoConstantsTestCase):
111
"""Test case for get_data_dir when constants module is defined."""
114
super(GetDataDirWithConstantsTestCase, self).setUp()
115
self.patch(utils.os.path, 'exists', lambda path: False)
116
self._constants = sys.modules.get(CONSTANTS_MODULE, NOT_DEFINED)
117
sys.modules[CONSTANTS_MODULE] = FakedConstantsModule()
119
def test_get_data_dir(self):
120
"""If the constants.py module exists, use DATA_DIR from it."""
121
result = utils.get_data_dir()
122
self.assertEqual(sys.modules[CONSTANTS_MODULE].DATA_DIR, result)
125
class GetDataFileTestCase(TestCase):
126
"""Test cases for get_data_file."""
39
128
def test_get_data_file(self):
40
129
"""The path for a data file is correctly retrieved."""