~dobey/dirspec/python3

« back to all changes in this revision

Viewing changes to dirspec/tests/test_utils.py

  • Committer: Rodney Dawes
  • Date: 2011-12-17 00:46:26 UTC
  • mto: This revision was merged to the branch mainline in revision 2.
  • Revision ID: rodney.dawes@canonical.com-20111217004626-0acfk0hg85ko9o3g
Add tests, and the implementation of the XDG Base Directory spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright 2011 Canonical Ltd.
 
4
#
 
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.
 
8
#
 
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.
 
13
#
 
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."""
 
17
import os
 
18
import sys
 
19
 
 
20
from dirspec import basedir
 
21
from dirspec.utils import get_env_path, get_special_folders, user_home
 
22
from dirspec.tests import BaseTestCase
 
23
 
 
24
 
 
25
class UtilsTestCase(BaseTestCase):
 
26
    """Test for the multiplatform directory utilities."""
 
27
 
 
28
    def test_user_home_is_utf8_bytes(self):
 
29
        """The returned path is bytes."""
 
30
        actual = user_home
 
31
        self.assert_utf8_bytes(actual)
 
32
 
 
33
 
 
34
class FakeShellConModule(object):
 
35
    """Override CSIDL_ constants."""
 
36
 
 
37
    CSIDL_PROFILE = 0
 
38
    CSIDL_LOCAL_APPDATA = 1
 
39
    CSIDL_COMMON_APPDATA = 2
 
40
 
 
41
 
 
42
class FakeShellModule(object):
 
43
 
 
44
    """Fake Shell Module."""
 
45
 
 
46
    def __init__(self):
 
47
        """Set the proper mapping between CSIDL_ consts."""
 
48
        self.values = {
 
49
            0: u'c:\\path\\to\\users\\home',
 
50
            1: u'c:\\path\\to\\users\\home\\appData\\local',
 
51
            2: u'c:\\programData',
 
52
        }
 
53
 
 
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
 
59
 
 
60
 
 
61
class TestBaseDirectoryWindows(BaseTestCase):
 
62
    """Tests for the BaseDirectory module."""
 
63
 
 
64
    def test_get_special_folders(self):
 
65
        """Make sure we can import the platform module."""
 
66
        import win32com.shell
 
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)
 
75
 
 
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])
 
84
 
 
85
        for val in special_folders.itervalues():
 
86
            self.assertIsInstance(val, str)
 
87
            val.decode('utf8')
 
88
        # Should not raise exceptions
 
89
 
 
90
    if sys.platform != 'win32':
 
91
        test_get_special_folders.skip = 'Only works on Win32'
 
92
 
 
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(
 
99
                dir_list)))
 
100
        dirs = basedir.get_xdg_data_dirs()[1:]
 
101
        self.assertEqual(dirs, dir_list)
 
102
 
 
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(
 
109
                dir_list)))
 
110
        dirs = basedir.get_xdg_config_dirs()[1:]
 
111
        self.assertEqual(dirs, dir_list)
 
112
 
 
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)
 
118
        else:
 
119
            self.addCleanup(os.environ.__delitem__, key)
 
120
        os.environ[key] = value
 
121
 
 
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)
 
127
            del(os.environ[key])
 
128
 
 
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"
 
133
 
 
134
        mbcs_path = fake_path.encode(sys.getfilesystemencoding())
 
135
        utf8_path = fake_path.encode("utf-8")
 
136
 
 
137
        self.set_fake_environ(fake_env_var, mbcs_path)
 
138
        self.assertEqual(get_env_path(fake_env_var, "unexpected"), utf8_path)
 
139
 
 
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())
 
145
 
 
146
        self.unset_fake_environ(fake_env_var)
 
147
        self.assertEqual(get_env_path(fake_env_var, default), default)