first commit
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
import subprocess
|
||||
import sys
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from tests.python.support.paths import add_repo_path, repo_path
|
||||
|
||||
add_repo_path("skills/dxf/scripts")
|
||||
|
||||
from dxf import cli as dxf
|
||||
|
||||
|
||||
class DxfCliTests(unittest.TestCase):
|
||||
def test_requires_explicit_target(self) -> None:
|
||||
with self.assertRaises(SystemExit) as cm:
|
||||
dxf.main([])
|
||||
self.assertEqual(2, cm.exception.code)
|
||||
|
||||
def test_passes_targets_in_order(self) -> None:
|
||||
with mock.patch.object(dxf, "generate_dxf_targets", return_value=0) as generate:
|
||||
self.assertEqual(0, dxf.main(["drawings/second.py", "drawings/first.py"]))
|
||||
|
||||
generate.assert_called_once_with(["drawings/second.py", "drawings/first.py"], output=None, verbose=False)
|
||||
|
||||
def test_passes_verbose_flag(self) -> None:
|
||||
with mock.patch.object(dxf, "generate_dxf_targets", return_value=0) as generate:
|
||||
self.assertEqual(0, dxf.main(["drawings/part.py", "--verbose"]))
|
||||
|
||||
generate.assert_called_once_with(["drawings/part.py"], output=None, verbose=True)
|
||||
|
||||
def test_passes_output_flag(self) -> None:
|
||||
with mock.patch.object(dxf, "generate_dxf_targets", return_value=0) as generate:
|
||||
self.assertEqual(0, dxf.main(["drawings/part.py", "-o", "DXF/part.dxf"]))
|
||||
|
||||
generate.assert_called_once_with(["drawings/part.py"], output="DXF/part.dxf", verbose=False)
|
||||
|
||||
def test_output_flag_rejects_multiple_targets(self) -> None:
|
||||
with self.assertRaises(SystemExit) as cm:
|
||||
dxf.main(["drawings/first.py", "drawings/second.py", "-o", "DXF/first.dxf"])
|
||||
self.assertEqual(2, cm.exception.code)
|
||||
|
||||
def test_scripts_dxf_directory_invokes_cli(self) -> None:
|
||||
skill_root = repo_path("skills/dxf")
|
||||
result = subprocess.run(
|
||||
[sys.executable, "scripts/dxf", "--help"],
|
||||
cwd=skill_root,
|
||||
check=False,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
text=True,
|
||||
)
|
||||
self.assertEqual("", result.stderr)
|
||||
self.assertEqual(0, result.returncode)
|
||||
self.assertIn("usage: dxf", result.stdout)
|
||||
self.assertIn("--output", result.stdout)
|
||||
|
||||
def test_cli_import_does_not_import_heavy_cad_modules(self) -> None:
|
||||
skill_root = repo_path("skills/dxf")
|
||||
code = (
|
||||
"import sys; sys.path.insert(0, 'scripts'); import dxf.cli; "
|
||||
"print('OCP.OCP' in sys.modules); "
|
||||
"print('cadpy.step_scene' in sys.modules)"
|
||||
)
|
||||
result = subprocess.run(
|
||||
[sys.executable, "-c", code],
|
||||
cwd=skill_root,
|
||||
check=False,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
text=True,
|
||||
)
|
||||
self.assertEqual("", result.stderr)
|
||||
self.assertEqual(0, result.returncode)
|
||||
self.assertEqual(["False", "False"], result.stdout.strip().splitlines())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,37 @@
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
import ezdxf
|
||||
|
||||
from tests.python.support.paths import add_repo_path
|
||||
from tests.python.support.tmp_root import temporary_directory
|
||||
|
||||
add_repo_path("skills/dxf/scripts")
|
||||
|
||||
from dxf.render_payload import build_dxf_render_payload
|
||||
|
||||
|
||||
class CadpyDxfTests(unittest.TestCase):
|
||||
def test_build_dxf_render_payload_supports_straight_lwpolyline(self) -> None:
|
||||
with temporary_directory(prefix="tmp-cad-dxf-") as tmpdir:
|
||||
dxf_path = Path(tmpdir) / "outline.dxf"
|
||||
doc = ezdxf.new("R2010")
|
||||
modelspace = doc.modelspace()
|
||||
modelspace.add_lwpolyline(
|
||||
[(0.0, 0.0), (10.0, 0.0), (10.0, 5.0), (0.0, 5.0)],
|
||||
close=True,
|
||||
dxfattribs={"layer": "CUT"},
|
||||
)
|
||||
doc.saveas(dxf_path)
|
||||
|
||||
payload = build_dxf_render_payload(dxf_path, file_ref="test/outline.dxf")
|
||||
|
||||
self.assertEqual(4, payload["counts"]["paths"])
|
||||
self.assertEqual(0, payload["counts"]["circles"])
|
||||
self.assertEqual(4, len(payload["geometry"]["lines"]))
|
||||
self.assertEqual(10.0, payload["bounds"]["width"])
|
||||
self.assertEqual(5.0, payload["bounds"]["height"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,77 @@
|
||||
import textwrap
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from cadpy import catalog as cad_catalog
|
||||
from cadpy import generation as cad_generation
|
||||
from cadpy.metadata import parse_generator_metadata
|
||||
from tests.python.support.tmp_root import temporary_directory
|
||||
|
||||
STANDALONE_DXF_SOURCE = textwrap.dedent(
|
||||
'''
|
||||
"""Standalone DXF drafting source."""
|
||||
|
||||
import ezdxf
|
||||
|
||||
|
||||
def gen_dxf():
|
||||
doc = ezdxf.new()
|
||||
msp = doc.modelspace()
|
||||
msp.add_lwpolyline([(0, 0), (40, 0), (40, 20), (0, 20)], close=True)
|
||||
return doc
|
||||
'''
|
||||
).strip()
|
||||
|
||||
|
||||
def _write_standalone_source(root: Path, stem: str = "outline") -> Path:
|
||||
script_path = root / f"{stem}.py"
|
||||
script_path.write_text(STANDALONE_DXF_SOURCE + "\n")
|
||||
return script_path
|
||||
|
||||
|
||||
class StandaloneDxfSourceTests(unittest.TestCase):
|
||||
def test_metadata_allows_gen_dxf_without_gen_step(self) -> None:
|
||||
with temporary_directory(prefix="dxf-skill") as root:
|
||||
script_path = _write_standalone_source(Path(root))
|
||||
metadata = parse_generator_metadata(script_path)
|
||||
|
||||
assert metadata is not None
|
||||
self.assertTrue(metadata.has_gen_dxf)
|
||||
self.assertFalse(metadata.has_gen_step)
|
||||
self.assertIsNone(metadata.kind)
|
||||
|
||||
def test_metadata_still_requires_gen_step_for_urdf_and_sdf(self) -> None:
|
||||
with temporary_directory(prefix="dxf-skill") as root:
|
||||
script_path = Path(root) / "robot.py"
|
||||
script_path.write_text("def gen_urdf():\n return '<robot/>'\n")
|
||||
with self.assertRaisesRegex(ValueError, "require gen_step"):
|
||||
parse_generator_metadata(script_path)
|
||||
|
||||
def test_explicit_target_resolves_dxf_only_source(self) -> None:
|
||||
with temporary_directory(prefix="dxf-skill") as root:
|
||||
script_path = _write_standalone_source(Path(root))
|
||||
source = cad_catalog.source_from_path(script_path)
|
||||
|
||||
assert source is not None
|
||||
self.assertEqual("dxf", source.kind)
|
||||
self.assertIsNone(source.step_path)
|
||||
self.assertEqual(script_path.with_suffix(".dxf"), source.dxf_path)
|
||||
|
||||
def test_directory_catalog_skips_dxf_only_source(self) -> None:
|
||||
with temporary_directory(prefix="dxf-skill") as root:
|
||||
_write_standalone_source(Path(root))
|
||||
self.assertEqual((), cad_catalog.iter_cad_sources(Path(root)))
|
||||
|
||||
def test_generate_dxf_targets_writes_sibling_output(self) -> None:
|
||||
with temporary_directory(prefix="dxf-skill") as root:
|
||||
script_path = _write_standalone_source(Path(root))
|
||||
|
||||
self.assertEqual(0, cad_generation.generate_dxf_targets([str(script_path)]))
|
||||
|
||||
output_path = script_path.with_suffix(".dxf")
|
||||
self.assertTrue(output_path.exists())
|
||||
self.assertGreater(output_path.stat().st_size, 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user