index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* eslint no-console: 0 */
  2. /**
  3. * TestRunner.js
  4. *
  5. * For running the tests in the test register.
  6. *
  7. * @author tlwr [toby@toby.codes]
  8. * @author n1474335 [n1474335@gmail.com]
  9. * @copyright Crown Copyright 2017
  10. * @license Apache-2.0
  11. */
  12. import "babel-polyfill";
  13. import TestRegister from "./TestRegister.js";
  14. import "./tests/operations/Base58.js";
  15. import "./tests/operations/Base64.js";
  16. import "./tests/operations/BCD.js";
  17. import "./tests/operations/BitwiseOp.js";
  18. import "./tests/operations/BSON.js";
  19. import "./tests/operations/ByteRepr.js";
  20. import "./tests/operations/CharEnc.js";
  21. import "./tests/operations/Cipher.js";
  22. import "./tests/operations/Code.js";
  23. import "./tests/operations/Compress.js";
  24. import "./tests/operations/DateTime.js";
  25. import "./tests/operations/FlowControl.js";
  26. import "./tests/operations/Hash.js";
  27. import "./tests/operations/Hexdump.js";
  28. import "./tests/operations/Image.js";
  29. import "./tests/operations/MorseCode.js";
  30. import "./tests/operations/MS.js";
  31. import "./tests/operations/PHP.js";
  32. import "./tests/operations/NetBIOS.js";
  33. import "./tests/operations/OTP.js";
  34. import "./tests/operations/Regex.js";
  35. import "./tests/operations/StrUtils.js";
  36. import "./tests/operations/SeqUtils.js";
  37. let allTestsPassing = true;
  38. const testStatusCounts = {
  39. total: 0,
  40. };
  41. /**
  42. * Helper function to convert a status to an icon.
  43. *
  44. * @param {string} status
  45. * @returns {string}
  46. */
  47. function statusToIcon(status) {
  48. const icons = {
  49. erroring: "🔥",
  50. failing: "❌",
  51. passing: "✔️️",
  52. };
  53. return icons[status] || "?";
  54. }
  55. /**
  56. * Displays a given test result in the console.
  57. *
  58. * @param {Object} testResult
  59. */
  60. function handleTestResult(testResult) {
  61. allTestsPassing = allTestsPassing && testResult.status === "passing";
  62. const newCount = (testStatusCounts[testResult.status] || 0) + 1;
  63. testStatusCounts[testResult.status] = newCount;
  64. testStatusCounts.total += 1;
  65. console.log([
  66. statusToIcon(testResult.status),
  67. testResult.test.name
  68. ].join(" "));
  69. if (testResult.output) {
  70. console.log(
  71. testResult.output
  72. .trim()
  73. .replace(/^/, "\t")
  74. .replace(/\n/g, "\n\t")
  75. );
  76. }
  77. }
  78. /**
  79. * Fail if the process takes longer than 10 seconds.
  80. */
  81. setTimeout(function() {
  82. console.log("Tests took longer than 10 seconds to run, returning.");
  83. process.exit(1);
  84. }, 10 * 1000);
  85. TestRegister.runTests()
  86. .then(function(results) {
  87. results.forEach(handleTestResult);
  88. console.log("\n");
  89. for (const testStatus in testStatusCounts) {
  90. const count = testStatusCounts[testStatus];
  91. if (count > 0) {
  92. console.log(testStatus.toUpperCase(), count);
  93. }
  94. }
  95. if (!allTestsPassing) {
  96. console.log("\nNot all tests are passing");
  97. }
  98. process.exit(allTestsPassing ? 0 : 1);
  99. });