ยปCore Development>Code coverage>Lib/test/test_pipes.py

Python code coverage for Lib/test/test_pipes.py

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