de368ea810
CI / Lint, Typecheck & Test (push) Successful in 1m11s
1. 删除 13 个已有 YAML 配置的 cli .py 入口脚本, 统一通过 pf 调用 2. gittool.yaml 用 CLEAN_EXCLUDES 数组变量配置 git clean 的 -e 参数, 保留 .venv/.tox/node_modules/.idea 等目录避免误删 3. run_cli 执行前打印调用信息: [gittool] 执行: c 4. 更新 pyproject.toml 移除 13 个冗余 entry points, 仅保留 pf 5. 清理测试文件中的 TestMain 类 (测 _ops 模块的测试保留)
111 lines
4.3 KiB
Python
111 lines
4.3 KiB
Python
"""Tests for cli.lscalc module."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from pyflowx.cli._ops import system
|
|
from pyflowx.conditions import Constants
|
|
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
# get_ls_dyna_command
|
|
# ---------------------------------------------------------------------- #
|
|
class TestGetLsDynaCommand:
|
|
"""Test get_ls_dyna_command function."""
|
|
|
|
def test_get_ls_dyna_command_windows(self) -> None:
|
|
"""Should get LS-DYNA command for Windows."""
|
|
with patch.object(Constants, "IS_WINDOWS", True), patch.object(Constants, "IS_MACOS", False):
|
|
cmd = system.get_ls_dyna_command("input.k", 4)
|
|
assert "ls-dyna_mpp" in cmd
|
|
assert "i=input.k" in cmd
|
|
assert "ncpu=4" in cmd
|
|
|
|
def test_get_ls_dyna_command_linux(self) -> None:
|
|
"""Should get LS-DYNA command for Linux."""
|
|
with patch.object(Constants, "IS_WINDOWS", False), patch.object(Constants, "IS_MACOS", False):
|
|
cmd = system.get_ls_dyna_command("input.k", 8)
|
|
assert "ls-dyna_mpp" in cmd
|
|
assert "i=input.k" in cmd
|
|
assert "ncpu=8" in cmd
|
|
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
# run_ls_dyna
|
|
# ---------------------------------------------------------------------- #
|
|
class TestRunLsDyna:
|
|
"""Test run_ls_dyna function."""
|
|
|
|
def test_run_ls_dyna_success(self, tmp_path: Path) -> None:
|
|
"""Should run LS-DYNA successfully."""
|
|
input_file = tmp_path / "input.k"
|
|
input_file.write_text("LS-DYNA input")
|
|
|
|
with patch("subprocess.run") as mock_run:
|
|
mock_run.return_value = MagicMock(returncode=0)
|
|
system.run_ls_dyna(str(input_file), ncpu=4)
|
|
assert mock_run.called
|
|
|
|
def test_run_ls_dyna_file_not_found(self, tmp_path: Path) -> None:
|
|
"""Should handle nonexistent input file."""
|
|
input_file = tmp_path / "nonexistent.k"
|
|
|
|
system.run_ls_dyna(str(input_file), ncpu=4)
|
|
# Should print error message
|
|
|
|
def test_run_ls_dyna_command_not_found(self, tmp_path: Path) -> None:
|
|
"""Should handle command not found."""
|
|
input_file = tmp_path / "input.k"
|
|
input_file.write_text("LS-DYNA input")
|
|
|
|
with patch("subprocess.run", side_effect=FileNotFoundError):
|
|
system.run_ls_dyna(str(input_file), ncpu=4)
|
|
# Should print error message
|
|
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
# run_ls_dyna_mpi
|
|
# ---------------------------------------------------------------------- #
|
|
class TestRunLsDynaMpi:
|
|
"""Test run_ls_dyna_mpi function."""
|
|
|
|
def test_run_ls_dyna_mpi_success(self, tmp_path: Path) -> None:
|
|
"""Should run LS-DYNA MPI successfully."""
|
|
input_file = tmp_path / "input.k"
|
|
input_file.write_text("LS-DYNA input")
|
|
|
|
with patch("subprocess.run") as mock_run:
|
|
mock_run.return_value = MagicMock(returncode=0)
|
|
system.run_ls_dyna_mpi(str(input_file), ncpu=8)
|
|
assert mock_run.called
|
|
|
|
def test_run_ls_dyna_mpi_file_not_found(self, tmp_path: Path) -> None:
|
|
"""Should handle nonexistent input file."""
|
|
input_file = tmp_path / "nonexistent.k"
|
|
|
|
system.run_ls_dyna_mpi(str(input_file), ncpu=8)
|
|
# Should print error message
|
|
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
# check_ls_dyna_status
|
|
# ---------------------------------------------------------------------- #
|
|
class TestCheckLsDynaStatus:
|
|
"""Test check_ls_dyna_status function."""
|
|
|
|
def test_check_ls_dyna_status_windows(self) -> None:
|
|
"""Should check LS-DYNA status on Windows."""
|
|
with patch.object(Constants, "IS_WINDOWS", True), patch("subprocess.run") as mock_run:
|
|
mock_run.return_value = MagicMock(stdout="ls-dyna_mpp.exe", returncode=0)
|
|
system.check_ls_dyna_status()
|
|
assert mock_run.called
|
|
|
|
def test_check_ls_dyna_status_linux(self) -> None:
|
|
"""Should check LS-DYNA status on Linux."""
|
|
with patch.object(Constants, "IS_WINDOWS", False), patch("subprocess.run") as mock_run:
|
|
mock_run.return_value = MagicMock(stdout="1234", returncode=0)
|
|
system.check_ls_dyna_status()
|
|
assert mock_run.called
|