forked from lightningpixel/modly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-pytests.mjs
More file actions
37 lines (32 loc) · 1.06 KB
/
Copy pathrun-pytests.mjs
File metadata and controls
37 lines (32 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Portable Python test runner.
// `python3` is the macOS/Linux name but does not exist on Windows (where the
// interpreter is `python` or the `py` launcher). Try each candidate until one
// actually runs, then forward unittest's exit code.
import { spawnSync } from 'node:child_process'
import { fileURLToPath } from 'node:url'
import { dirname, join } from 'node:path'
const apiDir = join(dirname(fileURLToPath(import.meta.url)), '..', 'api')
const candidates = [
['python3', []],
['python', []],
['py', ['-3']],
]
function works(cmd, prefix) {
try {
const r = spawnSync(cmd, [...prefix, '--version'], { stdio: 'ignore' })
return r.status === 0
} catch {
return false
}
}
const found = candidates.find(([cmd, prefix]) => works(cmd, prefix))
if (!found) {
console.error('[run-pytests] No Python interpreter found (tried python3, python, py -3).')
process.exit(1)
}
const [cmd, prefix] = found
const result = spawnSync(cmd, [...prefix, '-m', 'unittest', 'discover', '-s', 'tests'], {
cwd: apiDir,
stdio: 'inherit',
})
process.exit(result.status ?? 1)