Skip to content

Commit

Permalink
tests for new version
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jan 24, 2020
1 parent 3e68692 commit c360cf5
Show file tree
Hide file tree
Showing 24 changed files with 602 additions and 459 deletions.
104 changes: 104 additions & 0 deletions tap-snapshots/test-cmd.js-TAP.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/cmd.js TAP -h --help prints usage > --help output 1`] = `
Object {
"code": 0,
"signal": null,
"stderr": "",
"stdout": "\\nusage: mkdirp [DIR1,DIR2..] {OPTIONS}\\n\\n Create each supplied directory including any necessary parent directories\\n that don't yet exist.\\n\\n If the directory already exists, do nothing.\\n\\nOPTIONS are:\\n\\n -m<mode> If a directory needs to be created, set the mode as an octal\\n --mode=<mode> permission string.\\n\\n -v --version Print the mkdirp version number\\n\\n -h --help Print this helpful banner\\n\\n -p --print Print the first directories created for each path provided\\n\\n --manual Use manual implementation, even if native is available\\n\\n",
}
`

exports[`test/cmd.js TAP -v --version prints version > --version output 1`] = `
Object {
"code": 0,
"signal": null,
"stderr": "",
"stdout": "4.2.0-69.lol\\n",
}
`

exports[`test/cmd.js TAP failures > expect resolving Promise 1`] = `
Array [
Object {
"code": 1,
"signal": null,
"stderr": "nope\\n",
"stdout": "",
},
Object {
"code": 1,
"signal": null,
"stderr": "fail\\n code: EFAIL\\n",
"stdout": "",
},
]
`

exports[`test/cmd.js TAP invalid mode > expect resolving Promise 1`] = `
Object {
"code": 1,
"signal": null,
"stderr": "invalid mode argument: --mode=XYZ\\nMust be an octal number.\\n",
"stdout": "",
}
`

exports[`test/cmd.js TAP make dir named --help > expect resolving Promise 1`] = `
Object {
"code": 0,
"signal": null,
"stderr": "",
"stdout": "--help 0\\n",
}
`

exports[`test/cmd.js TAP making dirs > expect resolving Promise 1`] = `
Object {
"code": 0,
"signal": null,
"stderr": "",
"stdout": "",
}
`

exports[`test/cmd.js TAP manual > expect resolving Promise 1`] = `
Object {
"code": 0,
"signal": null,
"stderr": "",
"stdout": "MANUAL a 0\\nMANUAL b/c/d 0\\n",
}
`

exports[`test/cmd.js TAP no dirs -> stderr usage > expect resolving Promise 1`] = `
Object {
"code": 0,
"signal": null,
"stderr": "\\nusage: mkdirp [DIR1,DIR2..] {OPTIONS}\\n\\n Create each supplied directory including any necessary parent directories\\n that don't yet exist.\\n\\n If the directory already exists, do nothing.\\n\\nOPTIONS are:\\n\\n -m<mode> If a directory needs to be created, set the mode as an octal\\n --mode=<mode> permission string.\\n\\n -v --version Print the mkdirp version number\\n\\n -h --help Print this helpful banner\\n\\n -p --print Print the first directories created for each path provided\\n\\n --manual Use manual implementation, even if native is available\\n\\n",
"stdout": "",
}
`

exports[`test/cmd.js TAP noisily > expect resolving Promise 1`] = `
Object {
"code": 0,
"signal": null,
"stderr": "",
"stdout": "a 0\\nb/c/d 0\\n",
}
`

exports[`test/cmd.js TAP print modes > expect resolving Promise 1`] = `
Object {
"code": 0,
"signal": null,
"stderr": "",
"stdout": "a 509\\n",
}
`
41 changes: 0 additions & 41 deletions test/chmod.js

This file was deleted.

38 changes: 0 additions & 38 deletions test/clobber.js

This file was deleted.

69 changes: 69 additions & 0 deletions test/cmd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const cmd = require.resolve('../bin/cmd.js')
const requireInject = require('require-inject')

const {basename} = require('path')
const fakeMkdirp = (path, opts) =>
basename(path) === 'ERROR' ? Promise.reject(new Error('nope'))
: basename(path) === 'EFAIL' ? Promise.reject(Object.assign(new Error('fail'), { code: 'EFAIL' }))
: Promise.resolve(`${path} ${opts.mode || 0}`)

fakeMkdirp.manual = (path, opts) => fakeMkdirp(`MANUAL ${path}`, opts)

if (process.argv[2] === 'RUN') {
process.argv = [process.execPath, cmd, ...process.argv.slice(3)]
requireInject(cmd, {
'../': fakeMkdirp,
'../package.json': {
version: '4.2.0-69.lol',
},
})
} else {

const t = require('tap')

const {spawn} = require('child_process')
const run = (...args) => new Promise((res, rej) => {
const proc = spawn(process.execPath, [__filename, 'RUN', ...args])
const out = []
const err = []
proc.stdout.on('data', c => out.push(c))
proc.stderr.on('data', c => err.push(c))
proc.on('close', (code, signal) => {
res({
code,
signal,
stdout: Buffer.concat(out).toString('utf8'),
stderr: Buffer.concat(err).toString('utf8'),
})
})
})

t.test('-h --help prints usage', t => Promise.all([
run('-h'),
run('--help'),
]).then(res => {
t.strictSame(res[0], res[1], 'same for -h and --help')
t.matchSnapshot(res[0], '--help output')
}))

t.test('no dirs -> stderr usage', t => t.resolveMatchSnapshot(run()))

t.test('-v --version prints version', t => Promise.all([
run('-v'),
run('--version'),
]).then(res => {
t.strictSame(res[0], res[1], 'same for -v and --version')
t.matchSnapshot(res[0], '--version output')
}))

t.test('making dirs', t => t.resolveMatchSnapshot(run('a', 'b/c/d', 'e')))
t.test('noisily', t => t.resolveMatchSnapshot(run('a', 'b/c/d', '--print')))
t.test('manual', t => t.resolveMatchSnapshot(run('a', 'b/c/d', '-p', '--manual')))
t.test('print modes', t => t.resolveMatchSnapshot(run('a', '-m775', '-p')))
t.test('invalid mode', t => t.resolveMatchSnapshot(run('--mode=XYZ')))
t.test('make dir named --help', t => t.resolveMatchSnapshot(run('-p', '--', '--help')))
t.test('failures', t => t.resolveMatchSnapshot(Promise.all([
run('x/ERROR'),
run('x/EFAIL'),
])))
}
53 changes: 53 additions & 0 deletions test/find-made.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const t = require('tap')
const requireInject = require('require-inject')

const {basename, posix} = require('path')
const {promisify} = require('util')
const fs = require('fs')

const statAsync = (path) =>
basename(path) === 'error'
? Promise.reject(new Error('not a real error'))
: promisify(fs.stat)(path)

const statSync = path => {
if (basename(path) === 'error')
throw new Error('not a real error')
else
return fs.statSync(path)
}

const {findMade, findMadeSync} = requireInject('../lib/find-made.js', {
path: posix,
})

t.test('find what dir will be made', t => {
const dir = t.testdir({
file: 'txt',
subdir: {},
})

const o = {statAsync, statSync}

t.equal(findMadeSync(o, `${dir}/subdir/x/y/z`), `${dir}/subdir/x`)
t.equal(findMadeSync(o, `${dir}/subdir`), undefined)
t.equal(findMadeSync(o, `${dir}/file/x/y/z`), undefined)
t.equal(findMadeSync(o, `${dir}/file`, `${dir}/file/x`), undefined)
t.equal(findMadeSync(o, `${dir}/subdir/error`), undefined)
t.equal(findMadeSync(o, '/', '/'), undefined)
return Promise.all([
findMade(o, `${dir}/subdir/x/y/z`),
findMade(o, `${dir}/subdir`),
findMade(o, `${dir}/file/x/y/z`),
findMade(o, `${dir}/file`, `${dir}/file/x`),
findMade(o, `${dir}/subdir/error`),
findMade(o, '/', '/'),
]).then(made => t.strictSame(made, [
`${dir}/subdir/x`,
undefined,
undefined,
undefined,
undefined,
undefined,
]))
})
61 changes: 61 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const t = require('tap')
const mkdirp = require('../')

t.test('module shape', t => {
t.isa(mkdirp, Function)
t.isa(mkdirp.sync, Function)
t.isa(mkdirp.manual, Function)
t.isa(mkdirp.manualSync, Function)
t.isa(mkdirp.native, Function)
t.isa(mkdirp.nativeSync, Function)
t.end()
})

t.test('basic making of dirs should work', t => {
const dir = t.testdir({ a: {} })
const {statSync, mkdir, mkdirSync} = require('fs')
const check = d => t.ok(statSync(d).isDirectory())
t.equal(mkdirp.sync(`${dir}/a/sync`), `${dir}/a/sync`)
check(`${dir}/a/sync`)
t.equal(mkdirp.sync(`${dir}/a/sync`), undefined)

t.equal(mkdirp.manualSync(`${dir}/a/manual-sync`), `${dir}/a/manual-sync`)
check(`${dir}/a/manual-sync`)
t.equal(mkdirp.manualSync(`${dir}/a/manual-sync`), undefined)

t.equal(mkdirp.nativeSync(`${dir}/a/native-sync`), `${dir}/a/native-sync`)
check(`${dir}/a/native-sync`)
t.equal(mkdirp.nativeSync(`${dir}/a/native-sync`), undefined)

// override to force the manual option
const myMkdir = (path, opts, cb) => mkdir(path, opts, cb)
const myMkdirSync = (path, opts) => mkdirSync(path, opts)
const opts = { mkdir: myMkdir, mkdirSync: myMkdirSync }
t.equal(mkdirp.sync(`${dir}/a/custom-sync`, opts), `${dir}/a/custom-sync`)
check(`${dir}/a/custom-sync`)
t.equal(mkdirp.sync(`${dir}/a/custom-sync`, opts), undefined)

return Promise.all([
mkdirp(`${dir}/a/async`),
mkdirp.manual(`${dir}/a/manual-async`),
mkdirp.native(`${dir}/a/native-async`),
mkdirp(`${dir}/a/custom-async`, opts),
]).then(made => {
t.strictSame(made, [
`${dir}/a/async`,
`${dir}/a/manual-async`,
`${dir}/a/native-async`,
`${dir}/a/custom-async`,
])
check(`${dir}/a/async`)
check(`${dir}/a/manual-async`)
check(`${dir}/a/native-async`)
check(`${dir}/a/custom-async`)
return Promise.all([
mkdirp(`${dir}/a/async`),
mkdirp.manual(`${dir}/a/manual-async`),
mkdirp.native(`${dir}/a/native-async`),
mkdirp(`${dir}/a/custom-async`, opts),
])
}).then(made => t.strictSame(made, [undefined, undefined, undefined, undefined]))
})
Loading

0 comments on commit c360cf5

Please sign in to comment.