okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221host: one Python resolver for every spawn (env, repo venv, py -3, python.exe on Windows; python3 elsewhere)1ee7c7337

main
3.4 KiB62 linesraw
1import test from 'node:test';
2import assert from 'node:assert/strict';
3import { readFileSync, readdirSync } from 'node:fs';
4import { join, resolve } from 'node:path';
5import { fileURLToPath } from 'node:url';
6import { resolvePython, shellPrefix, venvPython } from '../server/python-resolver.mjs';
7
8const none = () => false;
9const pick = (options) => resolvePython({ useCache: false, env: {}, exists: none, starts: none, ...options });
10
11test('an explicit STRATA_PYTHON wins over everything', () => {
12  assert.deepEqual(pick({ platform: 'win32', repo: 'C:\\r', env: { STRATA_PYTHON: 'D:\\py\\python.exe' }, exists: () => true }),
13    { command: 'D:\\py\\python.exe', args: [], source: 'environment' });
14  assert.equal(pick({ platform: 'linux', env: { STRATA_NATIVE_PYTHON: '/opt/py' } }).command, '/opt/py');
15});
16
17test('the repository venv is used with the platform layout', () => {
18  assert.match(venvPython('C:\\repo', 'win32'), /Scripts[\\/]python\.exe$/);
19  assert.match(venvPython('/repo', 'linux'), /bin[\\/]python$/);
20  const seen = [];
21  const found = pick({ platform: 'win32', repo: 'C:\\repo', exists: (p) => { seen.push(p); return /Scripts[\\/]python\.exe$/.test(p); } });
22  assert.equal(found.source, 'venv');
23  assert.match(found.command, /Scripts[\\/]python\.exe$/);
24  assert.ok(seen.length >= 1);
25});
26
27test('Windows falls back to py -3, then python.exe, and skips an interpreter that does not start', () => {
28  const tried = [];
29  const starts = (command, args = []) => { tried.push([command, ...args].join(' ')); return command === 'py'; };
30  assert.deepEqual(pick({ platform: 'win32', starts }), { command: 'py', args: ['-3'], source: 'py launcher' });
31  const second = pick({ platform: 'win32', starts: (command) => command === 'python.exe' });
32  assert.deepEqual(second, { command: 'python.exe', args: [], source: 'PATH' });
33  // the Microsoft Store alias exists on PATH but fails to start: nothing works, so the plain name is returned for a clear error
34  assert.equal(pick({ platform: 'win32' }).command, 'python.exe');
35  assert.equal(pick({ platform: 'win32' }).source, 'fallback');
36});
37
38test('POSIX keeps Homebrew first, then python3', () => {
39  assert.equal(pick({ platform: 'darwin', exists: (p) => p.endsWith('python3.11') }).command, '/opt/homebrew/bin/python3.11');
40  assert.deepEqual(pick({ platform: 'linux' }), { command: 'python3', args: [], source: 'PATH' });
41});
42
43test('the shell prefix keeps the launcher arguments', () => {
44  assert.equal(shellPrefix({ command: 'py', args: ['-3'] }), '"py" -3');
45  assert.equal(shellPrefix({ command: 'python3', args: [] }), '"python3"');
46});
47
48test('the real resolver finds a Python that starts on this machine', () => {
49  const found = resolvePython({ useCache: false, env: {} });
50  const { spawnSync } = process.getBuiltinModule('node:child_process');
51  const run = spawnSync(found.command, [...found.args, '-c', 'import sys; print(sys.version_info[0])'], { encoding: 'utf8', windowsHide: true });
52  assert.equal(run.stdout.trim(), '3');
53});
54
55test('no server module spawns a bare python3 any more', () => {
56  const dir = resolve(fileURLToPath(new URL('../server', import.meta.url)));
57  for (const name of readdirSync(dir).filter((n) => /\.(mjs|cjs)$/.test(n))) {
58    const text = readFileSync(join(dir, name), 'utf8');
59    assert.doesNotMatch(text, /spawn\w*\(\s*['"]python3?['"]/, `${name} spawns python directly`);
60    if (name !== 'python-resolver.mjs') assert.doesNotMatch(text, /\|\|\s*'python3'/, `${name} defaults to python3`);
61  }
62});