flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
355 lines • 11.7 kB
JavaScript
"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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FlagpoleConfig = exports.ProjectConfig = exports.SuiteConfig = exports.EnvConfig = exports.getDefaultConfig = void 0;
const util_1 = require("./util");
const child_process_1 = require("child_process");
const fs = require("fs-extra");
const path = require("path");
const rimraf = require("rimraf");
const getDefaultConfig = (configFilePath) => {
const projectPath = path.dirname(configFilePath);
return {
project: {
name: path.basename(projectPath),
path: "tests",
},
environments: {},
suites: {},
};
};
exports.getDefaultConfig = getDefaultConfig;
class EnvConfig {
constructor(config, opts) {
this.config = config;
this.name = opts.name;
this.defaultDomain = opts.defaultDomain || "";
}
toJson() {
return {
name: this.name,
defaultDomain: this.defaultDomain,
};
}
}
exports.EnvConfig = EnvConfig;
class SuiteConfig {
constructor(config, opts) {
this.tags = [];
this.config = config;
this.name = opts.name || "";
this.id = opts.id || "";
if (opts.tags) {
opts.tags.forEach((tag) => {
this.tags.push(String(tag));
});
}
}
getSourcePath() {
return this.config.project.isSourceAndOutput
? path.join(this.config.getSourceFolder(), `${this.name}.ts`)
: this.getTestPath();
}
getTestPath() {
return path.join(this.config.getTestsFolder(), `${this.name}.js`);
}
clearTags() {
this.tags = [];
}
addTag(tag) {
tag = String(tag).trim();
if (tag.length && this.tags.indexOf(tag) < 0) {
this.tags.push(tag);
}
}
toJson() {
return {
id: this.id,
name: this.name,
tags: [...new Set(this.tags)].filter((value) => {
return value.trim().length > 0;
}),
};
}
}
exports.SuiteConfig = SuiteConfig;
class ProjectConfig {
constructor(config, opts) {
this.id = "";
this.config = config;
this.id = opts.id || "";
this.name = opts.name || "default";
this.path = opts.path || "tests";
this.images = opts.images || "images";
this.cache = opts.cache || "cache";
this.schemas = opts.schemas || "schemas";
this.reports = opts.reports || "reports";
this.source = opts.source;
this.output = opts.output;
this.pattern = opts.pattern || "";
}
get isSourceAndOutput() {
return this.source !== undefined && this.output !== undefined;
}
get isTypeScript() {
return this.isSourceAndOutput;
}
get hasId() {
return this.id.length > 0;
}
get patternRegEx() {
let pattern = this.pattern.trim();
if (pattern) {
pattern = pattern.replace(".", "\\.").replace("*", ".*");
return new RegExp(pattern);
}
return /.*/;
}
setTypeScriptFolders(rootFolder, srcFolder, outFolder) {
this.path = rootFolder;
this.source = srcFolder;
this.output = outFolder;
return this;
}
toJson() {
return {
id: this.id,
name: this.name,
path: this.path,
source: this.source,
output: this.output,
images: this.images,
cache: this.cache,
schemas: this.schemas,
reports: this.reports,
};
}
}
exports.ProjectConfig = ProjectConfig;
class FlagpoleConfig {
constructor(opts, configPath) {
this._onSave = [];
this.suites = {};
this.environments = {};
this.configPath = configPath;
this.project = new ProjectConfig(this, opts.project);
if (opts.suites !== undefined) {
Object.values(opts.suites).forEach((suiteOpts) => {
this.suites[suiteOpts.name] = new SuiteConfig(this, suiteOpts);
});
}
Object.values(opts.environments).forEach((envOpts) => {
this.environments[envOpts.name] = new EnvConfig(this, envOpts);
});
}
get defaultEnvironment() {
return Object.values(this.environments)[0];
}
getConfigFolder() {
return util_1.normalizePath(path.dirname(this.configPath));
}
getConfigPath() {
return this.configPath;
}
getRootFolder() {
return util_1.normalizePath(path.join(this.getConfigFolder(), this.project.path));
}
getTestsFolder() {
return util_1.normalizePath(path.join(this.getRootFolder(), this.project.output || ""));
}
getSourceFolder() {
return util_1.normalizePath(path.join(this.getRootFolder(), this.project.source || ""));
}
getImagesFolder() {
return util_1.normalizePath(path.join(this.getRootFolder(), this.project.images || "images"));
}
getSchemasFolder() {
return util_1.normalizePath(path.join(this.getRootFolder(), this.project.schemas || "schemas"));
}
getCacheFolder() {
return util_1.normalizePath(path.join(this.getRootFolder(), this.project.cache || "cache"));
}
getReportsFolder() {
return util_1.normalizePath(path.join(this.getRootFolder(), this.project.reports || "reports"));
}
getSuite(suiteName) {
return this.suites[suiteName];
}
addTagToSuite(suite, tag) {
if (typeof suite === "string") {
this.suites[suite].addTag(tag);
}
else {
suite.forEach((suiteName) => {
this.suites[suiteName].addTag(tag);
});
}
}
addEnvironment(opts) {
var _a;
if ((_a = opts.name) === null || _a === void 0 ? void 0 : _a.length) {
this.environments[opts.name] = new EnvConfig(this, opts);
}
}
addSuite(opts) {
var _a;
if ((_a = opts.name) === null || _a === void 0 ? void 0 : _a.length) {
this.suites[opts.name] = new SuiteConfig(this, opts);
}
}
removeEnvironment(name) {
delete this.environments[name];
}
removeSuite(name) {
delete this.suites[name];
}
getEnvironments() {
const envConfigs = [];
for (const key in this.environments) {
envConfigs.push(this.environments[key]);
}
return envConfigs;
}
getEnvironmentNames() {
const envs = [];
for (const key in this.environments) {
envs.push(this.environments[key].name);
}
return envs;
}
getTags() {
let tags = [];
for (const key in this.suites) {
tags = tags.concat(this.suites[key].tags);
}
tags = tags.filter((tag, i) => {
return tags.indexOf(tag) === i;
});
return tags.sort();
}
getSuites() {
const suiteConfigs = [];
for (const key in this.suites) {
suiteConfigs.push(this.suites[key]);
}
return suiteConfigs;
}
getSuiteNames() {
const suiteNames = [];
for (const key in this.suites) {
suiteNames.push(this.suites[key].name);
}
return suiteNames;
}
isValid() {
if (this.project === null ||
this.project.name.length == 0 ||
this.project.path.length == 0) {
return false;
}
if (typeof this.getTestsFolder() == "undefined" ||
!fs.existsSync(this.getTestsFolder())) {
return false;
}
if (Object.keys(this.environments).length == 0) {
return false;
}
return true;
}
toFileObject() {
return {
project: this.project.toJson(),
environments: (() => {
const envs = {};
Object.values(this.environments).forEach((env) => {
envs[env.name] = env.toJson();
});
return envs;
})(),
suites: (() => {
const suites = {};
Object.values(this.suites).forEach((suite) => {
suites[suite.name] = suite.toJson();
});
return suites;
})(),
};
}
toString() {
return JSON.stringify(this.toFileObject(), null, 2);
}
onSave(callback) {
this._onSave.push(callback);
}
save() {
return __awaiter(this, void 0, void 0, function* () {
yield fs.writeFile(this.getConfigPath(), this.toString());
this._onSave.forEach((callback) => {
callback();
});
});
}
writeTsConfig() {
return new Promise((resolve, reject) => {
if (!this.project.isSourceAndOutput) {
reject("Project config does not have a source and output folder defined.");
return;
}
const tsconfig = {
compilerOptions: {
module: "commonjs",
target: "es2016",
noImplicitAny: false,
sourceMap: false,
declaration: false,
rootDir: `.${path.sep}${this.project.source}`,
outDir: `.${path.sep}${this.project.output}`,
strict: true,
moduleResolution: "node",
removeComments: true,
preserveConstEnums: true,
},
include: [`src${path.sep}**${path.sep}*`],
exclude: ["node_modules", `**${path.sep}*.spec.ts`],
};
const tsconfigPath = path.join(this.getRootFolder(), "tsconfig.json");
fs.writeFile(tsconfigPath, JSON.stringify(tsconfig, null, 2), (err) => {
if (err) {
return reject(err);
}
resolve(tsconfigPath);
});
});
}
tsc() {
return new Promise((resolve, reject) => {
const rootFolder = this.getRootFolder();
const outFolder = this.getTestsFolder();
const cwd = process.cwd();
const rimRafPath = path.resolve(rootFolder, outFolder);
rimraf(rimRafPath, (err) => {
if (err) {
reject(err);
}
const command = `cd ${rootFolder} && tsc && cd ${cwd}`;
child_process_1.exec(command, (err, stdout, stderr) => {
if (err) {
reject(stdout || stderr || err);
return;
}
resolve(stdout);
});
});
});
}
}
exports.FlagpoleConfig = FlagpoleConfig;
//# sourceMappingURL=flagpole-config.js.map