Skip to content

Commit

Permalink
fix: write file task, write attachments separately in promise
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksandr Shevtsov committed Dec 31, 2020
1 parent 722ee6e commit d6dfea9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 36 deletions.
22 changes: 10 additions & 12 deletions reporter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ class CypressAllureReporter {
.now(
'task',
'writeAllureResults',
this.reporter.runtime.config,
{
results: this.reporter.runtime.config,
files: this.reporter.files
},
{ log: false }
)
.catch((e) => allureDebug && console.error(e));
Expand Down Expand Up @@ -120,17 +123,12 @@ Cypress.Allure = allureEnabled ? new CypressAllureReporter() : stubbedAllure;
Cypress.Screenshot.defaults({
onAfterScreenshot(_, details) {
if (allureEnabled) {
cy.task('copyFileToAllure', details.path, { log: false }).then(
(filePath) => {
filePath &&
Cypress.Allure.reporter.currentTest.addAttachment(
details.name ||
`${details.specName}:${details.takenAt}`,
ContentType.PNG,
filePath
);
}
);
Cypress.Allure.reporter.files.push({
name: details.name || `${details.specName}:${details.takenAt}`,
path: details.path,
type: ContentType.PNG,
testName: Cypress.Allure.reporter.testNameForAttachment
});
}
}
});
11 changes: 6 additions & 5 deletions reporter/mocha-allure/AllureInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ Allure.prototype.testAttachment = function (name, content, type) {
};

Allure.prototype.attachFile = function (name, path, type) {
cy.now('task', 'copyFileToAllure', path, {
log: false
}).then((allurePath) =>
this.currentTest.addAttachment(name, type, allurePath)
);
this.reporter.files.push({
name: name,
path: path,
type: type,
testName: this.reporter.testNameForAttachment
});
};

Allure.prototype.writeExecutorInfo = function (info) {
Expand Down
14 changes: 14 additions & 0 deletions reporter/mocha-allure/AllureReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ module.exports = class AllureReporter {
this.suites = [];
this.steps = [];
this.commands = [];
this.files = [];
this.currentChainer = null;
this.runningTest = null;
this.previousTestName = null;
this.runtime = runtime;
this.currentHook = null;
this.parentStep = null;
Expand Down Expand Up @@ -67,9 +69,21 @@ module.exports = class AllureReporter {
}

set currentTest(test) {
if (this.runningTest) {
this.previousTestName = this.runningTest.info.name;
}
this.runningTest = test;
}

get testNameForAttachment() {
const cyTest = cy.state().test;
return (
(cyTest && cyTest.title) ||
(this.currentTest && this.currentTest.info.name) ||
previousTestName
);
}

startSuite(suiteName) {
if (this.currentSuite) {
if (this.currentSuiteIsGlobal) {
Expand Down
47 changes: 28 additions & 19 deletions writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ function allureWriter(on, config) {
config.env.allureResultsPath || 'allure-results';

on('task', {
writeAllureResults: ({ resultsDir, writer }) => {
writeAllureResults: ({ results, files }) => {
const { resultsDir, writer } = results;
const {
groups,
tests,
Expand All @@ -23,6 +24,32 @@ function allureWriter(on, config) {
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}`
);

fs.copyFileSync(file.path, allureFilePath);

if (fs.existsSync(allureFilePath)) {
const testsForAttachment = tests.filter(
(t) => t.name === file.testName
);

testsForAttachment.forEach((test) => {
test.attachments.push({
name: file.name,
type: file.type,
source: path.basename(allureFilePath)
});
});
}
}
});
groups &&
groups.forEach((group) => {
if (group.children.length) {
Expand Down Expand Up @@ -67,24 +94,6 @@ function allureWriter(on, config) {
} finally {
return null;
}
},
copyFileToAllure: (filePath) => {
const resultsDir = process.env.allureResultsPath;
if (process.env.allure === 'true') {
!fs.existsSync(resultsDir) &&
fs.mkdirSync(resultsDir, { recursive: true });
const ext = path.extname(filePath);
const allurePath = path.join(
resultsDir,
`${uuid.v4()}-attachment${ext}`
);

fs.copyFileSync(filePath, allurePath);

return fs.existsSync(allurePath)
? path.basename(allurePath)
: null;
}
}
});
}
Expand Down

0 comments on commit d6dfea9

Please sign in to comment.