maketmpfile.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. var fs = require('fs'),
  2. path = require('path'),
  3. os = require('os'),
  4. vfs = require('../lib/fs'),
  5. tmpDir = os.tmpdir || os.tmpDir || function() { return '/tmp'; },
  6. TEST_DIR = path.join(__dirname, 'test-dir');
  7. module.exports = {
  8. setUp : function(done) {
  9. fs.mkdirSync(TEST_DIR);
  10. done();
  11. },
  12. tearDown : function(done) {
  13. fs.rmdirSync(TEST_DIR);
  14. done();
  15. },
  16. 'should make temporary file with default options' : function(test) {
  17. var filePath;
  18. vfs.makeTmpFile()
  19. .then(
  20. function(_filePath) {
  21. return vfs.exists(filePath = _filePath);
  22. },
  23. function() {
  24. test.ok(false);
  25. throw Error();
  26. })
  27. .then(function(exists) {
  28. fs.unlinkSync(filePath);
  29. test.strictEqual(path.dirname(filePath), path.resolve(tmpDir()));
  30. test.strictEqual(path.extname(filePath), '.tmp');
  31. test.ok(exists);
  32. })
  33. .always(function() {
  34. test.done();
  35. });
  36. },
  37. 'should make temporary file in custom directory' : function(test) {
  38. var filePath;
  39. vfs.makeTmpFile({ dir : TEST_DIR })
  40. .then(
  41. function(_filePath) {
  42. return vfs.exists(filePath = _filePath);
  43. },
  44. function() {
  45. test.ok(false);
  46. throw Error();
  47. })
  48. .then(function(exists) {
  49. fs.unlinkSync(filePath);
  50. test.ok(exists);
  51. test.strictEqual(path.dirname(filePath), TEST_DIR);
  52. })
  53. .always(function() {
  54. test.done();
  55. })
  56. },
  57. 'should make temporary file with custom prefix' : function(test) {
  58. var filePath;
  59. vfs.makeTmpFile({ prefix : '__prefix' })
  60. .then(
  61. function(_filePath) {
  62. return vfs.exists(filePath = _filePath);
  63. },
  64. function() {
  65. test.ok(false);
  66. throw Error();
  67. })
  68. .then(function(exists) {
  69. fs.unlinkSync(filePath);
  70. test.ok(exists);
  71. test.ok(filePath.indexOf('__prefix') > -1);
  72. })
  73. .always(function() {
  74. test.done();
  75. })
  76. },
  77. 'should make temporary file with custom extension' : function(test) {
  78. var filePath;
  79. vfs.makeTmpFile({ ext : '.css' })
  80. .then(
  81. function(_filePath) {
  82. return vfs.exists(filePath = _filePath);
  83. },
  84. function() {
  85. test.ok(false);
  86. throw Error();
  87. })
  88. .then(function(exists) {
  89. fs.unlinkSync(filePath);
  90. test.ok(exists);
  91. test.strictEqual(path.extname(filePath), '.css');
  92. })
  93. .always(function() {
  94. test.done();
  95. })
  96. }
  97. };