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