|
15 | 15 | from test.support.os_helper import TESTFN, EnvironmentVarGuard
|
16 | 16 | import ast
|
17 | 17 | import builtins
|
| 18 | +import fnmatch |
18 | 19 | import glob
|
19 | 20 | import io
|
20 | 21 | import os
|
|
25 | 26 | import sys
|
26 | 27 | import sysconfig
|
27 | 28 | import tempfile
|
| 29 | +from textwrap import dedent |
28 | 30 | import urllib.error
|
29 | 31 | import urllib.request
|
30 | 32 | from unittest import mock
|
@@ -805,5 +807,97 @@ def test_underpth_dll_file(self):
|
805 | 807 | self.assertTrue(rc, "sys.path is incorrect")
|
806 | 808 |
|
807 | 809 |
|
| 810 | +class CommandLineTests(unittest.TestCase): |
| 811 | + def setUp(self): |
| 812 | + super().setUp() |
| 813 | + if sys.path[0] != os.getcwd(): |
| 814 | + sys.path.remove(sys.path[0]) |
| 815 | + sys.path.insert(0, os.getcwd()) |
| 816 | + |
| 817 | + def get_excepted_output(self, *args): |
| 818 | + if len(args) == 0: |
| 819 | + user_base = site.getuserbase() |
| 820 | + user_site = site.getusersitepackages() |
| 821 | + output = "sys.path = [\n" |
| 822 | + for dir in sys.path: |
| 823 | + output += " %r,\n" % (dir,) |
| 824 | + output += "]\n" |
| 825 | + def exists(path): |
| 826 | + if path is not None and os.path.isdir(path): |
| 827 | + return "exists" |
| 828 | + else: |
| 829 | + return "doesn't exist" |
| 830 | + output += f"USER_BASE: {user_base!r} ({exists(user_base)})\n" |
| 831 | + output += f"USER_SITE: {user_site!r} ({exists(user_site)})\n" |
| 832 | + output += f"ENABLE_USER_SITE: {site.ENABLE_USER_SITE!r}\n" |
| 833 | + return 0, dedent(output).strip() |
| 834 | + |
| 835 | + buffer = [] |
| 836 | + if '--user-base' in args: |
| 837 | + buffer.append(site.getuserbase()) |
| 838 | + if '--user-site' in args: |
| 839 | + buffer.append(site.getusersitepackages()) |
| 840 | + |
| 841 | + if buffer: |
| 842 | + return_code = 3 |
| 843 | + if site.ENABLE_USER_SITE: |
| 844 | + return_code = 0 |
| 845 | + elif site.ENABLE_USER_SITE is False: |
| 846 | + return_code = 1 |
| 847 | + elif site.ENABLE_USER_SITE is None: |
| 848 | + return_code = 2 |
| 849 | + output = os.pathsep.join(buffer) |
| 850 | + return return_code, dedent(output).strip() |
| 851 | + else: |
| 852 | + return 10, None |
| 853 | + |
| 854 | + def invoke_command_line(self, *args): |
| 855 | + args = [sys.executable, "-m", "site", *args] |
| 856 | + proc = subprocess.Popen(args, |
| 857 | + stdout=subprocess.PIPE, |
| 858 | + stderr=subprocess.STDOUT,) |
| 859 | + proc.wait() |
| 860 | + output = proc.stdout.read().decode() |
| 861 | + return_code = proc.returncode |
| 862 | + proc.stdout.close() |
| 863 | + return return_code, dedent(output).strip() |
| 864 | + |
| 865 | + def test_no_args(self): |
| 866 | + return_code, output = self.invoke_command_line() |
| 867 | + excepted_return_code, excepted_output = self.get_excepted_output() |
| 868 | + self.assertEqual(return_code, excepted_return_code) |
| 869 | + self.assertEqual(output, excepted_output) |
| 870 | + # self.assertTrue(fnmatch.fnmatch(output, excepted_output)) |
| 871 | + |
| 872 | + def test_unknown_args(self): |
| 873 | + return_code, output = self.invoke_command_line("--unknown-arg") |
| 874 | + excepted_return_code, _ = self.get_excepted_output("--unknown-arg") |
| 875 | + self.assertEqual(return_code, excepted_return_code) |
| 876 | + self.assertIn('[--user-base] [--user-site]', output) |
| 877 | + |
| 878 | + def test_base_arg(self): |
| 879 | + return_code, output = self.invoke_command_line("--user-base") |
| 880 | + excepted = self.get_excepted_output("--user-base") |
| 881 | + excepted_return_code, excepted_output = excepted |
| 882 | + self.assertEqual(return_code, excepted_return_code) |
| 883 | + self.assertEqual(output, excepted_output) |
| 884 | + self.assertTrue(fnmatch.fnmatch(output, excepted_output)) |
| 885 | + |
| 886 | + def test_site_arg(self): |
| 887 | + return_code, output = self.invoke_command_line("--user-site") |
| 888 | + excepted = self.get_excepted_output("--user-site") |
| 889 | + excepted_return_code, excepted_output = excepted |
| 890 | + self.assertEqual(return_code, excepted_return_code) |
| 891 | + self.assertTrue(fnmatch.fnmatch(output, excepted_output)) |
| 892 | + |
| 893 | + def test_both_args(self): |
| 894 | + return_code, output = self.invoke_command_line("--user-base", |
| 895 | + "--user-site") |
| 896 | + excepted = self.get_excepted_output("--user-base", "--user-site") |
| 897 | + excepted_return_code, excepted_output = excepted |
| 898 | + self.assertEqual(return_code, excepted_return_code) |
| 899 | + self.assertTrue(fnmatch.fnmatch(output, excepted_output)) |
| 900 | + |
| 901 | + |
808 | 902 | if __name__ == "__main__":
|
809 | 903 | unittest.main()
|
0 commit comments