Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AmsterGet committed Aug 16, 2024
1 parent d350211 commit 2366a3e
Show file tree
Hide file tree
Showing 12 changed files with 1,336 additions and 1,135 deletions.
98 changes: 98 additions & 0 deletions lib/utils/attachments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2024 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const fs = require('fs');
const glob = require('glob');
const path = require('path');

const fsPromises = fs.promises;

const DEFAULT_WAIT_FOR_FILE_TIMEOUT = 10000;
const DEFAULT_WAIT_FOR_FILE_INTERVAL = 500;

const base64Encode = async (filePath) => {
const bitmap = await fsPromises.readFile(filePath);
return Buffer.from(bitmap).toString('base64');
};

const getScreenshotAttachment = async (absolutePath) => {
if (!absolutePath) return absolutePath;
const name = absolutePath.split(path.sep).pop();
return {
name,
type: 'image/png',
content: await base64Encode(absolutePath),
};
};

const waitForFile = (
globFilePattern,
timeout = DEFAULT_WAIT_FOR_FILE_TIMEOUT,
interval = DEFAULT_WAIT_FOR_FILE_INTERVAL,
) =>
new Promise((resolve, reject) => {
let totalTime = 0;

async function checkFileExistence() {
const files = await glob(globFilePattern);

if (files.length) {
resolve(files[0]);
} else if (totalTime >= timeout) {
reject(new Error(`Timeout of ${timeout}ms reached, file ${globFilePattern} not found.`));
} else {
totalTime += interval;
setTimeout(checkFileExistence, interval);
}
}

checkFileExistence().catch(reject);
});

const getVideoFile = async (
specFileName,
videosFolder = '**',
timeout = DEFAULT_WAIT_FOR_FILE_TIMEOUT,
interval = DEFAULT_WAIT_FOR_FILE_INTERVAL,
) => {
if (!specFileName) {
return null;
}
const fileName = specFileName.toLowerCase().endsWith('.mp4')
? specFileName
: `${specFileName}.mp4`;
const globFilePath = `**/${videosFolder}/${fileName}`;
let videoFilePath;

try {
videoFilePath = await waitForFile(globFilePath, timeout, interval);
} catch (e) {
console.warn(e.message);
return null;
}

return {
name: fileName,
type: 'video/mp4',
content: await fsPromises.readFile(videoFilePath, { encoding: 'base64' }),
};
};

module.exports = {
getScreenshotAttachment,
getVideoFile,
waitForFile,
};
22 changes: 22 additions & 0 deletions lib/utils/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2024 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const getCodeRef = (testItemPath, testFileName) =>
`${testFileName.replace(/\\/g, '/')}/${testItemPath.join('/')}`;

module.exports = {
getCodeRef,
};
27 changes: 27 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2024 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const attachmentUtils = require('./attachments');
const commonUtils = require('./common');
const objectCreators = require('./objectCreators');
const specCountCalculation = require('./specCountCalculation');

module.exports = {
...attachmentUtils,
...commonUtils,
...objectCreators,
...specCountCalculation,
};
154 changes: 7 additions & 147 deletions lib/utils.js → lib/utils/objectCreators.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,91 +14,13 @@
* limitations under the License.
*/

const fs = require('fs');
const glob = require('glob');
const path = require('path');
const minimatch = require('minimatch');
const { entityType, hookTypesMap, testItemStatuses } = require('./constants');
const pjson = require('./../package.json');

const fsPromises = fs.promises;
const pjson = require('../../package.json');
const { entityType, hookTypesMap, testItemStatuses } = require('../constants');
const { getCodeRef } = require('./common');

const { FAILED, PASSED, SKIPPED } = testItemStatuses;

const DEFAULT_WAIT_FOR_FILE_TIMEOUT = 10000;
const DEFAULT_WAIT_FOR_FILE_INTERVAL = 500;

const base64Encode = async (filePath) => {
const bitmap = await fsPromises.readFile(filePath);
return Buffer.from(bitmap).toString('base64');
};

const getScreenshotAttachment = async (absolutePath) => {
if (!absolutePath) return absolutePath;
const name = absolutePath.split(path.sep).pop();
return {
name,
type: 'image/png',
content: await base64Encode(absolutePath),
};
};

const waitForFile = (
globFilePath,
timeout = DEFAULT_WAIT_FOR_FILE_TIMEOUT,
interval = DEFAULT_WAIT_FOR_FILE_INTERVAL,
) =>
new Promise((resolve, reject) => {
let totalTime = 0;

async function checkFileExistence() {
const files = await glob(globFilePath);

if (files.length) {
resolve(files[0]);
} else if (totalTime >= timeout) {
reject(new Error(`Timeout of ${timeout}ms reached, file ${globFilePath} not found.`));
} else {
totalTime += interval;
setTimeout(checkFileExistence, interval);
}
}

checkFileExistence().catch(reject);
});

const getVideoFile = async (
specFileName,
videosFolder = '**',
timeout = DEFAULT_WAIT_FOR_FILE_TIMEOUT,
interval = DEFAULT_WAIT_FOR_FILE_INTERVAL,
) => {
if (!specFileName) {
return null;
}
const fileName = specFileName.toLowerCase().endsWith('.mp4')
? specFileName
: `${specFileName}.mp4`;
const globFilePath = `**/${videosFolder}/${fileName}`;
let videoFilePath;

try {
videoFilePath = await waitForFile(globFilePath, timeout, interval);
} catch (e) {
console.warn(e.message);
return null;
}

return {
name: fileName,
type: 'video/mp4',
content: await base64Encode(videoFilePath),
};
};

const getCodeRef = (testItemPath, testFileName) =>
`${testFileName.replace(/\\/g, '/')}/${testItemPath.join('/')}`;

const getAgentInfo = () => ({
version: pjson.version,
name: pjson.name,
Expand Down Expand Up @@ -273,82 +195,20 @@ const getHookStartObject = (hook) => {
codeRef: hook.codeRef,
};
};
const getFixtureFolderPattern = (config) => {
return [].concat(config.fixturesFolder ? path.join(config.fixturesFolder, '**', '*') : []);
};

const getExcludeSpecPattern = (config) => {
// Return cypress >= 10 pattern.
if (config.excludeSpecPattern) {
const excludePattern = Array.isArray(config.excludeSpecPattern)
? config.excludeSpecPattern
: [config.excludeSpecPattern];
return [...excludePattern];
}

// Return cypress <= 9 pattern
const ignoreTestFilesPattern = Array.isArray(config.ignoreTestFiles)
? config.ignoreTestFiles
: [config.ignoreTestFiles] || [];

return [...ignoreTestFilesPattern];
};

const getSpecPattern = (config) => {
if (config.specPattern) return [].concat(config.specPattern);

return Array.isArray(config.testFiles)
? config.testFiles.map((file) => path.join(config.integrationFolder, file))
: [].concat(path.join(config.integrationFolder, config.testFiles));
};

const getTotalSpecs = (config) => {
if (!config.testFiles && !config.specPattern)
throw new Error('Configuration property not set! Neither for cypress <= 9 nor cypress >= 10');

const specPattern = getSpecPattern(config);

const excludeSpecPattern = getExcludeSpecPattern(config);

const options = {
sort: true,
absolute: true,
nodir: true,
ignore: [config.supportFile].concat(getFixtureFolderPattern(config)),
};

const doesNotMatchAllIgnoredPatterns = (file) =>
excludeSpecPattern.every(
(pattern) => !minimatch(file, pattern, { dot: true, matchBase: true }),
);

const globResult = specPattern.reduce(
(files, pattern) => files.concat(glob.sync(pattern, options) || []),
[],
);

return globResult.filter(doesNotMatchAllIgnoredPatterns).length;
};

module.exports = {
getScreenshotAttachment,
getAgentInfo,
getCodeRef,
getSystemAttributes,
getConfig,
getLaunchStartObject,
getSuiteStartObject,
getSuiteEndObject,
getTestStartObject,
getTestEndObject,
getHookStartObject,
// there are utils to preprocess Mocha entities
getTestInfo,
getSuiteStartInfo,
getSuiteEndInfo,
getTestEndObject,
getHookInfo,
getHookStartObject,
getTotalSpecs,
getConfig,
getExcludeSpecPattern,
getFixtureFolderPattern,
getSpecPattern,
getVideoFile,
};
Loading

0 comments on commit 2366a3e

Please sign in to comment.