• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var path = require('path')
2
3var test = require('tap').test
4var statSync = require('graceful-fs').statSync
5var writeFile = require('graceful-fs').writeFile
6var readdirSync = require('graceful-fs').readdirSync
7var mkdtemp = require('tmp').dir
8var mkdirp = require('mkdirp')
9
10var vacuum = require('../vacuum.js')
11
12// CONSTANTS
13var TEMP_OPTIONS = {
14  unsafeCleanup: true,
15  mode: '0700'
16}
17var SHORT_PATH = path.join('i', 'am', 'a', 'path')
18var PARTIAL_PATH = path.join(SHORT_PATH, 'that', 'ends', 'at', 'a')
19var FULL_PATH = path.join(PARTIAL_PATH, 'file')
20
21var messages = []
22function log () { messages.push(Array.prototype.slice.call(arguments).join(' ')) }
23
24var testBase, partialPath, fullPath
25test('xXx setup xXx', function (t) {
26  mkdtemp(TEMP_OPTIONS, function (er, tmpdir) {
27    t.ifError(er, 'temp directory exists')
28
29    testBase = path.resolve(tmpdir, SHORT_PATH)
30    partialPath = path.resolve(tmpdir, PARTIAL_PATH)
31    fullPath = path.resolve(tmpdir, FULL_PATH)
32
33    mkdirp(partialPath, function (er) {
34      t.ifError(er, 'made test path')
35
36      writeFile(fullPath, new Buffer('hi'), function (er) {
37        t.ifError(er, 'made file')
38
39        t.end()
40      })
41    })
42  })
43})
44
45test('remove up to a point', function (t) {
46  vacuum(fullPath, {purge: false, base: testBase, log: log}, function (er) {
47    t.ifError(er, 'cleaned up to base')
48
49    t.equal(messages.length, 6, 'got 5 removal & 1 finish message')
50    t.equal(messages[5], 'finished vacuuming up to ' + testBase)
51
52    var stat
53    var verifyPath = fullPath
54
55    function verify () { stat = statSync(verifyPath) }
56
57    // handle the file separately
58    t.throws(verify, verifyPath + ' cannot be statted')
59    t.notOk(stat && stat.isFile(), verifyPath + ' is totally gone')
60    verifyPath = path.dirname(verifyPath)
61
62    for (var i = 0; i < 4; i++) {
63      t.throws(verify, verifyPath + ' cannot be statted')
64      t.notOk(stat && stat.isDirectory(), verifyPath + ' is totally gone')
65      verifyPath = path.dirname(verifyPath)
66    }
67
68    t.doesNotThrow(function () {
69      stat = statSync(testBase)
70    }, testBase + ' can be statted')
71    t.ok(stat && stat.isDirectory(), testBase + ' is still a directory')
72
73    var files = readdirSync(testBase)
74    t.equal(files.length, 0, 'nothing left in base directory')
75
76    t.end()
77  })
78})
79