|
| 1 | +// @ts-check |
| 2 | +'use strict' |
| 3 | +/** @typedef {import("tap")} Tap */ |
| 4 | + |
| 5 | +const test = require('tap').test |
| 6 | +const execFile = require('child_process').execFile |
| 7 | +const path = require('path') |
| 8 | + |
| 9 | +require('npmlog').level = 'warn' |
| 10 | + |
| 11 | +//* can use name as short descriptions |
| 12 | + |
| 13 | +/** |
| 14 | + * @typedef Check |
| 15 | + * @property {string} path - path to execurtable or command |
| 16 | + * @property {string} name - very little description |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * @type {Check[]} |
| 21 | + */ |
| 22 | +const checks = [ |
| 23 | + { path: process.env.PYTHON, name: 'env var PYTHON' }, |
| 24 | + { path: process.env.python2, name: 'env var python2' }, |
| 25 | + { path: 'python3', name: 'env var python3' } |
| 26 | +] |
| 27 | +const args = [path.resolve('./lib/find-python-script.py')] |
| 28 | +const options = { |
| 29 | + windowsHide: true |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + Getting output from find-python-script.py, |
| 34 | + compare it to path provided to terminal. |
| 35 | + If equale - test pass |
| 36 | +
|
| 37 | + runs for all checks |
| 38 | +
|
| 39 | + @private |
| 40 | + @argument {Error} err - exec error |
| 41 | + @argument {string} stdout - stdout buffer of child process |
| 42 | + @argument {string} stderr |
| 43 | + @this {{t, exec: Check}} |
| 44 | + */ |
| 45 | +function check (err, stdout, stderr) { |
| 46 | + const { t, exec } = this |
| 47 | + if (!err && !stderr) { |
| 48 | + t.strictEqual( |
| 49 | + stdout.trim(), |
| 50 | + exec.path, |
| 51 | + `${exec.name}: check path ${exec.path} equals ${stdout.trim()}` |
| 52 | + ) |
| 53 | + } else { |
| 54 | + // @ts-ignore |
| 55 | + if (err.code === 9009) { |
| 56 | + t.skip(`skipped: ${exec.name} file not found`) |
| 57 | + } else { |
| 58 | + t.fail(`error: ${err}\n\nstderr: ${stderr}`) |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +test('find-python-script', (t) => { |
| 64 | + t.plan(checks.length) |
| 65 | + |
| 66 | + // context for check functions |
| 67 | + const ctx = { |
| 68 | + t: t, |
| 69 | + exec: {} |
| 70 | + } |
| 71 | + |
| 72 | + for (const exec of checks) { |
| 73 | + // checking if env var exist |
| 74 | + if (!(exec.path === undefined || exec.path === null)) { |
| 75 | + ctx.exec = exec |
| 76 | + // passing ctx as coppied object to make properties immutable from here |
| 77 | + const boundedCheck = check.bind(Object.assign({}, ctx)) |
| 78 | + execFile(exec.path, args, options, boundedCheck) |
| 79 | + } else { |
| 80 | + t.skip(`skipped: ${exec.name} doesn't exist or unavailable`) |
| 81 | + } |
| 82 | + } |
| 83 | +}) |
0 commit comments