From 9285ae37827d87f31ae6707b1b531a67883f7557 Mon Sep 17 00:00:00 2001 From: gooker_young Date: Fri, 26 Jun 2026 01:47:24 +0800 Subject: [PATCH] =?UTF-8?q?test(packtool):=20=E4=BC=98=E5=8C=96=E6=89=93?= =?UTF-8?q?=E5=8C=85=E5=B7=A5=E5=85=B7=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= =?UTF-8?q?=EF=BC=8C=E7=BB=9F=E4=B8=80=E4=BD=BF=E7=94=A8=E4=B8=B4=E6=97=B6?= =?UTF-8?q?=E5=B7=A5=E4=BD=9C=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 新增自动切换临时工作目录的全局fixture,避免测试污染项目根目录 2. 移除测试中手动mock缓存目录的代码,复用全局fixture配置 3. 简化测试代码结构,提升测试可读性和维护性 --- tests/cli/test_packtool.py | 39 +++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/tests/cli/test_packtool.py b/tests/cli/test_packtool.py index c4bb288..915b1fd 100644 --- a/tests/cli/test_packtool.py +++ b/tests/cli/test_packtool.py @@ -5,10 +5,29 @@ from __future__ import annotations from pathlib import Path from unittest.mock import MagicMock, patch +import pytest + import pyflowx as px from pyflowx.cli import packtool +# ---------------------------------------------------------------------- # +# Fixtures: 确保所有测试都在临时目录执行,不污染项目根目录 +# ---------------------------------------------------------------------- # +@pytest.fixture(autouse=True) +def packtool_tmp_workdir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + """自动切换到临时工作目录,防止测试污染项目根目录. + + Args: + tmp_path: pytest 提供的临时目录 + monkeypatch: pytest 的 monkeypatch 工具 + """ + # 切换工作目录到 tmp_path + monkeypatch.chdir(tmp_path) + # Mock DEFAULT_CACHE_DIR 到临时目录 + monkeypatch.setattr(packtool, "DEFAULT_CACHE_DIR", str(tmp_path / ".cache" / "pypack")) + + # ---------------------------------------------------------------------- # # pack_source # ---------------------------------------------------------------------- # @@ -96,16 +115,14 @@ class TestInstallEmbedPython: mock_zip_instance = MagicMock() mock_zipfile.return_value.__enter__.return_value = mock_zip_instance - # Ensure cache doesn't exist by using tmp_path as cache dir - with patch.object(packtool, "DEFAULT_CACHE_DIR", str(tmp_path / ".cache")): - packtool.install_embed_python("3.10", output_dir) + packtool.install_embed_python("3.10", output_dir) - # Verify download was called - assert mock_urlretrieve.called - # Verify extraction was called - assert mock_zip_instance.extractall.called - # Verify output directory was created - assert output_dir.exists() + # Verify download was called + assert mock_urlretrieve.called + # Verify extraction was called + assert mock_zip_instance.extractall.called + # Verify output directory was created + assert output_dir.exists() def test_install_embed_python_with_cache(self, tmp_path: Path) -> None: """Should use cached Python if available.""" @@ -197,8 +214,8 @@ class TestInstallEmbedPython: packtool.install_embed_python("3.10", output_dir) - # Verify cache directory was created - Path(packtool.DEFAULT_CACHE_DIR) + # Verify cache directory was created (now in tmp_path) + cache_dir = Path(packtool.DEFAULT_CACHE_DIR) # Note: In test environment, cache might not persist due to mocking