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(err, name, fd) {
|
15 | assert.ok(existsSync(name), 'should exist');
|
16 |
|
17 | var stat = fs.statSync(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(fd);
|
26 | assert.deepEqual(fstat, stat, 'fstat results should be the same');
|
27 |
|
28 | var data = new Buffer('something');
|
29 | assert.equal(fs.writeSync(fd, data, 0, data.length, 0), data.length, 'should be writable');
|
30 | assert.ok(!fs.closeSync(fd), 'should not return with error');
|
31 | }
|
32 | };
|
33 | }
|
34 |
|
35 | vows.describe('File creation').addBatch({
|
36 | 'when using without parameters': {
|
37 | topic: function () {
|
38 | tmp.file(this.callback);
|
39 | },
|
40 |
|
41 | 'should not return with an error': assert.isNull,
|
42 | 'should return with a name': Test.assertName,
|
43 | 'should be a file': _testFile(0100600, true),
|
44 | 'should have the default prefix': Test.testPrefix('tmp-'),
|
45 | 'should have the default postfix': Test.testPostfix('.tmp')
|
46 | },
|
47 |
|
48 | 'when using with prefix': {
|
49 | topic: function () {
|
50 | tmp.file({ prefix: 'something' }, this.callback);
|
51 | },
|
52 |
|
53 | 'should not return with an error': assert.isNull,
|
54 | 'should return with a name': Test.assertName,
|
55 | 'should be a file': _testFile(0100600, true),
|
56 | 'should have the provided prefix': Test.testPrefix('something')
|
57 | },
|
58 |
|
59 | 'when using with postfix': {
|
60 | topic: function () {
|
61 | tmp.file({ postfix: '.txt' }, this.callback);
|
62 | },
|
63 |
|
64 | 'should not return with an error': assert.isNull,
|
65 | 'should return with a name': Test.assertName,
|
66 | 'should be a file': _testFile(0100600, true),
|
67 | 'should have the provided postfix': Test.testPostfix('.txt')
|
68 | },
|
69 |
|
70 | 'when using template': {
|
71 | topic: function () {
|
72 | tmp.file({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') }, this.callback);
|
73 | },
|
74 |
|
75 | 'should not return with an error': assert.isNull,
|
76 | 'should return with a name': Test.assertName,
|
77 | 'should be a file': _testFile(0100600, true),
|
78 | 'should have the provided prefix': Test.testPrefix('clike-'),
|
79 | 'should have the provided postfix': Test.testPostfix('-postfix')
|
80 | },
|
81 |
|
82 | 'when using name': {
|
83 | topic: function () {
|
84 | tmp.file({ name: 'using-name.tmp' }, this.callback);
|
85 | },
|
86 |
|
87 | 'should not return with an error': assert.isNull,
|
88 | 'should return with a name': Test.assertName,
|
89 | 'should have the provided name': Test.testName(path.join(tmp.tmpdir, 'using-name.tmp')),
|
90 | 'should be a file': function (err, name) {
|
91 | _testFile(0100600, true);
|
92 | fs.unlinkSync(name);
|
93 | }
|
94 | },
|
95 |
|
96 | 'when using multiple options': {
|
97 | topic: function () {
|
98 | tmp.file({ prefix: 'foo', postfix: 'bar', mode: 0640 }, this.callback);
|
99 | },
|
100 |
|
101 | 'should not return with an error': assert.isNull,
|
102 | 'should return with a name': Test.assertName,
|
103 | 'should be a file': _testFile(0100640, true),
|
104 | 'should have the provided prefix': Test.testPrefix('foo'),
|
105 | 'should have the provided postfix': Test.testPostfix('bar')
|
106 | },
|
107 |
|
108 | 'when using multiple options and mode': {
|
109 | topic: function () {
|
110 | tmp.file({ prefix: 'complicated', postfix: 'options', mode: 0644 }, this.callback);
|
111 | },
|
112 |
|
113 | 'should not return with an error': assert.isNull,
|
114 | 'should return with a name': Test.assertName,
|
115 | 'should be a file': _testFile(0100644, true),
|
116 | 'should have the provided prefix': Test.testPrefix('complicated'),
|
117 | 'should have the provided postfix': Test.testPostfix('options')
|
118 | },
|
119 |
|
120 | 'no tries': {
|
121 | topic: function () {
|
122 | tmp.file({ tries: -1 }, this.callback);
|
123 | },
|
124 |
|
125 | 'should not be created': assert.isObject
|
126 | },
|
127 |
|
128 | 'keep testing': {
|
129 | topic: function () {
|
130 | Test.testKeep('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)(err, name, null);
|
137 | fs.unlinkSync(name);
|
138 | }
|
139 | },
|
140 |
|
141 | 'unlink testing': {
|
142 | topic: function () {
|
143 | Test.testKeep('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.testGraceful('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)(err, name, null);
|
162 | fs.unlinkSync(name);
|
163 | }
|
164 | },
|
165 |
|
166 | 'graceful testing': {
|
167 | topic: function () {
|
168 | Test.testGraceful('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 | tmp.file(this.callback);
|
181 | },
|
182 |
|
183 | 'should not return with an error': assert.isNull,
|
184 | 'should return with a name': Test.assertName,
|
185 | 'removeCallback should remove file': function (_err, name, _fd, removeCallback) {
|
186 | removeCallback();
|
187 | assert.ok(!existsSync(name), 'File should be removed');
|
188 | }
|
189 | }
|
190 |
|
191 | }).exportTo(module);
|