Skip to content
Closed
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions Lib/test/test_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,33 @@ def test_failed_symlink(self):
filepath_regex = r"'[A-Z]:\\\\(?:[^\\\\]+\\\\)*[^\\\\]+'"
self.assertRegex(err, rf"Unable to symlink {filepath_regex} to {filepath_regex}")

@requireVenvCreate
@unittest.skipIf(os.name == 'nt', 'not relevant on Windows')
@unittest.skipUnless(can_symlink(), 'Needs symlinks')
def test_broken_symlink_in_existing_venv(self):
"""
Test creating a venv when a stale venv with broken symlinks exists.
"""
bindir = os.path.join(self.env_dir, self.bindir)
os.makedirs(bindir)
python = os.path.join(bindir, 'python3')
os.symlink('/path/to/deleted/conda/env/bin/python3', python)
self.assertTrue(os.path.islink(python))
self.assertFalse(os.path.exists(python))

builder = venv.EnvBuilder(with_pip=False, symlinks=True)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a symlinks=False (--copies) case so the path from the original report is covered:

builder = venv.EnvBuilder(with_pip=False, symlinks=False)
self.run_with_capture(builder.create, self.env_dir)
self.assertFalse(os.path.islink(python))
self.assertTrue(os.path.exists(python))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for spotting this, I pushed coverage for the --copies path

self.run_with_capture(builder.create, self.env_dir)
self.assertTrue(os.path.islink(python))
self.assertTrue(os.path.exists(python))

rmtree(self.env_dir)
os.makedirs(bindir)
os.symlink('/path/to/deleted/conda/env/bin/python3', python)
builder = venv.EnvBuilder(with_pip=False, symlinks=False)
self.run_with_capture(builder.create, self.env_dir)
self.assertFalse(os.path.islink(python))
self.assertTrue(os.path.exists(python))

@requireVenvCreate
def test_multiprocessing(self):
"""
Expand Down
2 changes: 2 additions & 0 deletions Lib/venv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ def symlink_or_copy(self, src, dst, relative_symlinks_ok=False):
switch to a different set of files instead.)
"""
assert os.name != 'nt'
if os.path.islink(dst) and not os.path.exists(dst):
os.unlink(dst)
force_copy = not self.symlinks
if not force_copy:
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`venv`: Rebuild broken interpreter symlinks when upgrading a virtual
environment. Fix by Clay Dugo.
Loading