test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. 'use strict';
  2. const fs = require('fs')
  3. const assert = require('assert')
  4. const { extname } = require('path')
  5. const tmp = require('.')
  6. async function checkFileResult(result) {
  7. assert.deepEqual(Object.keys(result).sort(), ['cleanup', 'fd', 'path'])
  8. const { path, fd, cleanup } = result
  9. assert.ok(typeof path === 'string')
  10. assert.ok(typeof fd === 'number')
  11. assert.ok(typeof cleanup === 'function')
  12. // Check that the path is a fille.
  13. assert.ok(fs.statSync(path).isFile())
  14. // Check that the fd is correct and points to the file.
  15. const message = 'hello there!'
  16. fs.writeSync(fd, message)
  17. fs.fdatasyncSync(fd)
  18. assert.equal(fs.readFileSync(path), message)
  19. // Check that the cleanup works.
  20. await cleanup()
  21. assert.throws(() => fs.statSync(path))
  22. }
  23. describe('file()', function()
  24. {
  25. context('when called without options', function()
  26. {
  27. it('creates the file, returns the expected result, and the cleanup function works', async function()
  28. {
  29. const result = await tmp.file()
  30. await checkFileResult(result)
  31. })
  32. })
  33. context('when called with options', function()
  34. {
  35. it('creates the file, returns the expected result, and the cleanup function works', async function()
  36. {
  37. const prefix = 'myTmpDir_'
  38. const result = await tmp.file({ prefix })
  39. await checkFileResult(result)
  40. assert.ok(result.path.includes(prefix))
  41. })
  42. })
  43. it('propagates errors', async function() {
  44. try {
  45. await tmp.file({ dir: 'nonexistent-path' });
  46. throw Error('Expected to throw');
  47. } catch (e) {
  48. assert.ok(e.message.startsWith('ENOENT: no such file or directory'));
  49. }
  50. });
  51. })
  52. async function checkDirResult(result) {
  53. assert.deepEqual(Object.keys(result).sort(), ['cleanup', 'path'])
  54. const { path, cleanup } = result
  55. assert.ok(typeof path === 'string')
  56. assert.ok(typeof cleanup === 'function')
  57. assert.ok(fs.statSync(path).isDirectory())
  58. await cleanup()
  59. assert.throws(() => fs.statSync(path))
  60. }
  61. describe('dir()', function()
  62. {
  63. context('when called without options', function()
  64. {
  65. it('creates the directory, returns the expected result, and the cleanup function works', async function()
  66. {
  67. const result = await tmp.dir()
  68. await checkDirResult(result)
  69. })
  70. })
  71. context('when called with options', function()
  72. {
  73. it('creates the directory, returns the expected result, and the cleanup function works', async function()
  74. {
  75. const prefix = 'myTmpDir_'
  76. const result = await tmp.dir({ prefix })
  77. await checkDirResult(result)
  78. assert.ok(result.path.includes(prefix))
  79. })
  80. })
  81. it('propagates errors', async function() {
  82. try {
  83. await tmp.dir({ dir: 'nonexistent-path' });
  84. throw Error('Expected to throw');
  85. } catch (e) {
  86. assert.ok(e.message.startsWith('ENOENT: no such file or directory'));
  87. }
  88. });
  89. })
  90. describe('withFile()', function()
  91. {
  92. it("file doesn't exist after going out of scope", function()
  93. {
  94. var filepath
  95. return tmp.withFile(function(o)
  96. {
  97. filepath = o.path
  98. fs.accessSync(filepath)
  99. assert.strictEqual(extname(filepath), '.txt')
  100. }, {discardDescriptor: true, postfix: '.txt'})
  101. .then(function()
  102. {
  103. assert.throws(function()
  104. {
  105. fs.accessSync(filepath)
  106. }, filepath + ' still exists')
  107. })
  108. })
  109. })
  110. describe('withDir()', function()
  111. {
  112. it("dir doesn't exist after going out of scope", function()
  113. {
  114. var filepath
  115. return tmp.withDir(function(o)
  116. {
  117. filepath = o.path
  118. fs.accessSync(filepath)
  119. assert.strictEqual(extname(filepath), '.dir')
  120. }, {postfix: '.dir'})
  121. .then(function()
  122. {
  123. assert.throws(function()
  124. {
  125. fs.accessSync(filepath)
  126. }, filepath + ' still exists')
  127. })
  128. })
  129. })