file-sync-test.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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(result) {
  11. assert.ok(existsSync(result.name), 'should exist');
  12. var stat = fs.statSync(result.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(result.fd);
  19. assert.deepEqual(fstat, stat, 'fstat results should be the same');
  20. var data = new Buffer('something');
  21. assert.equal(fs.writeSync(result.fd, data, 0, data.length, 0), data.length, 'should be writable');
  22. assert.ok(!fs.closeSync(result.fd), 'should not return with error');
  23. }
  24. };
  25. }
  26. vows.describe('Synchronous file creation').addBatch({
  27. 'when using without parameters': {
  28. topic: function () {
  29. return tmp.fileSync();
  30. },
  31. 'should return with a name': Test.assertNameSync,
  32. 'should be a file': _testFile(0100600, true),
  33. 'should have the default prefix': Test.testPrefixSync('tmp-'),
  34. 'should have the default postfix': Test.testPostfixSync('.tmp')
  35. },
  36. 'when using with prefix': {
  37. topic: function () {
  38. return tmp.fileSync({ prefix: 'something' });
  39. },
  40. 'should return with a name': Test.assertNameSync,
  41. 'should be a file': _testFile(0100600, true),
  42. 'should have the provided prefix': Test.testPrefixSync('something')
  43. },
  44. 'when using with postfix': {
  45. topic: function () {
  46. return tmp.fileSync({ postfix: '.txt' });
  47. },
  48. 'should return with a name': Test.assertNameSync,
  49. 'should be a file': _testFile(0100600, true),
  50. 'should have the provided postfix': Test.testPostfixSync('.txt')
  51. },
  52. 'when using template': {
  53. topic: function () {
  54. return tmp.fileSync({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') });
  55. },
  56. 'should return with a name': Test.assertNameSync,
  57. 'should be a file': _testFile(0100600, true),
  58. 'should have the provided prefix': Test.testPrefixSync('clike-'),
  59. 'should have the provided postfix': Test.testPostfixSync('-postfix')
  60. },
  61. 'when using name': {
  62. topic: function () {
  63. return tmp.fileSync({ name: 'using-name.tmp' });
  64. },
  65. 'should return with a name': Test.assertNameSync,
  66. 'should have the provided name': Test.testNameSync(path.join(tmp.tmpdir, 'using-name.tmp')),
  67. 'should be a file': function (result) {
  68. _testFile(0100600, true);
  69. fs.unlinkSync(result.name);
  70. }
  71. },
  72. 'when using multiple options': {
  73. topic: function () {
  74. return tmp.fileSync({ prefix: 'foo', postfix: 'bar', mode: 0640 });
  75. },
  76. 'should return with a name': Test.assertNameSync,
  77. 'should be a file': _testFile(0100640, true),
  78. 'should have the provided prefix': Test.testPrefixSync('foo'),
  79. 'should have the provided postfix': Test.testPostfixSync('bar')
  80. },
  81. 'when using multiple options and mode': {
  82. topic: function () {
  83. return tmp.fileSync({ prefix: 'complicated', postfix: 'options', mode: 0644 });
  84. },
  85. 'should return with a name': Test.assertNameSync,
  86. 'should be a file': _testFile(0100644, true),
  87. 'should have the provided prefix': Test.testPrefixSync('complicated'),
  88. 'should have the provided postfix': Test.testPostfixSync('options')
  89. },
  90. 'no tries': {
  91. topic: function () {
  92. try {
  93. return tmp.fileSync({ tries: -1 });
  94. }
  95. catch (e) {
  96. return e;
  97. }
  98. },
  99. 'should return with an error': function (topic) {
  100. assert.instanceOf(topic, Error);
  101. }
  102. },
  103. 'keep testing': {
  104. topic: function () {
  105. Test.testKeepSync('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)({name:name});
  111. fs.unlinkSync(name);
  112. }
  113. },
  114. 'unlink testing': {
  115. topic: function () {
  116. Test.testKeepSync('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.testGracefulSync('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)({name:name});
  132. fs.unlinkSync(name);
  133. }
  134. },
  135. 'graceful testing': {
  136. topic: function () {
  137. Test.testGracefulSync('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. return tmp.fileSync();
  148. },
  149. 'should return with a name': Test.assertNameSync,
  150. 'removeCallback should remove file': function (result) {
  151. result.removeCallback();
  152. assert.ok(!existsSync(result.name), 'File should be removed');
  153. }
  154. }
  155. }).exportTo(module);