test_text_file.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. """Tests for distutils.text_file."""
  2. import os
  3. import unittest
  4. from distutils.text_file import TextFile
  5. from distutils.tests import support
  6. from test.support import run_unittest
  7. TEST_DATA = """# test file
  8. line 3 \\
  9. # intervening comment
  10. continues on next line
  11. """
  12. class TextFileTestCase(support.TempdirManager, unittest.TestCase):
  13. def test_class(self):
  14. # old tests moved from text_file.__main__
  15. # so they are really called by the buildbots
  16. # result 1: no fancy options
  17. result1 = ['# test file\n', '\n', 'line 3 \\\n',
  18. '# intervening comment\n',
  19. ' continues on next line\n']
  20. # result 2: just strip comments
  21. result2 = ["\n",
  22. "line 3 \\\n",
  23. " continues on next line\n"]
  24. # result 3: just strip blank lines
  25. result3 = ["# test file\n",
  26. "line 3 \\\n",
  27. "# intervening comment\n",
  28. " continues on next line\n"]
  29. # result 4: default, strip comments, blank lines,
  30. # and trailing whitespace
  31. result4 = ["line 3 \\",
  32. " continues on next line"]
  33. # result 5: strip comments and blanks, plus join lines (but don't
  34. # "collapse" joined lines
  35. result5 = ["line 3 continues on next line"]
  36. # result 6: strip comments and blanks, plus join lines (and
  37. # "collapse" joined lines
  38. result6 = ["line 3 continues on next line"]
  39. def test_input(count, description, file, expected_result):
  40. result = file.readlines()
  41. self.assertEqual(result, expected_result)
  42. tmpdir = self.mkdtemp()
  43. filename = os.path.join(tmpdir, "test.txt")
  44. out_file = open(filename, "w")
  45. try:
  46. out_file.write(TEST_DATA)
  47. finally:
  48. out_file.close()
  49. in_file = TextFile(filename, strip_comments=0, skip_blanks=0,
  50. lstrip_ws=0, rstrip_ws=0)
  51. try:
  52. test_input(1, "no processing", in_file, result1)
  53. finally:
  54. in_file.close()
  55. in_file = TextFile(filename, strip_comments=1, skip_blanks=0,
  56. lstrip_ws=0, rstrip_ws=0)
  57. try:
  58. test_input(2, "strip comments", in_file, result2)
  59. finally:
  60. in_file.close()
  61. in_file = TextFile(filename, strip_comments=0, skip_blanks=1,
  62. lstrip_ws=0, rstrip_ws=0)
  63. try:
  64. test_input(3, "strip blanks", in_file, result3)
  65. finally:
  66. in_file.close()
  67. in_file = TextFile(filename)
  68. try:
  69. test_input(4, "default processing", in_file, result4)
  70. finally:
  71. in_file.close()
  72. in_file = TextFile(filename, strip_comments=1, skip_blanks=1,
  73. join_lines=1, rstrip_ws=1)
  74. try:
  75. test_input(5, "join lines without collapsing", in_file, result5)
  76. finally:
  77. in_file.close()
  78. in_file = TextFile(filename, strip_comments=1, skip_blanks=1,
  79. join_lines=1, rstrip_ws=1, collapse_join=1)
  80. try:
  81. test_input(6, "join lines with collapsing", in_file, result6)
  82. finally:
  83. in_file.close()
  84. def test_suite():
  85. return unittest.makeSuite(TextFileTestCase)
  86. if __name__ == "__main__":
  87. run_unittest(test_suite())