"""Runtime compilation, installation, and destination protection.""" from pathlib import Path import pytest from simplecadapi.cli import main, run from simplecadapi.skill import cli from simplecadapi.skill.compiler import SkillBuildError, load_project @pytest.fixture def project(tmp_path, monkeypatch): root = tmp_path / "project" source = root / "source" source.mkdir(parents=True) (source / "SKILL.md").write_text( "---\nname: demo\n---\nshared\n\nzcode-only\n\n", encoding="utf-8", ) (source / "asset.bin").write_bytes(b"\xff\x00") (root / "skillproj.toml").write_text( '[skill]\nname="demo"\nsource="source"\n' '[build]\ntargets=["zcode", "codex"]\n' '[build.output]\nzcode="out/zcode"\ncodex="out/codex"\n', encoding="utf-8", ) result = load_project(root) monkeypatch.setattr(cli, "bundled_project", lambda: result) monkeypatch.setattr(Path, "home", lambda: tmp_path / "home") monkeypatch.delenv("SCA_SKILLS_DIR", raising=False) monkeypatch.delenv("SCA_ADDON_HOME", raising=False) return result def install_args(*extra): return ["skill", "install", "--target", "zcode", *extra] def test_targets(project): report, code, printer = run(["skill", "targets"]) assert report == {"targets": ["zcode", "codex"]} assert code == 0 and callable(printer) printer(report) @pytest.mark.parametrize("target, included", [("zcode", True), ("codex", False)]) def test_install_compiles_selected_target(project, tmp_path, target, included): report, code, _ = run( ["skill", "install", "--target", target, "--skills-dir", str(tmp_path / "skills")] ) output = tmp_path / "skills" / "demo" text = (output / "SKILL.md").read_text() assert ("zcode-only" in text) == included assert "\nx\n\n") with pytest.raises(SkillBuildError, match="unknown target"): run(install_args("--skills-dir", str(skills_dir), "--force")) assert (output / "SKILL.md").read_bytes() == original def test_main_reports_unknown_target_without_traceback(project, tmp_path, capsys): code = main(["skill", "install", "--target", "typo", "--skills-dir", str(tmp_path / "s")]) assert code == 2 error = capsys.readouterr().err assert "unknown target" in error and "Traceback" not in error assert not (tmp_path / "s").exists()