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