Initial commit
[yaffs-website] / node_modules / tmp / test / base.js
1 var
2   assert = require('assert'),
3   path   = require('path'),
4   exec   = require('child_process').exec,
5   tmp    = require('../lib/tmp');
6
7 // make sure that we do not test spam the global tmp
8 tmp.TMP_DIR = './tmp';
9
10
11 function _spawnTestWithError(testFile, params, cb) {
12   _spawnTest(true, testFile, params, cb);
13 }
14
15 function _spawnTestWithoutError(testFile, params, cb) {
16   _spawnTest(false, testFile, params, cb);
17 }
18
19 function _spawnTest(passError, testFile, params, cb) {
20   var
21     node_path = process.argv[0],
22     command = [ node_path, path.join(__dirname, testFile) ].concat(params).join(' ');
23
24   exec(command, function _execDone(err, stdout, stderr) {
25     if (passError) {
26       if (err) {
27         return cb(err);
28       } else if (stderr.length > 0) {
29         return cb(stderr.toString());
30       }
31     }
32
33     return cb(null, stdout.toString());
34   });
35 }
36
37 function _testStat(stat, mode) {
38   assert.equal(stat.uid, process.getuid(), 'should have the same UID');
39   assert.equal(stat.gid, process.getgid(), 'should have the same GUID');
40   assert.equal(stat.mode, mode);
41 }
42
43 function _testPrefix(prefix) {
44   return function _testPrefixGenerated(err, name) {
45     assert.equal(path.basename(name).slice(0, prefix.length), prefix, 'should have the provided prefix');
46   };
47 }
48
49 function _testPrefixSync(prefix) {
50   return function _testPrefixGeneratedSync(result) {
51     if (result instanceof Error) {
52       throw result;
53     }
54     _testPrefix(prefix)(null, result.name, result.fd);
55   };
56 }
57
58 function _testPostfix(postfix) {
59   return function _testPostfixGenerated(err, name) {
60     assert.equal(name.slice(name.length - postfix.length, name.length), postfix, 'should have the provided postfix');
61   };
62 }
63
64 function _testPostfixSync(postfix) {
65   return function _testPostfixGeneratedSync(result) {
66     if (result instanceof Error) {
67       throw result;
68     }
69     _testPostfix(postfix)(null, result.name, result.fd);
70   };
71 }
72
73 function _testKeep(type, keep, cb) {
74   _spawnTestWithError('keep.js', [ type, keep ], cb);
75 }
76
77 function _testKeepSync(type, keep, cb) {
78   _spawnTestWithError('keep-sync.js', [ type, keep ], cb);
79 }
80
81 function _testGraceful(type, graceful, cb) {
82   _spawnTestWithoutError('graceful.js', [ type, graceful ], cb);
83 }
84
85 function _testGracefulSync(type, graceful, cb) {
86   _spawnTestWithoutError('graceful-sync.js', [ type, graceful ], cb);
87 }
88
89 function _assertName(err, name) {
90   assert.isString(name);
91   assert.isNotZero(name.length, 'an empty string is not a valid name');
92 }
93
94 function _assertNameSync(result) {
95   if (result instanceof Error) {
96     throw result;
97   }
98   var name = typeof(result) == 'string' ? result : result.name;
99   _assertName(null, name);
100 }
101
102 function _testName(expected){
103   return function _testNameGenerated(err, name) {
104     assert.equal(expected, name, 'should have the provided name');
105   };
106 }
107
108 function _testNameSync(expected){
109   return function _testNameGeneratedSync(result) {
110     if (result instanceof Error) {
111       throw result;
112     }
113     _testName(expected)(null, result.name, result.fd);
114   };
115 }
116
117 function _testUnsafeCleanup(unsafe, cb) {
118   _spawnTestWithoutError('unsafe.js', [ 'dir', unsafe ], cb);
119 }
120
121 function _testIssue62(cb) {
122   _spawnTestWithoutError('issue62.js', [], cb);
123 }
124
125 function _testUnsafeCleanupSync(unsafe, cb) {
126   _spawnTestWithoutError('unsafe-sync.js', [ 'dir', unsafe ], cb);
127 }
128
129 function _testIssue62Sync(cb) {
130   _spawnTestWithoutError('issue62-sync.js', [], cb);
131 }
132
133 module.exports.testStat = _testStat;
134 module.exports.testPrefix = _testPrefix;
135 module.exports.testPrefixSync = _testPrefixSync;
136 module.exports.testPostfix = _testPostfix;
137 module.exports.testPostfixSync = _testPostfixSync;
138 module.exports.testKeep = _testKeep;
139 module.exports.testKeepSync = _testKeepSync;
140 module.exports.testGraceful = _testGraceful;
141 module.exports.testGracefulSync = _testGracefulSync;
142 module.exports.assertName = _assertName;
143 module.exports.assertNameSync = _assertNameSync;
144 module.exports.testName = _testName;
145 module.exports.testNameSync = _testNameSync;
146 module.exports.testUnsafeCleanup = _testUnsafeCleanup;
147 module.exports.testIssue62 = _testIssue62;
148 module.exports.testUnsafeCleanupSync = _testUnsafeCleanupSync;
149 module.exports.testIssue62Sync = _testIssue62Sync;