index.mjs 4.7 KB

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