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