116 lines
18 KiB
JavaScript
116 lines
18 KiB
JavaScript
|
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
const lodash_1 = require("lodash");
|
||
|
const correct_file_path_1 = __importDefault(require("../utils/correct-file-path"));
|
||
|
const escape_user_agent_1 = __importDefault(require("../utils/escape-user-agent"));
|
||
|
const events_1 = __importDefault(require("events"));
|
||
|
const DATE_FORMAT = 'YYYY-MM-DD';
|
||
|
const TIME_FORMAT = 'HH-mm-ss';
|
||
|
const ERRORS_FOLDER = 'errors';
|
||
|
const PROBLEMATIC_PLACEHOLDER_VALUE = '';
|
||
|
const PLACEHOLDERS = {
|
||
|
DATE: '${DATE}',
|
||
|
TIME: '${TIME}',
|
||
|
TEST_INDEX: '${TEST_INDEX}',
|
||
|
FILE_INDEX: '${FILE_INDEX}',
|
||
|
QUARANTINE_ATTEMPT: '${QUARANTINE_ATTEMPT}',
|
||
|
FIXTURE: '${FIXTURE}',
|
||
|
TEST: '${TEST}',
|
||
|
USERAGENT: '${USERAGENT}',
|
||
|
BROWSER: '${BROWSER}',
|
||
|
BROWSER_VERSION: '${BROWSER_VERSION}',
|
||
|
OS: '${OS}',
|
||
|
OS_VERSION: '${OS_VERSION}',
|
||
|
TEST_ID: '${TEST_ID}',
|
||
|
RUN_ID: '${RUN_ID}',
|
||
|
};
|
||
|
const DEFAULT_PATH_PATTERN_FOR_REPORT = `${PLACEHOLDERS.DATE}_${PLACEHOLDERS.TIME}\\${PLACEHOLDERS.TEST_ID}\\` +
|
||
|
`${PLACEHOLDERS.RUN_ID}\\${PLACEHOLDERS.USERAGENT}\\${PLACEHOLDERS.FILE_INDEX}`;
|
||
|
const TEST_ID_TEMPLATE = data => data.testIndex ? `test-${data.testIndex}` : '';
|
||
|
const RUN_ID_TEMPLATE = data => data.quarantineAttempt ? `run-${data.quarantineAttempt}` : '';
|
||
|
class PathPattern extends events_1.default {
|
||
|
constructor(pattern, fileExtension, data) {
|
||
|
super();
|
||
|
this.pattern = this._ensurePattern(pattern);
|
||
|
this.data = this._addDefaultFields(data);
|
||
|
this.placeholderToDataMap = this._createPlaceholderToDataMap();
|
||
|
this.fileExtension = fileExtension;
|
||
|
}
|
||
|
_ensurePattern(pattern) {
|
||
|
if (pattern)
|
||
|
return pattern;
|
||
|
return DEFAULT_PATH_PATTERN_FOR_REPORT;
|
||
|
}
|
||
|
_addDefaultFields(data) {
|
||
|
const defaultFields = {
|
||
|
testId: TEST_ID_TEMPLATE(data),
|
||
|
runId: RUN_ID_TEMPLATE(data),
|
||
|
formattedDate: data.now.format(DATE_FORMAT),
|
||
|
formattedTime: data.now.format(TIME_FORMAT),
|
||
|
fileIndex: 1,
|
||
|
errorFileIndex: 1,
|
||
|
};
|
||
|
return Object.assign({}, defaultFields, data);
|
||
|
}
|
||
|
_createPlaceholderToDataMap() {
|
||
|
return {
|
||
|
[PLACEHOLDERS.TEST_ID]: this.data.testId,
|
||
|
[PLACEHOLDERS.RUN_ID]: this.data.runId,
|
||
|
[PLACEHOLDERS.DATE]: this.data.formattedDate,
|
||
|
[PLACEHOLDERS.TIME]: this.data.formattedTime,
|
||
|
[PLACEHOLDERS.TEST_INDEX]: this.data.testIndex,
|
||
|
[PLACEHOLDERS.QUARANTINE_ATTEMPT]: this.data.quarantineAttempt || 1,
|
||
|
[PLACEHOLDERS.FIXTURE]: this.data.fixture,
|
||
|
[PLACEHOLDERS.TEST]: this.data.test,
|
||
|
[PLACEHOLDERS.FILE_INDEX]: forError => forError ? this.data.errorFileIndex : this.data.fileIndex,
|
||
|
[PLACEHOLDERS.USERAGENT]: this.data.parsedUserAgent.prettyUserAgent,
|
||
|
[PLACEHOLDERS.BROWSER]: this.data.parsedUserAgent.name,
|
||
|
[PLACEHOLDERS.BROWSER_VERSION]: this.data.parsedUserAgent.version,
|
||
|
[PLACEHOLDERS.OS]: this.data.parsedUserAgent.os.name,
|
||
|
[PLACEHOLDERS.OS_VERSION]: this.data.parsedUserAgent.os.version,
|
||
|
};
|
||
|
}
|
||
|
_buildPath(pattern, placeholderToDataMap, forError) {
|
||
|
let resultFilePath = pattern;
|
||
|
const problematicPlaceholders = [];
|
||
|
for (const placeholder in placeholderToDataMap) {
|
||
|
const findPlaceholderRegExp = new RegExp((0, lodash_1.escapeRegExp)(placeholder), 'g');
|
||
|
resultFilePath = resultFilePath.replace(findPlaceholderRegExp, () => {
|
||
|
if (placeholder === PLACEHOLDERS.FILE_INDEX) {
|
||
|
const getFileIndexFn = placeholderToDataMap[placeholder];
|
||
|
let result = getFileIndexFn(forError);
|
||
|
if (forError)
|
||
|
result = `${ERRORS_FOLDER}\\${result}`;
|
||
|
return result;
|
||
|
}
|
||
|
else if (placeholder === PLACEHOLDERS.USERAGENT) {
|
||
|
const userAgent = placeholderToDataMap[placeholder];
|
||
|
return (0, escape_user_agent_1.default)(userAgent);
|
||
|
}
|
||
|
let calculatedValue = placeholderToDataMap[placeholder];
|
||
|
if (calculatedValue === null || calculatedValue === void 0) {
|
||
|
problematicPlaceholders.push(placeholder);
|
||
|
calculatedValue = PROBLEMATIC_PLACEHOLDER_VALUE;
|
||
|
}
|
||
|
return calculatedValue;
|
||
|
});
|
||
|
}
|
||
|
if (problematicPlaceholders.length)
|
||
|
this.emit('problematic-placeholders-found', { placeholders: problematicPlaceholders });
|
||
|
return resultFilePath;
|
||
|
}
|
||
|
getPath(forError) {
|
||
|
const path = this._buildPath(this.pattern, this.placeholderToDataMap, forError);
|
||
|
return (0, correct_file_path_1.default)(path, this.fileExtension);
|
||
|
}
|
||
|
// For testing purposes
|
||
|
static get PLACEHOLDERS() {
|
||
|
return PLACEHOLDERS;
|
||
|
}
|
||
|
}
|
||
|
exports.default = PathPattern;
|
||
|
module.exports = exports.default;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicGF0aC1wYXR0ZXJuLmp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