blob: 4fafdd4f2e4588b3a90d3c6ec8c402e7e19b4df3 [file] [log] [blame]
Éric Araujo9bce3112011-07-27 18:29:31 +02001import io
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +00002import shlex
Éric Araujo9bce3112011-07-27 18:29:31 +02003import string
4import unittest
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +00005
Neal Norwitzaa1ac542003-04-17 23:04:22 +00006
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +00007
8# The original test data set was from shellwords, by Hartmut Goebel.
9
10data = r"""x|x|
11foo bar|foo|bar|
12 foo bar|foo|bar|
13 foo bar |foo|bar|
14foo bar bla fasel|foo|bar|bla|fasel|
15x y z xxxx|x|y|z|xxxx|
16\x bar|\|x|bar|
17\ x bar|\|x|bar|
18\ bar|\|bar|
19foo \x bar|foo|\|x|bar|
20foo \ x bar|foo|\|x|bar|
21foo \ bar|foo|\|bar|
22foo "bar" bla|foo|"bar"|bla|
23"foo" "bar" "bla"|"foo"|"bar"|"bla"|
24"foo" bar "bla"|"foo"|bar|"bla"|
25"foo" bar bla|"foo"|bar|bla|
26foo 'bar' bla|foo|'bar'|bla|
27'foo' 'bar' 'bla'|'foo'|'bar'|'bla'|
28'foo' bar 'bla'|'foo'|bar|'bla'|
29'foo' bar bla|'foo'|bar|bla|
30blurb foo"bar"bar"fasel" baz|blurb|foo"bar"bar"fasel"|baz|
31blurb foo'bar'bar'fasel' baz|blurb|foo'bar'bar'fasel'|baz|
32""|""|
33''|''|
34foo "" bar|foo|""|bar|
35foo '' bar|foo|''|bar|
36foo "" "" "" bar|foo|""|""|""|bar|
37foo '' '' '' bar|foo|''|''|''|bar|
38\""|\|""|
39"\"|"\"|
40"foo\ bar"|"foo\ bar"|
41"foo\\ bar"|"foo\\ bar"|
42"foo\\ bar\"|"foo\\ bar\"|
43"foo\\" bar\""|"foo\\"|bar|\|""|
44"foo\\ bar\" dfadf"|"foo\\ bar\"|dfadf"|
45"foo\\\ bar\" dfadf"|"foo\\\ bar\"|dfadf"|
46"foo\\\x bar\" dfadf"|"foo\\\x bar\"|dfadf"|
47"foo\x bar\" dfadf"|"foo\x bar\"|dfadf"|
48\''|\|''|
49'foo\ bar'|'foo\ bar'|
50'foo\\ bar'|'foo\\ bar'|
51"foo\\\x bar\" df'a\ 'df'|"foo\\\x bar\"|df'a|\|'df'|
52\"foo"|\|"foo"|
53\"foo"\x|\|"foo"|\|x|
54"foo\x"|"foo\x"|
55"foo\ "|"foo\ "|
56foo\ xx|foo|\|xx|
57foo\ x\x|foo|\|x|\|x|
58foo\ x\x\""|foo|\|x|\|x|\|""|
59"foo\ x\x"|"foo\ x\x"|
60"foo\ x\x\\"|"foo\ x\x\\"|
61"foo\ x\x\\""foobar"|"foo\ x\x\\"|"foobar"|
62"foo\ x\x\\"\''"foobar"|"foo\ x\x\\"|\|''|"foobar"|
63"foo\ x\x\\"\'"fo'obar"|"foo\ x\x\\"|\|'"fo'|obar"|
64"foo\ x\x\\"\'"fo'obar" 'don'\''t'|"foo\ x\x\\"|\|'"fo'|obar"|'don'|\|''|t'|
65'foo\ bar'|'foo\ bar'|
66'foo\\ bar'|'foo\\ bar'|
67foo\ bar|foo|\|bar|
68foo#bar\nbaz|foobaz|
69:-) ;-)|:|-|)|;|-|)|
Antoine Pitroud72402e2010-10-27 18:52:48 +000070áéíóú|á|é|í|ó|ú|
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +000071"""
72
73posix_data = r"""x|x|
74foo bar|foo|bar|
75 foo bar|foo|bar|
76 foo bar |foo|bar|
77foo bar bla fasel|foo|bar|bla|fasel|
78x y z xxxx|x|y|z|xxxx|
79\x bar|x|bar|
80\ x bar| x|bar|
81\ bar| bar|
82foo \x bar|foo|x|bar|
83foo \ x bar|foo| x|bar|
84foo \ bar|foo| bar|
85foo "bar" bla|foo|bar|bla|
86"foo" "bar" "bla"|foo|bar|bla|
87"foo" bar "bla"|foo|bar|bla|
88"foo" bar bla|foo|bar|bla|
89foo 'bar' bla|foo|bar|bla|
90'foo' 'bar' 'bla'|foo|bar|bla|
91'foo' bar 'bla'|foo|bar|bla|
92'foo' bar bla|foo|bar|bla|
93blurb foo"bar"bar"fasel" baz|blurb|foobarbarfasel|baz|
94blurb foo'bar'bar'fasel' baz|blurb|foobarbarfasel|baz|
95""||
96''||
97foo "" bar|foo||bar|
98foo '' bar|foo||bar|
99foo "" "" "" bar|foo||||bar|
100foo '' '' '' bar|foo||||bar|
101\"|"|
102"\""|"|
103"foo\ bar"|foo\ bar|
104"foo\\ bar"|foo\ bar|
105"foo\\ bar\""|foo\ bar"|
106"foo\\" bar\"|foo\|bar"|
107"foo\\ bar\" dfadf"|foo\ bar" dfadf|
108"foo\\\ bar\" dfadf"|foo\\ bar" dfadf|
109"foo\\\x bar\" dfadf"|foo\\x bar" dfadf|
110"foo\x bar\" dfadf"|foo\x bar" dfadf|
111\'|'|
112'foo\ bar'|foo\ bar|
113'foo\\ bar'|foo\\ bar|
114"foo\\\x bar\" df'a\ 'df"|foo\\x bar" df'a\ 'df|
115\"foo|"foo|
116\"foo\x|"foox|
117"foo\x"|foo\x|
118"foo\ "|foo\ |
119foo\ xx|foo xx|
120foo\ x\x|foo xx|
121foo\ x\x\"|foo xx"|
122"foo\ x\x"|foo\ x\x|
123"foo\ x\x\\"|foo\ x\x\|
124"foo\ x\x\\""foobar"|foo\ x\x\foobar|
125"foo\ x\x\\"\'"foobar"|foo\ x\x\'foobar|
126"foo\ x\x\\"\'"fo'obar"|foo\ x\x\'fo'obar|
127"foo\ x\x\\"\'"fo'obar" 'don'\''t'|foo\ x\x\'fo'obar|don't|
128"foo\ x\x\\"\'"fo'obar" 'don'\''t' \\|foo\ x\x\'fo'obar|don't|\|
129'foo\ bar'|foo\ bar|
130'foo\\ bar'|foo\\ bar|
131foo\ bar|foo bar|
132foo#bar\nbaz|foo|baz|
133:-) ;-)|:-)|;-)|
Antoine Pitroud72402e2010-10-27 18:52:48 +0000134áéíóú|áéíóú|
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000135"""
136
137class ShlexTest(unittest.TestCase):
138 def setUp(self):
139 self.data = [x.split("|")[:-1]
140 for x in data.splitlines()]
141 self.posix_data = [x.split("|")[:-1]
142 for x in posix_data.splitlines()]
143 for item in self.data:
144 item[0] = item[0].replace(r"\n", "\n")
145 for item in self.posix_data:
146 item[0] = item[0].replace(r"\n", "\n")
147
Gustavo Niemeyer48f3dcc2003-04-20 01:57:03 +0000148 def splitTest(self, data, comments):
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000149 for i in range(len(data)):
Gustavo Niemeyer48f3dcc2003-04-20 01:57:03 +0000150 l = shlex.split(data[i][0], comments=comments)
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000151 self.assertEqual(l, data[i][1:],
152 "%s: %s != %s" %
153 (data[i][0], l, data[i][1:]))
154
155 def oldSplit(self, s):
156 ret = []
Walter Dörwald2c849f22007-06-12 17:43:43 +0000157 lex = shlex.shlex(io.StringIO(s))
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000158 tok = lex.get_token()
159 while tok:
160 ret.append(tok)
161 tok = lex.get_token()
162 return ret
Tim Peters0eadaac2003-04-24 16:02:54 +0000163
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000164 def testSplitPosix(self):
165 """Test data splitting with posix parser"""
Tim Peters0eadaac2003-04-24 16:02:54 +0000166 self.splitTest(self.posix_data, comments=True)
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000167
168 def testCompat(self):
169 """Test compatibility interface"""
170 for i in range(len(self.data)):
171 l = self.oldSplit(self.data[i][0])
172 self.assertEqual(l, self.data[i][1:],
173 "%s: %s != %s" %
174 (self.data[i][0], l, self.data[i][1:]))
175
Éric Araujo9bce3112011-07-27 18:29:31 +0200176 def testQuote(self):
177 safeunquoted = string.ascii_letters + string.digits + '@%_-+=:,./'
Éric Araujo7fc03942011-08-09 23:18:06 +0200178 unicode_sample = '\xe9\xe0\xdf' # e + acute accent, a + grave, sharp s
179 unsafe = '"`$\\!' + unicode_sample
Éric Araujo9bce3112011-07-27 18:29:31 +0200180
181 self.assertEqual(shlex.quote(''), "''")
182 self.assertEqual(shlex.quote(safeunquoted), safeunquoted)
183 self.assertEqual(shlex.quote('test file name'), "'test file name'")
184 for u in unsafe:
185 self.assertEqual(shlex.quote('test%sname' % u),
186 "'test%sname'" % u)
187 for u in unsafe:
188 self.assertEqual(shlex.quote("test%s'name'" % u),
189 "'test%s'\"'\"'name'\"'\"''" % u)
190
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000191# Allow this test to be used with old shlex.py
192if not getattr(shlex, "split", None):
193 for methname in dir(ShlexTest):
194 if methname.startswith("test") and methname != "testCompat":
195 delattr(ShlexTest, methname)
196
197if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500198 unittest.main()