index.js 2.8 KB

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