Skip to content

Commit

Permalink
Fix default pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyBelym committed Jan 21, 2019
1 parent 4874638 commit 68be7a0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 41 deletions.
1 change: 1 addition & 0 deletions src/utils/correct-file-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default function (filePath, expectedExtention) {

const correctedPath = filePath
.split(path.posix.sep)
.filter(fragment => !!fragment)
.map(str => sanitizeFilename(str))
.join(path.sep);

Expand Down
30 changes: 19 additions & 11 deletions src/utils/path-pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,49 @@ const PLACEHOLDERS = {
BROWSER: '${BROWSER}',
BROWSER_VERSION: '${BROWSER_VERSION}',
OS: '${OS}',
OS_VERSION: '${OS_VERSION}'
OS_VERSION: '${OS_VERSION}',
GENERIC_TEST_NAME: '${GENERIC_TEST_NAME}',
GENERIC_RUN_NAME: '${GENERIC_RUN_NAME}'
};

const DEFAULT_PATH_PATTERN_FOR_REPORT = `${PLACEHOLDERS.DATE}_${PLACEHOLDERS.TIME}\\test-${PLACEHOLDERS.TEST_INDEX}`;
const DEFAULT_PATH_PATTERN = `${DEFAULT_PATH_PATTERN_FOR_REPORT}\\${PLACEHOLDERS.USERAGENT}\\${PLACEHOLDERS.FILE_INDEX}`;
const QUARANTINE_MODE_DEFAULT_PATH_PATTERN = `${DEFAULT_PATH_PATTERN_FOR_REPORT}\\run-${PLACEHOLDERS.QUARANTINE_ATTEMPT}\\${PLACEHOLDERS.USERAGENT}\\${PLACEHOLDERS.FILE_INDEX}`;
const DEFAULT_PATH_PATTERN_FOR_REPORT = `${PLACEHOLDERS.DATE}_${PLACEHOLDERS.TIME}\\${PLACEHOLDERS.GENERIC_TEST_NAME}\\` +
`${PLACEHOLDERS.GENERIC_RUN_NAME}\\${PLACEHOLDERS.USERAGENT}\\${PLACEHOLDERS.FILE_INDEX}`;

const GENERIC_TEST_NAME_TEMPLATE = data => data.testIndex ? `test-${data.testIndex}` : '';
const GENERIC_RUN_NAME_TEMPLATE = data => data.quarantineAttempt ? `run-${data.quarantineAttempt}` : '';

export default class PathPattern {
constructor (pattern, fileExtension, data) {
this.pattern = this._ensurePattern(pattern, data.quarantineAttempt);
this.pattern = this._ensurePattern(pattern);
this.data = this._addDefaultFields(data);
this.placeholderToDataMap = this._createPlaceholderToDataMap();
this.fileExtension = fileExtension;
}

_ensurePattern (pattern, quarantineAttempt) {
_ensurePattern (pattern) {
if (pattern)
return pattern;

return quarantineAttempt ? QUARANTINE_MODE_DEFAULT_PATH_PATTERN : DEFAULT_PATH_PATTERN;
return DEFAULT_PATH_PATTERN_FOR_REPORT;
}

_addDefaultFields (data) {
const defaultFields = {
formattedDate: data.now.format(DATE_FORMAT),
formattedTime: data.now.format(TIME_FORMAT),
fileIndex: 1,
errorFileIndex: 1
genericTestName: GENERIC_TEST_NAME_TEMPLATE(data),
genericRunName: GENERIC_RUN_NAME_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.GENERIC_TEST_NAME]: this.data.genericTestName,
[PLACEHOLDERS.GENERIC_RUN_NAME]: this.data.genericRunName,
[PLACEHOLDERS.DATE]: this.data.formattedDate,
[PLACEHOLDERS.TIME]: this.data.formattedTime,
[PLACEHOLDERS.TEST_INDEX]: this.data.testIndex,
Expand Down
6 changes: 3 additions & 3 deletions src/video-recorder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ export default class VideoRecorder {
}

_getTargetVideoPath (testRunInfo) {
const { quarantine, test, index, testRun } = testRunInfo;
const { test, index, testRun } = testRunInfo;

const connection = testRun.browserConnection;

const pathPattern = new PathPattern(this.customPathPattern, VIDEO_EXTENSION, {
testIndex: this.singleFile ? 1 : index,
quarantineAttempt: quarantine && !this.singleFile ? quarantine.getNextAttemptNumber() : null,
testIndex: this.singleFile ? null : index,
quarantineAttempt: null,
now: this.timeStamp,
fixture: this.singleFile ? '' : test.fixture.name,
test: this.singleFile ? '' : test.name,
Expand Down
47 changes: 20 additions & 27 deletions test/server/path-pattern-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,39 @@ const PathPattern = require('../../lib/utils/path-pattern');
const SCREENSHOT_EXTENSION = 'png';

describe('Screenshot path pattern', () => {
const parsedUserAgentMock = {
toVersion: () => {},
os: { toVersion: () => {} }
};
const TEST_USER_AGENT = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36';

const createPathPattern = (pattern, data) => {
data = data || {};
data.now = data.now || moment();
data.parsedUserAgent = data.parsedUserAgent || parsedUserAgentMock;
data.quarantineAttempt = data.quarantineAttempt || null;
const createPathPatternData = ({ forQuarantine }) => ({
now: moment('2010-01-02 11:12:13'),
testIndex: 12,
fileIndex: 34,
quarantineAttempt: forQuarantine ? 2 : null,
fixture: 'fixture',
test: 'test',
parsedUserAgent: userAgent.parse(TEST_USER_AGENT)
});

return new PathPattern(pattern, SCREENSHOT_EXTENSION, data);
const createPathPattern = (pattern, { forQuarantine } = {}) => {
return new PathPattern(pattern, SCREENSHOT_EXTENSION, createPathPatternData({ forQuarantine }));
};

describe('Default pattern', () => {
it('Normal run', () => {
const pathPattern = createPathPattern();

expect(pathPattern.pattern).eql('${DATE}_${TIME}\\test-${TEST_INDEX}\\${USERAGENT}\\${FILE_INDEX}');
expect(pathPattern.getPath()).match(/2010-01-02_11-12-13[\\/]test-12[\\/]Chrome_68.0.3440_Windows_8.1.0.0[\\/]34.png/);
});

it('Quarantine mode', () => {
const pathPattern = createPathPattern(void 0, { quarantineAttempt: 1 });
const pathPattern = createPathPattern(void 0, { forQuarantine: true });

expect(pathPattern.pattern).eql('${DATE}_${TIME}\\test-${TEST_INDEX}\\run-${QUARANTINE_ATTEMPT}\\${USERAGENT}\\${FILE_INDEX}');
expect(pathPattern.getPath()).match(/2010-01-02_11-12-13[\\/]test-12[\\/]run-2[\\/]Chrome_68.0.3440_Windows_8.1.0.0[\\/]34.png/);
});
});

it('Should replace all placeholders', () => {
const pattern = Object.getOwnPropertyNames(PathPattern.PLACEHOLDERS).map(name => PathPattern.PLACEHOLDERS[name]).join('#');
const dateStr = '2010-01-02';
const timeStr = '11:12:13';
const parsedUserAgent = userAgent.parse('Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36');
const data = {
now: moment(dateStr + ' ' + timeStr),
testIndex: 12,
fileIndex: 34,
quarantineAttempt: 2,
fixture: 'fixture',
test: 'test',
parsedUserAgent
};

const expectedParsedPattern = [
'2010-01-02',
'11-12-13',
Expand All @@ -62,10 +53,12 @@ describe('Screenshot path pattern', () => {
'Chrome',
'68.0.3440',
'Windows',
'8.1.0.0'
'8.1.0.0',
'test-12',
'run-2'
].join('#') + '.png';

const pathPattern = createPathPattern(pattern, data);
const pathPattern = createPathPattern(pattern, { forQuarantine: true });

const resultPath = pathPattern.getPath(false);

Expand Down

0 comments on commit 68be7a0

Please sign in to comment.