]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_pipes.py
5 from test
.test_support
import TESTFN
, run_unittest
, unlink
, reap_children
8 raise unittest
.SkipTest('pipes module only works on posix')
10 TESTFN2
= TESTFN
+ "2"
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
)
15 class SimplePipeTests(unittest
.TestCase
):
17 for f
in (TESTFN
, TESTFN2
):
20 def testSimplePipe1(self
):
22 t
.append(s_command
, pipes
.STDIN_STDOUT
)
23 f
= t
.open(TESTFN
, 'w')
24 f
.write('hello world #1')
26 with
open(TESTFN
) as f
:
27 self
.assertEqual(f
.read(), 'HELLO WORLD #1')
29 def testSimplePipe2(self
):
30 with
open(TESTFN
, 'w') as f
:
31 f
.write('hello world #2')
33 t
.append(s_command
+ ' < $IN > $OUT', pipes
.FILEIN_FILEOUT
)
34 t
.copy(TESTFN
, TESTFN2
)
35 with
open(TESTFN2
) as f
:
36 self
.assertEqual(f
.read(), 'HELLO WORLD #2')
38 def testSimplePipe3(self
):
39 with
open(TESTFN
, 'w') as f
:
40 f
.write('hello world #2')
42 t
.append(s_command
+ ' < $IN', pipes
.FILEIN_STDOUT
)
43 with t
.open(TESTFN
, 'r') as f
:
44 self
.assertEqual(f
.read(), 'HELLO WORLD #2')
46 def testEmptyPipeline1(self
):
47 # copy through empty pipe
48 d
= 'empty pipeline test COPY'
49 with
open(TESTFN
, 'w') as f
:
51 with
open(TESTFN2
, 'w') as f
:
54 t
.copy(TESTFN
, TESTFN2
)
55 with
open(TESTFN2
) as f
:
56 self
.assertEqual(f
.read(), d
)
58 def testEmptyPipeline2(self
):
59 # read through empty pipe
60 d
= 'empty pipeline test READ'
61 with
open(TESTFN
, 'w') as f
:
64 with t
.open(TESTFN
, 'r') as f
:
65 self
.assertEqual(f
.read(), d
)
67 def testEmptyPipeline3(self
):
68 # write through empty pipe
69 d
= 'empty pipeline test WRITE'
71 with t
.open(TESTFN
, 'w') as f
:
73 with
open(TESTFN
) as f
:
74 self
.assertEqual(f
.read(), d
)
76 def testQuoting(self
):
77 safeunquoted
= string
.ascii_letters
+ string
.digits
+ '@%_-+=:,./'
80 self
.assertEqual(pipes
.quote(''), "''")
81 self
.assertEqual(pipes
.quote(safeunquoted
), safeunquoted
)
82 self
.assertEqual(pipes
.quote('test file name'), "'test file name'")
84 self
.assertEqual(pipes
.quote('test%sname' % u
),
87 self
.assertEqual(pipes
.quote("test%s'name'" % u
),
88 "'test%s'\"'\"'name'\"'\"''" % u
)
92 self
.assertEqual(repr(t
), "<Template instance, steps=[]>")
93 t
.append('tr a-z A-Z', pipes
.STDIN_STDOUT
)
94 self
.assertEqual(repr(t
),
95 "<Template instance, steps=[('tr a-z A-Z', '--')]>")
97 def testSetDebug(self
):
100 self
.assertEqual(t
.debugging
, False)
102 self
.assertEqual(t
.debugging
, True)
104 def testReadOpenSink(self
):
105 # check calling open('r') on a pipe ending with
106 # a sink raises ValueError
108 t
.append('boguscmd', pipes
.SINK
)
109 self
.assertRaises(ValueError, t
.open, 'bogusfile', 'r')
111 def testWriteOpenSource(self
):
112 # check calling open('w') on a pipe ending with
113 # a source raises ValueError
115 t
.prepend('boguscmd', pipes
.SOURCE
)
116 self
.assertRaises(ValueError, t
.open, 'bogusfile', 'w')
118 def testBadAppendOptions(self
):
121 # try a non-string command
122 self
.assertRaises(TypeError, t
.append
, 7, pipes
.STDIN_STDOUT
)
124 # try a type that isn't recognized
125 self
.assertRaises(ValueError, t
.append
, 'boguscmd', 'xx')
127 # shouldn't be able to append a source
128 self
.assertRaises(ValueError, t
.append
, 'boguscmd', pipes
.SOURCE
)
130 # check appending two sinks
132 t
.append('boguscmd', pipes
.SINK
)
133 self
.assertRaises(ValueError, t
.append
, 'boguscmd', pipes
.SINK
)
135 # command needing file input but with no $IN
137 self
.assertRaises(ValueError, t
.append
, 'boguscmd $OUT',
138 pipes
.FILEIN_FILEOUT
)
140 self
.assertRaises(ValueError, t
.append
, 'boguscmd',
143 # command needing file output but with no $OUT
145 self
.assertRaises(ValueError, t
.append
, 'boguscmd $IN',
146 pipes
.FILEIN_FILEOUT
)
148 self
.assertRaises(ValueError, t
.append
, 'boguscmd',
152 def testBadPrependOptions(self
):
155 # try a non-string command
156 self
.assertRaises(TypeError, t
.prepend
, 7, pipes
.STDIN_STDOUT
)
158 # try a type that isn't recognized
159 self
.assertRaises(ValueError, t
.prepend
, 'tr a-z A-Z', 'xx')
161 # shouldn't be able to prepend a sink
162 self
.assertRaises(ValueError, t
.prepend
, 'boguscmd', pipes
.SINK
)
164 # check prepending two sources
166 t
.prepend('boguscmd', pipes
.SOURCE
)
167 self
.assertRaises(ValueError, t
.prepend
, 'boguscmd', pipes
.SOURCE
)
169 # command needing file input but with no $IN
171 self
.assertRaises(ValueError, t
.prepend
, 'boguscmd $OUT',
172 pipes
.FILEIN_FILEOUT
)
174 self
.assertRaises(ValueError, t
.prepend
, 'boguscmd',
177 # command needing file output but with no $OUT
179 self
.assertRaises(ValueError, t
.prepend
, 'boguscmd $IN',
180 pipes
.FILEIN_FILEOUT
)
182 self
.assertRaises(ValueError, t
.prepend
, 'boguscmd',
185 def testBadOpenMode(self
):
187 self
.assertRaises(ValueError, t
.open, 'bogusfile', 'x')
191 t
.append('tr a-z A-Z', pipes
.STDIN_STDOUT
)
194 self
.assertNotEqual(id(t
), id(u
))
195 self
.assertEqual(t
.steps
, u
.steps
)
196 self
.assertNotEqual(id(t
.steps
), id(u
.steps
))
197 self
.assertEqual(t
.debugging
, u
.debugging
)
200 run_unittest(SimplePipeTests
)
203 if __name__
== "__main__":