annotate Lib/test/test_py_compile.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents b23453530d5f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
73753
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
1 import imp
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
2 import os
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
3 import py_compile
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
4 import shutil
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
5 import tempfile
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
6 import unittest
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
7
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
8 from test import test_support
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
9
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
10 class PyCompileTests(unittest.TestCase):
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
11
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
12 def setUp(self):
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
13 self.directory = tempfile.mkdtemp()
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
14 self.source_path = os.path.join(self.directory, '_test.py')
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
15 self.pyc_path = self.source_path + 'c'
73758
b23453530d5f Issue #12618: fix py_compile unit tests to handle different drives on Windows
Meador Inge <meadori@gmail.com>
parents: 73753
diff changeset
16 self.cwd_drive = os.path.splitdrive(os.getcwd())[0]
b23453530d5f Issue #12618: fix py_compile unit tests to handle different drives on Windows
Meador Inge <meadori@gmail.com>
parents: 73753
diff changeset
17 # In these tests we compute relative paths. When using Windows, the
b23453530d5f Issue #12618: fix py_compile unit tests to handle different drives on Windows
Meador Inge <meadori@gmail.com>
parents: 73753
diff changeset
18 # current working directory path and the 'self.source_path' might be
b23453530d5f Issue #12618: fix py_compile unit tests to handle different drives on Windows
Meador Inge <meadori@gmail.com>
parents: 73753
diff changeset
19 # on different drives. Therefore we need to switch to the drive where
b23453530d5f Issue #12618: fix py_compile unit tests to handle different drives on Windows
Meador Inge <meadori@gmail.com>
parents: 73753
diff changeset
20 # the temporary source file lives.
b23453530d5f Issue #12618: fix py_compile unit tests to handle different drives on Windows
Meador Inge <meadori@gmail.com>
parents: 73753
diff changeset
21 drive = os.path.splitdrive(self.source_path)[0]
b23453530d5f Issue #12618: fix py_compile unit tests to handle different drives on Windows
Meador Inge <meadori@gmail.com>
parents: 73753
diff changeset
22 if drive:
b23453530d5f Issue #12618: fix py_compile unit tests to handle different drives on Windows
Meador Inge <meadori@gmail.com>
parents: 73753
diff changeset
23 os.chdir(drive)
b23453530d5f Issue #12618: fix py_compile unit tests to handle different drives on Windows
Meador Inge <meadori@gmail.com>
parents: 73753
diff changeset
24
73753
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
25 with open(self.source_path, 'w') as file:
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
26 file.write('x = 123\n')
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
27
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
28 def tearDown(self):
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
29 shutil.rmtree(self.directory)
73758
b23453530d5f Issue #12618: fix py_compile unit tests to handle different drives on Windows
Meador Inge <meadori@gmail.com>
parents: 73753
diff changeset
30 if self.cwd_drive:
b23453530d5f Issue #12618: fix py_compile unit tests to handle different drives on Windows
Meador Inge <meadori@gmail.com>
parents: 73753
diff changeset
31 os.chdir(self.cwd_drive)
73753
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
32
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
33 def test_absolute_path(self):
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
34 py_compile.compile(self.source_path, self.pyc_path)
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
35 self.assertTrue(os.path.exists(self.pyc_path))
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
36
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
37 def test_cwd(self):
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
38 cwd = os.getcwd()
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
39 os.chdir(self.directory)
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
40 py_compile.compile(os.path.basename(self.source_path),
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
41 os.path.basename(self.pyc_path))
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
42 os.chdir(cwd)
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
43 self.assertTrue(os.path.exists(self.pyc_path))
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
44
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
45 def test_relative_path(self):
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
46 py_compile.compile(os.path.relpath(self.source_path),
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
47 os.path.relpath(self.pyc_path))
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
48 self.assertTrue(os.path.exists(self.pyc_path))
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
49
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
50 def test_main():
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
51 test_support.run_unittest(PyCompileTests)
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
52
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
53 if __name__ == "__main__":
bcc7bf3963cc Issue #12618: create unit tests for the py_compile module
Meador Inge <meadori@gmail.com>
parents:
diff changeset
54 test_main()