index.mjs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* eslint no-console: 0 */
  2. /**
  3. * Test Runner
  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. // Define global environment functions
  14. global.ENVIRONMENT_IS_WORKER = function() {
  15. return typeof importScripts === "function";
  16. };
  17. global.ENVIRONMENT_IS_NODE = function() {
  18. return typeof process === "object" && typeof require === "function";
  19. };
  20. global.ENVIRONMENT_IS_WEB = function() {
  21. return typeof window === "object";
  22. };
  23. import TestRegister from "./TestRegister";
  24. import "./tests/operations/Base58";
  25. import "./tests/operations/Base64";
  26. import "./tests/operations/BCD";
  27. import "./tests/operations/BitwiseOp";
  28. import "./tests/operations/BSON";
  29. import "./tests/operations/ByteRepr";
  30. import "./tests/operations/CartesianProduct";
  31. import "./tests/operations/CharEnc";
  32. import "./tests/operations/Ciphers";
  33. import "./tests/operations/Checksum";
  34. import "./tests/operations/Code";
  35. import "./tests/operations/Compress";
  36. import "./tests/operations/Crypt";
  37. import "./tests/operations/DateTime";
  38. import "./tests/operations/Fork";
  39. import "./tests/operations/Jump";
  40. import "./tests/operations/ConditionalJump";
  41. import "./tests/operations/Register";
  42. import "./tests/operations/Comment";
  43. import "./tests/operations/Hash";
  44. import "./tests/operations/HaversineDistance";
  45. import "./tests/operations/Hexdump";
  46. import "./tests/operations/Image";
  47. import "./tests/operations/MorseCode";
  48. import "./tests/operations/MS";
  49. import "./tests/operations/PGP";
  50. import "./tests/operations/PHP";
  51. import "./tests/operations/NetBIOS";
  52. import "./tests/operations/OTP";
  53. import "./tests/operations/PowerSet";
  54. import "./tests/operations/Regex";
  55. import "./tests/operations/Rotate";
  56. import "./tests/operations/StrUtils";
  57. import "./tests/operations/SeqUtils";
  58. import "./tests/operations/SetDifference";
  59. import "./tests/operations/SetIntersection";
  60. import "./tests/operations/SetUnion";
  61. import "./tests/operations/SymmetricDifference";
  62. let allTestsPassing = true;
  63. const testStatusCounts = {
  64. total: 0,
  65. };
  66. /**
  67. * Helper function to convert a status to an icon.
  68. *
  69. * @param {string} status
  70. * @returns {string}
  71. */
  72. function statusToIcon(status) {
  73. const icons = {
  74. erroring: "🔥",
  75. failing: "❌",
  76. passing: "✔️️",
  77. };
  78. return icons[status] || "?";
  79. }
  80. /**
  81. * Displays a given test result in the console.
  82. *
  83. * @param {Object} testResult
  84. */
  85. function handleTestResult(testResult) {
  86. allTestsPassing = allTestsPassing && testResult.status === "passing";
  87. const newCount = (testStatusCounts[testResult.status] || 0) + 1;
  88. testStatusCounts[testResult.status] = newCount;
  89. testStatusCounts.total += 1;
  90. console.log([
  91. statusToIcon(testResult.status),
  92. testResult.test.name
  93. ].join(" "));
  94. if (testResult.output) {
  95. console.log(
  96. testResult.output
  97. .trim()
  98. .replace(/^/, "\t")
  99. .replace(/\n/g, "\n\t")
  100. );
  101. }
  102. }
  103. /**
  104. * Fail if the process takes longer than 10 seconds.
  105. */
  106. setTimeout(function() {
  107. console.log("Tests took longer than 10 seconds to run, returning.");
  108. process.exit(1);
  109. }, 10 * 1000);
  110. TestRegister.runTests()
  111. .then(function(results) {
  112. results.forEach(handleTestResult);
  113. console.log("\n");
  114. for (const testStatus in testStatusCounts) {
  115. const count = testStatusCounts[testStatus];
  116. if (count > 0) {
  117. console.log(testStatus.toUpperCase(), count);
  118. }
  119. }
  120. if (!allTestsPassing) {
  121. console.log("\nNot all tests are passing");
  122. }
  123. process.exit(allTestsPassing ? 0 : 1);
  124. });