/node_modules/mongodb-core/lib/tools/smoke_plugin.js

https://bitbucket.org/coleman333/smartsite · JavaScript · 59 lines · 46 code · 6 blank · 7 comment · 1 complexity · b644a469b422da00cbab726775d46fe2 MD5 · raw file

  1. var fs = require('fs');
  2. /* Note: because this plugin uses process.on('uncaughtException'), only one
  3. * of these can exist at any given time. This plugin and anything else that
  4. * uses process.on('uncaughtException') will conflict. */
  5. exports.attachToRunner = function(runner, outputFile) {
  6. var smokeOutput = { results : [] };
  7. var runningTests = {};
  8. var integraPlugin = {
  9. beforeTest: function(test, callback) {
  10. test.startTime = Date.now();
  11. runningTests[test.name] = test;
  12. callback();
  13. },
  14. afterTest: function(test, callback) {
  15. smokeOutput.results.push({
  16. status: test.status,
  17. start: test.startTime,
  18. end: Date.now(),
  19. test_file: test.name,
  20. exit_code: 0,
  21. url: ""
  22. });
  23. delete runningTests[test.name];
  24. callback();
  25. },
  26. beforeExit: function(obj, callback) {
  27. fs.writeFile(outputFile, JSON.stringify(smokeOutput), function() {
  28. callback();
  29. });
  30. }
  31. };
  32. // In case of exception, make sure we write file
  33. process.on('uncaughtException', function(err) {
  34. // Mark all currently running tests as failed
  35. for (var testName in runningTests) {
  36. smokeOutput.results.push({
  37. status: "fail",
  38. start: runningTests[testName].startTime,
  39. end: Date.now(),
  40. test_file: testName,
  41. exit_code: 0,
  42. url: ""
  43. });
  44. }
  45. // write file
  46. fs.writeFileSync(outputFile, JSON.stringify(smokeOutput));
  47. // Standard NodeJS uncaught exception handler
  48. console.error(err.stack);
  49. process.exit(1);
  50. });
  51. runner.plugin(integraPlugin);
  52. return integraPlugin;
  53. };