|
4 | 4 | import py_compile
|
5 | 5 | import shutil
|
6 | 6 | import stat
|
| 7 | +import subprocess |
7 | 8 | import sys
|
8 | 9 | import tempfile
|
9 | 10 | import unittest
|
10 | 11 |
|
11 | 12 | from test import support
|
12 |
| -from test.support import os_helper |
| 13 | +from test.support import os_helper, script_helper |
13 | 14 |
|
14 | 15 |
|
15 | 16 | def without_source_date_epoch(fxn):
|
@@ -52,7 +53,7 @@ def __new__(mcls, name, bases, dct, *, source_date_epoch):
|
52 | 53 | class PyCompileTestsBase:
|
53 | 54 |
|
54 | 55 | def setUp(self):
|
55 |
| - self.directory = tempfile.mkdtemp() |
| 56 | + self.directory = tempfile.mkdtemp(dir=os.getcwd()) |
56 | 57 | self.source_path = os.path.join(self.directory, '_test.py')
|
57 | 58 | self.pyc_path = self.source_path + 'c'
|
58 | 59 | self.cache_path = importlib.util.cache_from_source(self.source_path)
|
@@ -255,5 +256,77 @@ class PyCompileTestsWithoutSourceEpoch(PyCompileTestsBase,
|
255 | 256 | pass
|
256 | 257 |
|
257 | 258 |
|
| 259 | +class PyCompileCLITestCase(unittest.TestCase): |
| 260 | + |
| 261 | + def setUp(self): |
| 262 | + self.directory = tempfile.mkdtemp() |
| 263 | + self.source_path = os.path.join(self.directory, '_test.py') |
| 264 | + self.cache_path = importlib.util.cache_from_source(self.source_path) |
| 265 | + with open(self.source_path, 'w') as file: |
| 266 | + file.write('x = 123\n') |
| 267 | + |
| 268 | + def tearDown(self): |
| 269 | + os_helper.rmtree(self.directory) |
| 270 | + |
| 271 | + def pycompilecmd(self, *args, **kwargs): |
| 272 | + # assert_python_* helpers don't return proc object. We'll just use |
| 273 | + # subprocess.run() instead of spawn_python() and its friends to test |
| 274 | + # stdin support of the CLI. |
| 275 | + if args and args[0] == '-' and 'input' in kwargs: |
| 276 | + return subprocess.run([sys.executable, '-m', 'py_compile', '-'], |
| 277 | + input=kwargs['input'].encode(), |
| 278 | + capture_output=True) |
| 279 | + return script_helper.assert_python_ok('-m', 'py_compile', *args, **kwargs) |
| 280 | + |
| 281 | + def pycompilecmd_failure(self, *args): |
| 282 | + return script_helper.assert_python_failure('-m', 'py_compile', *args) |
| 283 | + |
| 284 | + def test_stdin(self): |
| 285 | + result = self.pycompilecmd('-', input=self.source_path) |
| 286 | + self.assertEqual(result.returncode, 0) |
| 287 | + self.assertEqual(result.stdout, b'') |
| 288 | + self.assertEqual(result.stderr, b'') |
| 289 | + self.assertTrue(os.path.exists(self.cache_path)) |
| 290 | + |
| 291 | + def test_with_files(self): |
| 292 | + rc, stdout, stderr = self.pycompilecmd(self.source_path, self.source_path) |
| 293 | + self.assertEqual(rc, 0) |
| 294 | + self.assertEqual(stdout, b'') |
| 295 | + self.assertEqual(stderr, b'') |
| 296 | + self.assertTrue(os.path.exists(self.cache_path)) |
| 297 | + |
| 298 | + def test_bad_syntax(self): |
| 299 | + bad_syntax = os.path.join(os.path.dirname(__file__), 'badsyntax_3131.py') |
| 300 | + rc, stdout, stderr = self.pycompilecmd_failure(bad_syntax) |
| 301 | + self.assertEqual(rc, 1) |
| 302 | + self.assertEqual(stdout, b'') |
| 303 | + self.assertIn(b'SyntaxError', stderr) |
| 304 | + |
| 305 | + def test_bad_syntax_with_quiet(self): |
| 306 | + bad_syntax = os.path.join(os.path.dirname(__file__), 'badsyntax_3131.py') |
| 307 | + rc, stdout, stderr = self.pycompilecmd_failure('-q', bad_syntax) |
| 308 | + self.assertEqual(rc, 1) |
| 309 | + self.assertEqual(stdout, b'') |
| 310 | + self.assertEqual(stderr, b'') |
| 311 | + |
| 312 | + def test_file_not_exists(self): |
| 313 | + should_not_exists = os.path.join(os.path.dirname(__file__), 'should_not_exists.py') |
| 314 | + rc, stdout, stderr = self.pycompilecmd_failure(self.source_path, should_not_exists) |
| 315 | + self.assertEqual(rc, 1) |
| 316 | + self.assertEqual(stdout, b'') |
| 317 | + self.assertIn(b'no such file or directory', stderr.lower()) |
| 318 | + |
| 319 | + # TODO: RUSTPYTHON |
| 320 | + if sys.platform == "win32": |
| 321 | + test_file_not_exists = unittest.expectedFailure(test_file_not_exists) |
| 322 | + |
| 323 | + def test_file_not_exists_with_quiet(self): |
| 324 | + should_not_exists = os.path.join(os.path.dirname(__file__), 'should_not_exists.py') |
| 325 | + rc, stdout, stderr = self.pycompilecmd_failure('-q', self.source_path, should_not_exists) |
| 326 | + self.assertEqual(rc, 1) |
| 327 | + self.assertEqual(stdout, b'') |
| 328 | + self.assertEqual(stderr, b'') |
| 329 | + |
| 330 | + |
258 | 331 | if __name__ == "__main__":
|
259 | 332 | unittest.main()
|
0 commit comments