1 var assert = require('assert');
2 var path = require('path');
3 var fs = require('fs');
4 var util = require('util');
6 var temp = require('../lib/temp');
9 var existsSync = function(path){
18 // Use path.exists for 0.6 if necessary
19 var safeExists = fs.exists || path.exists;
21 var mkdirFired = false;
23 temp.mkdir('foo', function(err, tpath) {
25 assert.ok(!err, "temp.mkdir did not execute without errors");
26 assert.ok(path.basename(tpath).slice(0, 3) == 'foo', 'temp.mkdir did not use the prefix');
27 assert.ok(existsSync(tpath), 'temp.mkdir did not create the directory');
29 fs.writeFileSync(path.join(tpath, 'a file'), 'a content');
31 assert.ok(!existsSync(tpath), 'temp.cleanupSync did not remove the directory');
36 var openFired = false;
38 temp.open('bar', function(err, info) {
40 assert.equal('object', typeof(info), "temp.open did not invoke the callback with the err and info object");
41 assert.equal('number', typeof(info.fd), 'temp.open did not invoke the callback with an fd');
42 fs.writeSync(info.fd, 'foo');
43 fs.closeSync(info.fd);
44 assert.equal('string', typeof(info.path), 'temp.open did not invoke the callback with a path');
45 assert.ok(existsSync(info.path), 'temp.open did not create a file');
48 assert.ok(!existsSync(info.path), 'temp.cleanupSync did not remove the file');
54 var stream = temp.createWriteStream('baz');
55 assert.ok(stream instanceof fs.WriteStream, 'temp.createWriteStream did not invoke the callback with the err and stream object');
57 stream.end("More text here\nand more...");
58 assert.ok(existsSync(stream.path), 'temp.createWriteStream did not create a file');
60 var tempDir = temp.mkdirSync("foobar");
61 assert.ok(existsSync(tempDir), 'temp.mkdirTemp did not create a directory');
65 assert.ok(!existsSync(stream.path), 'temp.cleanupSync did not remove the createWriteStream file');
66 assert.ok(!existsSync(tempDir), 'temp.cleanupSync did not remove the mkdirSync directory');
69 var cleanupFired = false;
70 // Make a temp file just to cleanup
71 var tempFile = temp.openSync();
72 fs.writeSync(tempFile.fd, 'foo');
73 fs.closeSync(tempFile.fd);
74 assert.ok(existsSync(tempFile.path), 'temp.openSync did not create a file for cleanup');
77 temp.cleanup(function(err, counts) {
79 assert.ok(!err, 'temp.cleanup did not run without encountering an error');
80 assert.ok(!existsSync(tempFile.path), 'temp.cleanup did not remove the openSync file for cleanup');
81 assert.equal(1, counts.files, 'temp.cleanup did not report the correct removal statistics');
84 var tempPath = temp.path();
85 assert.ok(path.dirname(tempPath) === temp.dir, "temp.path does not work in default os temporary directory");
87 tempPath = temp.path({dir: process.cwd()});
88 assert.ok(path.dirname(tempPath) === process.cwd(), "temp.path does not work in user-provided temporary directory");
90 for (var i=0; i <= 10; i++) {
93 assert.equal(process.listeners('exit').length, 1, 'temp created more than one listener for exit');
95 process.addListener('exit', function() {
96 assert.ok(mkdirFired, "temp.mkdir callback did not fire");
97 assert.ok(openFired, "temp.open callback did not fire");
98 assert.ok(cleanupFired, "temp.cleanup callback did not fire");