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