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 | |
| 85 | def testRepr(self): |
| 86 | t = pipes.Template() |
| 87 | self.assertEqual(repr(t), "<Template instance, steps=[]>") |
| 88 | t.append('tr a-z A-Z', pipes.STDIN_STDOUT) |
| 89 | self.assertEqual(repr(t), |
| 90 | "<Template instance, steps=[('tr a-z A-Z', '--')]>") |
| 91 | |
| 92 | def testSetDebug(self): |
| 93 | t = pipes.Template() |
| 94 | t.debug(False) |
| 95 | self.assertEqual(t.debugging, False) |
| 96 | t.debug(True) |
| 97 | self.assertEqual(t.debugging, True) |
| 98 | |
| 99 | def testReadOpenSink(self): |
| 100 | # check calling open('r') on a pipe ending with |
| 101 | # a sink raises ValueError |
| 102 | t = pipes.Template() |
| 103 | t.append('boguscmd', pipes.SINK) |
| 104 | self.assertRaises(ValueError, t.open, 'bogusfile', 'r') |
| 105 | |
| 106 | def testWriteOpenSource(self): |
| 107 | # check calling open('w') on a pipe ending with |
| 108 | # a source raises ValueError |
| 109 | t = pipes.Template() |
| 110 | t.prepend('boguscmd', pipes.SOURCE) |
| 111 | self.assertRaises(ValueError, t.open, 'bogusfile', 'w') |
| 112 | |
| 113 | def testBadAppendOptions(self): |
| 114 | t = pipes.Template() |
| 115 | |
| 116 | # try a non-string command |
| 117 | self.assertRaises(TypeError, t.append, 7, pipes.STDIN_STDOUT) |
| 118 | |
| 119 | # try a type that isn't recognized |
| 120 | self.assertRaises(ValueError, t.append, 'boguscmd', 'xx') |
| 121 | |
| 122 | # shouldn't be able to append a source |
| 123 | self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SOURCE) |
| 124 | |
| 125 | # check appending two sinks |
| 126 | t = pipes.Template() |
| 127 | t.append('boguscmd', pipes.SINK) |
| 128 | self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SINK) |
| 129 | |
| 130 | # command needing file input but with no $IN |
| 131 | t = pipes.Template() |
| 132 | self.assertRaises(ValueError, t.append, 'boguscmd $OUT', |
| 133 | pipes.FILEIN_FILEOUT) |
| 134 | t = pipes.Template() |
| 135 | self.assertRaises(ValueError, t.append, 'boguscmd', |
| 136 | pipes.FILEIN_STDOUT) |
| 137 | |
| 138 | # command needing file output but with no $OUT |
| 139 | t = pipes.Template() |
| 140 | self.assertRaises(ValueError, t.append, 'boguscmd $IN', |
| 141 | pipes.FILEIN_FILEOUT) |
| 142 | t = pipes.Template() |
| 143 | self.assertRaises(ValueError, t.append, 'boguscmd', |
| 144 | pipes.STDIN_FILEOUT) |
| 145 | |
| 146 | |
| 147 | def testBadPrependOptions(self): |
| 148 | t = pipes.Template() |
| 149 | |
| 150 | # try a non-string command |
| 151 | self.assertRaises(TypeError, t.prepend, 7, pipes.STDIN_STDOUT) |
| 152 | |
| 153 | # try a type that isn't recognized |
| 154 | self.assertRaises(ValueError, t.prepend, 'tr a-z A-Z', 'xx') |
| 155 | |
| 156 | # shouldn't be able to prepend a sink |
| 157 | self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SINK) |
| 158 | |
| 159 | # check prepending two sources |
| 160 | t = pipes.Template() |
| 161 | t.prepend('boguscmd', pipes.SOURCE) |
| 162 | self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SOURCE) |
| 163 | |
| 164 | # command needing file input but with no $IN |
| 165 | t = pipes.Template() |
| 166 | self.assertRaises(ValueError, t.prepend, 'boguscmd $OUT', |
| 167 | pipes.FILEIN_FILEOUT) |
| 168 | t = pipes.Template() |
| 169 | self.assertRaises(ValueError, t.prepend, 'boguscmd', |
| 170 | pipes.FILEIN_STDOUT) |
| 171 | |
| 172 | # command needing file output but with no $OUT |
| 173 | t = pipes.Template() |
| 174 | self.assertRaises(ValueError, t.prepend, 'boguscmd $IN', |
| 175 | pipes.FILEIN_FILEOUT) |
| 176 | t = pipes.Template() |
| 177 | self.assertRaises(ValueError, t.prepend, 'boguscmd', |
| 178 | pipes.STDIN_FILEOUT) |
| 179 | |
| 180 | def testBadOpenMode(self): |
| 181 | t = pipes.Template() |
| 182 | self.assertRaises(ValueError, t.open, 'bogusfile', 'x') |
| 183 | |
| 184 | def testClone(self): |
| 185 | t = pipes.Template() |
| 186 | t.append('tr a-z A-Z', pipes.STDIN_STDOUT) |
| 187 | |
| 188 | u = t.clone() |
| 189 | self.assertNotEqual(id(t), id(u)) |
| 190 | self.assertEqual(t.steps, u.steps) |
| 191 | self.assertNotEqual(id(t.steps), id(u.steps)) |
| 192 | self.assertEqual(t.debugging, u.debugging) |
| 193 | |
| 194 | def test_main(): |
| 195 | run_unittest(SimplePipeTests) |
Antoine Pitrou | 9b14f60 | 2009-12-08 19:53:23 +0000 | [diff] [blame] | 196 | reap_children() |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 197 | |
| 198 | if __name__ == "__main__": |
| 199 | test_main() |