2 vows = require('vows'),
3 assert = require('assert'),
5 path = require('path'),
7 existsSync = fs.existsSync || path.existsSync,
9 tmp = require('../lib/tmp.js'),
10 Test = require('./base.js');
13 function _testDir(mode) {
14 return function _testDirGenerated(err, name) {
15 assert.ok(existsSync(name), 'should exist');
17 var stat = fs.statSync(name);
18 assert.ok(stat.isDirectory(), 'should be a directory');
20 Test.testStat(stat, mode);
24 vows.describe('Directory creation').addBatch({
25 'when using without parameters': {
27 tmp.dir(this.callback);
30 'should be a directory': _testDir(040700),
31 'should have the default prefix': Test.testPrefix('tmp-')
34 'when using with prefix': {
36 tmp.dir({ prefix: 'something' }, this.callback);
39 'should not return with an error': assert.isNull,
40 'should return with a name': Test.assertName,
41 'should be a directory': _testDir(040700),
42 'should have the provided prefix': Test.testPrefix('something')
45 'when using with postfix': {
47 tmp.dir({ postfix: '.txt' }, this.callback);
50 'should not return with an error': assert.isNull,
51 'should return with a name': Test.assertName,
52 'should be a directory': _testDir(040700),
53 'should have the provided postfix': Test.testPostfix('.txt')
56 'when using template': {
58 tmp.dir({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') }, this.callback);
61 'should not return with error': assert.isNull,
62 'should return with a name': Test.assertName,
63 'should be a directory': _testDir(040700),
64 'should have the provided prefix': Test.testPrefix('clike-'),
65 'should have the provided postfix': Test.testPostfix('-postfix')
70 tmp.dir({ name: 'using-name' }, this.callback);
73 'should not return with an error': assert.isNull,
74 'should return with a name': Test.assertName,
75 'should be a directory': _testDir(040700),
76 'should have the provided name': Test.testName(path.join(tmp.tmpdir, 'using-name'))
79 'when using multiple options': {
81 tmp.dir({ prefix: 'foo', postfix: 'bar', mode: 0750 }, this.callback);
84 'should not return with an error': assert.isNull,
85 'should return with a name': Test.assertName,
86 'should be a directory': _testDir(040750),
87 'should have the provided prefix': Test.testPrefix('foo'),
88 'should have the provided postfix': Test.testPostfix('bar')
91 'when using multiple options and mode': {
93 tmp.dir({ prefix: 'complicated', postfix: 'options', mode: 0755 }, this.callback);
96 'should not return with an error': assert.isNull,
97 'should return with a name': Test.assertName,
98 'should be a directory': _testDir(040755),
99 'should have the provided prefix': Test.testPrefix('complicated'),
100 'should have the provided postfix': Test.testPostfix('options')
105 tmp.dir({ tries: -1 }, this.callback);
108 'should return with an error': assert.isObject
113 Test.testKeep('dir', '1', this.callback);
116 'should not return with an error': assert.isNull,
117 'should return with a name': Test.assertName,
118 'should be a dir': function (err, name) {
119 _testDir(040700)(err, name);
126 Test.testKeep('dir', '0', this.callback);
129 'should not return with error': assert.isNull,
130 'should return with a name': Test.assertName,
131 'should not exist': function (err, name) {
132 assert.ok(!existsSync(name), 'Directory should be removed');
136 'non graceful testing': {
138 Test.testGraceful('dir', '0', this.callback);
141 'should not return with error': assert.isNull,
142 'should return with a name': Test.assertName,
143 'should be a dir': function (err, name) {
144 _testDir(040700)(err, name);
149 'graceful testing': {
151 Test.testGraceful('dir', '1', this.callback);
154 'should not return with an error': assert.isNull,
155 'should return with a name': Test.assertName,
156 'should not exist': function (err, name) {
157 assert.ok(!existsSync(name), 'Directory should be removed');
161 'unsafeCleanup === true': {
163 Test.testUnsafeCleanup('1', this.callback);
166 'should not return with an error': assert.isNull,
167 'should return with a name': Test.assertName,
168 'should not exist': function (err, name) {
169 assert.ok(!existsSync(name), 'Directory should be removed');
171 'should remove symlinked dir': function(err, name) {
173 !existsSync(name + '/symlinkme-target'),
174 'should remove target'
177 'should not remove contents of symlink dir': function(err, name) {
179 existsSync(__dirname + '/symlinkme/file.js'),
180 'should not remove symlinked directory\'s content'
185 'unsafeCleanup === true with issue62 structure': {
187 Test.testIssue62(this.callback);
190 'should not return with an error': assert.isNull,
191 'should return with a name': Test.assertName,
192 'should not exist': function (err, name) {
193 assert.ok(!existsSync(name), 'Directory should be removed');
197 'unsafeCleanup === false': {
199 Test.testUnsafeCleanup('0', this.callback);
202 'should not return with an error': assert.isNull,
203 'should return with a name': Test.assertName,
204 'should be a directory': function (err, name) {
205 _testDir(040700)(err, name);
206 // make sure that everything gets cleaned up
207 fs.unlinkSync(path.join(name, 'should-be-removed.file'));
208 fs.unlinkSync(path.join(name, 'symlinkme-target'));
215 tmp.dir(this.callback);
218 'should not return with an error': assert.isNull,
219 'should return with a name': Test.assertName,
220 'removeCallback should remove directory': function (_err, name, removeCallback) {
222 assert.ok(!existsSync(name), 'Directory should be removed');