test: 修复代码检查警告并优化测试用例
1. 为测试代码添加pyrefly忽略注释解决类型检查警告 2. 优化lambda参数命名为通配符符合PEP8规范 3. 增加断言检查任务函数非空并修正参数传递 4. 统一环境变量测试的命名和清理逻辑
This commit is contained in:
@@ -235,7 +235,7 @@ def test_is_running_windows_found(monkeypatch: pytest.MonkeyPatch):
|
||||
|
||||
monkeypatch.setattr(
|
||||
"subprocess.run",
|
||||
lambda *a, **kw: MockResult(),
|
||||
lambda *_, **__: MockResult(),
|
||||
)
|
||||
cond = BuiltinConditions.IS_RUNNING("explorer.exe")
|
||||
assert cond({}) is True
|
||||
@@ -252,7 +252,7 @@ def test_is_running_windows_not_found(monkeypatch: pytest.MonkeyPatch):
|
||||
|
||||
monkeypatch.setattr(
|
||||
"subprocess.run",
|
||||
lambda *a, **kw: MockResult(),
|
||||
lambda *_, **__: MockResult(),
|
||||
)
|
||||
cond = BuiltinConditions.IS_RUNNING("explorer.exe")
|
||||
assert cond({}) is False
|
||||
@@ -268,7 +268,7 @@ def test_is_running_linux_found(monkeypatch: pytest.MonkeyPatch):
|
||||
|
||||
monkeypatch.setattr(
|
||||
"subprocess.run",
|
||||
lambda *a, **kw: MockResult(),
|
||||
lambda *_, **__: MockResult(),
|
||||
)
|
||||
cond = BuiltinConditions.IS_RUNNING("nginx")
|
||||
assert cond({}) is True
|
||||
@@ -284,7 +284,7 @@ def test_is_running_linux_not_found(monkeypatch: pytest.MonkeyPatch):
|
||||
|
||||
monkeypatch.setattr(
|
||||
"subprocess.run",
|
||||
lambda *a, **kw: MockResult(),
|
||||
lambda *_, **__: MockResult(),
|
||||
)
|
||||
cond = BuiltinConditions.IS_RUNNING("nonexistent")
|
||||
assert cond({}) is False
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import sys
|
||||
from typing import Callable
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -326,7 +327,7 @@ def test_concurrency_key_thread() -> None:
|
||||
|
||||
order = []
|
||||
|
||||
def make(name: str) -> callable:
|
||||
def make(name: str) -> Callable[[], str]:
|
||||
def fn():
|
||||
order.append(f"{name}-start")
|
||||
time.sleep(0.1)
|
||||
@@ -374,7 +375,7 @@ def test_dependency_strategy_basic() -> None:
|
||||
"""dependency 策略应正确执行."""
|
||||
order = []
|
||||
|
||||
def make(name: str) -> callable:
|
||||
def make(name: str) -> Callable[[], str]:
|
||||
def fn():
|
||||
order.append(name)
|
||||
return name
|
||||
@@ -551,6 +552,7 @@ def test_unknown_strategy_raises() -> None:
|
||||
"""未知 strategy 应抛 ValueError."""
|
||||
graph = px.Graph.from_specs([px.TaskSpec("a", fn=lambda: 1)])
|
||||
with pytest.raises(ValueError, match="Unknown strategy"):
|
||||
# pyrefly: ignore [bad-argument-type]
|
||||
px.run(graph, strategy="unknown_strategy")
|
||||
|
||||
|
||||
|
||||
+30
-17
@@ -26,12 +26,13 @@ def test_clr_executes_on_linux(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(
|
||||
subprocess,
|
||||
"run",
|
||||
lambda cmd, **kw: ran.append(cmd),
|
||||
lambda *cmd, **__: ran.append(cmd),
|
||||
)
|
||||
|
||||
spec = clr()
|
||||
assert spec.fn is not None
|
||||
spec.fn()
|
||||
assert ran == [["clear"]]
|
||||
assert ran == [(["clear"],)]
|
||||
|
||||
|
||||
def test_clr_executes_on_windows(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
@@ -43,12 +44,13 @@ def test_clr_executes_on_windows(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(
|
||||
subprocess,
|
||||
"run",
|
||||
lambda cmd, **kw: ran.append(cmd),
|
||||
lambda *cmd, **__: ran.append(cmd),
|
||||
)
|
||||
|
||||
spec = clr()
|
||||
assert spec.fn is not None
|
||||
spec.fn()
|
||||
assert ran == [["cls"]]
|
||||
assert ran == [(["cls"],)]
|
||||
|
||||
|
||||
def test_reset_icon_cache_non_windows(monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]) -> None:
|
||||
@@ -83,31 +85,39 @@ def test_setenv_creates_task_spec() -> None:
|
||||
|
||||
def test_setenv_sets_environment_variable(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""setenv() 应设置环境变量。"""
|
||||
spec = setenv("TEST_VAR", "test_value")
|
||||
spec = setenv("PYFLOWX_TEST_VAR_1", "test_value")
|
||||
assert spec.fn is not None
|
||||
spec.fn()
|
||||
assert os.environ["TEST_VAR"] == "test_value"
|
||||
assert os.environ["PYFLOWX_TEST_VAR_1"] == "test_value"
|
||||
# Clean up
|
||||
del os.environ["PYFLOWX_TEST_VAR_1"]
|
||||
|
||||
|
||||
def test_setenv_default_not_overwrite(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""setenv(default=True) 不应覆盖已存在的环境变量。"""
|
||||
os.environ["TEST_VAR_EXISTS"] = "original"
|
||||
spec = setenv("TEST_VAR_EXISTS", "new_value", default=True)
|
||||
os.environ["PYFLOWX_TEST_VAR_EXISTS"] = "original"
|
||||
spec = setenv("PYFLOWX_TEST_VAR_EXISTS", "new_value", default=True)
|
||||
assert spec.fn is not None
|
||||
spec.fn()
|
||||
assert os.environ["TEST_VAR_EXISTS"] == "original"
|
||||
assert os.environ["PYFLOWX_TEST_VAR_EXISTS"] == "original"
|
||||
# Clean up
|
||||
del os.environ["PYFLOWX_TEST_VAR_EXISTS"]
|
||||
|
||||
|
||||
def test_setenv_default_sets_when_missing() -> None:
|
||||
"""setenv(default=True) 应在缺失时设置环境变量。"""
|
||||
# Ensure variable does not exist
|
||||
if "PYFLOWX_TEST_VAR_MISSING" in os.environ:
|
||||
del os.environ["PYFLOWX_TEST_VAR_MISSING"]
|
||||
var_name = "PYFLOWX_TEST_VAR_MISSING"
|
||||
if var_name in os.environ:
|
||||
del os.environ[var_name]
|
||||
|
||||
spec = setenv("PYFLOWX_TEST_VAR_MISSING", "default_value", default=True)
|
||||
spec = setenv(var_name, "default_value", default=True)
|
||||
assert spec.fn is not None
|
||||
spec.fn()
|
||||
assert os.environ["PYFLOWX_TEST_VAR_MISSING"] == "default_value"
|
||||
assert os.environ[var_name] == "default_value"
|
||||
|
||||
# Clean up after test
|
||||
del os.environ["PYFLOWX_TEST_VAR_MISSING"]
|
||||
del os.environ[var_name]
|
||||
|
||||
|
||||
def test_which_creates_task_spec() -> None:
|
||||
@@ -127,10 +137,11 @@ def test_which_linux_found(monkeypatch: pytest.MonkeyPatch, capsys: pytest.Captu
|
||||
monkeypatch.setattr(
|
||||
subprocess,
|
||||
"run",
|
||||
lambda cmd, **kw: MockResult(),
|
||||
lambda *_, **__: MockResult(),
|
||||
)
|
||||
|
||||
spec = which("python")
|
||||
assert spec.fn is not None
|
||||
spec.fn()
|
||||
captured = capsys.readouterr()
|
||||
assert "python ->" in captured.out
|
||||
@@ -148,10 +159,11 @@ def test_which_windows_found(monkeypatch: pytest.MonkeyPatch, capsys: pytest.Cap
|
||||
monkeypatch.setattr(
|
||||
subprocess,
|
||||
"run",
|
||||
lambda cmd, **kw: MockResult(),
|
||||
lambda *_, **__: MockResult(),
|
||||
)
|
||||
|
||||
spec = which("python")
|
||||
assert spec.fn is not None
|
||||
spec.fn()
|
||||
captured = capsys.readouterr()
|
||||
assert "python ->" in captured.out
|
||||
@@ -169,10 +181,11 @@ def test_which_not_found(monkeypatch: pytest.MonkeyPatch, capsys: pytest.Capture
|
||||
monkeypatch.setattr(
|
||||
subprocess,
|
||||
"run",
|
||||
lambda cmd, **kw: MockResult(),
|
||||
lambda *_, **__: MockResult(),
|
||||
)
|
||||
|
||||
spec = which("nonexistent_cmd")
|
||||
assert spec.fn is not None
|
||||
spec.fn()
|
||||
captured = capsys.readouterr()
|
||||
assert "nonexistent_cmd -> 未找到" in captured.out
|
||||
|
||||
@@ -127,6 +127,7 @@ def test_should_execute_condition_exception_returns_false() -> None:
|
||||
spec = TaskSpec("a", _fn, conditions=(bad_condition,))
|
||||
should_run, reason = spec.should_execute({})
|
||||
assert should_run is False
|
||||
# pyrefly: ignore [not-iterable]
|
||||
assert "匿名条件(执行错误)" in reason
|
||||
|
||||
|
||||
@@ -135,6 +136,7 @@ def test_should_execute_condition_lambda_name() -> None:
|
||||
spec = TaskSpec("a", _fn, conditions=(lambda _ctx: False,))
|
||||
should_run, reason = spec.should_execute({})
|
||||
assert should_run is False
|
||||
# pyrefly: ignore [not-iterable]
|
||||
assert "<lambda>" in reason
|
||||
|
||||
|
||||
@@ -143,6 +145,7 @@ def test_should_execute_skip_if_missing_cmd_not_found() -> None:
|
||||
spec = TaskSpec("a", cmd=["nonexistent_cmd_xyz"], skip_if_missing=True)
|
||||
should_run, reason = spec.should_execute({})
|
||||
assert should_run is False
|
||||
# pyrefly: ignore [not-iterable]
|
||||
assert "命令不存在" in reason
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user