Pathologic was missing because of a .git folder inside.
[yaffs-website] / node_modules / temp / test / temp-test.js
1 var assert = require('assert');
2 var path = require('path');
3 var fs = require('fs');
4 var util = require('util');
5
6 var temp = require('../lib/temp');
7 temp.track();
8
9 var existsSync = function(path){
10   try {
11     fs.statSync(path);
12     return true;
13   } catch (e){
14     return false;
15   }
16 };
17
18 // Use path.exists for 0.6 if necessary
19 var safeExists = fs.exists || path.exists;
20
21 var mkdirFired = false;
22 var mkdirPath = null;
23 temp.mkdir('foo', function(err, tpath) {
24   mkdirFired = true;
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');
28
29   fs.writeFileSync(path.join(tpath, 'a file'), 'a content');
30   temp.cleanupSync();
31   assert.ok(!existsSync(tpath), 'temp.cleanupSync did not remove the directory');
32
33   mkdirPath = tpath;
34 });
35
36 var openFired = false;
37 var openPath = null;
38 temp.open('bar', function(err, info) {
39   openFired = true;
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');
46
47   temp.cleanupSync();
48   assert.ok(!existsSync(info.path), 'temp.cleanupSync did not remove the file');
49
50   openPath = info.path;
51 });
52
53
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');
56 stream.write('foo');
57 stream.end("More text here\nand more...");
58 assert.ok(existsSync(stream.path), 'temp.createWriteStream did not create a file');
59
60 var tempDir = temp.mkdirSync("foobar");
61 assert.ok(existsSync(tempDir), 'temp.mkdirTemp did not create a directory');
62
63 // cleanupSync()
64 temp.cleanupSync();
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');
67
68 // cleanup()
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');
75
76 // run cleanup()
77 temp.cleanup(function(err, counts) {
78   cleanupFired = true;
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');
82 });
83
84 var tempPath = temp.path();
85 assert.ok(path.dirname(tempPath) === temp.dir, "temp.path does not work in default os temporary directory");
86
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");
89
90 for (var i=0; i <= 10; i++) {
91   temp.openSync();
92 }
93 assert.equal(process.listeners('exit').length, 1, 'temp created more than one listener for exit');
94
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");
99 });