Skip to content

Commit

Permalink
provide mkdirRecursive polyfill for old node versions
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Mar 31, 2021
1 parent 8f4c855 commit 2c2e75f
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 34 deletions.
6 changes: 4 additions & 2 deletions bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ const mainAsync = async options => {
if (options['libtap-settings'])
process.env.TAP_LIBTAP_SETTINGS = path.resolve(options['libtap-settings'])

require('../settings.js')

// we test this directly, not from here.
/* istanbul ignore next */
if (options.watch)
Expand Down Expand Up @@ -246,7 +248,7 @@ const mainAsync = async options => {
debug('after globbing', options.files)

if (options['output-dir'] !== null)
fs.mkdirSync(options['output-dir'], {recursive: true})
require('../settings.js').mkdirRecursiveSync(options['output-dir'])

if (options.files.length === 1 && options.files[0] === '-') {
debug('do stdin only')
Expand Down Expand Up @@ -607,7 +609,7 @@ const runAllFiles = (options, env, tap, processDB) => {
if (options['output-dir'] !== null) {
tap.on('spawn', t => {
const dir = options['output-dir'] + '/' + path.dirname(t.name)
fs.mkdirSync(dir, {recursive: true})
require('../settings.js').mkdirRecursiveSync(dir)
const file = dir + '/' + path.basename(t.name) + '.tap'
t.proc.stdout.pipe(fs.createWriteStream(file))
})
Expand Down
2 changes: 1 addition & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Repl {
saveHistory () {
const dir = process.env.HOME || 'node_modules/.cache/tap'

fs.mkdirSync(dir, {recursive: true})
require('../settings.js').mkdirRecursiveSync(dir)
try {
fs.writeFileSync(dir + '/.tap_repl_history',
this.repl.history.join('\n').trim())
Expand Down
9 changes: 1 addition & 8 deletions lib/tap.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
'use strict'
const {deprecate} = require('util')

const settings = require('../settings.js')
/* istanbul ignore next - version specific */
if (settings.rimrafNeeded) {
const rimraf = require('rimraf')
settings.rmdirRecursive = (path, cb) => rimraf(path, { glob: false }, cb)
settings.rmdirRecursiveSync = path => rimraf.sync(path, { glob: false })
}

const tap = require('libtap')

// Needs to be set before requiring mocha.js
module.exports = tap

Expand Down
7 changes: 3 additions & 4 deletions lib/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ const EE = require('events')
const Minipass = require('minipass')
const bin = require.resolve('../bin/run.js')
const {spawn} = require('child_process')
const rimraf = require('rimraf').sync
const onExit = require('signal-exit')
const {writeFileSync, readFileSync, mkdirSync} = require('fs')
const {writeFileSync, readFileSync} = require('fs')
const {stringify} = require('tap-yaml')
const {resolve} = require('path')

Expand All @@ -22,10 +21,10 @@ class Watch extends Minipass {
})
this.proc.on('close', () => this.main())
const saveFolder = 'node_modules/.cache/tap'
mkdirSync(saveFolder, {recursive: true})
require('../settings.js').mkdirRecursiveSync(saveFolder)
this.saveFile = saveFolder + '/watch-' + process.pid
/* istanbul ignore next */
onExit(() => rimraf(this.saveFile))
onExit(() => require('../settings.js').rmdirRecursiveSync(this.saveFile))
this.index = null
this.indexFile = '.nyc_output/processinfo/index.json'
this.fileList = []
Expand Down
31 changes: 24 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
"isexe": "^2.0.0",
"istanbul-lib-processinfo": "^2.0.2",
"jackspeak": "^1.4.0",
"libtap": "^1.0.1",
"libtap": "^1.1.1",
"minipass": "^3.1.1",
"mkdirp": "^1.0.4",
"nyc": "^15.1.0",
"opener": "^1.5.1",
"react": "^16.12.0",
Expand Down
14 changes: 14 additions & 0 deletions settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ if (+process.env.TAP_DEV_LONGSTACK !== 1) {

settings.stackUtils.wrapCallSite = sourceMapSupport.wrapCallSite

/* istanbul ignore next - version specific */
if (settings.rimrafNeeded) {
const rimraf = require('rimraf')
settings.rmdirRecursive = (path, cb) => rimraf(path, { glob: false }, cb)
settings.rmdirRecursiveSync = path => rimraf.sync(path, { glob: false })
}

/* istanbul ignore next - version specific */
if (settings.mkdirpNeeded) {
const mkdirp = require('mkdirp')
settings.mkdirRecursive = (path, cb) => mkdirp(path).then(() => cb).catch(cb)
settings.mkdirRecursiveSync = path => mkdirp.sync(path)
}

if (process.env.TAP_LIBTAP_SETTINGS) {
const overrides = require(process.env.TAP_LIBTAP_SETTINGS)
const type = typeof overrides
Expand Down
3 changes: 3 additions & 0 deletions tap-snapshots/test/settings/default.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
exports[`test/settings/default.js TAP > must match snapshot 1`] = `
Object {
"atTap": false,
"mkdirpNeeded": false,
"mkdirRecursive": "function(path, cb)",
"mkdirRecursiveSync": "function(path)",
"output": true,
"rimrafNeeded": false,
"rmdirRecursive": "function(path, cb)",
Expand Down
3 changes: 3 additions & 0 deletions tap-snapshots/test/settings/long-stack.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
exports[`test/settings/long-stack.js TAP > must match snapshot 1`] = `
Object {
"atTap": true,
"mkdirpNeeded": false,
"mkdirRecursive": "function(path, cb)",
"mkdirRecursiveSync": "function(path)",
"output": true,
"rimrafNeeded": false,
"rmdirRecursive": "function(path, cb)",
Expand Down
9 changes: 5 additions & 4 deletions test/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ const originalCwd = process.cwd()
const rimraf = require('rimraf').sync

const dir = 'repl-test'
fs.mkdirSync(dir, {recursive: true})
const mkdirp = require('mkdirp')
mkdirp.sync(dir)
process.chdir(dir)
t.teardown(() => {
process.chdir(originalCwd)
Expand Down Expand Up @@ -194,7 +195,7 @@ t.test('coverage report', t => {
})

t.test('clear', t => {
fs.mkdirSync('.nyc_output', {recursive: true})
mkdirp.sync('.nyc_output')
input.write('clear\n')
t.notOk(fs.existsSync('.nyc_output'))
fakeWatch.once('afterProcess', () => process.nextTick(() => {
Expand All @@ -204,8 +205,8 @@ t.test('clear', t => {
})

t.test('completer', t => {
fs.mkdirSync('test/foo', {recursive: true})
fs.mkdirSync('temp/orary', {recursive: true})
mkdirp.sync('test/foo')
mkdirp.sync('temp/orary')
fs.writeFileSync('test/foo/bar.js', '')
fs.writeFileSync('test/follow.js', '')
const tests = [
Expand Down
5 changes: 3 additions & 2 deletions test/run/basic.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs')
const mkdirp = require('mkdirp')
const {
tmpfile,
run,
Expand Down Expand Up @@ -106,8 +107,8 @@ t.test('basic test run', t => {
})

t.test('ignored files', t => {
fs.mkdirSync(`${dir}/ig/test/node_modules`, {recursive: true})
fs.mkdirSync(`${dir}/ig/node_modules`, {recursive: true})
mkdirp.sync(`${dir}/ig/test/node_modules`)
mkdirp.sync(`${dir}/ig/node_modules`)
const ok = tmpfile(t, 'ig/test/ok.js',
`require(${tap}).pass('this is fine')`)
const nope = tmpfile(t, 'ig/node_modules/nope.test.js',
Expand Down
5 changes: 3 additions & 2 deletions test/run/changed.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const fs = require('fs')

const rewrite = file => fs.writeFileSync(file, fs.readFileSync(file))

fs.mkdirSync(dir, {recursive: true})
const mkdirp = require('mkdirp')
mkdirp.sync(dir)
const cwd = process.cwd()
process.chdir(dir)
t.teardown(() => process.chdir(cwd))
Expand Down Expand Up @@ -61,7 +62,7 @@ t.test('no index file means we let everything through', t => {
})

t.test('with a real index', t => {
fs.mkdirSync('.nyc_output/processinfo', {recursive: true})
mkdirp.sync('.nyc_output/processinfo')
const pauseLength = 10
const index = '.nyc_output/processinfo/index.json'
const indexData = {
Expand Down
5 changes: 3 additions & 2 deletions test/run/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'
const fs = require('fs')
const mkdirp = require('mkdirp')
const rimraf = require('rimraf')
// const rimraf = { sync: () => {} }
const path = require('path')
Expand Down Expand Up @@ -52,7 +53,7 @@ const tmpfile = (t, filename, content) => {
const parts = filename.split('/')
// make any necessary dirs
if (parts.length > 1)
fs.mkdirSync(path.join(dir, parts.slice(0, -1).join('/')), {recursive: true})
mkdirp.sync(path.join(dir, parts.slice(0, -1).join('/')))
if (t.tmpfiles)
t.tmpfiles.push(path.join(dir, parts[0]))
else {
Expand Down Expand Up @@ -94,7 +95,7 @@ const escapeNYC = () => {
if (module === require.main)
t.pass('this is fine')
else {
fs.mkdirSync(dir, {recursive: true})
mkdirp.sync(dir)
if (process.env._TAP_TEST_NO_TEARDOWN_TMP !== '1') {
t.teardown(() => rimraf.sync(dir))
process.on('exit', () => rimraf.sync(dir))
Expand Down
3 changes: 3 additions & 0 deletions test/settings/clean-for-snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ module.exports = settings => ({
...settings.stackUtils,
internals: []
},
mkdirpNeeded: false,
mkdirRecursive: 'function(path, cb)',
mkdirRecursiveSync: 'function(path)',
rimrafNeeded: false,
rmdirRecursive: 'function(path, cb)',
rmdirRecursiveSync: 'function(path)',
Expand Down
3 changes: 2 additions & 1 deletion test/watch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// just load this to assign the runner test's output cleaner
require('./run/index.js')
const mkdirp = require('mkdirp')

// spawn mock
// log what's being run, and then write out the index
Expand Down Expand Up @@ -123,7 +124,7 @@ const index = {
const rimraf = require('rimraf').sync
const fs = require('fs')
const pidir = dir + '/.nyc_output/processinfo'
fs.mkdirSync(pidir, {recursive: true})
mkdirp.sync(pidir)
const read = file => fs.existsSync(file) && fs.readFileSync(file, 'utf8')
const saveFile = 'node_modules/.cache/tap/watch-' + process.pid

Expand Down

0 comments on commit 2c2e75f

Please sign in to comment.