|
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
|
| 13 | +from test.support import script_helper |
12 | 14 |
|
13 | 15 |
|
14 | 16 | def without_source_date_epoch(fxn):
|
@@ -216,5 +218,59 @@ class PyCompileTestsWithoutSourceEpoch(PyCompileTestsBase,
|
216 | 218 | pass
|
217 | 219 |
|
218 | 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_file_not_exists(self): |
| 268 | + should_not_exists = os.path.join(os.path.dirname(__file__), 'should_not_exists.py') |
| 269 | + rc, stdout, stderr = self.pycompilecmd_failure(self.source_path, should_not_exists) |
| 270 | + self.assertEqual(rc, 1) |
| 271 | + self.assertEqual(stdout, b'') |
| 272 | + self.assertIn(b'No such file or directory', stderr) |
| 273 | + |
| 274 | + |
219 | 275 | if __name__ == "__main__":
|
220 | 276 | unittest.main()
|
0 commit comments