temp-test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. var assert = require('assert');
  2. var path = require('path');
  3. var fs = require('fs');
  4. var util = require('util');
  5. var temp = require('../lib/temp');
  6. temp.track();
  7. var existsSync = function(path){
  8. try {
  9. fs.statSync(path);
  10. return true;
  11. } catch (e){
  12. return false;
  13. }
  14. };
  15. // Use path.exists for 0.6 if necessary
  16. var safeExists = fs.exists || path.exists;
  17. var mkdirFired = false;
  18. var mkdirPath = null;
  19. temp.mkdir('foo', function(err, tpath) {
  20. mkdirFired = true;
  21. assert.ok(!err, "temp.mkdir did not execute without errors");
  22. assert.ok(path.basename(tpath).slice(0, 3) == 'foo', 'temp.mkdir did not use the prefix');
  23. assert.ok(existsSync(tpath), 'temp.mkdir did not create the directory');
  24. fs.writeFileSync(path.join(tpath, 'a file'), 'a content');
  25. temp.cleanupSync();
  26. assert.ok(!existsSync(tpath), 'temp.cleanupSync did not remove the directory');
  27. mkdirPath = tpath;
  28. });
  29. var openFired = false;
  30. var openPath = null;
  31. temp.open('bar', function(err, info) {
  32. openFired = true;
  33. assert.equal('object', typeof(info), "temp.open did not invoke the callback with the err and info object");
  34. assert.equal('number', typeof(info.fd), 'temp.open did not invoke the callback with an fd');
  35. fs.writeSync(info.fd, 'foo');
  36. fs.closeSync(info.fd);
  37. assert.equal('string', typeof(info.path), 'temp.open did not invoke the callback with a path');
  38. assert.ok(existsSync(info.path), 'temp.open did not create a file');
  39. temp.cleanupSync();
  40. assert.ok(!existsSync(info.path), 'temp.cleanupSync did not remove the file');
  41. openPath = info.path;
  42. });
  43. var stream = temp.createWriteStream('baz');
  44. assert.ok(stream instanceof fs.WriteStream, 'temp.createWriteStream did not invoke the callback with the err and stream object');
  45. stream.write('foo');
  46. stream.end("More text here\nand more...");
  47. assert.ok(existsSync(stream.path), 'temp.createWriteStream did not create a file');
  48. var tempDir = temp.mkdirSync("foobar");
  49. assert.ok(existsSync(tempDir), 'temp.mkdirTemp did not create a directory');
  50. // cleanupSync()
  51. temp.cleanupSync();
  52. assert.ok(!existsSync(stream.path), 'temp.cleanupSync did not remove the createWriteStream file');
  53. assert.ok(!existsSync(tempDir), 'temp.cleanupSync did not remove the mkdirSync directory');
  54. // cleanup()
  55. var cleanupFired = false;
  56. // Make a temp file just to cleanup
  57. var tempFile = temp.openSync();
  58. fs.writeSync(tempFile.fd, 'foo');
  59. fs.closeSync(tempFile.fd);
  60. assert.ok(existsSync(tempFile.path), 'temp.openSync did not create a file for cleanup');
  61. // run cleanup()
  62. temp.cleanup(function(err, counts) {
  63. cleanupFired = true;
  64. assert.ok(!err, 'temp.cleanup did not run without encountering an error');
  65. assert.ok(!existsSync(tempFile.path), 'temp.cleanup did not remove the openSync file for cleanup');
  66. assert.equal(1, counts.files, 'temp.cleanup did not report the correct removal statistics');
  67. });
  68. var tempPath = temp.path();
  69. assert.ok(path.dirname(tempPath) === temp.dir, "temp.path does not work in default os temporary directory");
  70. tempPath = temp.path({dir: process.cwd()});
  71. assert.ok(path.dirname(tempPath) === process.cwd(), "temp.path does not work in user-provided temporary directory");
  72. for (var i=0; i <= 10; i++) {
  73. temp.openSync();
  74. }
  75. assert.equal(process.listeners('exit').length, 1, 'temp created more than one listener for exit');
  76. process.addListener('exit', function() {
  77. assert.ok(mkdirFired, "temp.mkdir callback did not fire");
  78. assert.ok(openFired, "temp.open callback did not fire");
  79. assert.ok(cleanupFired, "temp.cleanup callback did not fire");
  80. });