modules: remove default symlink on uninstall (#36454)

When app is uninstalled, if it matches a default, then remove the
default symlink targeting its modulefile.

Until now, when a default were uninstalled, the default symlink were
left pointing to a nonexistent modulefile.
This commit is contained in:
Xavier Delaruelle
2023-04-03 22:54:18 +02:00
committed by GitHub
parent 91636f0e9d
commit 7a77ecbdb6
2 changed files with 16 additions and 0 deletions

View File

@@ -935,6 +935,7 @@ def remove(self):
if os.path.exists(mod_file):
try:
os.remove(mod_file) # Remove the module file
self.remove_module_defaults() # Remove default targeting module file
os.removedirs(
os.path.dirname(mod_file)
) # Remove all the empty directories from the leaf up
@@ -942,6 +943,18 @@ def remove(self):
# removedirs throws OSError on first non-empty directory found
pass
def remove_module_defaults(self):
if not any(self.spec.satisfies(default) for default in self.conf.defaults):
return
# This spec matches a default, symlink needs to be removed as we remove the module
# file it targets.
default_symlink = os.path.join(os.path.dirname(self.layout.filename), "default")
try:
os.unlink(default_symlink)
except OSError:
pass
@contextlib.contextmanager
def disable_modules():

View File

@@ -77,6 +77,9 @@ def test_modules_default_symlink(
assert os.path.islink(link_path)
assert os.readlink(link_path) == mock_module_filename
generator.remove()
assert not os.path.lexists(link_path)
class MockDb(object):
def __init__(self, db_ids, spec_hash_to_db):