@mmisty/cypress-grep
Version:
Filters tests by tags/title using substring or regular expressions (can find dynamic tags)
63 lines (62 loc) • 2.32 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRootFolder = exports.commonPathPartList = exports.compareStr = void 0;
const fast_glob_1 = __importDefault(require("fast-glob"));
const logs_1 = require("../common/logs");
const compareStr = (pathSeparator, str1, str2) => {
const arr1 = str1.split(pathSeparator);
const arr2 = str2.split(pathSeparator);
const len = arr1.length > arr2.length ? arr2.length : arr1.length;
const common = [];
let index = 0;
for (let i = 0; i < len; i++) {
if (arr1[i] === arr2[i] && index === i) {
index++;
common.push(arr1[i]);
}
}
return common.join('/');
};
exports.compareStr = compareStr;
const commonPathPartList = (list) => {
const pathSeparator = '/';
// max length
let common = list.length > 0 ? list.reduce((p, c) => (p.length > c.length ? p : c)) : '';
if (list.length > 1) {
for (let i = 0; i < list.length - 1; i++) {
const commCurrent = (0, exports.compareStr)(pathSeparator, list[i], list[i + 1]);
if (common.length >= commCurrent.length) {
common = commCurrent;
}
}
}
const parts = common.split(pathSeparator);
// when file
if (list.length === 1 && parts[parts.length - 1].indexOf('.') !== -1) {
return parts.slice(0, parts.length - 1).join(pathSeparator);
}
return common;
};
exports.commonPathPartList = commonPathPartList;
const getRootFolder = (pattern, fallbackRoot) => {
// default cypress cypress/e2e/**/*.cy.{js,jsx,ts,tsx}
try {
if (!pattern) {
throw new Error('Spec pattern not specified');
}
const list = fast_glob_1.default.sync(pattern);
if (list.length === 0) {
throw new Error(`Not found tests by specPattern \`${pattern}\``);
}
return (0, exports.commonPathPartList)(list);
}
catch (err) {
// eslint-disable-next-line no-console
console.warn(`${logs_1.pkgName} Could not get root tests folder: \n ${err}`);
return fallbackRoot;
}
};
exports.getRootFolder = getRootFolder;