Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 1 | import pipes |
| 2 | import os |
| 3 | import string |
| 4 | import unittest |
Antoine Pitrou | 9b14f60 | 2009-12-08 19:53:23 +0000 | [diff] [blame] | 5 | from test.support import TESTFN, run_unittest, unlink, reap_children |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 6 | |
| 7 | if os.name != 'posix': |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 8 | raise unittest.SkipTest('pipes module only works on posix') |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 9 | |
| 10 | TESTFN2 = TESTFN + "2" |
| 11 | |
Thomas Wouters | bca5480 | 2007-09-10 19:32:14 +0000 | [diff] [blame] | 12 | # tr a-z A-Z is not portable, so make the ranges explicit |
| 13 | s_command = 'tr %s %s' % (string.ascii_lowercase, string.ascii_uppercase) |
| 14 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 15 | class SimplePipeTests(unittest.TestCase): |
| 16 | def tearDown(self): |
| 17 | for f in (TESTFN, TESTFN2): |
| 18 | unlink(f) |
| 19 | |
| 20 | def testSimplePipe1(self): |
| 21 | t = pipes.Template() |
Thomas Wouters | bca5480 | 2007-09-10 19:32:14 +0000 | [diff] [blame] | 22 | t.append(s_command, pipes.STDIN_STDOUT) |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 23 | f = t.open(TESTFN, 'w') |
| 24 | f.write('hello world #1') |
| 25 | f.close() |
Antoine Pitrou | 92f60ed | 2010-10-14 22:11:44 +0000 | [diff] [blame] | 26 | with open(TESTFN) as f: |
| 27 | self.assertEqual(f.read(), 'HELLO WORLD #1') |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 28 | |
| 29 | def testSimplePipe2(self): |
Antoine Pitrou | 92f60ed | 2010-10-14 22:11:44 +0000 | [diff] [blame] | 30 | with open(TESTFN, 'w') as f: |
| 31 | f.write('hello world #2') |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 32 | t = pipes.Template() |
Thomas Wouters | bca5480 | 2007-09-10 19:32:14 +0000 | [diff] [blame] | 33 | t.append(s_command + ' < $IN > $OUT', pipes.FILEIN_FILEOUT) |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 34 | t.copy(TESTFN, TESTFN2) |
Antoine Pitrou | 92f60ed | 2010-10-14 22:11:44 +0000 | [diff] [blame] | 35 | with open(TESTFN2) as f: |
| 36 | self.assertEqual(f.read(), 'HELLO WORLD #2') |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 37 | |
| 38 | def testSimplePipe3(self): |
Antoine Pitrou | 92f60ed | 2010-10-14 22:11:44 +0000 | [diff] [blame] | 39 | with open(TESTFN, 'w') as f: |
| 40 | f.write('hello world #2') |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 41 | t = pipes.Template() |
Thomas Wouters | bca5480 | 2007-09-10 19:32:14 +0000 | [diff] [blame] | 42 | t.append(s_command + ' < $IN', pipes.FILEIN_STDOUT) |
Antoine Pitrou | 9b14f60 | 2009-12-08 19:53:23 +0000 | [diff] [blame] | 43 | f = t.open(TESTFN, 'r') |
| 44 | try: |
| 45 | self.assertEqual(f.read(), 'HELLO WORLD #2') |
| 46 | finally: |
| 47 | f.close() |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 48 | |
| 49 | def testEmptyPipeline1(self): |
| 50 | # copy through empty pipe |
| 51 | d = 'empty pipeline test COPY' |
Antoine Pitrou | 92f60ed | 2010-10-14 22:11:44 +0000 | [diff] [blame] | 52 | with open(TESTFN, 'w') as f: |
| 53 | f.write(d) |
| 54 | with open(TESTFN2, 'w') as f: |
| 55 | f.write('') |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 56 | t=pipes.Template() |
| 57 | t.copy(TESTFN, TESTFN2) |
Antoine Pitrou | 92f60ed | 2010-10-14 22:11:44 +0000 | [diff] [blame] | 58 | with open(TESTFN2) as f: |
| 59 | self.assertEqual(f.read(), d) |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 60 | |
| 61 | def testEmptyPipeline2(self): |
| 62 | # read through empty pipe |
| 63 | d = 'empty pipeline test READ' |
Antoine Pitrou | 92f60ed | 2010-10-14 22:11:44 +0000 | [diff] [blame] | 64 | with open(TESTFN, 'w') as f: |
| 65 | f.write(d) |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 66 | t=pipes.Template() |
Antoine Pitrou | 9b14f60 | 2009-12-08 19:53:23 +0000 | [diff] [blame] | 67 | f = t.open(TESTFN, 'r') |
| 68 | try: |
| 69 | self.assertEqual(f.read(), d) |
| 70 | finally: |
| 71 | f.close() |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 72 | |
| 73 | def testEmptyPipeline3(self): |
| 74 | # write through empty pipe |
| 75 | d = 'empty pipeline test WRITE' |
| 76 | t = pipes.Template() |
Antoine Pitrou | 92f60ed | 2010-10-14 22:11:44 +0000 | [diff] [blame] | 77 | with t.open(TESTFN, 'w') as f: |
| 78 | f.write(d) |
| 79 | with open(TESTFN) as f: |
| 80 | self.assertEqual(f.read(), d) |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 81 | |
| 82 | def testQuoting(self): |
Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 83 | safeunquoted = string.ascii_letters + string.digits + '@%_-+=:,./' |
Éric Araujo | ef1e94a8 | 2011-08-09 23:03:43 +0200 | [diff] [blame^] | 84 | unicode_sample = '\xe9\xe0\xdf' # e + acute accent, a + grave, sharp s |
| 85 | unsafe = '"`$\\!' + unicode_sample |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 86 | |
Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 87 | self.assertEqual(pipes.quote(''), "''") |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 88 | self.assertEqual(pipes.quote(safeunquoted), safeunquoted) |
| 89 | self.assertEqual(pipes.quote('test file name'), "'test file name'") |
| 90 | for u in unsafe: |
| 91 | self.assertEqual(pipes.quote('test%sname' % u), |
| 92 | "'test%sname'" % u) |
| 93 | for u in unsafe: |
| 94 | self.assertEqual(pipes.quote("test%s'name'" % u), |
Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 95 | "'test%s'\"'\"'name'\"'\"''" % u) |
Benjamin Peterson | 21896a3 | 2010-03-21 22:03:03 +0000 | [diff] [blame] | 96 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 97 | def testRepr(self): |
| 98 | t = pipes.Template() |
| 99 | self.assertEqual(repr(t), "<Template instance, steps=[]>") |
| 100 | t.append('tr a-z A-Z', pipes.STDIN_STDOUT) |
| 101 | self.assertEqual(repr(t), |
| 102 | "<Template instance, steps=[('tr a-z A-Z', '--')]>") |
| 103 | |
| 104 | def testSetDebug(self): |
| 105 | t = pipes.Template() |
| 106 | t.debug(False) |
| 107 | self.assertEqual(t.debugging, False) |
| 108 | t.debug(True) |
| 109 | self.assertEqual(t.debugging, True) |
| 110 | |
| 111 | def testReadOpenSink(self): |
| 112 | # check calling open('r') on a pipe ending with |
| 113 | # a sink raises ValueError |
| 114 | t = pipes.Template() |
| 115 | t.append('boguscmd', pipes.SINK) |
| 116 | self.assertRaises(ValueError, t.open, 'bogusfile', 'r') |
| 117 | |
| 118 | def testWriteOpenSource(self): |
| 119 | # check calling open('w') on a pipe ending with |
| 120 | # a source raises ValueError |
| 121 | t = pipes.Template() |
| 122 | t.prepend('boguscmd', pipes.SOURCE) |
| 123 | self.assertRaises(ValueError, t.open, 'bogusfile', 'w') |
| 124 | |
| 125 | def testBadAppendOptions(self): |
| 126 | t = pipes.Template() |
| 127 | |
| 128 | # try a non-string command |
| 129 | self.assertRaises(TypeError, t.append, 7, pipes.STDIN_STDOUT) |
| 130 | |
| 131 | # try a type that isn't recognized |
| 132 | self.assertRaises(ValueError, t.append, 'boguscmd', 'xx') |
| 133 | |
| 134 | # shouldn't be able to append a source |
| 135 | self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SOURCE) |
| 136 | |
| 137 | # check appending two sinks |
| 138 | t = pipes.Template() |
| 139 | t.append('boguscmd', pipes.SINK) |
| 140 | self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SINK) |
| 141 | |
| 142 | # command needing file input but with no $IN |
| 143 | t = pipes.Template() |
| 144 | self.assertRaises(ValueError, t.append, 'boguscmd $OUT', |
| 145 | pipes.FILEIN_FILEOUT) |
| 146 | t = pipes.Template() |
| 147 | self.assertRaises(ValueError, t.append, 'boguscmd', |
| 148 | pipes.FILEIN_STDOUT) |
| 149 | |
| 150 | # command needing file output but with no $OUT |
| 151 | t = pipes.Template() |
| 152 | self.assertRaises(ValueError, t.append, 'boguscmd $IN', |
| 153 | pipes.FILEIN_FILEOUT) |
| 154 | t = pipes.Template() |
| 155 | self.assertRaises(ValueError, t.append, 'boguscmd', |
| 156 | pipes.STDIN_FILEOUT) |
| 157 | |
| 158 | |
| 159 | def testBadPrependOptions(self): |
| 160 | t = pipes.Template() |
| 161 | |
| 162 | # try a non-string command |
| 163 | self.assertRaises(TypeError, t.prepend, 7, pipes.STDIN_STDOUT) |
| 164 | |
| 165 | # try a type that isn't recognized |
| 166 | self.assertRaises(ValueError, t.prepend, 'tr a-z A-Z', 'xx') |
| 167 | |
| 168 | # shouldn't be able to prepend a sink |
| 169 | self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SINK) |
| 170 | |
| 171 | # check prepending two sources |
| 172 | t = pipes.Template() |
| 173 | t.prepend('boguscmd', pipes.SOURCE) |
| 174 | self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SOURCE) |
| 175 | |
| 176 | # command needing file input but with no $IN |
| 177 | t = pipes.Template() |
| 178 | self.assertRaises(ValueError, t.prepend, 'boguscmd $OUT', |
| 179 | pipes.FILEIN_FILEOUT) |
| 180 | t = pipes.Template() |
| 181 | self.assertRaises(ValueError, t.prepend, 'boguscmd', |
| 182 | pipes.FILEIN_STDOUT) |
| 183 | |
| 184 | # command needing file output but with no $OUT |
| 185 | t = pipes.Template() |
| 186 | self.assertRaises(ValueError, t.prepend, 'boguscmd $IN', |
| 187 | pipes.FILEIN_FILEOUT) |
| 188 | t = pipes.Template() |
| 189 | self.assertRaises(ValueError, t.prepend, 'boguscmd', |
| 190 | pipes.STDIN_FILEOUT) |
| 191 | |
| 192 | def testBadOpenMode(self): |
| 193 | t = pipes.Template() |
| 194 | self.assertRaises(ValueError, t.open, 'bogusfile', 'x') |
| 195 | |
| 196 | def testClone(self): |
| 197 | t = pipes.Template() |
| 198 | t.append('tr a-z A-Z', pipes.STDIN_STDOUT) |
| 199 | |
| 200 | u = t.clone() |
| 201 | self.assertNotEqual(id(t), id(u)) |
| 202 | self.assertEqual(t.steps, u.steps) |
| 203 | self.assertNotEqual(id(t.steps), id(u.steps)) |
| 204 | self.assertEqual(t.debugging, u.debugging) |
| 205 | |
| 206 | def test_main(): |
| 207 | run_unittest(SimplePipeTests) |
Antoine Pitrou | 9b14f60 | 2009-12-08 19:53:23 +0000 | [diff] [blame] | 208 | reap_children() |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 209 | |
| 210 | if __name__ == "__main__": |
| 211 | test_main() |