Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[minor] Allure TestOps: move passed results to watch folder right away #48

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions integration/e2e/watch.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
describe('should move passed test right away @watch', () => {
describe('more suite', () => {
before(() => {
cy.allure().step('some setup');
});

for (let i = 0; i < 10; i++) {
it(`test ${i}`, () => {
cy.allure().step('hello');
cy.allure().startStep('with attach');
cy.allure().attachment('out', `test number ${i}`, 'text/plain');
cy.allure().endStep();

cy.allure().startStep('with attach other');
cy.allure().attachment('out2', `test number ${i} - attach 2`, 'text/plain');
cy.allure().endStep();

cy.allure().startStep('may fail');
cy.wait(1000);

if (i % 5 === 0) {
cy.wrap(null).then(() => {
throw new Error('on purpose');
});
}
cy.allure().endStep();
});
}
});
});
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"@typescript-eslint/eslint-plugin": "^5.44.0",
"@typescript-eslint/parser": "^5.44.0",
"allure-commandline": "^2.24.0",
"cypress": "^13.3.0",
"cypress": "^13.3.3",
"cypress-redirect-browser-log": "^1.1.2",
"eslint": "^8.46.0",
"eslint-config-prettier": "^8.5.0",
Expand Down
21 changes: 18 additions & 3 deletions src/plugins/allure-reporter-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
AllureRuntime,
AllureStep,
AllureTest,
Attachment,
ExecutableItem,
ExecutableItemWrapper,
FixtureResult,
Expand Down Expand Up @@ -639,7 +640,7 @@
}

const group = this.currentGroup;
const test = group!.startTest(title);

Check warning on line 643 in src/plugins/allure-reporter-plugin.ts

View workflow job for this annotation

GitHub Actions / build

Forbidden non-null assertion

Check warning on line 643 in src/plugins/allure-reporter-plugin.ts

View workflow job for this annotation

GitHub Actions / build

Forbidden non-null assertion

allTests.push({ specRelative: this.currentSpec?.relative, fullTitle, mochaId: id, uuid: test.uuid }); // to show warning
this.tests.push(test);
Expand Down Expand Up @@ -732,7 +733,7 @@
}
}

endTest(arg: AllureTaskArgs<'testEnded'>) {
endTest(arg: AllureTaskArgs<'testEnded'>): { test: string; attachments: any[] } | undefined {

Check warning on line 736 in src/plugins/allure-reporter-plugin.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

Check warning on line 736 in src/plugins/allure-reporter-plugin.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
const { result, details } = arg;
const storedStatus = this.testStatusStored;
const storedDetails = this.testDetailsStored;
Expand All @@ -751,7 +752,7 @@
this.endAllSteps({ status: result, details });

if (!this.currentTest) {
return;
return undefined;
}
// filter steps here
this.filterSteps(this.currentTest.wrappedItem);
Expand All @@ -768,8 +769,20 @@

this.applyGroupLabels();
const uid = this.currentTest.uuid;
this.currentTest.endTest();

const getAllAttach = (res: Attachment[], item: ExecutableItem) => {
if (!this.currentTest) {
return [];
}
item.steps.forEach(step => {
res.push(...getAllAttach(res, step));
});

return [...res, ...item.attachments];
};
const resAtt: Attachment[] = [...this.currentTest.wrappedItem.attachments];
const attachments = getAllAttach(resAtt, this.currentTest.wrappedItem);
this.currentTest.endTest();
this.tests.pop();
this.descriptionHtml = [];
this.testStatusStored = undefined;
Expand All @@ -788,6 +801,8 @@
}
};
waitResultWritten(this.allureResults, `${this.allureResults}/${uid}-result.json`);

return { test: `${this.allureResults}/${uid}-result.json`, attachments };
}

startStep(arg: AllureTaskArgs<'stepStarted'>) {
Expand Down
71 changes: 69 additions & 2 deletions src/plugins/allure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AllureTaskArgs, AllureTasks, Status } from './allure-types';
import { appendFileSync, copyFile, existsSync, mkdirSync, readFileSync, rm, rmSync, writeFileSync } from 'fs';
import { delay, packageLog } from '../common';
import glob from 'fast-glob';
import { basename, dirname } from 'path';
import path, { basename, dirname } from 'path';

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

Expand All @@ -24,6 +24,66 @@ export type ReporterOptions = {
isTest: boolean;
};

const copyFileToWatch = async (
input: { test: string; attachments: { name: string; type: string; source: string }[] },
allureResultsWatch: string,
) => {
const { test: allureResultFile, attachments } = input;
const allureResults = path.dirname(allureResultFile);

if (allureResults === allureResultsWatch) {
log(`afterSpec allureResultsWatch the same as allureResults ${allureResults}, will not copy`);

return;
}

if (!existsSync(allureResultsWatch)) {
const mkdirSyncWithTry = (dir: string) => {
for (let i = 0; i < 5; i++) {
try {
mkdirSync(dir);

return;
} catch (err) {
// ignore
}
}
};
mkdirSyncWithTry(allureResultsWatch);
}
log(`allureResults: ${allureResults}`);
log(`allureResultsWatch: ${allureResultsWatch}`);
log(`attachments: ${JSON.stringify(attachments)}`);

attachments.forEach(attach => {
const attachTo = `${allureResultsWatch}/${attach.source}`;
const attachFrom = `${allureResults}/${attach.source}`;

log(`attachTo ${attachTo}`);
log(`attachFrom ${attachFrom}`);

copyFile(attachFrom, attachTo, err => {
if (err) {
log(err);
}
rm(attachFrom, () => {
// ignore
});
});
});

const to = `${allureResultsWatch}/${basename(allureResultFile)}`;
log(`copy file ${allureResultFile} to ${to}`);
copyFile(allureResultFile, to, err => {
if (err) {
log(err);
}
rm(allureResultFile, () => {
// ignore
});
});
};

const copyResultsToWatchFolder = async (allureResults: string, allureResultsWatch: string) => {
if (allureResults === allureResultsWatch) {
log(`afterSpec allureResultsWatch the same as allureResults ${allureResults}, will not copy`);
Expand Down Expand Up @@ -240,7 +300,14 @@ export const allureTasks = (opts: ReporterOptions): AllureTasks => {

testEnded: async (arg: AllureTaskArgs<'testEnded'>) => {
log(`testEnded ${JSON.stringify(arg)}`);
allureReporter.endTest(arg);
const res = allureReporter.endTest(arg);

if (res && !opts.allureAddVideoOnPass && arg.result === 'passed') {
// move to watch

log('testEnded: will move result to watch folder');
await copyFileToWatch(res, allureResultsWatch);
}
log('testEnded');
},

Expand Down
Loading