|
3 | 3 | import os
|
4 | 4 | import subprocess
|
5 | 5 | import shutil
|
| 6 | +import json |
| 7 | +import textwrap |
6 | 8 | from copy import copy
|
7 | 9 |
|
8 | 10 | from test.support import (
|
|
11 | 13 | from test.support.import_helper import import_module
|
12 | 14 | from test.support.os_helper import (TESTFN, unlink, skip_unless_symlink,
|
13 | 15 | change_cwd)
|
| 16 | +from test.support.venv import VirtualEnvironment |
14 | 17 |
|
15 | 18 | import sysconfig
|
16 | 19 | from sysconfig import (get_paths, get_platform, get_config_vars,
|
@@ -90,6 +93,12 @@ def _cleanup_testfn(self):
|
90 | 93 | elif os.path.isdir(path):
|
91 | 94 | shutil.rmtree(path)
|
92 | 95 |
|
| 96 | + def venv(self, **venv_create_args): |
| 97 | + return VirtualEnvironment.from_tmpdir( |
| 98 | + prefix=f'{self.id()}-venv-', |
| 99 | + **venv_create_args, |
| 100 | + ) |
| 101 | + |
93 | 102 | def test_get_path_names(self):
|
94 | 103 | self.assertEqual(get_path_names(), sysconfig._SCHEME_KEYS)
|
95 | 104 |
|
@@ -511,6 +520,72 @@ def test_osx_ext_suffix(self):
|
511 | 520 | suffix = sysconfig.get_config_var('EXT_SUFFIX')
|
512 | 521 | self.assertTrue(suffix.endswith('-darwin.so'), suffix)
|
513 | 522 |
|
| 523 | + @unittest.skipIf(sys.platform == 'wasi', 'venv is unsupported on WASI') |
| 524 | + def test_config_vars_depend_on_site_initialization(self): |
| 525 | + script = textwrap.dedent(""" |
| 526 | + import sysconfig |
| 527 | +
|
| 528 | + config_vars = sysconfig.get_config_vars() |
| 529 | +
|
| 530 | + import json |
| 531 | + print(json.dumps(config_vars, indent=2)) |
| 532 | + """) |
| 533 | + |
| 534 | + with self.venv() as venv: |
| 535 | + site_config_vars = json.loads(venv.run('-c', script).stdout) |
| 536 | + no_site_config_vars = json.loads(venv.run('-S', '-c', script).stdout) |
| 537 | + |
| 538 | + self.assertNotEqual(site_config_vars, no_site_config_vars) |
| 539 | + # With the site initialization, the virtual environment should be enabled. |
| 540 | + self.assertEqual(site_config_vars['base'], venv.prefix) |
| 541 | + self.assertEqual(site_config_vars['platbase'], venv.prefix) |
| 542 | + #self.assertEqual(site_config_vars['prefix'], venv.prefix) # # FIXME: prefix gets overwriten by _init_posix |
| 543 | + # Without the site initialization, the virtual environment should be disabled. |
| 544 | + self.assertEqual(no_site_config_vars['base'], site_config_vars['installed_base']) |
| 545 | + self.assertEqual(no_site_config_vars['platbase'], site_config_vars['installed_platbase']) |
| 546 | + |
| 547 | + @unittest.skipIf(sys.platform == 'wasi', 'venv is unsupported on WASI') |
| 548 | + def test_config_vars_recalculation_after_site_initialization(self): |
| 549 | + script = textwrap.dedent(""" |
| 550 | + import sysconfig |
| 551 | +
|
| 552 | + before = sysconfig.get_config_vars() |
| 553 | +
|
| 554 | + import site |
| 555 | + site.main() |
| 556 | +
|
| 557 | + after = sysconfig.get_config_vars() |
| 558 | +
|
| 559 | + import json |
| 560 | + print(json.dumps({'before': before, 'after': after}, indent=2)) |
| 561 | + """) |
| 562 | + |
| 563 | + with self.venv() as venv: |
| 564 | + config_vars = json.loads(venv.run('-S', '-c', script).stdout) |
| 565 | + |
| 566 | + self.assertNotEqual(config_vars['before'], config_vars['after']) |
| 567 | + self.assertEqual(config_vars['after']['base'], venv.prefix) |
| 568 | + #self.assertEqual(config_vars['after']['prefix'], venv.prefix) # FIXME: prefix gets overwriten by _init_posix |
| 569 | + #self.assertEqual(config_vars['after']['exec_prefix'], venv.prefix) # FIXME: exec_prefix gets overwriten by _init_posix |
| 570 | + |
| 571 | + @unittest.skipIf(sys.platform == 'wasi', 'venv is unsupported on WASI') |
| 572 | + def test_paths_depend_on_site_initialization(self): |
| 573 | + script = textwrap.dedent(""" |
| 574 | + import sysconfig |
| 575 | +
|
| 576 | + paths = sysconfig.get_paths() |
| 577 | +
|
| 578 | + import json |
| 579 | + print(json.dumps(paths, indent=2)) |
| 580 | + """) |
| 581 | + |
| 582 | + with self.venv() as venv: |
| 583 | + site_paths = json.loads(venv.run('-c', script).stdout) |
| 584 | + no_site_paths = json.loads(venv.run('-S', '-c', script).stdout) |
| 585 | + |
| 586 | + self.assertNotEqual(site_paths, no_site_paths) |
| 587 | + |
| 588 | + |
514 | 589 | class MakefileTests(unittest.TestCase):
|
515 | 590 |
|
516 | 591 | @unittest.skipIf(sys.platform.startswith('win'),
|
|
0 commit comments