/Lib/test/test_pipes.py

http://unladen-swallow.googlecode.com/ · Python · 190 lines · 134 code · 36 blank · 20 comment · 5 complexity · 28fe2c332b6aad5fb02d082feb326c86 MD5 · raw file

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