Skip to content

Commit

Permalink
feat: writer - use separate catch per allure entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksandr Shevtsov authored and Oleksandr Shevtsov committed Nov 3, 2022
1 parent c11a721 commit 617e033
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 198 deletions.
29 changes: 17 additions & 12 deletions reporter/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ const debug = require('debug');
const namespace = 'allure-plugin';
const scopes = ['allure', 'mocha', 'cy', 'command', 'writer'];

const logger = scopes.reduce((loggers, scope) => {
const base = debug(`${namespace}:${scope}`);
// eslint-disable-next-line no-console
base.log = console.log.bind(console);
loggers[scope] = base;
return loggers;
}, {});
/**
* @typedef Logger
* @property {*} allure
* @property {*} mocha
* @property {*} cy
* @property {*} command
* @property {*} writer
*/

/**
* Print out debug message
Expand All @@ -19,10 +20,14 @@ const logger = scopes.reduce((loggers, scope) => {
* %d Number (both integer and float).
* %j JSON. Replaced with the string '[Circular]' if the argument contains circular references.
* %% Single percent sign ('%'). This does not consume an argument.
* @property {*} allure
* @property {*} mocha
* @property {*} cy
* @property {*} command
* @property {*} writer
* @type {Logger}
*/
const logger = scopes.reduce((loggers, scope) => {
const base = debug(`${namespace}:${scope}`);
// eslint-disable-next-line no-console
base.log = console.log.bind(console);
loggers[scope] = base;
return loggers;
}, {});

module.exports = logger;
195 changes: 11 additions & 184 deletions writer.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
const path = require('path-browserify');
const fs = require('fs');
const process = require('process');
const uuid = require('uuid');
const logger = require('./reporter/debug');
const { allurePropertiesToEnvVars } = require('./writer/readProperties');
const { overwriteTestNameMaybe } = require('./writer/customTestName');
const { clearEmptyHookSteps } = require('./writer/clearEmptyHookSteps');
const { shouldUseAfterSpec } = require('./writer/useAfterSpec');
const { alreadyRegisteredAfterSpec } = require('./writer/checkPluginsFile');
const { handleResults } = require('./writer/handleCypressResults');
const {
writeEnvProperties,
writeInfoFile
} = require('./writer/writeInfoFiles');
const { writeResultFiles } = require('./writer/results');

function allureWriter(on, config) {
allurePropertiesToEnvVars(config.env);
Expand All @@ -28,8 +21,8 @@ function allureWriter(on, config) {
'allure should use "after:spec" for handling attachments'
);
if (
alreadyRegisteredAfterSpec(config) ||
config.env.allureReuseAfterSpec
config.env.allureReuseAfterSpec ||
alreadyRegisteredAfterSpec(config)
) {
logger.writer(
'you already have "after:spec", allure plugin will listen to process'
Expand All @@ -41,7 +34,7 @@ function allureWriter(on, config) {
// ( cypress uses it to trigger event for internal event emitter under the hood )
process.on('message', (message) => {
const [event, , args] = message.args;
if (event !== 'after:spec' || !config.env.allure) {
if (!config.env.allure || event !== 'after:spec') {
return;
}
const [, results] = args;
Expand All @@ -68,183 +61,17 @@ function allureWriter(on, config) {
? path.join(config.projectRoot, relativeResultsDir)
: relativeResultsDir;

logger.writer(
'starting writing allure results to "%s"',
resultsDir
);
const {
groups,
tests,
attachments,
envInfo,
categories,
executorInfo
} = writer;

config.env.allureResultsPath = resultsDir;
allureMapping = mapping;

try {
!fs.existsSync(resultsDir) &&
fs.mkdirSync(resultsDir, { recursive: true });
files &&
files.forEach((file) => {
if (fs.existsSync(file.path)) {
const ext = path.extname(file.path);
const allureFilePath = path.join(
resultsDir,
`${uuid.v4()}-attachment${ext}`
);
logger.writer(
'copy attachment "%s" to "%s"',
file.path,
allureFilePath
);

fs.copyFileSync(file.path, allureFilePath);

if (fs.existsSync(allureFilePath)) {
const testsForAttachment = tests.filter(
(t) =>
t.name === file.testName ||
t.name ===
`"before all" hook for "${file.testName}"` ||
t.name ===
`"after all" hook for "${file.testName}"`
);

testsForAttachment.forEach((test) => {
logger.writer(
'attach "%s" to test "%s"',
path.basename(allureFilePath),
test.name
);
test.attachments.push({
name: file.name,
type: file.type,
source: path.basename(allureFilePath)
});
});
}
}
});
groups &&
groups.forEach((group) => {
if (group.children.length) {
if (clearSkipped) {
logger.writer(
'clearing skipped tests enabled, removing tests from suite %s',
group.name
);
group.children = group.children.filter(
(testId) => {
const test = tests.find(
(test) => test.uuid === testId
);
return (
test && test.status !== 'skipped'
);
}
);
if (!group.children.length) {
logger.writer(
'skip suite as it has no tests remained'
);
return;
}
}

const fileName = `${group.uuid}-container.json`;
const groupResultPath = path.join(
resultsDir,
fileName
);

logger.writer(
'write suite "%s" to file "%s"',
group.name,
fileName
);

// remove empty set up and tear down global hooks
group.befores =
(group.befores &&
group.befores.filter(
(before) => before.steps.length
)) ||
[];
group.afters =
(group.afters &&
group.afters.filter(
(after) => after.steps.length
)) ||
[];

fs.writeFileSync(
groupResultPath,
JSON.stringify(group)
);
}
});
tests &&
tests.forEach((test) => {
if (clearSkipped && test.status === 'skipped') {
logger.writer('skipping test "%s"', test.name);
return;
}

const fileName = `${test.uuid}-result.json`;
logger.writer(
'write test "%s" to file "%s"',
test.name,
fileName
);
const testResultPath = path.join(resultsDir, fileName);
const updatedTest = overwriteTestNameMaybe(test);
const testResult = clearEmptyHookSteps(updatedTest);
fs.writeFileSync(
testResultPath,
JSON.stringify(testResult)
);
});
if (attachments) {
for (let [name, content] of Object.entries(attachments)) {
const attachmentPath = path.join(resultsDir, name);

logger.writer(
'write attachment "%s" to "%s"',
name,
attachmentPath
);

!fs.existsSync(attachmentPath) &&
fs.writeFileSync(attachmentPath, content, {
encoding: 'binary'
});
}
}
writeResultFiles({
resultsDir,
files,
clearSkipped,
writer
});

writeInfoFile(
path.join(resultsDir, 'categories.json'),
categories
);
writeInfoFile(
path.join(resultsDir, 'executor.json'),
executorInfo
);
writeEnvProperties(
path.join(resultsDir, 'environment.properties'),
envInfo
);
logger.writer('finished writing allure results');
} catch (e) {
process.stdout.write(
`error while writing allure results: ${e}`
);
logger.writer('failed to write allure results: %O', e);
} finally {
return null;
}
return null;
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion writer/attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fs = require('fs');
const path = require('path-browserify');
const uuid = require('uuid');
const logger = require('../reporter/debug');
const { createTest } = require('./write');
const { createTest } = require('./sampleEntity');

const videoContentType = 'video/mp4';
const imageContentType = 'image/png';
Expand Down
2 changes: 1 addition & 1 deletion writer/handleCrash.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fs = require('fs');
const path = require('path-browserify');
const uuid = require('uuid');
const logger = require('../reporter/debug');
const { createTest, createSuite } = require('./write');
const { createTest, createSuite } = require('./sampleEntity');

const handleCrash = (results, config) => {
if (!results.error) {
Expand Down
File renamed without changes.
Loading

0 comments on commit 617e033

Please sign in to comment.