1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
Author: Jérémy Lal <kapouer@melix.org>
Description: Fix failing tests
Original tests were completely overlooking node asynchronicity
Last-Update: 2019-01-10
Forwarded: https://github.com/bruce/node-temp/pull/80
--- a/test/temp-test.js
+++ b/test/temp-test.js
@@ -3,9 +3,6 @@ var path = require('path');
var fs = require('fs');
var util = require('util');
-var temp = require('../lib/temp');
-temp.track();
-
var existsSync = function(path){
try {
fs.statSync(path);
@@ -18,82 +15,91 @@ var existsSync = function(path){
// Use path.exists for 0.6 if necessary
var safeExists = fs.exists || path.exists;
-var mkdirFired = false;
-var mkdirPath = null;
-temp.mkdir('foo', function(err, tpath) {
- mkdirFired = true;
- assert.ok(!err, "temp.mkdir did not execute without errors");
- assert.ok(path.basename(tpath).slice(0, 3) == 'foo', 'temp.mkdir did not use the prefix');
- assert.ok(existsSync(tpath), 'temp.mkdir did not create the directory');
-
- fs.writeFileSync(path.join(tpath, 'a file'), 'a content');
- temp.cleanupSync();
- assert.ok(!existsSync(tpath), 'temp.cleanupSync did not remove the directory');
-
- mkdirPath = tpath;
-});
-
-var openFired = false;
-var openPath = null;
-temp.open('bar', function(err, info) {
- openFired = true;
- assert.equal('object', typeof(info), "temp.open did not invoke the callback with the err and info object");
- assert.equal('number', typeof(info.fd), 'temp.open did not invoke the callback with an fd');
- fs.writeSync(info.fd, 'foo');
- fs.closeSync(info.fd);
- assert.equal('string', typeof(info.path), 'temp.open did not invoke the callback with a path');
- assert.ok(existsSync(info.path), 'temp.open did not create a file');
-
- temp.cleanupSync();
- assert.ok(!existsSync(info.path), 'temp.cleanupSync did not remove the file');
-
- openPath = info.path;
-});
-
+var temp = require('../lib/temp');
+temp.track();
+describe("temp", function() {
+ it("mkdir", function(done) {
+ var mkdirPath = null;
+ temp.mkdir('foo', function(err, tpath) {
+ assert.ok(!err, "temp.mkdir did not execute without errors");
+ assert.ok(path.basename(tpath).slice(0, 3) == 'foo', 'temp.mkdir did not use the prefix');
+ assert.ok(existsSync(tpath), 'temp.mkdir did not create the directory');
+
+ fs.writeFileSync(path.join(tpath, 'a file'), 'a content');
+ temp.cleanupSync();
+ assert.ok(!existsSync(tpath), 'temp.cleanupSync did not remove the directory');
+
+ mkdirPath = tpath;
+ done();
+ });
+ });
+
+ it("open", function(done) {
+ var openPath = null;
+ temp.open('bar', function(err, info) {
+ assert.equal('object', typeof(info), "temp.open did not invoke the callback with the err and info object");
+ assert.equal('number', typeof(info.fd), 'temp.open did not invoke the callback with an fd');
+ fs.writeSync(info.fd, 'foo');
+ fs.closeSync(info.fd);
+ assert.equal('string', typeof(info.path), 'temp.open did not invoke the callback with a path');
+ assert.ok(existsSync(info.path), 'temp.open did not create a file');
+
+ temp.cleanupSync();
+ assert.ok(!existsSync(info.path), 'temp.cleanupSync did not remove the file');
+
+ openPath = info.path;
+ done();
+ });
+ });
+
+ it("stream", function(done) {
+ var stream = temp.createWriteStream('baz');
+ assert.ok(stream instanceof fs.WriteStream, 'temp.createWriteStream did not invoke the callback with the err and stream object');
+ stream.write('foo');
+ stream.end("More text here\nand more...", function() {
+ assert.ok(existsSync(stream.path), 'temp.createWriteStream did not create a file');
+
+ var tempDir = temp.mkdirSync("foobar");
+ assert.ok(existsSync(tempDir), 'temp.mkdirTemp did not create a directory');
+
+ // cleanupSync()
+ temp.cleanupSync();
+ assert.ok(!existsSync(stream.path), 'temp.cleanupSync did not remove the createWriteStream file');
+ assert.ok(!existsSync(tempDir), 'temp.cleanupSync did not remove the mkdirSync directory');
+ done();
+ });
+ });
+
+ it("cleanup", function(done) {
+ // Make a temp file just to cleanup
+ var tempFile = temp.openSync();
+ fs.writeSync(tempFile.fd, 'foo');
+ fs.closeSync(tempFile.fd);
+ assert.ok(existsSync(tempFile.path), 'temp.openSync did not create a file for cleanup');
+
+ // run cleanup()
+ temp.cleanup(function(err, counts) {
+ assert.ok(!err, 'temp.cleanup did not run without encountering an error');
+ assert.ok(!existsSync(tempFile.path), 'temp.cleanup did not remove the openSync file for cleanup');
+ assert.equal(1, counts.files, 'temp.cleanup did not report the correct removal statistics');
+ done();
+ });
+ });
+
+ it("path", function() {
+ var tempPath = temp.path();
+ assert.ok(path.dirname(tempPath) === temp.dir, "temp.path does not work in default os temporary directory");
+
+ tempPath = temp.path({dir: process.cwd()});
+ assert.ok(path.dirname(tempPath) === process.cwd(), "temp.path does not work in user-provided temporary directory");
+ });
+
+ it("singleton", function() {
+ for (var i=0; i <= 10; i++) {
+ temp.openSync();
+ }
+ assert.equal(process.listeners('exit').length, 1, 'temp created more than one listener for exit');
+ });
-var stream = temp.createWriteStream('baz');
-assert.ok(stream instanceof fs.WriteStream, 'temp.createWriteStream did not invoke the callback with the err and stream object');
-stream.write('foo');
-stream.end("More text here\nand more...");
-assert.ok(existsSync(stream.path), 'temp.createWriteStream did not create a file');
-
-var tempDir = temp.mkdirSync("foobar");
-assert.ok(existsSync(tempDir), 'temp.mkdirTemp did not create a directory');
-
-// cleanupSync()
-temp.cleanupSync();
-assert.ok(!existsSync(stream.path), 'temp.cleanupSync did not remove the createWriteStream file');
-assert.ok(!existsSync(tempDir), 'temp.cleanupSync did not remove the mkdirSync directory');
-
-// cleanup()
-var cleanupFired = false;
-// Make a temp file just to cleanup
-var tempFile = temp.openSync();
-fs.writeSync(tempFile.fd, 'foo');
-fs.closeSync(tempFile.fd);
-assert.ok(existsSync(tempFile.path), 'temp.openSync did not create a file for cleanup');
-
-// run cleanup()
-temp.cleanup(function(err, counts) {
- cleanupFired = true;
- assert.ok(!err, 'temp.cleanup did not run without encountering an error');
- assert.ok(!existsSync(tempFile.path), 'temp.cleanup did not remove the openSync file for cleanup');
- assert.equal(1, counts.files, 'temp.cleanup did not report the correct removal statistics');
});
-var tempPath = temp.path();
-assert.ok(path.dirname(tempPath) === temp.dir, "temp.path does not work in default os temporary directory");
-
-tempPath = temp.path({dir: process.cwd()});
-assert.ok(path.dirname(tempPath) === process.cwd(), "temp.path does not work in user-provided temporary directory");
-
-for (var i=0; i <= 10; i++) {
- temp.openSync();
-}
-assert.equal(process.listeners('exit').length, 1, 'temp created more than one listener for exit');
-
-process.addListener('exit', function() {
- assert.ok(mkdirFired, "temp.mkdir callback did not fire");
- assert.ok(openFired, "temp.open callback did not fire");
- assert.ok(cleanupFired, "temp.cleanup callback did not fire");
-});
|