1
# -*- coding: utf-8 -*-
3
# Copyright 2011 Canonical Ltd.
5
# This program is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU Lesser General Public License version 3
7
# as published by the Free Software Foundation.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU Lesser General Public License for more details.
14
# You should have received a copy of the GNU Lesser General Public License
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
"""Tests for utilities for the base directory implementation."""
20
from dirspec import basedir
21
from dirspec.utils import get_env_path, get_special_folders, user_home
22
from dirspec.tests import BaseTestCase
25
class UtilsTestCase(BaseTestCase):
26
"""Test for the multiplatform directory utilities."""
28
def test_user_home_is_utf8_bytes(self):
29
"""The returned path is bytes."""
31
self.assert_utf8_bytes(actual)
34
class FakeShellConModule(object):
35
"""Override CSIDL_ constants."""
38
CSIDL_LOCAL_APPDATA = 1
39
CSIDL_COMMON_APPDATA = 2
42
class FakeShellModule(object):
44
"""Fake Shell Module."""
47
"""Set the proper mapping between CSIDL_ consts."""
49
0: u'c:\\path\\to\\users\\home',
50
1: u'c:\\path\\to\\users\\home\\appData\\local',
51
2: u'c:\\programData',
54
# pylint: disable=C0103
55
def SHGetFolderPath(self, dummy0, shellconValue, dummy2, dummy3):
56
"""Override SHGetFolderPath functionality."""
57
return self.values[shellconValue]
58
# pylint: enable=C0103
61
class TestBaseDirectoryWindows(BaseTestCase):
62
"""Tests for the BaseDirectory module."""
64
def test_get_special_folders(self):
65
"""Make sure we can import the platform module."""
67
shell_module = FakeShellModule()
68
self.patch(win32com.shell, "shell", shell_module)
69
self.patch(win32com.shell, "shellcon", FakeShellConModule())
70
special_folders = get_special_folders()
71
self.assertTrue('Personal' in special_folders)
72
self.assertTrue('Local AppData' in special_folders)
73
self.assertTrue('AppData' in special_folders)
74
self.assertTrue('Common AppData' in special_folders)
76
self.assertTrue(special_folders['Personal'] == \
77
shell_module.values[FakeShellConModule.CSIDL_PROFILE])
78
self.assertTrue(special_folders['Local AppData'] == \
79
shell_module.values[FakeShellConModule.CSIDL_LOCAL_APPDATA])
80
self.assertTrue(special_folders['Local AppData'].startswith(
81
special_folders['AppData']))
82
self.assertTrue(special_folders['Common AppData'] == \
83
shell_module.values[FakeShellConModule.CSIDL_COMMON_APPDATA])
85
for val in special_folders.itervalues():
86
self.assertIsInstance(val, str)
88
# Should not raise exceptions
90
if sys.platform != 'win32':
91
test_get_special_folders.skip = 'Only works on Win32'
93
def test_get_data_dirs(self):
94
"""Check thet get_data_dirs uses pathsep correctly."""
95
bad_sep = filter(lambda x: x not in os.pathsep, ":;")
96
dir_list = ["A", "B", bad_sep, "C"]
97
self.patch(os, "environ",
98
dict(XDG_DATA_DIRS=os.pathsep.join(
100
dirs = basedir.get_xdg_data_dirs()[1:]
101
self.assertEqual(dirs, dir_list)
103
def test_get_config_dirs(self):
104
"""Check thet get_data_dirs uses pathsep correctly."""
105
bad_sep = filter(lambda x: x not in os.pathsep, ":;")
106
dir_list = ["A", "B", bad_sep, "C"]
107
self.patch(os, "environ",
108
dict(XDG_CONFIG_DIRS=os.pathsep.join(
110
dirs = basedir.get_xdg_config_dirs()[1:]
111
self.assertEqual(dirs, dir_list)
113
def set_fake_environ(self, key, value):
114
"""Set (and restore) a fake environ variable."""
115
if key in os.environ:
116
prev = os.environ[key]
117
self.addCleanup(os.environ.__setitem__, key, prev)
119
self.addCleanup(os.environ.__delitem__, key)
120
os.environ[key] = value
122
def unset_fake_environ(self, key):
123
"""Unset (and restore) a fake environ variable."""
124
if key in os.environ:
125
current_value = os.environ[key]
126
self.addCleanup(os.environ.__setitem__, key, current_value)
129
def test_get_env_path_var(self):
130
"""Test that get_env_path transforms an env var."""
131
fake_path = u"C:\\Users\\Ñandú"
132
fake_env_var = "FAKE_ENV_VAR"
134
mbcs_path = fake_path.encode(sys.getfilesystemencoding())
135
utf8_path = fake_path.encode("utf-8")
137
self.set_fake_environ(fake_env_var, mbcs_path)
138
self.assertEqual(get_env_path(fake_env_var, "unexpected"), utf8_path)
140
def test_get_env_path_no_var(self):
141
"""Test that get_env_path returns the default when env var not set."""
142
fake_path = u"C:\\Users\\Ñandú"
143
fake_env_var = "fake_env_var"
144
default = fake_path.encode(sys.getfilesystemencoding())
146
self.unset_fake_environ(fake_env_var)
147
self.assertEqual(get_env_path(fake_env_var, default), default)