Skip to content

gh-131178: Add tests for site command-line interface #137807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Lib/test/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,55 @@ def test_underpth_dll_file(self):
)], env=env)
self.assertTrue(rc, "sys.path is incorrect")

class TestSiteCLI(unittest.TestCase):
"""Tests for the site module command-line interface."""

@support.requires_subprocess()
def test_cli_no_args(self):
"""Test 'python -m site' with no arguments."""
output = subprocess.check_output([sys.executable, '-m', 'site'],
text=True, stderr=subprocess.STDOUT)
self.assertIn('sys.path', output)
self.assertIn('USER_BASE:', output)
self.assertIn('USER_SITE:', output)
self.assertIn('ENABLE_USER_SITE:', output)

@support.requires_subprocess()
def test_cli_user_site(self):
"""Test 'python -m site --user-site'."""
output = subprocess.check_output([sys.executable, '-m', 'site', '--user-site'],
text=True, stderr=subprocess.STDOUT)
self.assertIn('site-packages', output.strip())
self.assertNotIn('sys.path', output)

@support.requires_subprocess()
def test_cli_user_base(self):
"""Test 'python -m site --user-base'."""
output = subprocess.check_output([sys.executable, '-m', 'site', '--user-base'],
text=True, stderr=subprocess.STDOUT)
self.assertNotIn('site-packages', output)
self.assertNotIn('sys.path', output)
self.assertTrue(len(output.strip()) > 0)

@support.requires_subprocess()
def test_cli_user_base_and_user_site(self):
"""Test 'python -m site --user-base --user-site'."""
output = subprocess.check_output([sys.executable, '-m', 'site', '--user-base', '--user-site'],
text=True, stderr=subprocess.STDOUT)
lines = output.strip().split('\n')
self.assertEqual(len(lines), 1)
paths = lines[0].split(':')
self.assertEqual(len(paths), 2)
self.assertNotIn('site-packages', paths[0])
self.assertIn('site-packages', paths[1])

@support.requires_subprocess()
def test_cli_invalid_option(self):
"""Test 'python -m site' with invalid option."""
with self.assertRaises(subprocess.CalledProcessError) as cm:
subprocess.check_output([sys.executable, '-m', 'site', '--invalid'],
text=True, stderr=subprocess.STDOUT)
self.assertNotEqual(cm.exception.returncode, 0)

if __name__ == "__main__":
unittest.main()
Loading