Skip to content

Commit

Permalink
fix(test): use new tests with old find-python.js
Browse files Browse the repository at this point in the history
  • Loading branch information
owl-from-hogvarts committed Jun 3, 2021
1 parent 0f499bd commit cceb6d9
Show file tree
Hide file tree
Showing 3 changed files with 381 additions and 238 deletions.
20 changes: 20 additions & 0 deletions test/rm.js
@@ -0,0 +1,20 @@
const fs = require('fs')
const path = require('path')

/** recursively delete files, symlinks (without following them) and dirs */
module.exports = function rmRecSync (pth) {
pth = path.normalize(pth)

rm(pth)

function rm (pth) {
const pathStat = fs.statSync(pth)
// trick with lstat is used to avoid following symlinks (especially junctions on windows)
if (pathStat.isDirectory() && !fs.lstatSync(pth).isSymbolicLink()) {
fs.readdirSync(pth).forEach((nextPath) => rm(path.join(pth, nextPath)))
fs.rmdirSync(pth)
} else {
fs.unlinkSync(pth)
}
}
}
25 changes: 13 additions & 12 deletions test/test-find-python-script.js
Expand Up @@ -8,21 +8,22 @@ const path = require('path')

require('npmlog').level = 'warn'

//* can use name as short descriptions
//* name can be used as short descriptions

/**
* @typedef Check
* @property {string} path - path to execurtable or command
* @property {string} path - path to executable or command
* @property {string} name - very little description
*/

// TODO: add symlinks to python which will contain utf-8 chars
/**
* @type {Check[]}
*/
const checks = [
{ path: process.env.PYTHON, name: 'env var PYTHON' },
{ path: process.env.python2, name: 'env var python2' },
{ path: 'python3', name: 'env var python3' }
{ path: 'python3', name: 'python3 in PATH' },
{ path: 'python', name: 'python in PATH' }
]
const args = [path.resolve('./lib/find-python-script.py')]
const options = {
Expand All @@ -32,37 +33,37 @@ const options = {
/**
Getting output from find-python-script.py,
compare it to path provided to terminal.
If equale - test pass
If equals - test pass
runs for all checks
@private
@argument {Error} err - exec error
@argument {string} stdout - stdout buffer of child process
@argument {string} stderr
@this {{t, exec: Check}}
@this {{t: Tap, exec: Check}}
*/
function check (err, stdout, stderr) {
const { t, exec } = this
if (!err && !stderr) {
t.strictEqual(
t.ok(
stdout.trim(),
exec.path,
`${exec.name}: check path ${exec.path} equals ${stdout.trim()}`
)
} else {
// @ts-ignore
if (err.code === 9009) {
if (err.code === 9009 || err.code === 'ENOENT') {
t.skip(`skipped: ${exec.name} file not found`)
} else {
t.fail(`error: ${err}\n\nstderr: ${stderr}`)
t.skip(`error: ${err}\n\nstderr: ${stderr}`)
}
}
}

test('find-python-script', (t) => {
test('find-python-script', { buffered: false }, (t) => {
t.plan(checks.length)

// ? may be more elegant way to pass context
// context for check functions
const ctx = {
t: t,
Expand All @@ -73,7 +74,7 @@ test('find-python-script', (t) => {
// checking if env var exist
if (!(exec.path === undefined || exec.path === null)) {
ctx.exec = exec
// passing ctx as coppied object to make properties immutable from here
// passing ctx as copied object to make properties immutable from here
const boundedCheck = check.bind(Object.assign({}, ctx))
execFile(exec.path, args, options, boundedCheck)
} else {
Expand Down

0 comments on commit cceb6d9

Please sign in to comment.