dir-test.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. var
  2. vows = require('vows'),
  3. assert = require('assert'),
  4. path = require('path'),
  5. fs = require('fs'),
  6. existsSync = fs.existsSync || path.existsSync,
  7. tmp = require('../lib/tmp.js'),
  8. Test = require('./base.js');
  9. function _testDir(mode) {
  10. return function _testDirGenerated(err, name) {
  11. assert.ok(existsSync(name), 'should exist');
  12. var stat = fs.statSync(name);
  13. assert.ok(stat.isDirectory(), 'should be a directory');
  14. Test.testStat(stat, mode);
  15. };
  16. }
  17. vows.describe('Directory creation').addBatch({
  18. 'when using without parameters': {
  19. topic: function () {
  20. tmp.dir(this.callback);
  21. },
  22. 'should be a directory': _testDir(040700),
  23. 'should have the default prefix': Test.testPrefix('tmp-')
  24. },
  25. 'when using with prefix': {
  26. topic: function () {
  27. tmp.dir({ prefix: 'something' }, this.callback);
  28. },
  29. 'should not return with an error': assert.isNull,
  30. 'should return with a name': Test.assertName,
  31. 'should be a directory': _testDir(040700),
  32. 'should have the provided prefix': Test.testPrefix('something')
  33. },
  34. 'when using with postfix': {
  35. topic: function () {
  36. tmp.dir({ postfix: '.txt' }, this.callback);
  37. },
  38. 'should not return with an error': assert.isNull,
  39. 'should return with a name': Test.assertName,
  40. 'should be a directory': _testDir(040700),
  41. 'should have the provided postfix': Test.testPostfix('.txt')
  42. },
  43. 'when using template': {
  44. topic: function () {
  45. tmp.dir({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') }, this.callback);
  46. },
  47. 'should not return with error': assert.isNull,
  48. 'should return with a name': Test.assertName,
  49. 'should be a directory': _testDir(040700),
  50. 'should have the provided prefix': Test.testPrefix('clike-'),
  51. 'should have the provided postfix': Test.testPostfix('-postfix')
  52. },
  53. 'when using name': {
  54. topic: function () {
  55. tmp.dir({ name: 'using-name' }, this.callback);
  56. },
  57. 'should not return with an error': assert.isNull,
  58. 'should return with a name': Test.assertName,
  59. 'should be a directory': _testDir(040700),
  60. 'should have the provided name': Test.testName(path.join(tmp.tmpdir, 'using-name'))
  61. },
  62. 'when using multiple options': {
  63. topic: function () {
  64. tmp.dir({ prefix: 'foo', postfix: 'bar', mode: 0750 }, this.callback);
  65. },
  66. 'should not return with an error': assert.isNull,
  67. 'should return with a name': Test.assertName,
  68. 'should be a directory': _testDir(040750),
  69. 'should have the provided prefix': Test.testPrefix('foo'),
  70. 'should have the provided postfix': Test.testPostfix('bar')
  71. },
  72. 'when using multiple options and mode': {
  73. topic: function () {
  74. tmp.dir({ prefix: 'complicated', postfix: 'options', mode: 0755 }, this.callback);
  75. },
  76. 'should not return with an error': assert.isNull,
  77. 'should return with a name': Test.assertName,
  78. 'should be a directory': _testDir(040755),
  79. 'should have the provided prefix': Test.testPrefix('complicated'),
  80. 'should have the provided postfix': Test.testPostfix('options')
  81. },
  82. 'no tries': {
  83. topic: function () {
  84. tmp.dir({ tries: -1 }, this.callback);
  85. },
  86. 'should return with an error': assert.isObject
  87. },
  88. 'keep testing': {
  89. topic: function () {
  90. Test.testKeep('dir', '1', this.callback);
  91. },
  92. 'should not return with an error': assert.isNull,
  93. 'should return with a name': Test.assertName,
  94. 'should be a dir': function (err, name) {
  95. _testDir(040700)(err, name);
  96. fs.rmdirSync(name);
  97. }
  98. },
  99. 'unlink testing': {
  100. topic: function () {
  101. Test.testKeep('dir', '0', this.callback);
  102. },
  103. 'should not return with error': assert.isNull,
  104. 'should return with a name': Test.assertName,
  105. 'should not exist': function (err, name) {
  106. assert.ok(!existsSync(name), 'Directory should be removed');
  107. }
  108. },
  109. 'non graceful testing': {
  110. topic: function () {
  111. Test.testGraceful('dir', '0', this.callback);
  112. },
  113. 'should not return with error': assert.isNull,
  114. 'should return with a name': Test.assertName,
  115. 'should be a dir': function (err, name) {
  116. _testDir(040700)(err, name);
  117. fs.rmdirSync(name);
  118. }
  119. },
  120. 'graceful testing': {
  121. topic: function () {
  122. Test.testGraceful('dir', '1', this.callback);
  123. },
  124. 'should not return with an error': assert.isNull,
  125. 'should return with a name': Test.assertName,
  126. 'should not exist': function (err, name) {
  127. assert.ok(!existsSync(name), 'Directory should be removed');
  128. }
  129. },
  130. 'unsafeCleanup === true': {
  131. topic: function () {
  132. Test.testUnsafeCleanup('1', this.callback);
  133. },
  134. 'should not return with an error': assert.isNull,
  135. 'should return with a name': Test.assertName,
  136. 'should not exist': function (err, name) {
  137. assert.ok(!existsSync(name), 'Directory should be removed');
  138. },
  139. 'should remove symlinked dir': function(err, name) {
  140. assert.ok(
  141. !existsSync(name + '/symlinkme-target'),
  142. 'should remove target'
  143. );
  144. },
  145. 'should not remove contents of symlink dir': function(err, name) {
  146. assert.ok(
  147. existsSync(__dirname + '/symlinkme/file.js'),
  148. 'should not remove symlinked directory\'s content'
  149. );
  150. }
  151. },
  152. 'unsafeCleanup === true with issue62 structure': {
  153. topic: function () {
  154. Test.testIssue62(this.callback);
  155. },
  156. 'should not return with an error': assert.isNull,
  157. 'should return with a name': Test.assertName,
  158. 'should not exist': function (err, name) {
  159. assert.ok(!existsSync(name), 'Directory should be removed');
  160. }
  161. },
  162. 'unsafeCleanup === false': {
  163. topic: function () {
  164. Test.testUnsafeCleanup('0', this.callback);
  165. },
  166. 'should not return with an error': assert.isNull,
  167. 'should return with a name': Test.assertName,
  168. 'should be a directory': function (err, name) {
  169. _testDir(040700)(err, name);
  170. // make sure that everything gets cleaned up
  171. fs.unlinkSync(path.join(name, 'should-be-removed.file'));
  172. fs.unlinkSync(path.join(name, 'symlinkme-target'));
  173. fs.rmdirSync(name);
  174. }
  175. },
  176. 'remove callback': {
  177. topic: function () {
  178. tmp.dir(this.callback);
  179. },
  180. 'should not return with an error': assert.isNull,
  181. 'should return with a name': Test.assertName,
  182. 'removeCallback should remove directory': function (_err, name, removeCallback) {
  183. removeCallback();
  184. assert.ok(!existsSync(name), 'Directory should be removed');
  185. }
  186. }
  187. }).exportTo(module);