index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. let allTestsPassing = true;
  35. const testStatusCounts = {
  36. total: 0,
  37. };
  38. /**
  39. * Helper function to convert a status to an icon.
  40. *
  41. * @param {string} status
  42. * @returns {string}
  43. */
  44. function statusToIcon(status) {
  45. const icons = {
  46. erroring: "🔥",
  47. failing: "❌",
  48. passing: "✔️️",
  49. };
  50. return icons[status] || "?";
  51. }
  52. /**
  53. * Displays a given test result in the console.
  54. *
  55. * @param {Object} testResult
  56. */
  57. function handleTestResult(testResult) {
  58. allTestsPassing = allTestsPassing && testResult.status === "passing";
  59. const newCount = (testStatusCounts[testResult.status] || 0) + 1;
  60. testStatusCounts[testResult.status] = newCount;
  61. testStatusCounts.total += 1;
  62. console.log([
  63. statusToIcon(testResult.status),
  64. testResult.test.name
  65. ].join(" "));
  66. if (testResult.output) {
  67. console.log(
  68. testResult.output
  69. .trim()
  70. .replace(/^/, "\t")
  71. .replace(/\n/g, "\n\t")
  72. );
  73. }
  74. }
  75. /**
  76. * Fail if the process takes longer than 10 seconds.
  77. */
  78. setTimeout(function() {
  79. console.log("Tests took longer than 10 seconds to run, returning.");
  80. process.exit(1);
  81. }, 10 * 1000);
  82. TestRegister.runTests()
  83. .then(function(results) {
  84. results.forEach(handleTestResult);
  85. console.log("\n");
  86. for (const testStatus in testStatusCounts) {
  87. const count = testStatusCounts[testStatus];
  88. if (count > 0) {
  89. console.log(testStatus.toUpperCase(), count);
  90. }
  91. }
  92. if (!allTestsPassing) {
  93. console.log("\nNot all tests are passing");
  94. }
  95. process.exit(allTestsPassing ? 0 : 1);
  96. });