fix: make the patched get_default_project_directory order-safe in tests

A product module first-imported while run_around_tests' autouse
monkeypatch of gns3server.utils.path.get_default_project_directory is
active (e.g. 'from gns3server.api.server import app' inside a test body)
binds the patched lambda into its own namespace forever: module-level
from-imports capture the object by value. Once the patch is reverted,
that module keeps calling the stale lambda, which closed over the very
test's tmppath — a directory deleted at that test's teardown. Every
later test hitting psutil.disk_usage(get_default_project_directory())
then fails with FileNotFoundError, but only when the importing test runs
before the API test files (full-suite collection order hides it).

The replacement now resolves Config.instance().settings at call time
instead of closing over the tmppath, so a frozen reference stays
correct; the test that triggered this imports gns3_app at module top
so no product import ever happens inside a patched window.

Also pins pytest-random-order (inert without --random-order) and
documents the order-independence rules for new tests in the
gns3-api-test-writing skill, including the known-red legacy files
whose tests share rows sequentially.
This commit is contained in:
YueGuobin committed 2026-09-05 21:49:24 +08:00
1 parent b72b8b44b4
commit 49c8a72103
4 files changed
+28 -3

No files matched your search

+7 -1
View File
@@ -418,7 +418,13 @@ def run_around_tests(monkeypatch, config, port_manager):
# avoid monitoring for new images while testing
config.settings.Server.auto_discover_images = False
monkeypatch.setattr("gns3server.utils.path.get_default_project_directory", lambda *args: os.path.join(tmppath, 'projects'))
# Resolve the projects directory from the Config singleton at call time instead of
# closing over this test's tmppath: a from-import executed anywhere while this
# patch is active (e.g. the first import of gns3server.api.server from inside a
# test body) freezes the patched object into the importing module's namespace
# forever, and a closed-over path would then point at a deleted directory in
# every later test (order-dependent FileNotFoundError in psutil.disk_usage).
monkeypatch.setattr("gns3server.utils.path.get_default_project_directory", lambda *args: Config.instance().settings.Server.projects_path)
# Force sys.platform to the original value. Because it seems not be restored correctly after each test
sys.platform = sys.original_platform