dir-sync-test.js 6.5 KB

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