»Core Development>Code coverage>Lib/lib2to3/tests/test_main.py

Python code coverage for Lib/lib2to3/tests/test_main.py

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