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 _testFile(mode, fdTest) {
14 return function _testFileGenerated(result) {
15 assert.ok(existsSync(result.name), 'should exist');
17 var stat = fs.statSync(result.name);
18 assert.equal(stat.size, 0, 'should have zero size');
19 assert.ok(stat.isFile(), 'should be a file');
21 Test.testStat(stat, mode);
23 // check with fstat as well (fd checking)
25 var fstat = fs.fstatSync(result.fd);
26 assert.deepEqual(fstat, stat, 'fstat results should be the same');
28 var data = new Buffer('something');
29 assert.equal(fs.writeSync(result.fd, data, 0, data.length, 0), data.length, 'should be writable');
30 assert.ok(!fs.closeSync(result.fd), 'should not return with error');
35 vows.describe('Synchronous file creation').addBatch({
36 'when using without parameters': {
38 return tmp.fileSync();
41 'should return with a name': Test.assertNameSync,
42 'should be a file': _testFile(0100600, true),
43 'should have the default prefix': Test.testPrefixSync('tmp-'),
44 'should have the default postfix': Test.testPostfixSync('.tmp')
47 'when using with prefix': {
49 return tmp.fileSync({ prefix: 'something' });
52 'should return with a name': Test.assertNameSync,
53 'should be a file': _testFile(0100600, true),
54 'should have the provided prefix': Test.testPrefixSync('something')
57 'when using with postfix': {
59 return tmp.fileSync({ postfix: '.txt' });
62 'should return with a name': Test.assertNameSync,
63 'should be a file': _testFile(0100600, true),
64 'should have the provided postfix': Test.testPostfixSync('.txt')
67 'when using template': {
69 return tmp.fileSync({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') });
72 'should return with a name': Test.assertNameSync,
73 'should be a file': _testFile(0100600, true),
74 'should have the provided prefix': Test.testPrefixSync('clike-'),
75 'should have the provided postfix': Test.testPostfixSync('-postfix')
80 return tmp.fileSync({ name: 'using-name.tmp' });
83 'should return with a name': Test.assertNameSync,
84 'should have the provided name': Test.testNameSync(path.join(tmp.tmpdir, 'using-name.tmp')),
85 'should be a file': function (result) {
86 _testFile(0100600, true);
87 fs.unlinkSync(result.name);
91 'when using multiple options': {
93 return tmp.fileSync({ prefix: 'foo', postfix: 'bar', mode: 0640 });
96 'should return with a name': Test.assertNameSync,
97 'should be a file': _testFile(0100640, true),
98 'should have the provided prefix': Test.testPrefixSync('foo'),
99 'should have the provided postfix': Test.testPostfixSync('bar')
102 'when using multiple options and mode': {
104 return tmp.fileSync({ prefix: 'complicated', postfix: 'options', mode: 0644 });
107 'should return with a name': Test.assertNameSync,
108 'should be a file': _testFile(0100644, true),
109 'should have the provided prefix': Test.testPrefixSync('complicated'),
110 'should have the provided postfix': Test.testPostfixSync('options')
116 return tmp.fileSync({ tries: -1 });
123 'should return with an error': function (topic) {
124 assert.instanceOf(topic, Error);
130 Test.testKeepSync('file', '1', this.callback);
133 'should not return with an error': assert.isNull,
134 'should return with a name': Test.assertName,
135 'should be a file': function (err, name) {
136 _testFile(0100600, false)({name:name});
143 Test.testKeepSync('file', '0', this.callback);
146 'should not return with an error': assert.isNull,
147 'should return with a name': Test.assertName,
148 'should not exist': function (err, name) {
149 assert.ok(!existsSync(name), 'File should be removed');
153 'non graceful testing': {
155 Test.testGracefulSync('file', '0', this.callback);
158 'should not return with error': assert.isNull,
159 'should return with a name': Test.assertName,
160 'should be a file': function (err, name) {
161 _testFile(0100600, false)({name:name});
166 'graceful testing': {
168 Test.testGracefulSync('file', '1', this.callback);
171 'should not return with an error': assert.isNull,
172 'should return with a name': Test.assertName,
173 'should not exist': function (err, name) {
174 assert.ok(!existsSync(name), 'File should be removed');
180 return tmp.fileSync();
183 'should return with a name': Test.assertNameSync,
184 'removeCallback should remove file': function (result) {
185 result.removeCallback();
186 assert.ok(!existsSync(result.name), 'File should be removed');