file-test.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 _testFile(mode, fdTest) {
  10. return function _testFileGenerated(err, name, fd) {
  11. assert.ok(existsSync(name), 'should exist');
  12. var stat = fs.statSync(name);
  13. assert.equal(stat.size, 0, 'should have zero size');
  14. assert.ok(stat.isFile(), 'should be a file');
  15. Test.testStat(stat, mode);
  16. // check with fstat as well (fd checking)
  17. if (fdTest) {
  18. var fstat = fs.fstatSync(fd);
  19. assert.deepEqual(fstat, stat, 'fstat results should be the same');
  20. var data = new Buffer('something');
  21. assert.equal(fs.writeSync(fd, data, 0, data.length, 0), data.length, 'should be writable');
  22. assert.ok(!fs.closeSync(fd), 'should not return with error');
  23. }
  24. };
  25. }
  26. vows.describe('File creation').addBatch({
  27. 'when using without parameters': {
  28. topic: function () {
  29. tmp.file(this.callback);
  30. },
  31. 'should not return with an error': assert.isNull,
  32. 'should return with a name': Test.assertName,
  33. 'should be a file': _testFile(0100600, true),
  34. 'should have the default prefix': Test.testPrefix('tmp-'),
  35. 'should have the default postfix': Test.testPostfix('.tmp')
  36. },
  37. 'when using with prefix': {
  38. topic: function () {
  39. tmp.file({ prefix: 'something' }, this.callback);
  40. },
  41. 'should not return with an error': assert.isNull,
  42. 'should return with a name': Test.assertName,
  43. 'should be a file': _testFile(0100600, true),
  44. 'should have the provided prefix': Test.testPrefix('something')
  45. },
  46. 'when using with postfix': {
  47. topic: function () {
  48. tmp.file({ postfix: '.txt' }, this.callback);
  49. },
  50. 'should not return with an error': assert.isNull,
  51. 'should return with a name': Test.assertName,
  52. 'should be a file': _testFile(0100600, true),
  53. 'should have the provided postfix': Test.testPostfix('.txt')
  54. },
  55. 'when using template': {
  56. topic: function () {
  57. tmp.file({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') }, this.callback);
  58. },
  59. 'should not return with an error': assert.isNull,
  60. 'should return with a name': Test.assertName,
  61. 'should be a file': _testFile(0100600, true),
  62. 'should have the provided prefix': Test.testPrefix('clike-'),
  63. 'should have the provided postfix': Test.testPostfix('-postfix')
  64. },
  65. 'when using name': {
  66. topic: function () {
  67. tmp.file({ name: 'using-name.tmp' }, this.callback);
  68. },
  69. 'should not return with an error': assert.isNull,
  70. 'should return with a name': Test.assertName,
  71. 'should have the provided name': Test.testName(path.join(tmp.tmpdir, 'using-name.tmp')),
  72. 'should be a file': function (err, name) {
  73. _testFile(0100600, true);
  74. fs.unlinkSync(name);
  75. }
  76. },
  77. 'when using multiple options': {
  78. topic: function () {
  79. tmp.file({ prefix: 'foo', postfix: 'bar', mode: 0640 }, this.callback);
  80. },
  81. 'should not return with an error': assert.isNull,
  82. 'should return with a name': Test.assertName,
  83. 'should be a file': _testFile(0100640, true),
  84. 'should have the provided prefix': Test.testPrefix('foo'),
  85. 'should have the provided postfix': Test.testPostfix('bar')
  86. },
  87. 'when using multiple options and mode': {
  88. topic: function () {
  89. tmp.file({ prefix: 'complicated', postfix: 'options', mode: 0644 }, this.callback);
  90. },
  91. 'should not return with an error': assert.isNull,
  92. 'should return with a name': Test.assertName,
  93. 'should be a file': _testFile(0100644, true),
  94. 'should have the provided prefix': Test.testPrefix('complicated'),
  95. 'should have the provided postfix': Test.testPostfix('options')
  96. },
  97. 'no tries': {
  98. topic: function () {
  99. tmp.file({ tries: -1 }, this.callback);
  100. },
  101. 'should not be created': assert.isObject
  102. },
  103. 'keep testing': {
  104. topic: function () {
  105. Test.testKeep('file', '1', this.callback);
  106. },
  107. 'should not return with an error': assert.isNull,
  108. 'should return with a name': Test.assertName,
  109. 'should be a file': function (err, name) {
  110. _testFile(0100600, false)(err, name, null);
  111. fs.unlinkSync(name);
  112. }
  113. },
  114. 'unlink testing': {
  115. topic: function () {
  116. Test.testKeep('file', '0', this.callback);
  117. },
  118. 'should not return with an error': assert.isNull,
  119. 'should return with a name': Test.assertName,
  120. 'should not exist': function (err, name) {
  121. assert.ok(!existsSync(name), 'File should be removed');
  122. }
  123. },
  124. 'non graceful testing': {
  125. topic: function () {
  126. Test.testGraceful('file', '0', this.callback);
  127. },
  128. 'should not return with error': assert.isNull,
  129. 'should return with a name': Test.assertName,
  130. 'should be a file': function (err, name) {
  131. _testFile(0100600, false)(err, name, null);
  132. fs.unlinkSync(name);
  133. }
  134. },
  135. 'graceful testing': {
  136. topic: function () {
  137. Test.testGraceful('file', '1', this.callback);
  138. },
  139. 'should not return with an error': assert.isNull,
  140. 'should return with a name': Test.assertName,
  141. 'should not exist': function (err, name) {
  142. assert.ok(!existsSync(name), 'File should be removed');
  143. }
  144. },
  145. 'remove callback': {
  146. topic: function () {
  147. tmp.file(this.callback);
  148. },
  149. 'should not return with an error': assert.isNull,
  150. 'should return with a name': Test.assertName,
  151. 'removeCallback should remove file': function (_err, name, _fd, removeCallback) {
  152. removeCallback();
  153. assert.ok(!existsSync(name), 'File should be removed');
  154. }
  155. }
  156. }).exportTo(module);