Skip to content

Commit c360cf5

Browse files
committed
tests for new version
1 parent 3e68692 commit c360cf5

24 files changed

+602
-459
lines changed

tap-snapshots/test-cmd.js-TAP.test.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/* IMPORTANT
2+
* This snapshot file is auto-generated, but designed for humans.
3+
* It should be checked into source control and tracked carefully.
4+
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
5+
* Make sure to inspect the output below. Do not ignore changes!
6+
*/
7+
'use strict'
8+
exports[`test/cmd.js TAP -h --help prints usage > --help output 1`] = `
9+
Object {
10+
"code": 0,
11+
"signal": null,
12+
"stderr": "",
13+
"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",
14+
}
15+
`
16+
17+
exports[`test/cmd.js TAP -v --version prints version > --version output 1`] = `
18+
Object {
19+
"code": 0,
20+
"signal": null,
21+
"stderr": "",
22+
"stdout": "4.2.0-69.lol\\n",
23+
}
24+
`
25+
26+
exports[`test/cmd.js TAP failures > expect resolving Promise 1`] = `
27+
Array [
28+
Object {
29+
"code": 1,
30+
"signal": null,
31+
"stderr": "nope\\n",
32+
"stdout": "",
33+
},
34+
Object {
35+
"code": 1,
36+
"signal": null,
37+
"stderr": "fail\\n code: EFAIL\\n",
38+
"stdout": "",
39+
},
40+
]
41+
`
42+
43+
exports[`test/cmd.js TAP invalid mode > expect resolving Promise 1`] = `
44+
Object {
45+
"code": 1,
46+
"signal": null,
47+
"stderr": "invalid mode argument: --mode=XYZ\\nMust be an octal number.\\n",
48+
"stdout": "",
49+
}
50+
`
51+
52+
exports[`test/cmd.js TAP make dir named --help > expect resolving Promise 1`] = `
53+
Object {
54+
"code": 0,
55+
"signal": null,
56+
"stderr": "",
57+
"stdout": "--help 0\\n",
58+
}
59+
`
60+
61+
exports[`test/cmd.js TAP making dirs > expect resolving Promise 1`] = `
62+
Object {
63+
"code": 0,
64+
"signal": null,
65+
"stderr": "",
66+
"stdout": "",
67+
}
68+
`
69+
70+
exports[`test/cmd.js TAP manual > expect resolving Promise 1`] = `
71+
Object {
72+
"code": 0,
73+
"signal": null,
74+
"stderr": "",
75+
"stdout": "MANUAL a 0\\nMANUAL b/c/d 0\\n",
76+
}
77+
`
78+
79+
exports[`test/cmd.js TAP no dirs -> stderr usage > expect resolving Promise 1`] = `
80+
Object {
81+
"code": 0,
82+
"signal": null,
83+
"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",
84+
"stdout": "",
85+
}
86+
`
87+
88+
exports[`test/cmd.js TAP noisily > expect resolving Promise 1`] = `
89+
Object {
90+
"code": 0,
91+
"signal": null,
92+
"stderr": "",
93+
"stdout": "a 0\\nb/c/d 0\\n",
94+
}
95+
`
96+
97+
exports[`test/cmd.js TAP print modes > expect resolving Promise 1`] = `
98+
Object {
99+
"code": 0,
100+
"signal": null,
101+
"stderr": "",
102+
"stdout": "a 509\\n",
103+
}
104+
`

test/chmod.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

test/clobber.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

test/cmd.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const cmd = require.resolve('../bin/cmd.js')
2+
const requireInject = require('require-inject')
3+
4+
const {basename} = require('path')
5+
const fakeMkdirp = (path, opts) =>
6+
basename(path) === 'ERROR' ? Promise.reject(new Error('nope'))
7+
: basename(path) === 'EFAIL' ? Promise.reject(Object.assign(new Error('fail'), { code: 'EFAIL' }))
8+
: Promise.resolve(`${path} ${opts.mode || 0}`)
9+
10+
fakeMkdirp.manual = (path, opts) => fakeMkdirp(`MANUAL ${path}`, opts)
11+
12+
if (process.argv[2] === 'RUN') {
13+
process.argv = [process.execPath, cmd, ...process.argv.slice(3)]
14+
requireInject(cmd, {
15+
'../': fakeMkdirp,
16+
'../package.json': {
17+
version: '4.2.0-69.lol',
18+
},
19+
})
20+
} else {
21+
22+
const t = require('tap')
23+
24+
const {spawn} = require('child_process')
25+
const run = (...args) => new Promise((res, rej) => {
26+
const proc = spawn(process.execPath, [__filename, 'RUN', ...args])
27+
const out = []
28+
const err = []
29+
proc.stdout.on('data', c => out.push(c))
30+
proc.stderr.on('data', c => err.push(c))
31+
proc.on('close', (code, signal) => {
32+
res({
33+
code,
34+
signal,
35+
stdout: Buffer.concat(out).toString('utf8'),
36+
stderr: Buffer.concat(err).toString('utf8'),
37+
})
38+
})
39+
})
40+
41+
t.test('-h --help prints usage', t => Promise.all([
42+
run('-h'),
43+
run('--help'),
44+
]).then(res => {
45+
t.strictSame(res[0], res[1], 'same for -h and --help')
46+
t.matchSnapshot(res[0], '--help output')
47+
}))
48+
49+
t.test('no dirs -> stderr usage', t => t.resolveMatchSnapshot(run()))
50+
51+
t.test('-v --version prints version', t => Promise.all([
52+
run('-v'),
53+
run('--version'),
54+
]).then(res => {
55+
t.strictSame(res[0], res[1], 'same for -v and --version')
56+
t.matchSnapshot(res[0], '--version output')
57+
}))
58+
59+
t.test('making dirs', t => t.resolveMatchSnapshot(run('a', 'b/c/d', 'e')))
60+
t.test('noisily', t => t.resolveMatchSnapshot(run('a', 'b/c/d', '--print')))
61+
t.test('manual', t => t.resolveMatchSnapshot(run('a', 'b/c/d', '-p', '--manual')))
62+
t.test('print modes', t => t.resolveMatchSnapshot(run('a', '-m775', '-p')))
63+
t.test('invalid mode', t => t.resolveMatchSnapshot(run('--mode=XYZ')))
64+
t.test('make dir named --help', t => t.resolveMatchSnapshot(run('-p', '--', '--help')))
65+
t.test('failures', t => t.resolveMatchSnapshot(Promise.all([
66+
run('x/ERROR'),
67+
run('x/EFAIL'),
68+
])))
69+
}

test/find-made.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const t = require('tap')
2+
const requireInject = require('require-inject')
3+
4+
const {basename, posix} = require('path')
5+
const {promisify} = require('util')
6+
const fs = require('fs')
7+
8+
const statAsync = (path) =>
9+
basename(path) === 'error'
10+
? Promise.reject(new Error('not a real error'))
11+
: promisify(fs.stat)(path)
12+
13+
const statSync = path => {
14+
if (basename(path) === 'error')
15+
throw new Error('not a real error')
16+
else
17+
return fs.statSync(path)
18+
}
19+
20+
const {findMade, findMadeSync} = requireInject('../lib/find-made.js', {
21+
path: posix,
22+
})
23+
24+
t.test('find what dir will be made', t => {
25+
const dir = t.testdir({
26+
file: 'txt',
27+
subdir: {},
28+
})
29+
30+
const o = {statAsync, statSync}
31+
32+
t.equal(findMadeSync(o, `${dir}/subdir/x/y/z`), `${dir}/subdir/x`)
33+
t.equal(findMadeSync(o, `${dir}/subdir`), undefined)
34+
t.equal(findMadeSync(o, `${dir}/file/x/y/z`), undefined)
35+
t.equal(findMadeSync(o, `${dir}/file`, `${dir}/file/x`), undefined)
36+
t.equal(findMadeSync(o, `${dir}/subdir/error`), undefined)
37+
t.equal(findMadeSync(o, '/', '/'), undefined)
38+
return Promise.all([
39+
findMade(o, `${dir}/subdir/x/y/z`),
40+
findMade(o, `${dir}/subdir`),
41+
findMade(o, `${dir}/file/x/y/z`),
42+
findMade(o, `${dir}/file`, `${dir}/file/x`),
43+
findMade(o, `${dir}/subdir/error`),
44+
findMade(o, '/', '/'),
45+
]).then(made => t.strictSame(made, [
46+
`${dir}/subdir/x`,
47+
undefined,
48+
undefined,
49+
undefined,
50+
undefined,
51+
undefined,
52+
]))
53+
})

test/index.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const t = require('tap')
2+
const mkdirp = require('../')
3+
4+
t.test('module shape', t => {
5+
t.isa(mkdirp, Function)
6+
t.isa(mkdirp.sync, Function)
7+
t.isa(mkdirp.manual, Function)
8+
t.isa(mkdirp.manualSync, Function)
9+
t.isa(mkdirp.native, Function)
10+
t.isa(mkdirp.nativeSync, Function)
11+
t.end()
12+
})
13+
14+
t.test('basic making of dirs should work', t => {
15+
const dir = t.testdir({ a: {} })
16+
const {statSync, mkdir, mkdirSync} = require('fs')
17+
const check = d => t.ok(statSync(d).isDirectory())
18+
t.equal(mkdirp.sync(`${dir}/a/sync`), `${dir}/a/sync`)
19+
check(`${dir}/a/sync`)
20+
t.equal(mkdirp.sync(`${dir}/a/sync`), undefined)
21+
22+
t.equal(mkdirp.manualSync(`${dir}/a/manual-sync`), `${dir}/a/manual-sync`)
23+
check(`${dir}/a/manual-sync`)
24+
t.equal(mkdirp.manualSync(`${dir}/a/manual-sync`), undefined)
25+
26+
t.equal(mkdirp.nativeSync(`${dir}/a/native-sync`), `${dir}/a/native-sync`)
27+
check(`${dir}/a/native-sync`)
28+
t.equal(mkdirp.nativeSync(`${dir}/a/native-sync`), undefined)
29+
30+
// override to force the manual option
31+
const myMkdir = (path, opts, cb) => mkdir(path, opts, cb)
32+
const myMkdirSync = (path, opts) => mkdirSync(path, opts)
33+
const opts = { mkdir: myMkdir, mkdirSync: myMkdirSync }
34+
t.equal(mkdirp.sync(`${dir}/a/custom-sync`, opts), `${dir}/a/custom-sync`)
35+
check(`${dir}/a/custom-sync`)
36+
t.equal(mkdirp.sync(`${dir}/a/custom-sync`, opts), undefined)
37+
38+
return Promise.all([
39+
mkdirp(`${dir}/a/async`),
40+
mkdirp.manual(`${dir}/a/manual-async`),
41+
mkdirp.native(`${dir}/a/native-async`),
42+
mkdirp(`${dir}/a/custom-async`, opts),
43+
]).then(made => {
44+
t.strictSame(made, [
45+
`${dir}/a/async`,
46+
`${dir}/a/manual-async`,
47+
`${dir}/a/native-async`,
48+
`${dir}/a/custom-async`,
49+
])
50+
check(`${dir}/a/async`)
51+
check(`${dir}/a/manual-async`)
52+
check(`${dir}/a/native-async`)
53+
check(`${dir}/a/custom-async`)
54+
return Promise.all([
55+
mkdirp(`${dir}/a/async`),
56+
mkdirp.manual(`${dir}/a/manual-async`),
57+
mkdirp.native(`${dir}/a/native-async`),
58+
mkdirp(`${dir}/a/custom-async`, opts),
59+
])
60+
}).then(made => t.strictSame(made, [undefined, undefined, undefined, undefined]))
61+
})

0 commit comments

Comments
 (0)