Skip to content

Commit

Permalink
fix(tests): need to restore sys.meta_path along with sys.path
Browse files Browse the repository at this point in the history
In the restore_import_state contextmanager, when the pre-test import
state is restored, sys.meta_path must be restored along with sys.path.

In some cases, setuptools appends it's finders to sys.meta_path.
Since restore_import_state might "unimport" setuptools after a test,
it can get confused and append its finders again in a second test.
This results in import_metadata.distributions() listing all
distributions twice. This results, e.g., in the cryptic 'RuntimeError:
Plugin "webpack-support" is already registered' test failure.
  • Loading branch information
dairiki committed Apr 23, 2023
1 parent b7f4aa8 commit 160d768
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def restore_import_state():
"""
save_path = sys.path
sys.path = save_path.copy()
save_meta_path = sys.meta_path
sys.meta_path = save_meta_path.copy()

# Restoring `sys.modules` is an attempt to unload any
# modules loaded during the test so that they can be re-loaded for
Expand All @@ -95,6 +97,7 @@ def restore_import_state():
del sys.modules[name]
sys.modules.update(saved_modules)
sys.path = save_path
sys.meta_path = save_meta_path


@pytest.fixture
Expand Down

0 comments on commit 160d768

Please sign in to comment.