13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20
from bzrlib import osutils
20
from bzrlib.tests import TestCase, TestCaseInTempDir, Feature
21
from bzrlib.win32utils import glob_expand
21
from bzrlib.tests import TestCase, TestCaseInTempDir, TestSkipped, Feature
22
from bzrlib.win32utils import glob_expand, get_app_path
23
from bzrlib import win32utils
35
37
NeedsGlobExpansionFeature = _NeedsGlobExpansionFeature()
40
class _RequiredModuleFeature(Feature):
42
def __init__(self, mod_name):
43
self.mod_name = mod_name
44
super(_RequiredModuleFeature, self).__init__()
48
__import__(self.mod_name)
53
def feature_name(self):
56
Win32RegistryFeature = _RequiredModuleFeature('_winreg')
57
CtypesFeature = _RequiredModuleFeature('ctypes')
58
Win32comShellFeature = _RequiredModuleFeature('win32com.shell')
41
64
class TestNeedsGlobExpansionFeature(TestCase):
43
66
def test_available(self):
44
self.assertEqual(sys.platform == 'win32',
67
self.assertEqual(sys.platform == 'win32',
45
68
NeedsGlobExpansionFeature.available())
47
70
def test_str(self):
48
71
self.assertTrue("performed" in str(NeedsGlobExpansionFeature))
61
84
[['a', 'a'], ['a', 'a']]])
63
86
def test_tree_ascii(self):
64
87
"""Checks the glob expansion and path separation char
66
89
self.build_tree(['a', 'a1', 'a2', 'a11', 'a.1',
67
90
'b', 'b1', 'b2', 'b3',
69
92
'd/', 'd/d1', 'd/d2', 'd/e/', 'd/e/e1'])
70
93
self._run_testset([
73
96
[[u'a', u'a' ], [u'a', u'a']],
77
100
[[u'd/'], [u'd/']],
78
101
[[u'd\\'], [u'd/']],
81
104
[[u'a*'], [u'a', u'a1', u'a2', u'a11', u'a.1']],
82
105
[[u'?'], [u'a', u'b', u'c', u'd']],
84
107
[[u'a??'], [u'a11', u'a.1']],
85
108
[[u'b[1-2]'], [u'b1', u'b2']],
86
109
[[u'A?'], [u'a1', u'a2']],
88
111
[[u'd/*'], [u'd/d1', u'd/d2', u'd/e']],
89
112
[[u'd\\*'], [u'd/d1', u'd/d2', u'd/e']],
90
113
[[u'?\\*'], [u'c/c1', u'c/c2', u'd/d1', u'd/d2', u'd/e']],
91
114
[[u'*\\*'], [u'c/c1', u'c/c2', u'd/d1', u'd/d2', u'd/e']],
92
115
[[u'*/'], [u'c/', u'd/']],
93
116
[[u'*\\'], [u'c/', u'd/']]])
95
118
def test_tree_unicode(self):
96
119
"""Checks behaviour with non-ascii filenames"""
97
120
self.build_tree([u'\u1234', u'\u1234\u1234', u'\u1235/', u'\u1235/\u1235'])
100
123
[[u'\u1234'], [u'\u1234']],
101
124
[[u'\u1235'], [u'\u1235']],
103
126
[[u'\u1235/'], [u'\u1235/']],
104
127
[[u'\u1235/\u1235'], [u'\u1235/\u1235']],
107
130
[[u'?'], [u'\u1234', u'\u1235']],
108
131
[[u'*'], [u'\u1234', u'\u1234\u1234', u'\u1235']],
109
132
[[u'\u1234*'], [u'\u1234', u'\u1234\u1234']],
111
134
[[u'\u1235/?'], [u'\u1235/\u1235']],
112
135
[[u'\u1235/*'], [u'\u1235/\u1235']],
113
136
[[u'\u1235\\?'], [u'\u1235/\u1235']],
129
152
self.assertEqual(expected, result, 'pattern %s' % pattern)
155
class TestAppPaths(TestCase):
157
_test_needs_features = [Win32RegistryFeature]
159
def test_iexplore(self):
160
# typical windows users should have IE installed
161
for a in ('iexplore', 'iexplore.exe'):
163
d, b = os.path.split(p)
164
self.assertEquals('iexplore.exe', b.lower())
165
self.assertNotEquals('', d)
167
def test_not_existing(self):
168
p = get_app_path('not-existing')
169
self.assertEquals('not-existing', p)
172
class TestLocationsCtypes(TestCase):
174
_test_needs_features = [CtypesFeature]
176
def assertPathsEqual(self, p1, p2):
177
# TODO: The env var values in particular might return the "short"
178
# version (ie, "C:\DOCUME~1\..."). Its even possible the returned
179
# values will differ only by case - handle these situations as we
181
self.assertEquals(p1, p2)
183
def test_appdata_not_using_environment(self):
184
# Test that we aren't falling back to the environment
185
first = win32utils.get_appdata_location()
186
self._captureVar("APPDATA", None)
187
self.assertPathsEqual(first, win32utils.get_appdata_location())
189
def test_appdata_matches_environment(self):
190
# Typically the APPDATA environment variable will match
191
# get_appdata_location
192
# XXX - See bug 262874, which asserts the correct encoding is 'mbcs',
193
encoding = osutils.get_user_encoding()
194
env_val = os.environ.get("APPDATA", None)
196
raise TestSkipped("No APPDATA environment variable exists")
197
self.assertPathsEqual(win32utils.get_appdata_location(),
198
env_val.decode(encoding))
200
def test_local_appdata_not_using_environment(self):
201
# Test that we aren't falling back to the environment
202
first = win32utils.get_local_appdata_location()
203
self._captureVar("LOCALAPPDATA", None)
204
self.assertPathsEqual(first, win32utils.get_local_appdata_location())
206
def test_local_appdata_matches_environment(self):
207
# LOCALAPPDATA typically only exists on Vista, so we only attempt to
208
# compare when it exists.
209
lad = win32utils.get_local_appdata_location()
210
env = os.environ.get("LOCALAPPDATA")
212
# XXX - See bug 262874, which asserts the correct encoding is 'mbcs'
213
encoding = osutils.get_user_encoding()
214
self.assertPathsEqual(lad, env.decode(encoding))
217
class TestLocationsPywin32(TestLocationsCtypes):
219
_test_needs_features = [Win32comShellFeature]
222
super(TestLocationsPywin32, self).setUp()
223
# We perform the exact same tests after disabling the use of ctypes.
224
# This causes the implementation to fall back to pywin32.
225
self.old_ctypes = win32utils.has_ctypes
226
win32utils.has_ctypes = False
227
self.addCleanup(self.restoreCtypes)
229
def restoreCtypes(self):
230
win32utils.has_ctypes = self.old_ctypes