index.mjs 4.6 KB

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