okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1ee7c7337
main
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' , () => { 12assert . 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' }); 14assert . 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' , () => { 18assert . match ( venvPython ( 'C:\\repo' , 'win32' ), / Scripts [ \\ / ] python\.exe$ / ); 19assert . match ( venvPython ( '/repo' , 'linux' ), / bin [ \\ / ] python$ / ); 20const seen = []; 21const found = pick ({ platform :'win32' , repo :'C:\\repo' , exists :( p ) => { seen . push ( p ); return / Scripts [ \\ / ] python\.exe$ / . test ( p ); } }); 22assert . equal ( found . source , 'venv' ); 23assert . match ( found . command , / Scripts [ \\ / ] python\.exe$ / ); 24assert . ok ( seen . length >= 1 ); 25}); 26 27test ( 'Windows falls back to py -3, then python.exe, and skips an interpreter that does not start' , () => { 28const tried = []; 29const starts = ( command , args = []) => { tried . push ([ command , ...args ]. join ( ' ' )); return command === 'py' ; }; 30assert . deepEqual ( pick ({ platform :'win32' , starts}), { command :'py' , args :[ '-3' ], source :'py launcher' }); 31const second = pick ({ platform :'win32' , starts :( command ) => command === 'python.exe' }); 32assert . 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 34assert . equal ( pick ({ platform :'win32' }). command , 'python.exe' ); 35assert . equal ( pick ({ platform :'win32' }). source , 'fallback' ); 36}); 37 38test ( 'POSIX keeps Homebrew first, then python3' , () => { 39assert . equal ( pick ({ platform :'darwin' , exists :( p ) => p . endsWith ( 'python3.11' ) }). command , '/opt/homebrew/bin/python3.11' ); 40assert . deepEqual ( pick ({ platform :'linux' }), { command :'python3' , args :[], source :'PATH' }); 41}); 42 43test ( 'the shell prefix keeps the launcher arguments' , () => { 44assert . equal ( shellPrefix ({ command :'py' , args :[ '-3' ] }), '"py" -3' ); 45assert . equal ( shellPrefix ({ command :'python3' , args :[] }), '"python3"' ); 46}); 47 48test ( 'the real resolver finds a Python that starts on this machine' , () => { 49const found = resolvePython ({ useCache :false , env :{} }); 50const { spawnSync} = process . getBuiltinModule ( 'node:child_process' ); 51const run = spawnSync ( found . command , [ ...found . args , '-c' , 'import sys; print(sys.version_info[0])' ], { encoding :'utf8' , windowsHide :true }); 52assert . equal ( run . stdout . trim (), '3' ); 53}); 54 55test ( 'no server module spawns a bare python3 any more' , () => { 56const dir = resolve ( fileURLToPath ( new URL ( '../server' , import . meta. url ))); 57for ( const name of readdirSync ( dir ). filter (( n ) => / \. ( mjs | cjs ) $ / . test ( n ))) { 58const text = readFileSync ( join ( dir , name ), 'utf8' ); 59assert . doesNotMatch ( text , / spawn\w * \(\s * [ '" ] python3 ? [ '" ] / , ` ${ name } spawns python directly` ); 60if ( name !== 'python-resolver.mjs' ) assert . doesNotMatch ( text , / \|\|\s * 'python3' / , ` ${ name } defaults to python3` ); 61} 62});