|
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): |
@@ -217,5 +218,73 @@ class PyCompileTestsWithoutSourceEpoch(PyCompileTestsBase, |
217 | 218 | pass |
218 | 219 |
|
219 | 220 |
|
| 221 | +class PyCompileCLITestCase(unittest.TestCase): |
| 222 | + |
| 223 | + def setUp(self): |
| 224 | + self.directory = tempfile.mkdtemp() |
| 225 | + self.source_path = os.path.join(self.directory, '_test.py') |
| 226 | + self.cache_path = importlib.util.cache_from_source(self.source_path) |
| 227 | + with open(self.source_path, 'w') as file: |
| 228 | + file.write('x = 123\n') |
| 229 | + |
| 230 | + def tearDown(self): |
| 231 | + support.rmtree(self.directory) |
| 232 | + |
| 233 | + def pycompilecmd(self, *args, **kwargs): |
| 234 | + # assert_python_* helpers don't return proc object. We'll just use |
| 235 | + # subprocess.run() instead of spawn_python() and its friends to test |
| 236 | + # stdin support of the CLI. |
| 237 | + if args and args[0] == '-' and 'input' in kwargs: |
| 238 | + return subprocess.run([sys.executable, '-m', 'py_compile', '-'], |
| 239 | + input=kwargs['input'].encode(), |
| 240 | + capture_output=True) |
| 241 | + return script_helper.assert_python_ok('-m', 'py_compile', *args, **kwargs) |
| 242 | + |
| 243 | + def pycompilecmd_failure(self, *args): |
| 244 | + return script_helper.assert_python_failure('-m', 'py_compile', *args) |
| 245 | + |
| 246 | + def test_stdin(self): |
| 247 | + result = self.pycompilecmd('-', input=self.source_path) |
| 248 | + self.assertEqual(result.returncode, 0) |
| 249 | + self.assertEqual(result.stdout, b'') |
| 250 | + self.assertEqual(result.stderr, b'') |
| 251 | + self.assertTrue(os.path.exists(self.cache_path)) |
| 252 | + |
| 253 | + def test_with_files(self): |
| 254 | + rc, stdout, stderr = self.pycompilecmd(self.source_path, self.source_path) |
| 255 | + self.assertEqual(rc, 0) |
| 256 | + self.assertEqual(stdout, b'') |
| 257 | + self.assertEqual(stderr, b'') |
| 258 | + self.assertTrue(os.path.exists(self.cache_path)) |
| 259 | + |
| 260 | + def test_bad_syntax(self): |
| 261 | + bad_syntax = os.path.join(os.path.dirname(__file__), 'badsyntax_3131.py') |
| 262 | + rc, stdout, stderr = self.pycompilecmd_failure(bad_syntax) |
| 263 | + self.assertEqual(rc, 1) |
| 264 | + self.assertEqual(stdout, b'') |
| 265 | + self.assertIn(b'SyntaxError', stderr) |
| 266 | + |
| 267 | + def test_bad_syntax_with_quiet(self): |
| 268 | + bad_syntax = os.path.join(os.path.dirname(__file__), 'badsyntax_3131.py') |
| 269 | + rc, stdout, stderr = self.pycompilecmd_failure('-q', bad_syntax) |
| 270 | + self.assertEqual(rc, 1) |
| 271 | + self.assertEqual(stdout, b'') |
| 272 | + self.assertEqual(stderr, b'') |
| 273 | + |
| 274 | + def test_file_not_exists(self): |
| 275 | + should_not_exists = os.path.join(os.path.dirname(__file__), 'should_not_exists.py') |
| 276 | + rc, stdout, stderr = self.pycompilecmd_failure(self.source_path, should_not_exists) |
| 277 | + self.assertEqual(rc, 1) |
| 278 | + self.assertEqual(stdout, b'') |
| 279 | + self.assertIn(b'No such file or directory', stderr) |
| 280 | + |
| 281 | + def test_file_not_exists_with_quiet(self): |
| 282 | + should_not_exists = os.path.join(os.path.dirname(__file__), 'should_not_exists.py') |
| 283 | + rc, stdout, stderr = self.pycompilecmd_failure('-q', self.source_path, should_not_exists) |
| 284 | + self.assertEqual(rc, 1) |
| 285 | + self.assertEqual(stdout, b'') |
| 286 | + self.assertEqual(stderr, b'') |
| 287 | + |
| 288 | + |
220 | 289 | if __name__ == "__main__": |
221 | 290 | unittest.main() |
0 commit comments