2007-08-30 07:38:56 -03:00
|
|
|
import pipes
|
|
|
|
import os
|
|
|
|
import string
|
|
|
|
import unittest
|
2009-12-08 15:46:38 -04:00
|
|
|
from test.test_support import TESTFN, run_unittest, unlink, reap_children
|
2007-08-30 09:32:23 -03:00
|
|
|
|
|
|
|
if os.name != 'posix':
|
2009-03-26 18:10:30 -03:00
|
|
|
raise unittest.SkipTest('pipes module only works on posix')
|
2007-08-30 07:38:56 -03:00
|
|
|
|
|
|
|
TESTFN2 = TESTFN + "2"
|
|
|
|
|
2007-09-10 03:18:32 -03:00
|
|
|
# tr a-z A-Z is not portable, so make the ranges explicit
|
|
|
|
s_command = 'tr %s %s' % (string.ascii_lowercase, string.ascii_uppercase)
|
|
|
|
|
2007-08-30 07:38:56 -03:00
|
|
|
class SimplePipeTests(unittest.TestCase):
|
|
|
|
def tearDown(self):
|
|
|
|
for f in (TESTFN, TESTFN2):
|
|
|
|
unlink(f)
|
|
|
|
|
|
|
|
def testSimplePipe1(self):
|
|
|
|
t = pipes.Template()
|
2007-09-10 03:18:32 -03:00
|
|
|
t.append(s_command, pipes.STDIN_STDOUT)
|
2007-08-30 07:38:56 -03:00
|
|
|
f = t.open(TESTFN, 'w')
|
|
|
|
f.write('hello world #1')
|
|
|
|
f.close()
|
2010-10-14 19:22:30 -03:00
|
|
|
with open(TESTFN) as f:
|
|
|
|
self.assertEqual(f.read(), 'HELLO WORLD #1')
|
2007-08-30 07:38:56 -03:00
|
|
|
|
|
|
|
def testSimplePipe2(self):
|
2010-10-14 19:22:30 -03:00
|
|
|
with open(TESTFN, 'w') as f:
|
|
|
|
f.write('hello world #2')
|
2007-08-30 07:38:56 -03:00
|
|
|
t = pipes.Template()
|
2007-09-10 03:18:32 -03:00
|
|
|
t.append(s_command + ' < $IN > $OUT', pipes.FILEIN_FILEOUT)
|
2007-08-30 07:38:56 -03:00
|
|
|
t.copy(TESTFN, TESTFN2)
|
2010-10-14 19:22:30 -03:00
|
|
|
with open(TESTFN2) as f:
|
|
|
|
self.assertEqual(f.read(), 'HELLO WORLD #2')
|
2007-08-30 07:38:56 -03:00
|
|
|
|
|
|
|
def testSimplePipe3(self):
|
2010-10-14 19:22:30 -03:00
|
|
|
with open(TESTFN, 'w') as f:
|
|
|
|
f.write('hello world #2')
|
2007-08-30 07:38:56 -03:00
|
|
|
t = pipes.Template()
|
2007-09-10 03:18:32 -03:00
|
|
|
t.append(s_command + ' < $IN', pipes.FILEIN_STDOUT)
|
2009-12-08 15:46:38 -04:00
|
|
|
with t.open(TESTFN, 'r') as f:
|
|
|
|
self.assertEqual(f.read(), 'HELLO WORLD #2')
|
2007-08-30 07:38:56 -03:00
|
|
|
|
|
|
|
def testEmptyPipeline1(self):
|
|
|
|
# copy through empty pipe
|
|
|
|
d = 'empty pipeline test COPY'
|
2010-10-14 19:22:30 -03:00
|
|
|
with open(TESTFN, 'w') as f:
|
|
|
|
f.write(d)
|
|
|
|
with open(TESTFN2, 'w') as f:
|
|
|
|
f.write('')
|
2007-08-30 07:38:56 -03:00
|
|
|
t=pipes.Template()
|
|
|
|
t.copy(TESTFN, TESTFN2)
|
2010-10-14 19:22:30 -03:00
|
|
|
with open(TESTFN2) as f:
|
|
|
|
self.assertEqual(f.read(), d)
|
2007-08-30 07:38:56 -03:00
|
|
|
|
|
|
|
def testEmptyPipeline2(self):
|
|
|
|
# read through empty pipe
|
|
|
|
d = 'empty pipeline test READ'
|
2010-10-14 19:22:30 -03:00
|
|
|
with open(TESTFN, 'w') as f:
|
|
|
|
f.write(d)
|
2007-08-30 07:38:56 -03:00
|
|
|
t=pipes.Template()
|
2009-12-08 15:46:38 -04:00
|
|
|
with t.open(TESTFN, 'r') as f:
|
|
|
|
self.assertEqual(f.read(), d)
|
2007-08-30 07:38:56 -03:00
|
|
|
|
|
|
|
def testEmptyPipeline3(self):
|
|
|
|
# write through empty pipe
|
|
|
|
d = 'empty pipeline test WRITE'
|
|
|
|
t = pipes.Template()
|
2010-10-14 19:22:30 -03:00
|
|
|
with t.open(TESTFN, 'w') as f:
|
|
|
|
f.write(d)
|
|
|
|
with open(TESTFN) as f:
|
|
|
|
self.assertEqual(f.read(), d)
|
2007-08-30 07:38:56 -03:00
|
|
|
|
|
|
|
def testQuoting(self):
|
2010-04-24 06:08:10 -03:00
|
|
|
safeunquoted = string.ascii_letters + string.digits + '@%_-+=:,./'
|
|
|
|
unsafe = '"`$\\!'
|
2007-08-30 07:38:56 -03:00
|
|
|
|
2010-04-24 06:08:10 -03:00
|
|
|
self.assertEqual(pipes.quote(''), "''")
|
2007-08-30 07:38:56 -03:00
|
|
|
self.assertEqual(pipes.quote(safeunquoted), safeunquoted)
|
|
|
|
self.assertEqual(pipes.quote('test file name'), "'test file name'")
|
|
|
|
for u in unsafe:
|
|
|
|
self.assertEqual(pipes.quote('test%sname' % u),
|
|
|
|
"'test%sname'" % u)
|
|
|
|
for u in unsafe:
|
|
|
|
self.assertEqual(pipes.quote("test%s'name'" % u),
|
2010-04-24 06:08:10 -03:00
|
|
|
"'test%s'\"'\"'name'\"'\"''" % u)
|
2010-02-22 17:27:38 -04:00
|
|
|
|
2007-08-30 07:38:56 -03:00
|
|
|
def testRepr(self):
|
|
|
|
t = pipes.Template()
|
|
|
|
self.assertEqual(repr(t), "<Template instance, steps=[]>")
|
|
|
|
t.append('tr a-z A-Z', pipes.STDIN_STDOUT)
|
|
|
|
self.assertEqual(repr(t),
|
|
|
|
"<Template instance, steps=[('tr a-z A-Z', '--')]>")
|
|
|
|
|
|
|
|
def testSetDebug(self):
|
|
|
|
t = pipes.Template()
|
|
|
|
t.debug(False)
|
|
|
|
self.assertEqual(t.debugging, False)
|
|
|
|
t.debug(True)
|
|
|
|
self.assertEqual(t.debugging, True)
|
|
|
|
|
|
|
|
def testReadOpenSink(self):
|
|
|
|
# check calling open('r') on a pipe ending with
|
|
|
|
# a sink raises ValueError
|
|
|
|
t = pipes.Template()
|
|
|
|
t.append('boguscmd', pipes.SINK)
|
|
|
|
self.assertRaises(ValueError, t.open, 'bogusfile', 'r')
|
|
|
|
|
|
|
|
def testWriteOpenSource(self):
|
|
|
|
# check calling open('w') on a pipe ending with
|
|
|
|
# a source raises ValueError
|
|
|
|
t = pipes.Template()
|
|
|
|
t.prepend('boguscmd', pipes.SOURCE)
|
|
|
|
self.assertRaises(ValueError, t.open, 'bogusfile', 'w')
|
|
|
|
|
|
|
|
def testBadAppendOptions(self):
|
|
|
|
t = pipes.Template()
|
|
|
|
|
|
|
|
# try a non-string command
|
|
|
|
self.assertRaises(TypeError, t.append, 7, pipes.STDIN_STDOUT)
|
|
|
|
|
|
|
|
# try a type that isn't recognized
|
|
|
|
self.assertRaises(ValueError, t.append, 'boguscmd', 'xx')
|
|
|
|
|
|
|
|
# shouldn't be able to append a source
|
|
|
|
self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SOURCE)
|
|
|
|
|
|
|
|
# check appending two sinks
|
|
|
|
t = pipes.Template()
|
|
|
|
t.append('boguscmd', pipes.SINK)
|
|
|
|
self.assertRaises(ValueError, t.append, 'boguscmd', pipes.SINK)
|
|
|
|
|
|
|
|
# command needing file input but with no $IN
|
|
|
|
t = pipes.Template()
|
|
|
|
self.assertRaises(ValueError, t.append, 'boguscmd $OUT',
|
|
|
|
pipes.FILEIN_FILEOUT)
|
|
|
|
t = pipes.Template()
|
|
|
|
self.assertRaises(ValueError, t.append, 'boguscmd',
|
|
|
|
pipes.FILEIN_STDOUT)
|
|
|
|
|
|
|
|
# command needing file output but with no $OUT
|
|
|
|
t = pipes.Template()
|
|
|
|
self.assertRaises(ValueError, t.append, 'boguscmd $IN',
|
|
|
|
pipes.FILEIN_FILEOUT)
|
|
|
|
t = pipes.Template()
|
|
|
|
self.assertRaises(ValueError, t.append, 'boguscmd',
|
|
|
|
pipes.STDIN_FILEOUT)
|
|
|
|
|
|
|
|
|
|
|
|
def testBadPrependOptions(self):
|
|
|
|
t = pipes.Template()
|
|
|
|
|
|
|
|
# try a non-string command
|
|
|
|
self.assertRaises(TypeError, t.prepend, 7, pipes.STDIN_STDOUT)
|
|
|
|
|
|
|
|
# try a type that isn't recognized
|
|
|
|
self.assertRaises(ValueError, t.prepend, 'tr a-z A-Z', 'xx')
|
|
|
|
|
|
|
|
# shouldn't be able to prepend a sink
|
|
|
|
self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SINK)
|
|
|
|
|
|
|
|
# check prepending two sources
|
|
|
|
t = pipes.Template()
|
|
|
|
t.prepend('boguscmd', pipes.SOURCE)
|
|
|
|
self.assertRaises(ValueError, t.prepend, 'boguscmd', pipes.SOURCE)
|
|
|
|
|
|
|
|
# command needing file input but with no $IN
|
|
|
|
t = pipes.Template()
|
|
|
|
self.assertRaises(ValueError, t.prepend, 'boguscmd $OUT',
|
|
|
|
pipes.FILEIN_FILEOUT)
|
|
|
|
t = pipes.Template()
|
|
|
|
self.assertRaises(ValueError, t.prepend, 'boguscmd',
|
|
|
|
pipes.FILEIN_STDOUT)
|
|
|
|
|
|
|
|
# command needing file output but with no $OUT
|
|
|
|
t = pipes.Template()
|
|
|
|
self.assertRaises(ValueError, t.prepend, 'boguscmd $IN',
|
|
|
|
pipes.FILEIN_FILEOUT)
|
|
|
|
t = pipes.Template()
|
|
|
|
self.assertRaises(ValueError, t.prepend, 'boguscmd',
|
|
|
|
pipes.STDIN_FILEOUT)
|
|
|
|
|
|
|
|
def testBadOpenMode(self):
|
|
|
|
t = pipes.Template()
|
|
|
|
self.assertRaises(ValueError, t.open, 'bogusfile', 'x')
|
|
|
|
|
|
|
|
def testClone(self):
|
|
|
|
t = pipes.Template()
|
|
|
|
t.append('tr a-z A-Z', pipes.STDIN_STDOUT)
|
|
|
|
|
|
|
|
u = t.clone()
|
|
|
|
self.assertNotEqual(id(t), id(u))
|
|
|
|
self.assertEqual(t.steps, u.steps)
|
|
|
|
self.assertNotEqual(id(t.steps), id(u.steps))
|
|
|
|
self.assertEqual(t.debugging, u.debugging)
|
|
|
|
|
|
|
|
def test_main():
|
|
|
|
run_unittest(SimplePipeTests)
|
2009-12-08 15:46:38 -04:00
|
|
|
reap_children()
|
2007-08-30 07:38:56 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|