1 | n/a | # -*- coding: utf-8 -*- |
---|
2 | n/a | import codecs |
---|
3 | n/a | import io |
---|
4 | n/a | import logging |
---|
5 | n/a | import os |
---|
6 | n/a | import re |
---|
7 | n/a | import shutil |
---|
8 | n/a | import sys |
---|
9 | n/a | import tempfile |
---|
10 | n/a | import unittest |
---|
11 | n/a | |
---|
12 | n/a | from lib2to3 import main |
---|
13 | n/a | |
---|
14 | n/a | |
---|
15 | n/a | TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data") |
---|
16 | n/a | PY2_TEST_MODULE = os.path.join(TEST_DATA_DIR, "py2_test_grammar.py") |
---|
17 | n/a | |
---|
18 | n/a | |
---|
19 | n/a | class TestMain(unittest.TestCase): |
---|
20 | n/a | |
---|
21 | n/a | def setUp(self): |
---|
22 | n/a | self.temp_dir = None # tearDown() will rmtree this directory if set. |
---|
23 | n/a | |
---|
24 | n/a | def tearDown(self): |
---|
25 | n/a | # Clean up logging configuration down by main. |
---|
26 | n/a | del logging.root.handlers[:] |
---|
27 | n/a | if self.temp_dir: |
---|
28 | n/a | shutil.rmtree(self.temp_dir) |
---|
29 | n/a | |
---|
30 | n/a | def run_2to3_capture(self, args, in_capture, out_capture, err_capture): |
---|
31 | n/a | save_stdin = sys.stdin |
---|
32 | n/a | save_stdout = sys.stdout |
---|
33 | n/a | save_stderr = sys.stderr |
---|
34 | n/a | sys.stdin = in_capture |
---|
35 | n/a | sys.stdout = out_capture |
---|
36 | n/a | sys.stderr = err_capture |
---|
37 | n/a | try: |
---|
38 | n/a | return main.main("lib2to3.fixes", args) |
---|
39 | n/a | finally: |
---|
40 | n/a | sys.stdin = save_stdin |
---|
41 | n/a | sys.stdout = save_stdout |
---|
42 | n/a | sys.stderr = save_stderr |
---|
43 | n/a | |
---|
44 | n/a | def test_unencodable_diff(self): |
---|
45 | n/a | input_stream = io.StringIO("print 'nothing'\nprint u'über'\n") |
---|
46 | n/a | out = io.BytesIO() |
---|
47 | n/a | out_enc = codecs.getwriter("ascii")(out) |
---|
48 | n/a | err = io.StringIO() |
---|
49 | n/a | ret = self.run_2to3_capture(["-"], input_stream, out_enc, err) |
---|
50 | n/a | self.assertEqual(ret, 0) |
---|
51 | n/a | output = out.getvalue().decode("ascii") |
---|
52 | n/a | self.assertIn("-print 'nothing'", output) |
---|
53 | n/a | self.assertIn("WARNING: couldn't encode <stdin>'s diff for " |
---|
54 | n/a | "your terminal", err.getvalue()) |
---|
55 | n/a | |
---|
56 | n/a | def setup_test_source_trees(self): |
---|
57 | n/a | """Setup a test source tree and output destination tree.""" |
---|
58 | n/a | self.temp_dir = tempfile.mkdtemp() # tearDown() cleans this up. |
---|
59 | n/a | self.py2_src_dir = os.path.join(self.temp_dir, "python2_project") |
---|
60 | n/a | self.py3_dest_dir = os.path.join(self.temp_dir, "python3_project") |
---|
61 | n/a | os.mkdir(self.py2_src_dir) |
---|
62 | n/a | os.mkdir(self.py3_dest_dir) |
---|
63 | n/a | # Turn it into a package with a few files. |
---|
64 | n/a | self.setup_files = [] |
---|
65 | n/a | open(os.path.join(self.py2_src_dir, "__init__.py"), "w").close() |
---|
66 | n/a | self.setup_files.append("__init__.py") |
---|
67 | n/a | shutil.copy(PY2_TEST_MODULE, self.py2_src_dir) |
---|
68 | n/a | self.setup_files.append(os.path.basename(PY2_TEST_MODULE)) |
---|
69 | n/a | self.trivial_py2_file = os.path.join(self.py2_src_dir, "trivial.py") |
---|
70 | n/a | self.init_py2_file = os.path.join(self.py2_src_dir, "__init__.py") |
---|
71 | n/a | with open(self.trivial_py2_file, "w") as trivial: |
---|
72 | n/a | trivial.write("print 'I need a simple conversion.'") |
---|
73 | n/a | self.setup_files.append("trivial.py") |
---|
74 | n/a | |
---|
75 | n/a | def test_filename_changing_on_output_single_dir(self): |
---|
76 | n/a | """2to3 a single directory with a new output dir and suffix.""" |
---|
77 | n/a | self.setup_test_source_trees() |
---|
78 | n/a | out = io.StringIO() |
---|
79 | n/a | err = io.StringIO() |
---|
80 | n/a | suffix = "TEST" |
---|
81 | n/a | ret = self.run_2to3_capture( |
---|
82 | n/a | ["-n", "--add-suffix", suffix, "--write-unchanged-files", |
---|
83 | n/a | "--no-diffs", "--output-dir", |
---|
84 | n/a | self.py3_dest_dir, self.py2_src_dir], |
---|
85 | n/a | io.StringIO(""), out, err) |
---|
86 | n/a | self.assertEqual(ret, 0) |
---|
87 | n/a | stderr = err.getvalue() |
---|
88 | n/a | self.assertIn(" implies -w.", stderr) |
---|
89 | n/a | self.assertIn( |
---|
90 | n/a | "Output in %r will mirror the input directory %r layout" % ( |
---|
91 | n/a | self.py3_dest_dir, self.py2_src_dir), stderr) |
---|
92 | n/a | self.assertEqual(set(name+suffix for name in self.setup_files), |
---|
93 | n/a | set(os.listdir(self.py3_dest_dir))) |
---|
94 | n/a | for name in self.setup_files: |
---|
95 | n/a | self.assertIn("Writing converted %s to %s" % ( |
---|
96 | n/a | os.path.join(self.py2_src_dir, name), |
---|
97 | n/a | os.path.join(self.py3_dest_dir, name+suffix)), stderr) |
---|
98 | n/a | sep = re.escape(os.sep) |
---|
99 | n/a | self.assertRegex( |
---|
100 | n/a | stderr, r"No changes to .*/__init__\.py".replace("/", sep)) |
---|
101 | n/a | self.assertNotRegex( |
---|
102 | n/a | stderr, r"No changes to .*/trivial\.py".replace("/", sep)) |
---|
103 | n/a | |
---|
104 | n/a | def test_filename_changing_on_output_two_files(self): |
---|
105 | n/a | """2to3 two files in one directory with a new output dir.""" |
---|
106 | n/a | self.setup_test_source_trees() |
---|
107 | n/a | err = io.StringIO() |
---|
108 | n/a | py2_files = [self.trivial_py2_file, self.init_py2_file] |
---|
109 | n/a | expected_files = set(os.path.basename(name) for name in py2_files) |
---|
110 | n/a | ret = self.run_2to3_capture( |
---|
111 | n/a | ["-n", "-w", "--write-unchanged-files", |
---|
112 | n/a | "--no-diffs", "--output-dir", self.py3_dest_dir] + py2_files, |
---|
113 | n/a | io.StringIO(""), io.StringIO(), err) |
---|
114 | n/a | self.assertEqual(ret, 0) |
---|
115 | n/a | stderr = err.getvalue() |
---|
116 | n/a | self.assertIn( |
---|
117 | n/a | "Output in %r will mirror the input directory %r layout" % ( |
---|
118 | n/a | self.py3_dest_dir, self.py2_src_dir), stderr) |
---|
119 | n/a | self.assertEqual(expected_files, set(os.listdir(self.py3_dest_dir))) |
---|
120 | n/a | |
---|
121 | n/a | def test_filename_changing_on_output_single_file(self): |
---|
122 | n/a | """2to3 a single file with a new output dir.""" |
---|
123 | n/a | self.setup_test_source_trees() |
---|
124 | n/a | err = io.StringIO() |
---|
125 | n/a | ret = self.run_2to3_capture( |
---|
126 | n/a | ["-n", "-w", "--no-diffs", "--output-dir", self.py3_dest_dir, |
---|
127 | n/a | self.trivial_py2_file], |
---|
128 | n/a | io.StringIO(""), io.StringIO(), err) |
---|
129 | n/a | self.assertEqual(ret, 0) |
---|
130 | n/a | stderr = err.getvalue() |
---|
131 | n/a | self.assertIn( |
---|
132 | n/a | "Output in %r will mirror the input directory %r layout" % ( |
---|
133 | n/a | self.py3_dest_dir, self.py2_src_dir), stderr) |
---|
134 | n/a | self.assertEqual(set([os.path.basename(self.trivial_py2_file)]), |
---|
135 | n/a | set(os.listdir(self.py3_dest_dir))) |
---|
136 | n/a | |
---|
137 | n/a | |
---|
138 | n/a | if __name__ == '__main__': |
---|
139 | n/a | unittest.main() |
---|