Skip to content

Commit 2ef6a9b

Browse files
authored
[minor] Allure TestOps: move passed results to watch folder right away (#48)
1 parent 5182a8f commit 2ef6a9b

File tree

5 files changed

+125
-13
lines changed

5 files changed

+125
-13
lines changed

integration/e2e/watch.cy.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
describe('should move passed test right away @watch', () => {
2+
describe('more suite', () => {
3+
before(() => {
4+
cy.allure().step('some setup');
5+
});
6+
7+
for (let i = 0; i < 10; i++) {
8+
it(`test ${i}`, () => {
9+
cy.allure().step('hello');
10+
cy.allure().startStep('with attach');
11+
cy.allure().attachment('out', `test number ${i}`, 'text/plain');
12+
cy.allure().endStep();
13+
14+
cy.allure().startStep('with attach other');
15+
cy.allure().attachment('out2', `test number ${i} - attach 2`, 'text/plain');
16+
cy.allure().endStep();
17+
18+
cy.allure().startStep('may fail');
19+
cy.wait(1000);
20+
21+
if (i % 5 === 0) {
22+
cy.wrap(null).then(() => {
23+
throw new Error('on purpose');
24+
});
25+
}
26+
cy.allure().endStep();
27+
});
28+
}
29+
});
30+
});

package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
"@typescript-eslint/eslint-plugin": "^5.44.0",
8989
"@typescript-eslint/parser": "^5.44.0",
9090
"allure-commandline": "^2.24.0",
91-
"cypress": "^13.3.0",
91+
"cypress": "^13.3.3",
9292
"cypress-redirect-browser-log": "^1.1.2",
9393
"eslint": "^8.46.0",
9494
"eslint-config-prettier": "^8.5.0",

src/plugins/allure-reporter-plugin.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
AllureRuntime,
44
AllureStep,
55
AllureTest,
6+
Attachment,
67
ExecutableItem,
78
ExecutableItemWrapper,
89
FixtureResult,
@@ -732,7 +733,7 @@ export class AllureReporter {
732733
}
733734
}
734735

735-
endTest(arg: AllureTaskArgs<'testEnded'>) {
736+
endTest(arg: AllureTaskArgs<'testEnded'>): { test: string; attachments: any[] } | undefined {
736737
const { result, details } = arg;
737738
const storedStatus = this.testStatusStored;
738739
const storedDetails = this.testDetailsStored;
@@ -751,7 +752,7 @@ export class AllureReporter {
751752
this.endAllSteps({ status: result, details });
752753

753754
if (!this.currentTest) {
754-
return;
755+
return undefined;
755756
}
756757
// filter steps here
757758
this.filterSteps(this.currentTest.wrappedItem);
@@ -768,8 +769,20 @@ export class AllureReporter {
768769

769770
this.applyGroupLabels();
770771
const uid = this.currentTest.uuid;
771-
this.currentTest.endTest();
772772

773+
const getAllAttach = (res: Attachment[], item: ExecutableItem) => {
774+
if (!this.currentTest) {
775+
return [];
776+
}
777+
item.steps.forEach(step => {
778+
res.push(...getAllAttach(res, step));
779+
});
780+
781+
return [...res, ...item.attachments];
782+
};
783+
const resAtt: Attachment[] = [...this.currentTest.wrappedItem.attachments];
784+
const attachments = getAllAttach(resAtt, this.currentTest.wrappedItem);
785+
this.currentTest.endTest();
773786
this.tests.pop();
774787
this.descriptionHtml = [];
775788
this.testStatusStored = undefined;
@@ -788,6 +801,8 @@ export class AllureReporter {
788801
}
789802
};
790803
waitResultWritten(this.allureResults, `${this.allureResults}/${uid}-result.json`);
804+
805+
return { test: `${this.allureResults}/${uid}-result.json`, attachments };
791806
}
792807

793808
startStep(arg: AllureTaskArgs<'stepStarted'>) {

src/plugins/allure.ts

+69-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { AllureTaskArgs, AllureTasks, Status } from './allure-types';
44
import { appendFileSync, copyFile, existsSync, mkdirSync, readFileSync, rm, rmSync, writeFileSync } from 'fs';
55
import { delay, packageLog } from '../common';
66
import glob from 'fast-glob';
7-
import { basename, dirname } from 'path';
7+
import path, { basename, dirname } from 'path';
88

99
const debug = Debug('cypress-allure:proxy');
1010

@@ -24,6 +24,66 @@ export type ReporterOptions = {
2424
isTest: boolean;
2525
};
2626

27+
const copyFileToWatch = async (
28+
input: { test: string; attachments: { name: string; type: string; source: string }[] },
29+
allureResultsWatch: string,
30+
) => {
31+
const { test: allureResultFile, attachments } = input;
32+
const allureResults = path.dirname(allureResultFile);
33+
34+
if (allureResults === allureResultsWatch) {
35+
log(`afterSpec allureResultsWatch the same as allureResults ${allureResults}, will not copy`);
36+
37+
return;
38+
}
39+
40+
if (!existsSync(allureResultsWatch)) {
41+
const mkdirSyncWithTry = (dir: string) => {
42+
for (let i = 0; i < 5; i++) {
43+
try {
44+
mkdirSync(dir);
45+
46+
return;
47+
} catch (err) {
48+
// ignore
49+
}
50+
}
51+
};
52+
mkdirSyncWithTry(allureResultsWatch);
53+
}
54+
log(`allureResults: ${allureResults}`);
55+
log(`allureResultsWatch: ${allureResultsWatch}`);
56+
log(`attachments: ${JSON.stringify(attachments)}`);
57+
58+
attachments.forEach(attach => {
59+
const attachTo = `${allureResultsWatch}/${attach.source}`;
60+
const attachFrom = `${allureResults}/${attach.source}`;
61+
62+
log(`attachTo ${attachTo}`);
63+
log(`attachFrom ${attachFrom}`);
64+
65+
copyFile(attachFrom, attachTo, err => {
66+
if (err) {
67+
log(err);
68+
}
69+
rm(attachFrom, () => {
70+
// ignore
71+
});
72+
});
73+
});
74+
75+
const to = `${allureResultsWatch}/${basename(allureResultFile)}`;
76+
log(`copy file ${allureResultFile} to ${to}`);
77+
copyFile(allureResultFile, to, err => {
78+
if (err) {
79+
log(err);
80+
}
81+
rm(allureResultFile, () => {
82+
// ignore
83+
});
84+
});
85+
};
86+
2787
const copyResultsToWatchFolder = async (allureResults: string, allureResultsWatch: string) => {
2888
if (allureResults === allureResultsWatch) {
2989
log(`afterSpec allureResultsWatch the same as allureResults ${allureResults}, will not copy`);
@@ -240,7 +300,14 @@ export const allureTasks = (opts: ReporterOptions): AllureTasks => {
240300

241301
testEnded: async (arg: AllureTaskArgs<'testEnded'>) => {
242302
log(`testEnded ${JSON.stringify(arg)}`);
243-
allureReporter.endTest(arg);
303+
const res = allureReporter.endTest(arg);
304+
305+
if (res && !opts.allureAddVideoOnPass && arg.result === 'passed') {
306+
// move to watch
307+
308+
log('testEnded: will move result to watch folder');
309+
await copyFileToWatch(res, allureResultsWatch);
310+
}
244311
log('testEnded');
245312
},
246313

0 commit comments

Comments
 (0)