The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
store: test runtimes.sh and its copy keeper; package commands fall back to Harness's Node
dee00834854ebbd
diff --git a/cli/src/dsh/shell.spec.ts b/cli/src/dsh/shell.spec.tsindex e52bca860..fe45adc5e 100644--- a/cli/src/dsh/shell.spec.ts+++ b/cli/src/dsh/shell.spec.ts@@ -1,8 +1,8 @@import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import { EventEmitter } from 'node:events'-import { mkdtempSync, realpathSync, rmSync } from 'node:fs'+import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs'import { tmpdir } from 'node:os'-import { join } from 'node:path'+import { dirname, join } from 'node:path'import type { ChildProcess } from 'node:child_process'// Two switches, both off by default: `sh` makes commands run under plain `/bin/sh -c` (as when no user@@ -18,7 +18,8 @@ vi.mock('node:child_process', async (importOriginal) => {return { ...actual, spawn, default: { ...actual, spawn } }})-const { dshShellArgv, isShellNoise, killProcessGroup, runDshCommand, spawnDshCommand } = await import('./shell.js')+const { managedNodePath } = await import('../lib/nodeRuntime.js')+const { dshNodeFallback, dshShellArgv, isShellNoise, killProcessGroup, runDshCommand, spawnDshCommand } = await import('./shell.js')describe('dshShellArgv', () => {const original = process.env.SHELL@@ -26,24 +27,50 @@ describe('dshShellArgv', () => {it('runs a bash user\'s setup and doctor as a LOGIN shell, where .bash_profile (and nvm) live', () => {process.env.SHELL = '/bin/bash'- expect(dshShellArgv('./doctor.sh')).toEqual({ path: '/bin/bash', args: ['-lic', './doctor.sh'] })+ expect(dshShellArgv('./doctor.sh')).toEqual({ path: '/bin/bash', args: ['-lic', `${dshNodeFallback()}\n./doctor.sh`] })})it('keeps zsh as it already was', () => {process.env.SHELL = '/bin/zsh'- expect(dshShellArgv('./doctor.sh')).toEqual({ path: '/bin/zsh', args: ['-lic', './doctor.sh'] })+ expect(dshShellArgv('./doctor.sh')).toEqual({ path: '/bin/zsh', args: ['-lic', `${dshNodeFallback()}\n./doctor.sh`] })})it('falls back to /bin/sh -c when no user shell is known', () => {seams.sh = truetry {- expect(dshShellArgv('./doctor.sh')).toEqual({ path: '/bin/sh', args: ['-c', './doctor.sh'] })+ expect(dshShellArgv('./doctor.sh')).toEqual({ path: '/bin/sh', args: ['-c', `${dshNodeFallback()}\n./doctor.sh`] })} finally {seams.sh = false}})})+describe('dshNodeFallback', () => {+ let dir: string+ beforeEach(() => { seams.sh = true; dir = realpathSync(mkdtempSync(join(tmpdir(), 'dsh-node-'))) })+ afterEach(() => { seams.sh = false; rmSync(dir, { recursive: true, force: true }) })++ it('puts the Node this daemon runs on at the END of a PATH that has no node', async () => {+ const result = await runDshCommand('echo "$PATH"; command -v node', { cwd: dir, env: { PATH: '/usr/bin:/bin' } })+ const runtimeBin = dirname(managedNodePath())+ expect(result.lines).toEqual([`/usr/bin:/bin:${runtimeBin}`, join(runtimeBin, 'node')])+ })++ it('leaves a PATH that already has a node alone', async () => {+ const own = join(dir, 'bin')+ mkdirSync(own)+ writeFileSync(join(own, 'node'), '#!/bin/sh\n', { mode: 0o755 })+ const result = await runDshCommand('echo "$PATH"; command -v node', { cwd: dir, env: { PATH: `${own}:/usr/bin:/bin` } })+ expect(result.lines).toEqual([`${own}:/usr/bin:/bin`, join(own, 'node')])+ })++ it('is one line, run before the command, that keeps an empty PATH from starting with a colon', () => {+ const line = dshNodeFallback()+ expect(line).toContain('"${PATH:+$PATH:}"\'')+ expect(line.startsWith('if ! command -v node >/dev/null 2>&1; then PATH=')).toBe(true)+ })+})+describe('isShellNoise', () => {it('drops only what zsh says about zle without a terminal', () => {expect(isShellNoise("zsh: can't change option: zle")).toBe(true)diff --git a/cli/src/dsh/shell.ts b/cli/src/dsh/shell.tsindex 5255e72a1..915be7631 100644--- a/cli/src/dsh/shell.ts+++ b/cli/src/dsh/shell.ts@@ -5,7 +5,9 @@* same shell selection) `engineLaunch.ts` uses to exec an engine in a pane.*/import { spawn, type ChildProcess } from 'node:child_process'-import { interactiveEngineShell } from '../lib/engineLaunch.js'+import { dirname } from 'node:path'+import { interactiveEngineShell, shellSingleQuote } from '../lib/engineLaunch.js'+import { managedNodePath } from '../lib/nodeRuntime.js'export interface DshCommandOptions {cwd: string@@ -45,11 +47,25 @@ export function isShellNoise(line: string): boolean {*/export function dshShellArgv(script: string): { path: string; args: string[] } {const shell = interactiveEngineShell()+ const body = `${dshNodeFallback()}\n${script}`if (shell) {const args = shell.args.map((a) => (a === '-ic' ? '-lic' : a))- return { path: shell.path, args: [...args, script] }+ return { path: shell.path, args: [...args, body] }}- return { path: '/bin/sh', args: ['-c', script] }+ return { path: '/bin/sh', args: ['-c', body] }+}++/**+ * The line run before a DSH's command: when the login shell's PATH has no `node`, the Node this+ * daemon runs on joins the END of it. Since the product moved to a private runtime, a machine with no+ * node on PATH is the normal case (see nodeRuntime.ts), and a package whose setup says `npm ci` or+ * whose viewer is `node viewer.mjs` would otherwise fail on exactly the machines Harness set up+ * itself. Appended, not prepended, so a node the person installed always wins; and run after the rc+ * files, so a profile that assigns PATH outright cannot drop it.+ */+export function dshNodeFallback(): string {+ const bin = shellSingleQuote(dirname(managedNodePath()))+ return `if ! command -v node >/dev/null 2>&1; then PATH="\${PATH:+$PATH:}"${bin}; export PATH; fi`}export function spawnDshCommand(script: string, opts: { cwd: string; env?: Record<string, string> }): ChildProcess {diff --git a/cli/src/dsh/store.spec.ts b/cli/src/dsh/store.spec.tsindex 367f63835..eeb9f1599 100644--- a/cli/src/dsh/store.spec.ts+++ b/cli/src/dsh/store.spec.ts@@ -114,4 +114,11 @@ describe('the built-in shelf (store/)', () => {resetBundledDshRegistry()}})++ it('every package that sources runtimes.sh carries the canonical copy', async () => {+ // A package installs alone, so each carries store/tools/runtimes.sh; a stale copy is a package+ // that still asks a fresh machine for an interpreter the helper has since learnt to fetch.+ const { check } = await import(join(STORE, 'tools', 'sync-runtimes.mjs')) as { check: (root?: string) => string[] }+ expect(check(STORE), 'run: node store/tools/sync-runtimes.mjs').toEqual([])+ })})diff --git a/cli/src/lib/engineLaunch.ts b/cli/src/lib/engineLaunch.tsindex ae61b62a3..bda91903e 100644--- a/cli/src/lib/engineLaunch.ts+++ b/cli/src/lib/engineLaunch.ts@@ -243,7 +243,7 @@ function installThenExecScript(install: string): string {].join('\n')}-function shellSingleQuote(value: string): string {+export function shellSingleQuote(value: string): string {return `'${value.replace(/'/g, `'"'"'`)}'`}diff --git a/store/agents/blender/toolchain/runtimes.sh b/store/agents/blender/toolchain/runtimes.shindex 7fe40936c..6c8556043 100644--- a/store/agents/blender/toolchain/runtimes.sh+++ b/store/agents/blender/toolchain/runtimes.sh@@ -67,7 +67,7 @@ _harness_node_at_least() {}harness_node() {- local min="${1:-18}" recorded dir+ local min="${1:-18}" recorded_harness_node_at_least "$min" && return 0recorded="$(cat "$HARNESS_RUNTIME/current-node" 2>/dev/null || true)"if [ -n "$recorded" ] && [ -x "${recorded%/*}/node" ]; then@@ -105,7 +105,8 @@ harness_uv() {tar -xzf "$tmp/uv.tar.gz" -C "$tmp" && chmod +x "$tmp/$asset/uv" "$tmp/$asset/uvx" \|| { rm -rf "$tmp"; echo "miss the uv archive would not unpack"; return 1; }# Two installs at once both get here; whichever renames first wins and the other's copy goes.- mv "$tmp/$asset" "$dir" 2>/dev/null || true+ # (mv onto a directory that exists would nest the copy inside it, hence the test.)+ [ -e "$dir" ] || mv "$tmp/$asset" "$dir" 2>/dev/null || truerm -rf "$tmp"[ -x "$dir/uv" ] || { echo "miss uv did not land in $dir"; return 1; }fi@@ -156,7 +157,8 @@ harness_micromamba() {echo " fetching micromamba $HARNESS_MICROMAMBA_VERSION into $HARNESS_RUNTIME (native libraries PyPI has no wheel for)"mkdir -p "$dir" || { echo "miss cannot write $dir"; return 1; }_harness_fetch "https://github.com/mamba-org/micromamba-releases/releases/download/$HARNESS_MICROMAMBA_VERSION/$asset" "$dir/micromamba.$$" "$sum" || return 1- chmod +x "$dir/micromamba.$$" && mv -f "$dir/micromamba.$$" "$dir/micromamba"+ chmod +x "$dir/micromamba.$$" && mv -f "$dir/micromamba.$$" "$dir/micromamba" \+ || { rm -f "$dir/micromamba.$$"; echo "miss micromamba would not install into $dir"; return 1; }fiHARNESS_MICROMAMBA="$dir/micromamba"export HARNESS_MICROMAMBAdiff --git a/store/tools/runtimes.sh b/store/tools/runtimes.shindex 7fe40936c..6c8556043 100644--- a/store/tools/runtimes.sh+++ b/store/tools/runtimes.sh@@ -67,7 +67,7 @@ _harness_node_at_least() {}harness_node() {- local min="${1:-18}" recorded dir+ local min="${1:-18}" recorded_harness_node_at_least "$min" && return 0recorded="$(cat "$HARNESS_RUNTIME/current-node" 2>/dev/null || true)"if [ -n "$recorded" ] && [ -x "${recorded%/*}/node" ]; then@@ -105,7 +105,8 @@ harness_uv() {tar -xzf "$tmp/uv.tar.gz" -C "$tmp" && chmod +x "$tmp/$asset/uv" "$tmp/$asset/uvx" \|| { rm -rf "$tmp"; echo "miss the uv archive would not unpack"; return 1; }# Two installs at once both get here; whichever renames first wins and the other's copy goes.- mv "$tmp/$asset" "$dir" 2>/dev/null || true+ # (mv onto a directory that exists would nest the copy inside it, hence the test.)+ [ -e "$dir" ] || mv "$tmp/$asset" "$dir" 2>/dev/null || truerm -rf "$tmp"[ -x "$dir/uv" ] || { echo "miss uv did not land in $dir"; return 1; }fi@@ -156,7 +157,8 @@ harness_micromamba() {echo " fetching micromamba $HARNESS_MICROMAMBA_VERSION into $HARNESS_RUNTIME (native libraries PyPI has no wheel for)"mkdir -p "$dir" || { echo "miss cannot write $dir"; return 1; }_harness_fetch "https://github.com/mamba-org/micromamba-releases/releases/download/$HARNESS_MICROMAMBA_VERSION/$asset" "$dir/micromamba.$$" "$sum" || return 1- chmod +x "$dir/micromamba.$$" && mv -f "$dir/micromamba.$$" "$dir/micromamba"+ chmod +x "$dir/micromamba.$$" && mv -f "$dir/micromamba.$$" "$dir/micromamba" \+ || { rm -f "$dir/micromamba.$$"; echo "miss micromamba would not install into $dir"; return 1; }fiHARNESS_MICROMAMBA="$dir/micromamba"export HARNESS_MICROMAMBAdiff --git a/store/tools/sync-runtimes.mjs b/store/tools/sync-runtimes.mjsindex 726a7c156..0f7098082 100644--- a/store/tools/sync-runtimes.mjs+++ b/store/tools/sync-runtimes.mjs@@ -9,7 +9,7 @@// node store/tools/sync-runtimes.mjs --check exit 1, naming each copy that differs or is missing//// "Missing" means a script in the package sources runtimes.sh and the package has no copy of it.-import { existsSync, readFileSync, readdirSync, writeFileSync } from 'node:fs'+import { existsSync, mkdirSync, readFileSync, readdirSync, realpathSync, writeFileSync } from 'node:fs'import { dirname, join, relative } from 'node:path'import { fileURLToPath } from 'node:url'@@ -38,7 +38,7 @@ export function sourcesRuntimes(dir) {const p = join(d, e.name)if (e.isDirectory()) { if (depth < 2 && walk(p, depth + 1)) return true; continue }if (!e.name.endsWith('.sh') || e.name === 'runtimes.sh') continue- if (/^\s*(\.|source)\s+\S*runtimes\.sh/m.test(readFileSync(p, 'utf8'))) return true+ if (/^\s*(\.|source)\s+.*runtimes\.sh/m.test(readFileSync(p, 'utf8'))) return true}return false}@@ -64,13 +64,15 @@ export function sync(root = store) {for (const { dir, copy } of packages(root)) {if (!existsSync(copy) && !sourcesRuntimes(dir)) continueif (existsSync(copy) && readFileSync(copy, 'utf8') === canonical) continue+ mkdirSync(dirname(copy), { recursive: true })writeFileSync(copy, canonical)written.push(relative(root, copy))}return written}-if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {+// Run as a command (not imported): argv[1] may reach this file through a symlink, /var → /private/var.+if (process.argv[1] && fileURLToPath(import.meta.url) === realpathSync(process.argv[1])) {if (process.argv.includes('--check')) {const problems = check()for (const p of problems) console.error(p)diff --git a/store/tools/sync-runtimes.test.mjs b/store/tools/sync-runtimes.test.mjsnew file mode 100644index 000000000..4e02535b7--- /dev/null+++ b/store/tools/sync-runtimes.test.mjs@@ -0,0 +1,89 @@+// node --test store/tools/sync-runtimes.test.mjs — the copy keeper, on a temporary store.+import { test } from 'node:test'+import assert from 'node:assert/strict'+import { execFileSync } from 'node:child_process'+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync, existsSync } from 'node:fs'+import { tmpdir } from 'node:os'+import { join } from 'node:path'+import { fileURLToPath } from 'node:url'+import { check, packages, sourcesRuntimes, sync } from './sync-runtimes.mjs'++const CANONICAL = '# runtimes.sh, canonical\n'++function store(t) {+ const root = mkdtempSync(join(tmpdir(), 'sync-runtimes-'))+ t.after(() => rmSync(root, { recursive: true, force: true }))+ const put = (rel, text) => { mkdirSync(join(root, rel, '..'), { recursive: true }); writeFileSync(join(root, rel), text) }+ put('tools/runtimes.sh', CANONICAL)+ // An agent that sources it from toolchain/, with a stale copy.+ put('agents/stale/toolchain/setup.sh', '#!/usr/bin/env bash\n. toolchain/runtimes.sh\n')+ put('agents/stale/toolchain/runtimes.sh', '# old\n')+ // An agent that sources it (via `source`, from viewer.sh at its root) and has no copy.+ put('agents/missing/viewer.sh', '#!/usr/bin/env bash\nsource "$(dirname "$0")/toolchain/runtimes.sh"\n')+ // A viewer with a current copy beside setup.sh.+ put('viewers/current/setup.sh', '#!/usr/bin/env bash\n. ./runtimes.sh\n')+ put('viewers/current/runtimes.sh', CANONICAL)+ // Packages that never mention it, one with the word only in a comment or in node_modules.+ put('agents/plain/toolchain/setup.sh', '#!/usr/bin/env bash\n# no runtimes.sh here\nnpm ci\n')+ put('agents/plain/node_modules/x/run.sh', '. runtimes.sh\n')+ put('agents/plain/a/b/c/deep.sh', '. runtimes.sh\n') // deeper than the walk goes+ put('agents/plain/README.md', '. runtimes.sh\n')+ put('agents/not-a-dir', 'a file where a package would be\n')+ return root+}++test('packages lists every agent and viewer with where its copy lives', (t) => {+ const root = store(t)+ const got = packages(root).map(({ dir, copy }) => [dir.slice(root.length + 1), copy.slice(root.length + 1)])+ assert.deepEqual(got.sort(), [+ ['agents/missing', 'agents/missing/toolchain/runtimes.sh'],+ ['agents/plain', 'agents/plain/toolchain/runtimes.sh'],+ ['agents/stale', 'agents/stale/toolchain/runtimes.sh'],+ ['viewers/current', 'viewers/current/runtimes.sh'],+ ])+})++test('a store with no viewers folder is fine', (t) => {+ const root = mkdtempSync(join(tmpdir(), 'sync-runtimes-'))+ t.after(() => rmSync(root, { recursive: true, force: true }))+ mkdirSync(join(root, 'agents', 'a'), { recursive: true })+ assert.deepEqual(packages(root).map((p) => p.dir), [join(root, 'agents', 'a')])+})++test('sourcesRuntimes reads shell scripts only, skipping installed trees', (t) => {+ const root = store(t)+ assert.equal(sourcesRuntimes(join(root, 'agents/stale')), true)+ assert.equal(sourcesRuntimes(join(root, 'agents/missing')), true)+ assert.equal(sourcesRuntimes(join(root, 'agents/plain')), false)+})++test('check names the stale copy and the missing one', (t) => {+ const root = store(t)+ assert.deepEqual(check(root), [+ 'agents/missing sources runtimes.sh but has no toolchain/runtimes.sh',+ 'agents/stale/toolchain/runtimes.sh differs from tools/runtimes.sh',+ ])+})++test('sync writes what check complained about, and nothing else', (t) => {+ const root = store(t)+ assert.deepEqual(sync(root).sort(), ['agents/missing/toolchain/runtimes.sh', 'agents/stale/toolchain/runtimes.sh'])+ assert.equal(readFileSync(join(root, 'agents/stale/toolchain/runtimes.sh'), 'utf8'), CANONICAL)+ assert.equal(existsSync(join(root, 'agents/plain/toolchain/runtimes.sh')), false)+ assert.deepEqual(check(root), [])+ assert.deepEqual(sync(root), [])+})++test('the command line: --check exits 1 with the problems, then a plain run fixes them', (t) => {+ const root = store(t)+ const script = join(root, 'tools', 'sync-runtimes.mjs')+ writeFileSync(script, readFileSync(fileURLToPath(new URL('./sync-runtimes.mjs', import.meta.url)), 'utf8'))+ assert.throws(() => execFileSync(process.execPath, [script, '--check'], { stdio: 'pipe' }), (error) => {+ assert.equal(error.status, 1)+ assert.match(error.stderr.toString(), /agents\/stale\/toolchain\/runtimes\.sh differs/)+ return true+ })+ const out = execFileSync(process.execPath, [script], { encoding: 'utf8' })+ assert.match(out, /wrote agents\/missing\/toolchain\/runtimes\.sh/)+ assert.equal(execFileSync(process.execPath, [script, '--check'], { encoding: 'utf8' }), '')+})diff --git a/store/tools/test_runtimes.py b/store/tools/test_runtimes.pynew file mode 100644index 000000000..e3bc888cb--- /dev/null+++ b/store/tools/test_runtimes.py@@ -0,0 +1,484 @@+"""runtimes.sh, sourced for real by bash against a sandbox whose PATH holds only stubs and the few+real commands it uses — no network, no uv, no Python download:++ python3 -m unittest store/tools/test_runtimes.py++Downloads are a `curl` stub that copies a fixture; checksums a `shasum`/`sha256sum` stub that answers+the pinned sum (or a wrong one), so the real archive handling runs on a small tarball.+"""+import os, subprocess, tarfile, tempfile, unittest+from pathlib import Path++HELPER = Path(__file__).resolve().parent / "runtimes.sh"+TRACER = {k: os.environ[k] for k in ("BASH_ENV", "SHCOV_OUT") if k in os.environ}+REAL = ("cat", "cut", "mkdir", "rm", "mv", "chmod", "mktemp", "tar", "gzip")+UV = {+ ("Darwin", "arm64"): ("uv-aarch64-apple-darwin", "dc304b9ed1b24174572290fba60ac3f6fe63c73a671f0439e62a91375841964d"),+ ("Darwin", "x86_64"): ("uv-x86_64-apple-darwin", "e9ca61775532368fe518ab03e7a354c7ecab8ccb3c7d941c775fcc4a362b801b"),+ ("Linux", "x86_64"): ("uv-x86_64-unknown-linux-gnu", "f97935763c04be3e692460a7aaeaaab8fc3b78fcf8b389da820b38ae7423a638"),+ ("Linux", "aarch64"): ("uv-aarch64-unknown-linux-gnu", "0e9a3499b0587d449c9ff684c0160da607826e4af1cee220bc87f378702d3e08"),+}+MAMBA = {+ ("Darwin", "arm64"): ("micromamba-osx-arm64", "ec2a072f028e1a7cf20f3e2e74d5a8127cf5a5f27636375b5359811565f4e5be"),+ ("Darwin", "x86_64"): ("micromamba-osx-64", "1e71054bb3ac9a076e21f7ec48acfef536f9b3f1408f371a942784bf5ef83d8a"),+ ("Linux", "x86_64"): ("micromamba-linux-64", "366cd9cd8be14df1ab8ed50352a82111082a36686b2d389fdb79a92c3fafb3e3"),+ ("Linux", "aarch64"): ("micromamba-linux-aarch64", "9f93b974adcb4d166996af969b6cd371287d1a3e52733704727884d9b74cb7a7"),+}+UV_VERSION = "0.12.15"+MAMBA_VERSION = "2.9.0-0"++# node: `-v` prints NODE_VERSION; `-e SCRIPT MIN` is the at-least probe, answered in bash.+NODE = r'''case "$1" in+ -v) echo "v$NODE_VERSION" ;;+ -e) IFS=. read -r a b _ <<< "$3.0"; IFS=. read -r x y _ <<< "$NODE_VERSION"+ if [ "$x" -gt "$a" ] || { [ "$x" -eq "$a" ] && [ "$y" -ge "$b" ]; }; then exit 0; fi; exit 1 ;;+esac'''+++class Box:+ def __init__(self, test: unittest.TestCase, real=REAL):+ tmp = tempfile.TemporaryDirectory()+ test.addCleanup(tmp.cleanup)+ self.root = Path(tmp.name).resolve()+ self.bin = self.root / "bin"+ self.bin.mkdir()+ self.home = self.root / "home"+ self.home.mkdir()+ self.runtime = self.home / ".harness" / "runtime"+ self.calls = self.root / "calls.log"+ self.calls.touch()+ self.fixtures = self.root / "fixtures"+ self.fixtures.mkdir()+ for name in real:+ found = next(p for p in (Path("/bin") / name, Path("/usr/bin") / name) if p.exists())+ (self.bin / name).symlink_to(found)+ self.platform("Darwin", "arm64")++ def stub(self, name: str, body: str = "", where: Path | None = None) -> Path:+ path = (where or self.bin) / name+ path.parent.mkdir(parents=True, exist_ok=True)+ path.unlink(missing_ok=True) # never write through a link to a real command+ path.write_text(f'#!/bin/bash\necho "{name} $*" >> "$CALLS"\n{body}\n')+ path.chmod(0o755)+ return path++ def platform(self, system: str, machine: str):+ self.stub("uname", f'case "$1" in -s) echo {system} ;; -m) echo {machine} ;; esac')++ def serve(self, body: bytes, name: str = "download"):+ """curl hands back this file for any URL (a `-o DEST URL` call)."""+ fixture = self.fixtures / name+ fixture.write_bytes(body)+ self.stub("curl", f'for a in "$@"; do if [ "$prev" = -o ]; then out="$a"; fi; prev="$a"; done; cp "{fixture}" "$out"')+ (self.bin / "cp").unlink(missing_ok=True)+ (self.bin / "cp").symlink_to("/bin/cp")++ def checksum(self, value: str, tool: str = "shasum"):+ self.stub(tool, f'for last in "$@"; do :; done; echo "{value} $last"')++ def uv_tarball(self, asset: str, with_uv: bool = True) -> bytes:+ src = self.fixtures / "tar-src" / asset+ src.mkdir(parents=True)+ if with_uv:+ (src / "uv").write_text('#!/bin/bash\necho "uv $*" >> "$CALLS"\n')+ (src / "uvx").write_text("#!/bin/bash\n")+ archive = self.fixtures / f"{asset}.tar.gz"+ with tarfile.open(archive, "w:gz") as t:+ t.add(src, arcname=asset)+ return archive.read_bytes()++ def run(self, snippet: str, home: bool = True, **env: str) -> subprocess.CompletedProcess:+ base = {"PATH": str(self.bin), "CALLS": str(self.calls), **TRACER}+ if home:+ base["HOME"] = str(self.home)+ script = f'set -euo pipefail\n. "{HELPER}"\n{snippet}\n'+ return subprocess.run(["/bin/bash", "-c", script], cwd=self.root, env={**base, **env},+ capture_output=True, text=True, timeout=60)++ def logged(self) -> list[str]:+ return self.calls.read_text().splitlines()+++def lines(r: subprocess.CompletedProcess) -> list[str]:+ return r.stdout.splitlines()+++class Platform(unittest.TestCase):+ def test_each_machine_and_the_rest(self):+ box = Box(self)+ for (system, machine), want in [(("Darwin", "arm64"), "darwin-arm64"), (("Darwin", "x86_64"), "darwin-x64"),+ (("Linux", "x86_64"), "linux-x64"), (("Linux", "aarch64"), "linux-arm64"),+ (("Linux", "arm64"), "linux-arm64")]:+ with self.subTest(system=system, machine=machine):+ box.platform(system, machine)+ r = box.run("_harness_platform")+ self.assertEqual((r.returncode, r.stdout.strip()), (0, want), r.stderr)+ box.platform("FreeBSD", "amd64")+ r = box.run('_harness_platform || echo "rc=$?"')+ self.assertEqual(r.stdout.strip(), "rc=1")+++class Checksum(unittest.TestCase):+ def test_shasum_first_then_sha256sum(self):+ box = Box(self)+ (box.root / "f").write_text("x")+ box.checksum("aaa")+ self.assertEqual(box.run('_harness_sha256 f').stdout.strip(), "aaa")+ (box.bin / "shasum").unlink()+ box.checksum("bbb", tool="sha256sum")+ self.assertEqual(box.run('_harness_sha256 f').stdout.strip(), "bbb")+ self.assertIn("sha256sum f", box.logged())+++class Fetch(unittest.TestCase):+ def test_a_download_that_fails_leaves_nothing(self):+ box = Box(self)+ box.stub("curl", 'for a in "$@"; do if [ "$prev" = -o ]; then echo partial > "$a"; fi; prev="$a"; done; exit 22')+ r = box.run('_harness_fetch https://example.com/x "$PWD/x" abc || echo "rc=$?"')+ self.assertEqual(lines(r), ["miss could not download https://example.com/x — check this machine's internet connection", "rc=1"])+ self.assertEqual(sorted(p.name for p in box.root.iterdir() if p.name.startswith("x")), [])++ def test_a_checksum_that_differs_leaves_nothing(self):+ box = Box(self)+ box.serve(b"payload")+ box.checksum("not-the-pin")+ r = box.run('_harness_fetch https://example.com/x "$PWD/x" abc || echo "rc=$?"')+ self.assertEqual(lines(r), ["miss https://example.com/x did not match its pinned checksum; nothing was installed", "rc=1"])+ self.assertFalse(any(p.name.startswith("x") for p in box.root.iterdir()))++ def test_a_matching_download_lands(self):+ box = Box(self)+ box.serve(b"payload")+ box.checksum("abc")+ r = box.run('_harness_fetch https://example.com/x "$PWD/x" abc')+ self.assertEqual(r.returncode, 0, r.stderr)+ self.assertEqual((box.root / "x").read_bytes(), b"payload")+ self.assertIn("curl -fsSL --retry 3 --connect-timeout 20 --max-time 900 -o", box.logged()[0])+++class Node(unittest.TestCase):+ def runtime_node(self, box: Box, version: str) -> Path:+ node = box.stub("node", NODE.replace("$NODE_VERSION", version), where=box.root / "harness-node" / "bin")+ box.runtime.mkdir(parents=True, exist_ok=True)+ (box.runtime / "current-node").write_text(f"{node}\n")+ return node++ def test_the_machines_own_node_when_new_enough(self):+ box = Box(self)+ box.stub("node", NODE)+ self.runtime_node(box, "22.23.2")+ r = box.run('harness_node 18 && command -v node', NODE_VERSION="20.1.0")+ self.assertEqual((r.returncode, r.stdout.strip()), (0, str(box.bin / "node")), r.stderr)++ def test_minor_versions_count(self):+ box = Box(self)+ box.stub("node", NODE)+ self.assertEqual(box.run('harness_node 22.12', NODE_VERSION="22.12.0").returncode, 0)+ self.assertEqual(box.run('harness_node || echo "rc=$?"', NODE_VERSION="17.9.0").stdout.splitlines()[-1], "rc=1")++ def test_harness_node_when_the_machine_has_none(self):+ box = Box(self)+ node = self.runtime_node(box, "22.23.2")+ r = box.run('harness_node 18 && command -v node && echo "$PATH"')+ self.assertEqual(lines(r), [str(node), f"{node.parent}:{box.bin}"], r.stderr)++ def test_harness_node_when_the_machines_is_too_old(self):+ box = Box(self)+ box.stub("node", NODE)+ node = self.runtime_node(box, "22.23.2")+ r = box.run('harness_node 22.12 && command -v node', NODE_VERSION="20.0.0")+ self.assertEqual((r.returncode, r.stdout.strip()), (0, str(node)), r.stderr)++ def test_harness_node_too_old_as_well(self):+ box = Box(self)+ self.runtime_node(box, "22.23.2")+ r = box.run('harness_node 24 || echo "rc=$?"')+ self.assertEqual(lines(r), ["miss node >= 24 — this machine's newest is v22.23.2, Harness's own; update Harness", "rc=1"])++ def test_no_node_anywhere(self):+ box = Box(self)+ r = box.run('harness_node 18 || echo "rc=$?"')+ self.assertEqual(lines(r), [f"miss node >= 18, and Harness's own Node is not in {box.runtime} — run `harness start` once to lay it down", "rc=1"])++ def test_a_current_node_naming_nothing_is_no_node(self):+ box = Box(self)+ box.runtime.mkdir(parents=True)+ (box.runtime / "current-node").write_text(str(box.root / "gone" / "node"))+ r = box.run('harness_node 18 || echo "rc=$?"', ADAPTER_RUNTIME_DIR=str(box.runtime))+ self.assertEqual(lines(r)[-1], "rc=1")+ self.assertIn("Harness's own Node is not in", r.stdout)++ @unittest.skipUnless(Path.home().joinpath(".harness/runtime/current-node").exists(), "no real Harness node to run the probe on")+ def test_the_probe_itself_on_a_real_node(self):+ real = Path(Path.home().joinpath(".harness/runtime/current-node").read_text().strip())+ box = Box(self)+ (box.bin / "node").symlink_to(real)+ major = int(subprocess.run([str(real), "-p", "process.versions.node.split('.')[0]"], capture_output=True, text=True).stdout)+ self.assertEqual(box.run(f"_harness_node_at_least {major}").returncode, 0)+ self.assertEqual(box.run(f"_harness_node_at_least {major}.0").returncode, 0)+ self.assertEqual(box.run(f'_harness_node_at_least {major + 1} || echo "rc=$?"').stdout.strip(), "rc=1")+ self.assertEqual(box.run(f'_harness_node_at_least {major}.999 || echo "rc=$?"').stdout.strip(), "rc=1")+ self.assertEqual(box.run(f"_harness_node_at_least {major - 1}.999").returncode, 0)+++class Uv(unittest.TestCase):+ def test_uv_on_path_is_used_as_it_is(self):+ box = Box(self)+ box.stub("uv")+ r = box.run('harness_uv && echo "$PATH"')+ self.assertEqual((r.returncode, r.stdout.strip()), (0, str(box.bin)))++ def test_the_installers_places_are_looked_in(self):+ for where in (".local/bin", ".cargo/bin"):+ with self.subTest(where=where):+ box = Box(self)+ uv = box.stub("uv", where=box.home / where)+ r = box.run('harness_uv && command -v uv')+ self.assertEqual((r.returncode, r.stdout.strip()), (0, str(uv)))++ def test_without_home_the_runtime_copy_is_used(self):+ box = Box(self)+ uv = box.stub("uv", where=box.root / "rt" / f"uv-{UV_VERSION}")+ r = box.run('harness_uv && command -v uv', home=False, ADAPTER_RUNTIME_DIR=str(box.root / "rt"))+ self.assertEqual((r.returncode, r.stdout.strip()), (0, str(uv)), r.stderr)+ self.assertNotIn("curl", " ".join(box.logged()))++ def test_fetches_the_pinned_release_for_each_machine(self):+ for (system, machine), (asset, pin) in UV.items():+ with self.subTest(asset=asset):+ box = Box(self)+ box.platform(system, machine)+ box.serve(box.uv_tarball(asset))+ box.checksum(pin)+ r = box.run('harness_uv && command -v uv && uv --version')+ dest = box.runtime / f"uv-{UV_VERSION}"+ self.assertEqual(lines(r), [f" fetching uv {UV_VERSION} into {box.runtime} (it brings Python and the packages)",+ str(dest / "uv")], r.stderr)+ self.assertIn(f"https://github.com/astral-sh/uv/releases/download/{UV_VERSION}/{asset}.tar.gz", box.logged()[-3])+ self.assertEqual(sorted(p.name for p in box.runtime.iterdir()), [f"uv-{UV_VERSION}"], "the temp dir is gone")+ self.assertTrue(os.access(dest / "uvx", os.X_OK))++ def test_no_release_for_this_machine(self):+ box = Box(self)+ box.platform("SunOS", "sparc")+ r = box.run('harness_uv || echo "rc=$?"')+ self.assertEqual(lines(r), ["miss uv — there is no uv release for SunOS sparc; install it from https://docs.astral.sh/uv/", "rc=1"])++ def test_a_runtime_dir_that_cannot_be_made(self):+ box = Box(self)+ (box.root / "file").write_text("")+ rt = box.root / "file" / "runtime"+ r = box.run('harness_uv || echo "rc=$?"', ADAPTER_RUNTIME_DIR=str(rt))+ self.assertEqual(lines(r)[-2:], [f"miss cannot write {rt}", "rc=1"])++ def test_a_runtime_dir_that_cannot_be_written(self):+ box = Box(self)+ rt = box.root / "rt"+ rt.mkdir()+ rt.chmod(0o555)+ self.addCleanup(rt.chmod, 0o755)+ r = box.run('harness_uv || echo "rc=$?"', ADAPTER_RUNTIME_DIR=str(rt))+ self.assertEqual(lines(r)[-2:], [f"miss cannot write {rt}", "rc=1"])++ def test_a_failed_download_leaves_no_temp_dir(self):+ box = Box(self)+ box.stub("curl", "exit 7")+ r = box.run('harness_uv || echo "rc=$?"')+ self.assertEqual(lines(r)[-1], "rc=1")+ self.assertEqual(list(box.runtime.iterdir()), [])++ def test_an_archive_that_will_not_unpack(self):+ box = Box(self)+ box.serve(b"not a tarball")+ box.checksum(UV[("Darwin", "arm64")][1])+ r = box.run('harness_uv || echo "rc=$?"')+ self.assertEqual(lines(r)[-2:], ["miss the uv archive would not unpack", "rc=1"])+ self.assertEqual(list(box.runtime.iterdir()), [])++ def test_another_install_that_got_there_first_wins(self):+ box = Box(self)+ asset, pin = UV[("Darwin", "arm64")]+ box.serve(box.uv_tarball(asset))+ box.checksum(pin)+ dest = box.runtime / f"uv-{UV_VERSION}"+ # The other install lands its copy while this one downloads: curl makes the directory.+ box.stub("curl", f'mkdir -p "{dest}" && printf "#!/bin/bash\\n" > "{dest}/uv" && chmod +x "{dest}/uv"; '+ f'for a in "$@"; do if [ "$prev" = -o ]; then out="$a"; fi; prev="$a"; done; cp "{box.fixtures / "download"}" "$out"')+ r = box.run('harness_uv && command -v uv')+ self.assertEqual((r.returncode, lines(r)[-1]), (0, str(dest / "uv")), r.stderr)+ self.assertEqual(sorted(p.name for p in dest.iterdir()), ["uv"], "not nested inside the winner's copy")++ def test_a_copy_that_never_lands(self):+ box = Box(self, real=tuple(n for n in REAL if n != "mv"))+ asset, pin = UV[("Darwin", "arm64")]+ box.serve(box.uv_tarball(asset))+ box.checksum(pin)+ box.stub("mv", 'case "$1" in -f) exec /bin/mv "$@" ;; esac; exit 1')+ r = box.run('harness_uv || echo "rc=$?"')+ self.assertEqual(lines(r)[-2:], [f"miss uv did not land in {box.runtime}/uv-{UV_VERSION}", "rc=1"])+++class Venv(unittest.TestCase):+ def uv(self, box: Box, venv_exit: int = 0):+ template = box.stub("python", 'case "$1" in --version) echo "Python 3.12.7" ;; esac', where=box.fixtures)+ box.stub("uv", f'''case "$1" in+ venv) [ {venv_exit} = 0 ] || exit {venv_exit}; for last in "$@"; do :; done; mkdir -p "$last/bin" && cp "{template}" "$last/bin/python" ;;+esac''')+ (box.bin / "cp").unlink(missing_ok=True)+ (box.bin / "cp").symlink_to("/bin/cp")++ def system_python(self, box: Box):+ """A venv whose python is Apple's 3.9 — a wrapper, since /usr/bin/python3 picks its tool by the name it is run as."""+ py = box.root / ".venv" / "bin" / "python"+ py.parent.mkdir(parents=True)+ py.write_text('#!/bin/bash\nexec /usr/bin/python3 "$@"\n')+ py.chmod(0o755)++ def test_a_venv_in_range_is_kept(self):+ box = Box(self)+ self.uv(box)+ self.system_python(box)+ r = box.run('harness_venv .venv 3.12 3.9 3.10')+ self.assertEqual((r.returncode, lines(r)), (0, [f"ok {subprocess.run(['/usr/bin/python3', '--version'], capture_output=True, text=True).stdout.strip()} in .venv"]), r.stderr)+ self.assertEqual(box.logged(), [])++ def test_a_venv_out_of_range_is_replaced(self):+ for low, below in (("3.10", ""), ("3.8", "3.9")):+ with self.subTest(low=low, below=below):+ box = Box(self)+ self.uv(box)+ self.system_python(box)+ r = box.run(f'harness_venv .venv 3.12 {low} {below}')+ self.assertEqual(lines(r), [" python 3.12 in .venv (uv downloads it when this machine has none)",+ "ok Python 3.12.7 in .venv"], r.stderr)+ self.assertEqual(box.logged(), ["uv venv --quiet --seed --python 3.12 --python-preference only-managed .venv", "python --version"])+ self.assertNotIn("exec /usr/bin/python3", (box.root / ".venv" / "bin" / "python").read_text(), "the old venv went first")++ def test_no_venv_makes_one_at_the_version_asked(self):+ box = Box(self)+ self.uv(box)+ r = box.run('harness_venv .venv 3.11')+ self.assertEqual(r.returncode, 0, r.stderr)+ self.assertIn("uv venv --quiet --seed --python 3.11 --python-preference only-managed .venv", box.logged())++ def test_no_uv_to_be_had(self):+ box = Box(self)+ box.stub("curl", "exit 6")+ r = box.run('harness_venv .venv 3.12 || echo "rc=$?"')+ self.assertEqual(lines(r)[-1], "rc=1")+ self.assertIn("miss could not download", r.stdout)++ def test_uv_that_cannot_make_it(self):+ box = Box(self)+ self.uv(box, venv_exit=2)+ r = box.run('harness_venv .venv 3.12 || echo "rc=$?"')+ self.assertEqual(lines(r)[-2:], ["miss could not make a Python 3.12 environment in .venv", "rc=1"])+++class Pip(unittest.TestCase):+ def test_installs_into_the_venv_through_uv(self):+ box = Box(self)+ box.stub("uv", 'exit "${PIP_EXIT:-0}"')+ r = box.run('harness_pip .venv "rdkit==2025.3.1" numpy')+ self.assertEqual(r.returncode, 0, r.stderr)+ self.assertEqual(box.logged(), ["uv pip install --quiet --python .venv/bin/python rdkit==2025.3.1 numpy"])+ self.assertEqual(box.run('harness_pip .venv x || echo "rc=$?"', PIP_EXIT="4").stdout.strip(), "rc=4")++ def test_no_uv_to_be_had(self):+ box = Box(self)+ box.stub("curl", "exit 6")+ self.assertEqual(lines(box.run('harness_pip .venv x || echo "rc=$?"'))[-1], "rc=1")+++class Micromamba(unittest.TestCase):+ def test_an_installed_copy_is_used(self):+ box = Box(self)+ mm = box.stub("micromamba", where=box.runtime / f"micromamba-{MAMBA_VERSION}")+ r = box.run('harness_micromamba && echo "$HARNESS_MICROMAMBA"')+ self.assertEqual((r.returncode, r.stdout.strip()), (0, str(mm)))+ self.assertEqual(box.logged(), [])++ def test_fetches_the_pinned_binary_for_each_machine(self):+ for (system, machine), (asset, pin) in MAMBA.items():+ with self.subTest(asset=asset):+ box = Box(self)+ box.platform(system, machine)+ box.serve(b"#!/bin/bash\necho micromamba\n")+ box.checksum(pin)+ r = box.run('harness_micromamba && "$HARNESS_MICROMAMBA"')+ self.assertEqual(lines(r), [f" fetching micromamba {MAMBA_VERSION} into {box.runtime} (native libraries PyPI has no wheel for)",+ "micromamba"], r.stderr)+ self.assertIn(f"https://github.com/mamba-org/micromamba-releases/releases/download/{MAMBA_VERSION}/{asset}", box.logged()[-2])+ self.assertEqual([p.name for p in (box.runtime / f"micromamba-{MAMBA_VERSION}").iterdir()], ["micromamba"])++ def test_no_release_for_this_machine(self):+ box = Box(self)+ box.platform("Plan9", "mips")+ r = box.run('harness_micromamba || echo "rc=$?"')+ self.assertEqual(lines(r), ["miss micromamba — there is no release for Plan9 mips", "rc=1"])++ def test_a_dir_that_cannot_be_made(self):+ box = Box(self)+ (box.root / "file").write_text("")+ r = box.run('harness_micromamba || echo "rc=$?"', ADAPTER_RUNTIME_DIR=str(box.root / "file" / "rt"))+ self.assertEqual(lines(r)[-2:], [f"miss cannot write {box.root}/file/rt/micromamba-{MAMBA_VERSION}", "rc=1"])++ def test_a_failed_download(self):+ box = Box(self)+ box.stub("curl", "exit 6")+ self.assertEqual(lines(box.run('harness_micromamba || echo "rc=$?"'))[-1], "rc=1")++ def test_a_binary_that_will_not_move_into_place(self):+ box = Box(self, real=tuple(n for n in REAL if n != "mv"))+ box.serve(b"#!/bin/bash\n")+ box.checksum(MAMBA[("Darwin", "arm64")][1])+ box.stub("mv", 'case "$2" in *.part.*) exec /bin/mv "$@" ;; esac; exit 1')+ r = box.run('harness_micromamba || echo "rc=$?"')+ dest = box.runtime / f"micromamba-{MAMBA_VERSION}"+ self.assertEqual(lines(r)[-2:], [f"miss micromamba would not install into {dest}", "rc=1"])+ self.assertEqual(list(dest.iterdir()), [])+++class CondaEnv(unittest.TestCase):+ def micromamba(self, box: Box, body: str) -> Path:+ return box.stub("micromamba", f'echo "MAMBA_ROOT_PREFIX=$MAMBA_ROOT_PREFIX" >> "$CALLS"\n{body}',+ where=box.runtime / f"micromamba-{MAMBA_VERSION}")++ def test_a_relative_dir_is_made_absolute(self):+ box = Box(self)+ self.micromamba(box, 'for a in "$@"; do if [ "$prev" = --prefix ]; then mkdir -p "$a/conda-meta"; fi; prev="$a"; done')+ r = box.run('harness_conda_env .venv python=3.12 pycairo pip')+ self.assertEqual(r.returncode, 0, r.stderr)+ self.assertEqual(box.logged(), [+ f"micromamba create --yes --quiet --prefix {box.root}/.venv --override-channels --channel conda-forge python=3.12 pycairo pip",+ f"MAMBA_ROOT_PREFIX={box.runtime}/mamba",+ ])+ self.assertTrue((box.root / ".venv" / "conda-meta").is_dir())++ def test_an_absolute_dir_is_kept(self):+ box = Box(self)+ self.micromamba(box, f'mkdir -p "{box.root}/env/conda-meta"')+ r = box.run(f'harness_conda_env "{box.root}/env" python=3.12')+ self.assertEqual(r.returncode, 0, r.stderr)+ self.assertIn(f"--prefix {box.root}/env ", box.logged()[0])++ def test_a_create_that_fails(self):+ box = Box(self)+ self.micromamba(box, "exit 1")+ r = box.run('harness_conda_env .venv python=3.12 || echo "rc=$?"')+ self.assertEqual(lines(r), [f"miss could not make the conda-forge environment in {box.root}/.venv (python=3.12)", "rc=1"])++ def test_a_create_that_succeeds_somewhere_else(self):+ box = Box(self)+ self.micromamba(box, "exit 0")+ r = box.run('harness_conda_env .venv python=3.12 || echo "rc=$?"')+ self.assertEqual(lines(r)[-1], "rc=1")++ def test_no_micromamba_to_be_had(self):+ box = Box(self)+ box.stub("curl", "exit 6")+ self.assertEqual(lines(box.run('harness_conda_env .venv python=3.12 || echo "rc=$?"'))[-1], "rc=1")+++if __name__ == "__main__":+ unittest.main()