| 1 |
import imp |
| 2 |
import os |
| 3 |
import py_compile |
| 4 |
import shutil |
| 5 |
import tempfile |
| 6 |
import unittest |
| 7 |
|
| 8 |
from test import test_support as support |
| 9 |
|
| 10 |
class PyCompileTests(unittest.TestCase): |
| 11 |
|
| 12 |
def setUp(self): |
| 13 |
self.directory = tempfile.mkdtemp(dir=os.getcwd()) |
| 14 |
self.source_path = os.path.join(self.directory, '_test.py') |
| 15 |
self.pyc_path = self.source_path + 'c' |
| 16 |
self.cwd_drive = os.path.splitdrive(os.getcwd())[0] |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
drive = os.path.splitdrive(self.source_path)[0] |
| 22 |
if drive: |
| 23 |
os.chdir(drive) |
| 24 |
|
| 25 |
with open(self.source_path, 'w') as file: |
| 26 |
file.write('x = 123\n') |
| 27 |
|
| 28 |
def tearDown(self): |
| 29 |
shutil.rmtree(self.directory) |
| 30 |
if self.cwd_drive: |
| 31 |
os.chdir(self.cwd_drive) |
| 32 |
|
| 33 |
def test_absolute_path(self): |
| 34 |
py_compile.compile(self.source_path, self.pyc_path) |
| 35 |
self.assertTrue(os.path.exists(self.pyc_path)) |
| 36 |
|
| 37 |
def test_cwd(self): |
| 38 |
with support.change_cwd(self.directory): |
| 39 |
py_compile.compile(os.path.basename(self.source_path), |
| 40 |
os.path.basename(self.pyc_path)) |
| 41 |
self.assertTrue(os.path.exists(self.pyc_path)) |
| 42 |
|
| 43 |
def test_relative_path(self): |
| 44 |
py_compile.compile(os.path.relpath(self.source_path), |
| 45 |
os.path.relpath(self.pyc_path)) |
| 46 |
self.assertTrue(os.path.exists(self.pyc_path)) |
| 47 |
|
| 48 |
def test_main(): |
| 49 |
support.run_unittest(PyCompileTests) |
| 50 |
|
| 51 |
if __name__ == "__main__": |
| 52 |
test_main() |