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