forked from miracle2k/python-glob2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
174 lines (142 loc) · 4.94 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import os
from os import path
import shutil
import tempfile
import glob2
from glob2 import fnmatch
class TestFnmatch(object):
def test_filter_everything(self):
names = (
'fooABC', 'barABC', 'foo',)
assert fnmatch.filter(names, 'foo*') == [
('fooABC', ('ABC',)),
('foo', ('',))
]
assert fnmatch.filter(names, '*AB*') == [
('fooABC', ('foo', 'C')),
('barABC', ('bar', 'C'))
]
def test_filter_single_character(self):
names = (
'fooA', 'barA', 'foo',)
assert fnmatch.filter(names, 'foo?') == [
('fooA', ('A',)),
]
assert fnmatch.filter(names, '???A') == [
('fooA', ('f', 'o', 'o',)),
('barA', ('b', 'a', 'r',)),
]
def test_sequence(self):
names = (
'fooA', 'fooB', 'fooC', 'foo',)
assert fnmatch.filter(names, 'foo[AB]') == [
('fooA', ('A',)),
('fooB', ('B',)),
]
assert fnmatch.filter(names, 'foo[!AB]') == [
('fooC', ('C',)),
]
class BaseTest(object):
def setup(self):
self.basedir = tempfile.mkdtemp()
self._old_cwd = os.getcwd()
os.chdir(self.basedir)
self.setup_files()
def setup_files(self):
pass
def teardown(self):
os.chdir(self._old_cwd)
shutil.rmtree(self.basedir)
def makedirs(self, *names):
for name in names:
os.makedirs(path.join(self.basedir, name))
def touch(self, *names):
for name in names:
open(path.join(self.basedir, name), 'w').close()
class TestPatterns(BaseTest):
def test(self):
self.makedirs('dir1', 'dir22')
self.touch(
'dir1/a-file', 'dir1/b-file', 'dir22/a-file', 'dir22/b-file')
assert glob2.glob('dir?/a-*', True) == [
('dir1/a-file', ('1', 'file'))
]
class TestRecursive(BaseTest):
def setup_files(self):
self.makedirs('a', 'b', 'a/foo')
self.touch('file.py', 'file.txt', 'a/bar.py', 'README', 'b/py',
'b/bar.py', 'a/foo/hello.py', 'a/foo/world.txt')
def test_recursive(self):
# ** includes the current directory
assert sorted(glob2.glob('**/*.py', True)) == [
('a/bar.py', ('a', 'bar')),
('a/foo/hello.py', ('a/foo', 'hello')),
('b/bar.py', ('b', 'bar')),
('file.py', ('', 'file')),
]
def test_exclude_root_directory(self):
# If files from the root directory should not be included,
# this is the syntax to use:
assert sorted(glob2.glob('*/**/*.py', True)) == [
('a/bar.py', ('a', '', 'bar')),
('a/foo/hello.py', ('a', 'foo', 'hello')),
('b/bar.py', ('b', '', 'bar'))
]
def test_only_directories(self):
# Return directories only
assert sorted(glob2.glob('**/', True)) == [
('a/', ('a',)),
('a/foo/', ('a/foo',)),
('b/', ('b',)),
]
def test_parent_dir(self):
# Make sure ".." can be used
os.chdir(path.join(self.basedir, 'b'))
assert sorted(glob2.glob('../a/**/*.py', True)), [
('../a/bar.py', ('', 'bar')),
('../a/foo/hello.py', ('foo', 'hello'))
]
def test_fixed_basename(self):
assert sorted(glob2.glob('**/bar.py', True)) == [
('a/bar.py', ('a',)),
('b/bar.py', ('b',)),
]
def test_all_files(self):
# Return all files
os.chdir(path.join(self.basedir, 'a'))
assert sorted(glob2.glob('**', True)) == [
('bar.py', ('bar.py',)),
('foo', ('foo',)),
('foo/hello.py', ('foo/hello.py',)),
('foo/world.txt', ('foo/world.txt',)),
]
def test_root_directory_not_returned(self):
# Ensure that a certain codepath (when the basename is globbed
# with ** as opposed to the dirname) does not cause
# the root directory to be part of the result.
# -> b/ is NOT in the result!
assert sorted(glob2.glob('b/**', True)) == [
('b/bar.py', ('bar.py',)),
('b/py', ('py',)),
]
def test_non_glob(self):
# Test without patterns.
assert glob2.glob(__file__, True) == [
(__file__, ())
]
assert glob2.glob(__file__) == [
(__file__)
]
class TestIncludeHidden(BaseTest):
def setup_files(self):
self.makedirs('a', 'b', 'a/.foo')
self.touch('file.py', 'file.txt', 'a/.bar', 'README', 'b/py',
'b/.bar', 'a/.foo/hello.py', 'a/.foo/world.txt')
def test_hidden(self):
# ** includes the current directory
assert sorted(glob2.glob('*/*', True, include_hidden=True)), [
('a/.bar', ('a', '.bar')),
('a/.foo', ('a', '.foo')),
('b/.bar', ('b', '.bar')),
('b/py', ('b', 'py')),
]