test_pipes.py 6.8 KB

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