forked from oils-for-unix/oils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glob_test.py
executable file
·163 lines (131 loc) · 4.2 KB
/
glob_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
#!/usr/bin/env python2
"""
glob_test.py: Tests for glob.py
"""
from __future__ import print_function
import re
import unittest
from frontend import match
from osh import glob_
class GlobEscapeTest(unittest.TestCase):
def testEscapeUnescape(self):
esc = glob_.GlobEscape
unesc = glob_.GlobUnescape
pairs = [
(r'\*.py', '*.py'),
(r'\?.py', '?.py'),
(r'\[a\-z\]\[\[\:punct\:\]\]', '[a-z][[:punct:]]'),
(r'\\n', r'\n'),
]
for e, u in pairs:
self.assertEqual(e, esc(u))
self.assertEqual(u, unesc(e))
def testLooksLikeGlob(self):
# The way to test bash behavior is:
# $ shopt -s nullglob; argv [ # not a glob
# $ shopt -s nullglob; argv [] # is a glob
# $ shopt -s nullglob; argv [][ # is a glob
CASES = [
(r'[]', True),
(r'[a]', True),
(r'[][', True),
(r'][', False), # no balanced pair
(r'\[]', False), # no balanced pair
(r'[', False), # no balanced pair
(r']', False), # no balanced pair
(r'echo', False),
(r'status=0', False),
(r'*', True),
(r'\*', False),
(r'\*.sh', False),
('\\', False),
('*\\', True),
('?', True),
]
for pat, expected in CASES:
self.assertEqual(expected, glob_.LooksLikeGlob(pat),
'%s: expected %r' % (pat, expected))
def testGlobStripRegexes(self):
s = 'aabbccdd'
# ${v%c*} # shortest suffix
m = re.match('^(.*)c.*$', s)
self.assertEqual('aabbc', m.group(1))
# ${v%%c*} # longest suffix
m = re.match('^(.*?)c.*$', s)
self.assertEqual('aabb', m.group(1))
# ${v#*b} # shortest prefix
m = re.match('^.*?b(.*)$', s)
self.assertEqual('bccdd', m.group(1))
# ${v##*b} # longest prefix
m = re.match('^.*b(.*)$', s)
self.assertEqual('ccdd', m.group(1))
def testPatSubRegexes(self):
# x=~/git/oil
# ${x//git*/X/}
# git*
r1 = re.compile('git.*')
result = r1.sub('X', '~/git/oil')
self.assertEqual('~/X', result)
r2 = re.compile('[a-z]')
result = r2.sub('X', 'a-b-c')
self.assertEqual('X-X-X', result)
# Substitute the first one only
r2 = re.compile('[a-z]')
result = r2.sub('X', 'a-b-c', count=1)
self.assertEqual('X-b-c', result)
def _ReadTokens(s):
lex = match.GLOB_LEXER
return list(lex.Tokens(s))
class GlobParserTest(unittest.TestCase):
def testGlobLexer(self):
print(_ReadTokens(''))
print(_ReadTokens('*.py'))
print(_ReadTokens('\*.py'))
print(_ReadTokens('[abc]'))
print(_ReadTokens('\\')) # Enf
print(_ReadTokens('\\x'))
print(_ReadTokens(r'\\'))
print(_ReadTokens(r'[[:alpha:]]'))
print(_ReadTokens(r'[?]'))
def testGlobParser(self):
CASES = [
# (glob input, expected AST, expected extended regexp, has error)
('*.py', '.*\.py', False),
('*.?', '.*\..', False),
('<*>', '<.*>', False),
('\**+', '\*.*\+', False),
('\**', '\*.*', False),
('*.[ch]pp', '.*\.[ch]pp', False),
# not globs
('abc', 'abc', False),
('\\*', '\\*', False),
('c:\\foo', 'c:foo', False),
('strange]one', 'strange\\]one', False),
# character class globs
('[[:space:]abc]', '[[:space:]abc]', False),
('[abc]', '[abc]', False),
(r'[\a\b\c]', r'[\a\b\c]', False),
('[abc\[]', '[abc\[]', False),
('[!not]', '[^not]', False),
('[^also_not]', '[^also_not]', False),
('[!*?!\\[]', '[^*?!\\[]', False),
('[!\]foo]', '[^\]foo]', False),
# invalid globs
('not_closed[a-z', 'not_closed\\[a-z', True),
('[[:spa[ce:]]', '\\[\\[:spa\\[ce:\\]\\]', True),
# Regression test for IndexError.
('[', '\\[', True),
('\\', '\\\\', True),
(']', '\\]', False),
]
for glob, expected_ere, expected_err in CASES:
print('===')
print(glob)
regex, warnings = glob_.GlobToERE(glob)
self.assertEqual(
expected_ere, regex,
'Expected %r to translate to %r, got %r' % (glob, expected_ere, regex))
print('regex : %s' % regex)
print('warnings: %s' % warnings)
if __name__ == '__main__':
unittest.main()