@averagehelper/corde
Version:
A simple library for Discord bot tests. (Republished fork to demonstrate a bugfix)
156 lines (155 loc) • 5.76 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.go = void 0;
const fs_1 = __importDefault(require("fs"));
const ora_1 = __importDefault(require("ora"));
const path_1 = __importDefault(require("path"));
const runtime_1 = require("../common/runtime");
const testCollector_1 = require("../common/testCollector");
const reader_1 = __importDefault(require("../core/reader"));
const reporter_1 = require("../core/reporter");
const runner_1 = require("../core/runner");
const validate_1 = require("./validate");
const errors_1 = require("../errors");
process.on("uncaughtException", () => {
stopLoading();
});
let spinner;
function go() {
return __awaiter(this, void 0, void 0, function* () {
loadConfigs();
const files = readDir(runtime_1.runtime.testFiles);
if (!files || files.length === 0) {
throw new errors_1.FileError(`No test file was found in the path '${runtime_1.runtime.testFiles}'`);
}
yield runTests(files);
});
}
exports.go = go;
function loadConfigs() {
const configs = reader_1.default.loadConfig();
runtime_1.runtime.setConfigs(configs);
validate_1.validate(runtime_1.runtime.configs);
}
function runTests(files) {
return __awaiter(this, void 0, void 0, function* () {
startLoading("login to corde bot");
yield runtime_1.runtime.loginBot(runtime_1.runtime.cordeTestToken);
runtime_1.runtime.onBotStart().subscribe((isReady) => __awaiter(this, void 0, void 0, function* () {
if (isReady) {
const groups = yield reader_1.default.getTestsFromFiles(files);
spinner.text = "executing before start functions";
yield testCollector_1.testCollector.beforeStartFunctions.executeAsync();
spinner.text = "starting bots";
const tests = runner_1.getTestsFromGroup(groups);
if (!hasTestsToBeExecuted(tests)) {
spinner.succeed();
reporter_1.reporter.printNoTestFound();
process.exit(0);
}
spinner.text = "running tests";
yield runTestsAndPrint(groups, tests);
}
}));
});
}
function runTestsAndPrint(groups, tests) {
return __awaiter(this, void 0, void 0, function* () {
yield runner_1.executeTests(tests);
spinner.succeed();
const hasAllTestsPassed = reporter_1.reporter.outPutResult(groups);
if (hasAllTestsPassed) {
yield finishProcess(0);
}
else {
yield finishProcess(1);
}
});
}
function finishProcess(code, error) {
return __awaiter(this, void 0, void 0, function* () {
try {
if (error) {
console.log(error);
}
if (testCollector_1.testCollector.afterAllFunctions) {
yield testCollector_1.testCollector.afterAllFunctions.executeAsync();
}
runtime_1.runtime.logoffBot();
}
finally {
process.exit(code);
}
});
}
function startLoading(initialMessage) {
spinner = ora_1.default(initialMessage).start();
spinner._spinner = {
interval: 80,
frames: ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"],
};
spinner.color = getRandomSpinnerColor();
}
function getRandomSpinnerColor() {
const colors = ["red", "green", "yellow", "blue", "magenta", "cyan"];
let random = Math.random() * (colors.length - 1);
random = Math.round(random);
return colors[random];
}
function stopLoading() {
if (spinner) {
spinner.stop();
spinner.clear();
}
}
/**
* Load tests files into configs
*/
function readDir(directories) {
const files = [];
for (const dir of directories) {
const resolvedPath = path_1.default.resolve(process.cwd(), dir);
if (fs_1.default.existsSync(resolvedPath)) {
const stats = fs_1.default.lstatSync(resolvedPath);
if (stats.isDirectory()) {
const dirContent = fs_1.default.readdirSync(resolvedPath);
const dirContentPaths = [];
for (const singleDirContent of dirContent) {
dirContentPaths.push(path_1.default.resolve(dir, singleDirContent));
}
files.push(...readDir(dirContentPaths));
}
else if (stats.isFile() && dir.includes(".test.")) {
files.push(resolvedPath);
}
}
}
return files;
}
function hasTestsToBeExecuted(tests) {
if (!tests) {
return false;
}
for (const test of tests) {
if (test && test.testsFunctions) {
for (const fn of test.testsFunctions) {
if (fn) {
return true;
}
}
}
}
return false;
}