Tarek Ziadé | 5e25f9f | 2009-02-22 20:05:16 +0000 | [diff] [blame] | 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 |
Éric Araujo | 70ec44a | 2010-11-06 02:44:43 +0000 | [diff] [blame] | 6 | from test.support import run_unittest |
Tarek Ziadé | 5e25f9f | 2009-02-22 20:05:16 +0000 | [diff] [blame] | 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.assertEquals(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 | |
Éric Araujo | bee5cef | 2010-11-05 23:51:56 +0000 | [diff] [blame] | 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() |
Tarek Ziadé | 5e25f9f | 2009-02-22 20:05:16 +0000 | [diff] [blame] | 68 | |
Éric Araujo | bee5cef | 2010-11-05 23:51:56 +0000 | [diff] [blame] | 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() |
Tarek Ziadé | 5e25f9f | 2009-02-22 20:05:16 +0000 | [diff] [blame] | 75 | |
Éric Araujo | bee5cef | 2010-11-05 23:51:56 +0000 | [diff] [blame] | 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() |
Tarek Ziadé | 5e25f9f | 2009-02-22 20:05:16 +0000 | [diff] [blame] | 82 | |
Éric Araujo | bee5cef | 2010-11-05 23:51:56 +0000 | [diff] [blame] | 83 | in_file = TextFile(filename) |
| 84 | try: |
| 85 | test_input(4, "default processing", in_file, result4) |
| 86 | finally: |
| 87 | in_file.close() |
Tarek Ziadé | 5e25f9f | 2009-02-22 20:05:16 +0000 | [diff] [blame] | 88 | |
Éric Araujo | bee5cef | 2010-11-05 23:51:56 +0000 | [diff] [blame] | 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() |
Tarek Ziadé | 5e25f9f | 2009-02-22 20:05:16 +0000 | [diff] [blame] | 95 | |
Éric Araujo | bee5cef | 2010-11-05 23:51:56 +0000 | [diff] [blame] | 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() |
Tarek Ziadé | 5e25f9f | 2009-02-22 20:05:16 +0000 | [diff] [blame] | 102 | |
| 103 | def test_suite(): |
| 104 | return unittest.makeSuite(TextFileTestCase) |
| 105 | |
| 106 | if __name__ == "__main__": |
Éric Araujo | 70ec44a | 2010-11-06 02:44:43 +0000 | [diff] [blame] | 107 | run_unittest(test_suite()) |