|
6 | 6 | import io |
7 | 7 | import operator |
8 | 8 | import os |
| 9 | +import py_compile |
9 | 10 | import shutil |
10 | 11 | import stat |
11 | 12 | import sys |
|
15 | 16 | import argparse |
16 | 17 | import warnings |
17 | 18 |
|
18 | | -from test.support import os_helper, captured_stderr |
| 19 | +from test.support import captured_stderr |
| 20 | +from test.support import import_helper |
| 21 | +from test.support import os_helper |
| 22 | +from test.support import script_helper |
19 | 23 | from unittest import mock |
20 | 24 |
|
21 | 25 |
|
| 26 | +py = os.path.basename(sys.executable) |
| 27 | + |
| 28 | + |
22 | 29 | class StdIOBuffer(io.TextIOWrapper): |
23 | 30 | '''Replacement for writable io.StringIO that behaves more like real file |
24 | 31 |
|
@@ -6561,6 +6568,99 @@ def test_os_error(self): |
6561 | 6568 | self.parser.parse_args, ['@no-such-file']) |
6562 | 6569 |
|
6563 | 6570 |
|
| 6571 | +class TestProgName(TestCase): |
| 6572 | + source = textwrap.dedent('''\ |
| 6573 | + import argparse |
| 6574 | + parser = argparse.ArgumentParser() |
| 6575 | + parser.parse_args() |
| 6576 | + ''') |
| 6577 | + |
| 6578 | + def setUp(self): |
| 6579 | + self.dirname = 'package' + os_helper.FS_NONASCII |
| 6580 | + self.addCleanup(os_helper.rmtree, self.dirname) |
| 6581 | + os.mkdir(self.dirname) |
| 6582 | + |
| 6583 | + def make_script(self, dirname, basename, *, compiled=False): |
| 6584 | + script_name = script_helper.make_script(dirname, basename, self.source) |
| 6585 | + if not compiled: |
| 6586 | + return script_name |
| 6587 | + py_compile.compile(script_name, doraise=True) |
| 6588 | + os.remove(script_name) |
| 6589 | + pyc_file = import_helper.make_legacy_pyc(script_name) |
| 6590 | + return pyc_file |
| 6591 | + |
| 6592 | + def make_zip_script(self, script_name, name_in_zip=None): |
| 6593 | + zip_name, _ = script_helper.make_zip_script(self.dirname, 'test_zip', |
| 6594 | + script_name, name_in_zip) |
| 6595 | + return zip_name |
| 6596 | + |
| 6597 | + def check_usage(self, expected, *args, **kwargs): |
| 6598 | + res = script_helper.assert_python_ok(*args, '-h', **kwargs) |
| 6599 | + self.assertEqual(os.fsdecode(res.out.splitlines()[0]), |
| 6600 | + f'usage: {expected} [-h]') |
| 6601 | + |
| 6602 | + def test_script(self, compiled=False): |
| 6603 | + basename = os_helper.TESTFN |
| 6604 | + script_name = self.make_script(self.dirname, basename, compiled=compiled) |
| 6605 | + self.check_usage(os.path.basename(script_name), script_name, '-h') |
| 6606 | + |
| 6607 | + def test_script_compiled(self): |
| 6608 | + self.test_script(compiled=True) |
| 6609 | + |
| 6610 | + def test_directory(self, compiled=False): |
| 6611 | + dirname = os.path.join(self.dirname, os_helper.TESTFN) |
| 6612 | + os.mkdir(dirname) |
| 6613 | + self.make_script(dirname, '__main__', compiled=compiled) |
| 6614 | + self.check_usage(f'{py} {dirname}', dirname) |
| 6615 | + dirname2 = os.path.join(os.curdir, dirname) |
| 6616 | + self.check_usage(f'{py} {dirname2}', dirname2) |
| 6617 | + |
| 6618 | + def test_directory_compiled(self): |
| 6619 | + self.test_directory(compiled=True) |
| 6620 | + |
| 6621 | + def test_module(self, compiled=False): |
| 6622 | + basename = 'module' + os_helper.FS_NONASCII |
| 6623 | + modulename = f'{self.dirname}.{basename}' |
| 6624 | + self.make_script(self.dirname, basename, compiled=compiled) |
| 6625 | + self.check_usage(f'{py} -m {modulename}', |
| 6626 | + '-m', modulename, PYTHONPATH=os.curdir) |
| 6627 | + |
| 6628 | + def test_module_compiled(self): |
| 6629 | + self.test_module(compiled=True) |
| 6630 | + |
| 6631 | + def test_package(self, compiled=False): |
| 6632 | + basename = 'subpackage' + os_helper.FS_NONASCII |
| 6633 | + packagename = f'{self.dirname}.{basename}' |
| 6634 | + subdirname = os.path.join(self.dirname, basename) |
| 6635 | + os.mkdir(subdirname) |
| 6636 | + self.make_script(subdirname, '__main__', compiled=compiled) |
| 6637 | + self.check_usage(f'{py} -m {packagename}', |
| 6638 | + '-m', packagename, PYTHONPATH=os.curdir) |
| 6639 | + self.check_usage(f'{py} -m {packagename}', |
| 6640 | + '-m', packagename + '.__main__', PYTHONPATH=os.curdir) |
| 6641 | + |
| 6642 | + def test_package_compiled(self): |
| 6643 | + self.test_package(compiled=True) |
| 6644 | + |
| 6645 | + def test_zipfile(self, compiled=False): |
| 6646 | + script_name = self.make_script(self.dirname, '__main__', compiled=compiled) |
| 6647 | + zip_name = self.make_zip_script(script_name) |
| 6648 | + self.check_usage(f'{py} {zip_name}', zip_name) |
| 6649 | + |
| 6650 | + def test_zipfile_compiled(self): |
| 6651 | + self.test_zipfile(compiled=True) |
| 6652 | + |
| 6653 | + def test_directory_in_zipfile(self, compiled=False): |
| 6654 | + script_name = self.make_script(self.dirname, '__main__', compiled=compiled) |
| 6655 | + name_in_zip = 'package/subpackage/__main__' + ('.py', '.pyc')[compiled] |
| 6656 | + zip_name = self.make_zip_script(script_name, name_in_zip) |
| 6657 | + dirname = os.path.join(zip_name, 'package', 'subpackage') |
| 6658 | + self.check_usage(f'{py} {dirname}', dirname) |
| 6659 | + |
| 6660 | + def test_directory_in_zipfile_compiled(self): |
| 6661 | + self.test_directory_in_zipfile(compiled=True) |
| 6662 | + |
| 6663 | + |
6564 | 6664 | def tearDownModule(): |
6565 | 6665 | # Remove global references to avoid looking like we have refleaks. |
6566 | 6666 | RFile.seen = {} |
|
0 commit comments