Browse Source

util for test

ameba23 2 years ago
parent
commit
e76fe04953
1 changed files with 41 additions and 0 deletions
  1. 41
    0
      test/util.js

+ 41
- 0
test/util.js View File

@@ -0,0 +1,41 @@
1
+const tmpdir = require('tmp').dirSync
2
+const mkdirp = require('mkdirp')
3
+const rimraf = require('rimraf')
4
+
5
+function cleanup (dirs, cb) {
6
+  if (!cb) cb = noop
7
+  if (!Array.isArray(dirs)) dirs = [dirs]
8
+  var pending = 1
9
+
10
+  function next (n) {
11
+    var dir = dirs[n]
12
+    if (!dir) return done()
13
+    ++pending
14
+    process.nextTick(next, n + 1)
15
+
16
+    rimraf(dir, (err) => {
17
+      if (err) return done(err)
18
+      done()
19
+    })
20
+  }
21
+
22
+  function done (err) {
23
+    if (err) {
24
+      pending = Infinity
25
+      return cb(err)
26
+    }
27
+    if (!--pending) return cb()
28
+  }
29
+
30
+  next(0)
31
+}
32
+
33
+function tmp () {
34
+  var path = tmpdir().name
35
+  mkdirp.sync(path)
36
+  return path
37
+}
38
+
39
+function noop () {}
40
+
41
+module.exports = { cleanup, tmp }