base.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. var
  2. assert = require('assert'),
  3. path = require('path'),
  4. exec = require('child_process').exec,
  5. tmp = require('../lib/tmp');
  6. // make sure that we do not test spam the global tmp
  7. tmp.TMP_DIR = './tmp';
  8. function _spawnTestWithError(testFile, params, cb) {
  9. _spawnTest(true, testFile, params, cb);
  10. }
  11. function _spawnTestWithoutError(testFile, params, cb) {
  12. _spawnTest(false, testFile, params, cb);
  13. }
  14. function _spawnTest(passError, testFile, params, cb) {
  15. var
  16. node_path = process.argv[0],
  17. command = [ node_path, path.join(__dirname, testFile) ].concat(params).join(' ');
  18. exec(command, function _execDone(err, stdout, stderr) {
  19. if (passError) {
  20. if (err) {
  21. return cb(err);
  22. } else if (stderr.length > 0) {
  23. return cb(stderr.toString());
  24. }
  25. }
  26. return cb(null, stdout.toString());
  27. });
  28. }
  29. function _testStat(stat, mode) {
  30. assert.equal(stat.uid, process.getuid(), 'should have the same UID');
  31. assert.equal(stat.gid, process.getgid(), 'should have the same GUID');
  32. assert.equal(stat.mode, mode);
  33. }
  34. function _testPrefix(prefix) {
  35. return function _testPrefixGenerated(err, name) {
  36. assert.equal(path.basename(name).slice(0, prefix.length), prefix, 'should have the provided prefix');
  37. };
  38. }
  39. function _testPrefixSync(prefix) {
  40. return function _testPrefixGeneratedSync(result) {
  41. if (result instanceof Error) {
  42. throw result;
  43. }
  44. _testPrefix(prefix)(null, result.name, result.fd);
  45. };
  46. }
  47. function _testPostfix(postfix) {
  48. return function _testPostfixGenerated(err, name) {
  49. assert.equal(name.slice(name.length - postfix.length, name.length), postfix, 'should have the provided postfix');
  50. };
  51. }
  52. function _testPostfixSync(postfix) {
  53. return function _testPostfixGeneratedSync(result) {
  54. if (result instanceof Error) {
  55. throw result;
  56. }
  57. _testPostfix(postfix)(null, result.name, result.fd);
  58. };
  59. }
  60. function _testKeep(type, keep, cb) {
  61. _spawnTestWithError('keep.js', [ type, keep ], cb);
  62. }
  63. function _testKeepSync(type, keep, cb) {
  64. _spawnTestWithError('keep-sync.js', [ type, keep ], cb);
  65. }
  66. function _testGraceful(type, graceful, cb) {
  67. _spawnTestWithoutError('graceful.js', [ type, graceful ], cb);
  68. }
  69. function _testGracefulSync(type, graceful, cb) {
  70. _spawnTestWithoutError('graceful-sync.js', [ type, graceful ], cb);
  71. }
  72. function _assertName(err, name) {
  73. assert.isString(name);
  74. assert.isNotZero(name.length, 'an empty string is not a valid name');
  75. }
  76. function _assertNameSync(result) {
  77. if (result instanceof Error) {
  78. throw result;
  79. }
  80. var name = typeof(result) == 'string' ? result : result.name;
  81. _assertName(null, name);
  82. }
  83. function _testName(expected){
  84. return function _testNameGenerated(err, name) {
  85. assert.equal(expected, name, 'should have the provided name');
  86. };
  87. }
  88. function _testNameSync(expected){
  89. return function _testNameGeneratedSync(result) {
  90. if (result instanceof Error) {
  91. throw result;
  92. }
  93. _testName(expected)(null, result.name, result.fd);
  94. };
  95. }
  96. function _testUnsafeCleanup(unsafe, cb) {
  97. _spawnTestWithoutError('unsafe.js', [ 'dir', unsafe ], cb);
  98. }
  99. function _testIssue62(cb) {
  100. _spawnTestWithoutError('issue62.js', [], cb);
  101. }
  102. function _testUnsafeCleanupSync(unsafe, cb) {
  103. _spawnTestWithoutError('unsafe-sync.js', [ 'dir', unsafe ], cb);
  104. }
  105. function _testIssue62Sync(cb) {
  106. _spawnTestWithoutError('issue62-sync.js', [], cb);
  107. }
  108. module.exports.testStat = _testStat;
  109. module.exports.testPrefix = _testPrefix;
  110. module.exports.testPrefixSync = _testPrefixSync;
  111. module.exports.testPostfix = _testPostfix;
  112. module.exports.testPostfixSync = _testPostfixSync;
  113. module.exports.testKeep = _testKeep;
  114. module.exports.testKeepSync = _testKeepSync;
  115. module.exports.testGraceful = _testGraceful;
  116. module.exports.testGracefulSync = _testGracefulSync;
  117. module.exports.assertName = _assertName;
  118. module.exports.assertNameSync = _assertNameSync;
  119. module.exports.testName = _testName;
  120. module.exports.testNameSync = _testNameSync;
  121. module.exports.testUnsafeCleanup = _testUnsafeCleanup;
  122. module.exports.testIssue62 = _testIssue62;
  123. module.exports.testUnsafeCleanupSync = _testUnsafeCleanupSync;
  124. module.exports.testIssue62Sync = _testIssue62Sync;