tests: fix testsuite virtualenv support
Fix sites ordering: add user site first, before system site.
@@ -157,16 +157,37 @@ def virtualenv_template(tmpdir_factory):
pip_source_dir=pip_src,
relocatable=True,
)
- # Ensure `virtualenv.system_site_packages = True` (needed for testing
- # `--user`) does not result in adding the real site-packages' directory
- # to `sys.path`.
+ # Fix `site.py`.
site_py = venv.lib / 'site.py'
with open(site_py) as fp:
site_contents = fp.read()
- site_pattern = '\ndef virtual_addsitepackages(known_paths):\n'
- assert site_pattern in site_contents
- site_contents = site_contents.replace(site_pattern, site_pattern +
- ' return known_paths\n')
+ for pattern, replace in (
+ (
+ # Ensure `virtualenv.system_site_packages = True` (needed
+ # for testing `--user`) does not result in adding the real
+ # site-packages' directory to `sys.path`.
+ (
+ '\ndef virtual_addsitepackages(known_paths):\n'
+ ),
+ (
+ '\ndef virtual_addsitepackages(known_paths):\n'
+ ' return known_paths\n'
+ ),
+ ),
+ (
+ # Fix sites ordering: user site must be added before system site.
+ (
+ '\n paths_in_sys = addsitepackages(paths_in_sys)'
+ '\n paths_in_sys = addusersitepackages(paths_in_sys)\n'
+ ),
+ (
+ '\n paths_in_sys = addusersitepackages(paths_in_sys)'
+ '\n paths_in_sys = addsitepackages(paths_in_sys)\n'
+ ),
+ ),
+ ):
+ assert pattern in site_contents
+ site_contents = site_contents.replace(pattern, replace)
with open(site_py, 'w') as fp:
fp.write(site_contents)
if sys.platform == 'win32':
