1 | var
|
2 | vows = require('vows'),
|
3 | assert = require('assert'),
|
4 |
|
5 | path = require('path'),
|
6 | fs = require('fs'),
|
7 | existsSync = fs.existsSync || path.existsSync,
|
8 |
|
9 | tmp = require('../lib/tmp.js'),
|
10 | Test = require('./base.js');
|
11 |
|
12 |
|
13 | function _testFile(mode, fdTest) {
|
14 | return function _testFileGenerated(result) {
|
15 | assert.ok(existsSync(result.name), 'should exist');
|
16 |
|
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');
|
20 |
|
21 | Test.testStat(stat, mode);
|
22 |
|
23 |
|
24 | if (fdTest) {
|
25 | var fstat = fs.fstatSync(result.fd);
|
26 | assert.deepEqual(fstat, stat, 'fstat results should be the same');
|
27 |
|
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');
|
31 | }
|
32 | };
|
33 | }
|
34 |
|
35 | vows.describe('Synchronous file creation').addBatch({
|
36 | 'when using without parameters': {
|
37 | topic: function () {
|
38 | return tmp.fileSync();
|
39 | },
|
40 |
|
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')
|
45 | },
|
46 |
|
47 | 'when using with prefix': {
|
48 | topic: function () {
|
49 | return tmp.fileSync({ prefix: 'something' });
|
50 | },
|
51 |
|
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')
|
55 | },
|
56 |
|
57 | 'when using with postfix': {
|
58 | topic: function () {
|
59 | return tmp.fileSync({ postfix: '.txt' });
|
60 | },
|
61 |
|
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')
|
65 | },
|
66 |
|
67 | 'when using template': {
|
68 | topic: function () {
|
69 | return tmp.fileSync({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') });
|
70 | },
|
71 |
|
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')
|
76 | },
|
77 |
|
78 | 'when using name': {
|
79 | topic: function () {
|
80 | return tmp.fileSync({ name: 'using-name.tmp' });
|
81 | },
|
82 |
|
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);
|
88 | }
|
89 | },
|
90 |
|
91 | 'when using multiple options': {
|
92 | topic: function () {
|
93 | return tmp.fileSync({ prefix: 'foo', postfix: 'bar', mode: 0640 });
|
94 | },
|
95 |
|
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')
|
100 | },
|
101 |
|
102 | 'when using multiple options and mode': {
|
103 | topic: function () {
|
104 | return tmp.fileSync({ prefix: 'complicated', postfix: 'options', mode: 0644 });
|
105 | },
|
106 |
|
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')
|
111 | },
|
112 |
|
113 | 'no tries': {
|
114 | topic: function () {
|
115 | try {
|
116 | return tmp.fileSync({ tries: -1 });
|
117 | }
|
118 | catch (e) {
|
119 | return e;
|
120 | }
|
121 | },
|
122 |
|
123 | 'should return with an error': function (topic) {
|
124 | assert.instanceOf(topic, Error);
|
125 | }
|
126 | },
|
127 |
|
128 | 'keep testing': {
|
129 | topic: function () {
|
130 | Test.testKeepSync('file', '1', this.callback);
|
131 | },
|
132 |
|
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});
|
137 | fs.unlinkSync(name);
|
138 | }
|
139 | },
|
140 |
|
141 | 'unlink testing': {
|
142 | topic: function () {
|
143 | Test.testKeepSync('file', '0', this.callback);
|
144 | },
|
145 |
|
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');
|
150 | }
|
151 | },
|
152 |
|
153 | 'non graceful testing': {
|
154 | topic: function () {
|
155 | Test.testGracefulSync('file', '0', this.callback);
|
156 | },
|
157 |
|
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});
|
162 | fs.unlinkSync(name);
|
163 | }
|
164 | },
|
165 |
|
166 | 'graceful testing': {
|
167 | topic: function () {
|
168 | Test.testGracefulSync('file', '1', this.callback);
|
169 | },
|
170 |
|
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');
|
175 | }
|
176 | },
|
177 |
|
178 | 'remove callback': {
|
179 | topic: function () {
|
180 | return tmp.fileSync();
|
181 | },
|
182 |
|
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');
|
187 | }
|
188 | }
|
189 |
|
190 | }).exportTo(module);
|