ModelHandle/tools/step_service/tests/test_freecad_worker.py
sladro 16a2e43649
Some checks failed
Build / build (18.x, macos-latest) (push) Has been cancelled
Build / build (18.x, ubuntu-latest) (push) Has been cancelled
Build / build (18.x, windows-latest) (push) Has been cancelled
Build / build (20.x, macos-latest) (push) Has been cancelled
Build / build (20.x, ubuntu-latest) (push) Has been cancelled
Build / build (20.x, windows-latest) (push) Has been cancelled
fix step save flow and add portable backend runtime support
2026-04-13 15:16:30 +08:00

65 lines
2.9 KiB
Python

import pathlib
import subprocess
import tempfile
import unittest
from unittest import mock
from tools.step_service.freecad_worker import FreeCADWorker
class FreeCADWorkerTests(unittest.TestCase):
def test_prefers_project_local_freecad_command(self):
with tempfile.TemporaryDirectory() as temp_dir:
project_root = temp_dir
freecad_cmd = mock.Mock()
freecad_cmd.exists.return_value = True
freecad_cmd.__fspath__ = mock.Mock(return_value="D:/portable/freecad/bin/FreeCADCmd.exe")
worker = FreeCADWorker(project_root=project_root)
with mock.patch.object(worker, "_get_project_freecad_cmd", return_value=freecad_cmd):
self.assertEqual(worker.get_freecad_cmd(), freecad_cmd)
def test_falls_back_to_system_command_when_project_runtime_is_missing(self):
with tempfile.TemporaryDirectory() as temp_dir:
worker = FreeCADWorker(project_root=temp_dir)
with mock.patch.object(worker, "_get_project_freecad_cmd", return_value=None):
self.assertEqual(worker.get_freecad_cmd(), "FreeCADCmd")
def test_detects_freecad_runtime_with_versioned_folder_name(self):
with tempfile.TemporaryDirectory() as temp_dir:
project_root_path = pathlib.Path(temp_dir)
command_path = project_root_path / "runtime" / "FreeCAD 1.0" / "bin" / "freecadcmd.exe"
command_path.parent.mkdir(parents=True)
command_path.write_text("", encoding="utf-8")
worker = FreeCADWorker(project_root=project_root_path)
self.assertEqual(worker.get_freecad_cmd(), command_path)
@mock.patch("tools.step_service.freecad_worker.subprocess.run")
def test_passes_script_arguments_through_freecad(self, run_mock):
run_mock.return_value = subprocess.CompletedProcess(args=[], returncode=0, stdout="", stderr="")
worker = FreeCADWorker(freecad_cmd="FreeCADCmd")
with tempfile.TemporaryDirectory() as temp_dir:
with mock.patch("tools.step_service.freecad_worker.tempfile.TemporaryDirectory") as temp_dir_mock:
temp_dir_mock.return_value.__enter__.return_value = temp_dir
temp_dir_mock.return_value.__exit__.return_value = False
with mock.patch.object(worker, "get_freecad_cmd", return_value="FreeCADCmd"):
with mock.patch("pathlib.Path.read_bytes", return_value=b"STEPDATA"):
with mock.patch("pathlib.Path.exists", return_value=True):
output_bytes, output_name = worker.trim_step_file("demo.step", b"INPUT", ["0/1"])
self.assertEqual(output_bytes, b"STEPDATA")
self.assertEqual(output_name, "demo-edited.step")
command = run_mock.call_args.args[0]
self.assertIn("--pass", command)
self.assertEqual(command[0], "FreeCADCmd")
if __name__ == "__main__":
unittest.main()