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(result) {
15 assert.ok(existsSync(result.name), 'should exist');
17 var stat = fs.statSync(result.name);
18 assert.ok(stat.isDirectory(), 'should be a directory');
20 Test.testStat(stat, mode);
24 vows.describe('Synchronous directory creation').addBatch({
25 'when using without parameters': {
30 'should return with a name': Test.assertNameSync,
31 'should be a directory': _testDir(040700),
32 'should have the default prefix': Test.testPrefixSync('tmp-')
35 'when using with prefix': {
37 return tmp.dirSync({ prefix: 'something' });
40 'should return with a name': Test.assertNameSync,
41 'should be a directory': _testDir(040700),
42 'should have the provided prefix': Test.testPrefixSync('something')
45 'when using with postfix': {
47 return tmp.dirSync({ postfix: '.txt' });
50 'should return with a name': Test.assertNameSync,
51 'should be a directory': _testDir(040700),
52 'should have the provided postfix': Test.testPostfixSync('.txt')
55 'when using template': {
57 return tmp.dirSync({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') });
60 'should return with a name': Test.assertNameSync,
61 'should be a directory': _testDir(040700),
62 'should have the provided prefix': Test.testPrefixSync('clike-'),
63 'should have the provided postfix': Test.testPostfixSync('-postfix')
68 return tmp.dirSync({ name: 'using-name' });
71 'should return with a name': Test.assertNameSync,
72 'should have the provided name': Test.testNameSync(path.join(tmp.tmpdir, 'using-name')),
73 'should be a directory': function (result) {
74 _testDir(040700)(result);
75 result.removeCallback();
76 assert.ok(!existsSync(result.name), 'Directory should be removed');
80 'when using multiple options': {
82 return tmp.dirSync({ prefix: 'foo', postfix: 'bar', mode: 0750 });
85 'should return with a name': Test.assertNameSync,
86 'should be a directory': _testDir(040750),
87 'should have the provided prefix': Test.testPrefixSync('foo'),
88 'should have the provided postfix': Test.testPostfixSync('bar')
91 'when using multiple options and mode': {
93 return tmp.dirSync({ prefix: 'complicated', postfix: 'options', mode: 0755 });
96 'should return with a name': Test.assertNameSync,
97 'should be a directory': _testDir(040755),
98 'should have the provided prefix': Test.testPrefixSync('complicated'),
99 'should have the provided postfix': Test.testPostfixSync('options')
105 return tmp.dirSync({ tries: -1 });
112 'should return with an error': function (topic) {
113 assert.instanceOf(topic, Error);
119 Test.testKeepSync('dir', '1', this.callback);
122 'should not return with an error': assert.isNull,
123 'should return with a name': Test.assertName,
124 'should be a dir': function (err, name) {
125 _testDir(040700)({ name: name });
132 Test.testKeepSync('dir', '0', this.callback);
135 'should not return with error': assert.isNull,
136 'should return with a name': Test.assertName,
137 'should not exist': function (err, name) {
138 assert.ok(!existsSync(name), 'Directory should be removed');
142 'non graceful testing': {
144 Test.testGracefulSync('dir', '0', this.callback);
147 'should not return with error': assert.isNull,
148 'should return with a name': Test.assertName,
149 'should be a dir': function (err, name) {
150 _testDir(040700)({ name: name });
155 'graceful testing': {
157 Test.testGracefulSync('dir', '1', this.callback);
160 'should not return with an error': assert.isNull,
161 'should return with a name': Test.assertName,
162 'should not exist': function (err, name) {
163 assert.ok(!existsSync(name), 'Directory should be removed');
167 'unsafeCleanup === true': {
169 Test.testUnsafeCleanupSync('1', this.callback);
172 'should not return with an error': assert.isNull,
173 'should return with a name': Test.assertName,
174 'should not exist': function (err, name) {
175 assert.ok(!existsSync(name), 'Directory should be removed');
177 'should remove symlinked dir': function(err, name) {
179 !existsSync(name + '/symlinkme-target'),
180 'should remove target'
183 'should not remove contents of symlink dir': function(err, name) {
185 existsSync(__dirname + '/symlinkme/file.js'),
186 'should not remove symlinked directory\'s content'
191 'unsafeCleanup === true with issue62 structure': {
193 Test.testIssue62Sync(this.callback);
196 'should not return with an error': assert.isNull,
197 'should return with a name': Test.assertName,
198 'should not exist': function (err, name) {
199 assert.ok(!existsSync(name), 'Directory should be removed');
203 'unsafeCleanup === false': {
205 Test.testUnsafeCleanupSync('0', this.callback);
208 'should not return with an error': assert.isNull,
209 'should return with a name': Test.assertName,
210 'should be a directory': function (err, name) {
211 _testDir(040700)({name:name});
212 // make sure that everything gets cleaned up
213 fs.unlinkSync(path.join(name, 'should-be-removed.file'));
214 fs.unlinkSync(path.join(name, 'symlinkme-target'));
221 return tmp.dirSync();
224 'should return with a name': Test.assertNameSync,
225 'removeCallback should remove directory': function (result) {
226 result.removeCallback();
227 assert.ok(!existsSync(result.name), 'Directory should be removed');