index.mjs 4.2 KB

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