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