Files
make-cad/test/test_os_compat.py
T
NiJingzhe 3dd8c77b8d refactor!: replace the part cache with the marimo notebook runtime
A SimpleCAD model is now a marimo notebook: a PEP 723 header with a
[tool.simplecadapi] table (id, revision, tolerance_profile, inputs), one
FTC block per cell, and the product is the top-level value whose id is the
notebook id. The notebook file is the only source of truth; .scadpkg is
only for exchange and publishing.

Runtime (simplecadapi.runtime):
- run_notebook(path, *, id=None, overrides=None, cache=True) and
  sca run <notebook.py> [--id] [--set NAME=VALUE] [--no-cache] [--out]
- use(path, id=..., **overrides) composes child notebooks; notebook_id()
  lets one notebook serve a part family under per-use ids
- each cell records into its own session; cells are the unit of caching,
  keyed by the notebook dependency digest (deps.json)
- marimo cell executor entry point, so marimo edit runs the same way
- shapes and read-only definition mappings pickle for the cell cache

Removed: the content-addressed part cache (simplecadapi.cache,
CachePolicy, CacheReport, sca cache ...), the incremental assembly solver
and its dependency/state modules, and part-interface diffs with latest-
state persistence. Part and assembly builds and the notebook projection
now share one definition writer (build/definition.py).

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-02 17:16:16 +08:00

83 lines
2.5 KiB
Python

import io
import os
from simplecadapi._internal.os_compat import harden_console_streams, with_binary_flag
from simplecadapi.build.dependencies import read_stable_file
def test_with_binary_flag_or_in_the_attribute_when_present(monkeypatch):
fake = 1 << 28
monkeypatch.setattr(os, "O_BINARY", fake, raising=False)
assert with_binary_flag(os.O_RDONLY) == os.O_RDONLY | fake
def test_with_binary_flag_passes_through_when_attribute_missing(monkeypatch):
monkeypatch.delattr(os, "O_BINARY", raising=False)
assert with_binary_flag(os.O_RDONLY) == os.O_RDONLY
def _spy_binary_open(monkeypatch):
fake = 1 << 28
real_open = os.open
seen = []
def spy(path, flags, *args, **kwargs):
seen.append(flags)
return real_open(path, flags, *args, **kwargs)
monkeypatch.setattr(os, "open", spy)
monkeypatch.setattr(os, "O_BINARY", fake, raising=False)
return seen, fake
def test_read_stable_file_opens_in_binary_mode(tmp_path, monkeypatch):
seen, fake = _spy_binary_open(monkeypatch)
target = tmp_path / "builder.py"
target.write_bytes(b"x = 1\r\ny = 2\r\n")
payload = read_stable_file(
target, max_bytes=1024, error_path="/file_input/path"
)
assert payload == b"x = 1\r\ny = 2\r\n"
assert seen and seen[0] & fake
def test_stable_source_bytes_opens_in_binary_mode(tmp_path, monkeypatch):
from simplecadapi.artifacts.feature_graph import _stable_source_bytes
seen, fake = _spy_binary_open(monkeypatch)
target = tmp_path / "source.py"
target.write_bytes(b"a = 1\n")
payload = _stable_source_bytes(target, max_bytes=1024, error_path="/source")
assert payload == b"a = 1\n"
assert seen and seen[0] & fake
def test_harden_console_streams_escapes_unencodable_diagnostics():
raw = io.BytesIO()
stream = io.TextIOWrapper(raw, encoding="ascii", errors="strict", newline="\n")
harden_console_streams((stream,))
stream.write("警告: wire fault\n")
stream.flush()
assert raw.getvalue() == b"\\u8b66\\u544a: wire fault\n"
def test_harden_console_streams_keeps_utf_streams_untouched():
stream = io.TextIOWrapper(io.BytesIO(), encoding="utf-8", errors="strict")
harden_console_streams((stream,))
assert stream.errors == "strict"
def test_harden_console_streams_tolerates_plain_and_closed_streams():
harden_console_streams((object(),))
closed = io.TextIOWrapper(io.BytesIO(), encoding="ascii")
closed.close()
harden_console_streams((closed,))