Files
make-cad/test/test_sca_cli.py
T
NiJingzhe b939f44043 feat(cli): single 'sca' entry point + 'sca init' shell integration
- simplecad-cache/simplecad-export entry points are gone (hard cut):
  the same commands live under the unified dispatcher
  (simplecadapi.cli) as 'sca cache ...' and 'sca export ...', each
  group registered at parser level so per-group --help works
- sca init now wires the shell by default (--no-shell skips): a
  ~/.sca/bin/sca shim (Windows sca.cmd) execs the installing venv's
  console script — the venv bin itself never goes on PATH — and the
  shim dir lands on PATH via a marked, removable block in ~/.zshenv
  (read by non-interactive shells, so agents see the command),
  ~/.bashrc, fish conf.d, or the Windows user PATH registry value
  with a change broadcast; rc files are created only for the login
  shell or when they already exist; re-running init refreshes the
  shim (target discovery is symlink-safe, no resolve() out of venv)
- addon/cache/export cli modules expose configure(subparsers) +
  _execute(args); module run() keeps the CLI-shaped argv
- docs: sca namespace across evergreen docs, shell-integration
  section, 'never writes shell profiles' contract line retired
- tests: +17 (dispatcher groups, shell integration, entry-point
  absence); cache/export suites moved to group-token argv
2026-09-13 04:19:17 +08:00

82 lines
2.4 KiB
Python

"""Tests for the unified ``sca`` entry point (:mod:`simplecadapi.cli`)."""
from importlib.metadata import entry_points
from pathlib import Path
import pytest
from simplecadapi.cli import run
@pytest.fixture(autouse=True)
def fake_user_home(tmp_path, monkeypatch):
monkeypatch.setattr(Path, "home", lambda: tmp_path)
monkeypatch.delenv("SCA_ADDON_HOME", raising=False)
monkeypatch.delenv("SCA_SKILLS_DIR", raising=False)
return tmp_path
def test_init_group_runs_with_shell_integration(tmp_path):
report, code, printer = run(["init"])
assert code == 0
assert Path(report["home"]) == tmp_path / ".sca" / "addons"
assert report["shell"]["shim"]
assert (tmp_path / ".zshenv").is_file()
assert callable(printer)
def test_init_no_shell_writes_no_shim(tmp_path):
report, code, _ = run(["init", "--no-shell"])
assert code == 0
assert report["shell"] == {"skipped": True}
assert not (tmp_path / ".sca" / "bin").exists()
def test_addon_group_dispatches(tmp_path):
run(["init"])
report, code, _ = run(["addon", "list"])
assert code == 0
assert report["addons"] == []
def test_cache_group_dispatches(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
report, code, printer = run(
["cache", "--cache-dir", str(tmp_path / "cache"), "status"]
)
assert code == 0
assert isinstance(report, dict)
assert callable(printer)
def test_export_group_dispatches(tmp_path, monkeypatch):
from simplecadapi.exporter import cli as export_cli
package = type("P", (), {"root_kind": "part", "root_id": "part-bracket"})
monkeypatch.setattr(export_cli, "read_product_package", lambda value: package)
pkg = tmp_path / "bracket.scadpkg"
pkg.write_bytes(b"stub")
report, code, _ = run(
["export", str(pkg), "--check", "--output-dir", str(tmp_path / "out")]
)
assert code == 0
assert report["ok"] is True
assert set(report["formats"]) == {"step", "stl", "obj"}
def test_version_reports_the_sdk_version(capsys):
from simplecadapi.addon.install import sca_version
with pytest.raises(SystemExit) as excinfo:
run(["--version"])
assert excinfo.value.code == 0
assert sca_version() in capsys.readouterr().out
def test_legacy_entry_points_are_gone():
scripts = {ep.name for ep in entry_points(group="console_scripts")}
assert "sca" in scripts
assert "simplecad-cache" not in scripts
assert "simplecad-export" not in scripts