UNPKG

@averagehelper/corde

Version:

A simple library for Discord bot tests. (Republished fork to demonstrate a bugfix)

114 lines (113 loc) 5 kB
"use strict"; 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 }); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const common_1 = require("../common"); const testCollector_1 = require("../common/testCollector"); const errors_1 = require("../errors"); class Reader { /** * Read config file(*.json) from root of project * and validates it * @throws */ loadConfig() { let _config; const jsonFilePath = path_1.default.resolve(process.cwd(), "corde.config.json"); const tsFilePath = path_1.default.resolve(process.cwd(), "corde.config.ts"); const jsFilePath = path_1.default.resolve(process.cwd(), "corde.config.js"); if (common_1.runtime.configFilePath) { return loadConfigFromConfigFilePath(); } if (fs_1.default.existsSync(jsonFilePath)) { _config = JSON.parse(fs_1.default.readFileSync(jsonFilePath).toString()); } else if (fs_1.default.existsSync(tsFilePath)) { _config = require(tsFilePath); } else if (fs_1.default.existsSync(jsFilePath)) { _config = require(jsFilePath); } else { throw new errors_1.FileError("No config file was found"); } if (!_config || Object.keys(_config).length === 0) { throw new errors_1.FileError("This appears to be a invalid config file"); } else { return _config; } } getTestsFromFiles(files) { return __awaiter(this, void 0, void 0, function* () { if (!files) { throw new errors_1.FileError("No file was informed."); } testCollector_1.testCollector.isCollecting = true; for (const file of files) { require(file); yield testCollector_1.testCollector.executeGroupClojure(); yield testCollector_1.testCollector.executeTestClojure(); } testCollector_1.testCollector.isCollecting = false; addTestsGroupmentToGroupIfExist(); addIsolatedTestFunctionsToGroupIfExists(); addTestFunctionsToGroupIfExists(); return testCollector_1.testCollector.groups; }); } } function loadConfigFromConfigFilePath() { let filePath = ""; if (fs_1.default.existsSync(common_1.runtime.configFilePath)) { filePath = path_1.default.resolve(process.cwd(), common_1.runtime.configFilePath); } else { throw new errors_1.FileError(`The path '${common_1.runtime.configFilePath}' do not appears to be a valid path`); } const fileExt = path_1.default.extname(filePath); if (fileExt === ".json") { return JSON.parse(fs_1.default.readFileSync(filePath).toString()); } else if (fileExt === ".js" || fileExt === ".ts") { return require(filePath); } else { throw new errors_1.FileError(`Extension '${fileExt}' is not supported`); } } function addTestsGroupmentToGroupIfExist() { if (testCollector_1.testCollector.tests && testCollector_1.testCollector.tests.length > 0) { const testsCloned = testCollector_1.testCollector.tests.map((test) => test); testCollector_1.testCollector.groups.push({ tests: testsCloned }); testCollector_1.testCollector.tests = []; } } function addIsolatedTestFunctionsToGroupIfExists() { if (testCollector_1.testCollector.hasIsolatedTestFunctions()) { const testsCloned = testCollector_1.testCollector.cloneIsolatedTestFunctions(); testCollector_1.testCollector.groups.push({ tests: [{ testsFunctions: testsCloned }] }); testCollector_1.testCollector.clearIsolatedTestFunctions(); } } function addTestFunctionsToGroupIfExists() { if (testCollector_1.testCollector.hasTestFunctions()) { const testsCloned = testCollector_1.testCollector.cloneTestFunctions(); testCollector_1.testCollector.groups.push({ tests: [{ testsFunctions: testsCloned }] }); testCollector_1.testCollector.clearTestFunctions(); } } const reader = new Reader(); exports.default = reader;