• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var path = require('path')
2
3var test = require('tap').test
4var statSync = require('graceful-fs').statSync
5var writeFileSync = require('graceful-fs').writeFileSync
6var symlinkSync = require('graceful-fs').symlinkSync
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 TARGET_PATH = 'link-target'
19var FIRST_FILE = path.join(TARGET_PATH, 'monsieurs')
20var SECOND_FILE = path.join(TARGET_PATH, 'mesdames')
21var PARTIAL_PATH = path.join(SHORT_PATH, 'with', 'a', 'definite')
22var FULL_PATH = path.join(PARTIAL_PATH, 'target')
23
24var messages = []
25function log () { messages.push(Array.prototype.slice.call(arguments).join(' ')) }
26
27var testBase, partialPath, fullPath, targetPath
28test('xXx setup xXx', function (t) {
29  mkdtemp(TEMP_OPTIONS, function (er, tmpdir) {
30    t.ifError(er, 'temp directory exists')
31
32    testBase = path.resolve(tmpdir, SHORT_PATH)
33    targetPath = path.resolve(tmpdir, TARGET_PATH)
34    partialPath = path.resolve(tmpdir, PARTIAL_PATH)
35    fullPath = path.resolve(tmpdir, FULL_PATH)
36
37    mkdirp(partialPath, function (er) {
38      t.ifError(er, 'made test path')
39
40      mkdirp(targetPath, function (er) {
41        t.ifError(er, 'made target path')
42
43        writeFileSync(path.resolve(tmpdir, FIRST_FILE), new Buffer("c'est vraiment joli"))
44        writeFileSync(path.resolve(tmpdir, SECOND_FILE), new Buffer('oui oui'))
45        symlinkSync(targetPath, fullPath)
46
47        t.end()
48      })
49    })
50  })
51})
52
53test('remove up to a point', function (t) {
54  vacuum(fullPath, {purge: true, base: testBase, log: log}, function (er) {
55    t.ifError(er, 'cleaned up to base')
56
57    t.equal(messages.length, 5, 'got 4 removal & 1 finish message')
58    t.equal(messages[0], 'purging ' + fullPath)
59    t.equal(messages[4], 'finished vacuuming up to ' + testBase)
60
61    var stat
62    var verifyPath = fullPath
63    function verify () { stat = statSync(verifyPath) }
64
65    for (var i = 0; i < 4; i++) {
66      t.throws(verify, verifyPath + ' cannot be statted')
67      t.notOk(stat && stat.isDirectory(), verifyPath + ' is totally gone')
68      verifyPath = path.dirname(verifyPath)
69    }
70
71    t.doesNotThrow(function () {
72      stat = statSync(testBase)
73    }, testBase + ' can be statted')
74    t.ok(stat && stat.isDirectory(), testBase + ' is still a directory')
75
76    t.end()
77  })
78})
79