Skip to content

Commit

Permalink
feat: properly handle videoUploadOnPasses #109, remove obsolete proce…
Browse files Browse the repository at this point in the history
…ss.env usage #110, add labels for skipped tests #112, throw error for incorrect testParameter #114
  • Loading branch information
Shelex committed Jan 28, 2022
1 parent 57d5b23 commit 5b42f75
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 36 deletions.
1 change: 1 addition & 0 deletions allure.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ allure.link.tms.pattern=tmsPrefix-
#allure.cypress.log.gherkin=true
#allure.omit.previous.attempt.screenshot=false
#allure.analytics=false
#allure.clear.skipped=false
#allure.video.passed=false
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
"lint": "yarn eslint ."
},
"dependencies": {
"@shelex/allure-js-commons-browser": "1.2.0",
"@shelex/allure-js-commons-browser": "1.3.0",
"crypto-js": "4.1.1",
"debug": "4.3.2",
"debug": "4.3.3",
"uuid": "8.3.2"
},
"devDependencies": {
Expand Down
10 changes: 9 additions & 1 deletion reporter/allure-cypress/AllureInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ Allure.prototype.stepEnd = function () {
};

Allure.prototype.parameter = function (name, value) {
shouldBePrimitiveType(value);
this.reporter.currentExecutable.addParameter(name, value);
};

Allure.prototype.testParameter = function (name, value) {
shouldBePrimitiveType(value);
this.reporter.currentTest.addParameter(name, value);
};

Expand All @@ -98,7 +100,7 @@ Allure.prototype.testName = function (name) {
};

Allure.prototype.label = function (name, value) {
if (this.reporter.currentTest) {
if (this.reporter.currentTest && !this.reporter.currentHook) {
const labelIndex = (name) =>
this.reporter.currentTest.info.labels.findIndex(
(label) => label.name === name
Expand Down Expand Up @@ -137,3 +139,9 @@ module.exports = class AllureInterface {
this.currentHook = this.reporter.currentHook;
}
};

const shouldBePrimitiveType = (value) => {
if (typeof value === 'object' || typeof value === 'function') {
throw new Error('allure.parameter value should be primitive type');
}
};
21 changes: 16 additions & 5 deletions reporter/allure-cypress/AllureReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ module.exports = class AllureReporter {
this.previousTestName = this.runningTest.info.name;
}
this.runningTest = test;
// in case labels were defined outside of test
// we could attach them from storage
if (this.runningTest) {
this.runningTest.info.labels.push(...this.labelStorage);
}
}

get testNameForAttachment() {
Expand Down Expand Up @@ -411,6 +406,7 @@ module.exports = class AllureReporter {
this.currentHook && this.currentHook.endStep();
}
!failed && (this.currentHook = null);
this.finishRemainingSteps(currentHookInfo.status);
}

pushSuite(suite) {
Expand All @@ -425,6 +421,21 @@ module.exports = class AllureReporter {
if (this.currentTest === null) {
throw new Error('finishing test while no test is running');
}

// in case labels were defined outside of test
// we could attach them from storage
if (this.currentTest) {
this.labelStorage.forEach((label) => {
const indexOfExisting = this.currentTest.info.labels.findIndex(
(existingLabel) => existingLabel.name === label.name
);
indexOfExisting === -1
? this.currentTest.info.labels.push(label)
: (this.currentTest.info.labels[indexOfExisting].value =
label.value);
});
}

(this.config.shouldLogCypress() ||
this.config.shouldLogGherkinSteps()) &&
this.cy.handleRemainingCommands(status);
Expand Down
14 changes: 6 additions & 8 deletions writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ const { handleCrash } = require('./writer/handleCrash');
function allureWriter(on, config) {
allurePropertiesToEnvVars(config.env);

// pass allure config from Cypress.env to process.env
// to get access from node context
process.env.allure = config.env.allure;

process.env.allureResultsPath =
config.env.allureResultsPath || 'allure-results';
if (!config.env.allureResultsPath) {
config.env.allureResultsPath = 'allure-results';
}

let allureMapping = null;

Expand All @@ -25,10 +22,11 @@ function allureWriter(on, config) {
if (!config.env.allure) {
return;
}
logger.writer('inside "after:spec" event');

try {
results.error
? handleCrash(results)
? handleCrash(results, config)
: attachScreenshotsAndVideo(allureMapping, results, config);
} catch (e) {
logger.writer(
Expand Down Expand Up @@ -56,7 +54,7 @@ function allureWriter(on, config) {
executorInfo
} = writer;

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

try {
Expand Down
8 changes: 4 additions & 4 deletions writer/attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const attachScreenshotsAndVideo = (allureMapping, results, config) => {
results.video &&
`${uuid.v4()}-attachment${path.extname(results.video)}`;

const shouldAddVideoOnPass = config.env.allureAddVideoOnPass !== false;
const shouldAddVideoOnPass = config.env.allureAddVideoOnPass === true;

const needVideo = results.tests.filter((test) => {
const allureId = allureMapping[test.testId];
Expand All @@ -28,7 +28,7 @@ const attachScreenshotsAndVideo = (allureMapping, results, config) => {

const fileName = `${allureId}-result.json`;

const testFilePath = path.join(process.env.allureResultsPath, fileName);
const testFilePath = path.join(config.env.allureResultsPath, fileName);

const content =
fs.existsSync(testFilePath) && fs.readFileSync(testFilePath);
Expand All @@ -50,7 +50,7 @@ const attachScreenshotsAndVideo = (allureMapping, results, config) => {
)}`;
logger.writer('going to attach screenshot to "%s"', allureId);
const allureScreenshotPath = path.join(
process.env.allureResultsPath,
config.env.allureResultsPath,
allureScreenshotFileName
);

Expand Down Expand Up @@ -100,7 +100,7 @@ const attachScreenshotsAndVideo = (allureMapping, results, config) => {

if (needVideo.length) {
logger.writer('found %d tests that require video', needVideo.length);
const resultsPath = path.join(process.env.allureResultsPath, videoPath);
const resultsPath = path.join(config.env.allureResultsPath, videoPath);

logger.writer(
'copying video from "%s" to "%s"',
Expand Down
12 changes: 6 additions & 6 deletions writer/handleCrash.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ const path = require('path');
const uuid = require('uuid');
const logger = require('../reporter/debug');

const handleCrash = (results) => {
const handleCrash = (results, config) => {
if (!results.error) {
return;
}

!fs.existsSync(process.env.allureResultsPath) &&
fs.mkdirSync(process.env.allureResultsPath, { recursive: true });
!fs.existsSync(config.env.allureResultsPath) &&
fs.mkdirSync(config.env.allureResultsPath, { recursive: true });

logger.writer('identified cypress error');

Expand All @@ -30,20 +30,20 @@ const handleCrash = (results) => {
source: videoPath
});

const resultsPath = path.join(process.env.allureResultsPath, videoPath);
const resultsPath = path.join(config.env.allureResultsPath, videoPath);

fs.copyFileSync(results.video, resultsPath);
}

const suiteFileName = `${suite.uuid}-container.json`;

const suitePath = path.join(process.env.allureResultsPath, suiteFileName);
const suitePath = path.join(config.env.allureResultsPath, suiteFileName);

fs.writeFileSync(suitePath, JSON.stringify(suite));

const testFileName = `${test.uuid}-result.json`;

const testPath = path.join(process.env.allureResultsPath, testFileName);
const testPath = path.join(config.env.allureResultsPath, testFileName);

fs.writeFileSync(testPath, JSON.stringify(test));

Expand Down
1 change: 1 addition & 0 deletions writer/readProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const allurePropertiesToEnvVars = (envVars) => {
'allureOmitPreviousAttemptScreenshots'
],
['allure.analytics', 'allureAddAnalyticLabels'],
['allure.clear.skipped', 'allureClearSkippedTests'],
['allure.video.passed', 'allureAddVideoOnPass']
]);

Expand Down
22 changes: 12 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1192,12 +1192,12 @@
lodash "^4.17.4"
read-pkg-up "^7.0.0"

"@shelex/allure-js-commons-browser@1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@shelex/allure-js-commons-browser/-/allure-js-commons-browser-1.2.0.tgz#16e8752e100860a3ecd5bb2581113f131e27cf39"
integrity sha512-BlRjExO+Y/Y0OMwEUhyCinf6rkcqTVPeAC7W5QENTXrvhu6CKXqUlhSTM+GUu4tOSlNSwdyvSI+fiMjCV1TI5g==
"@shelex/allure-js-commons-browser@1.3.0":
version "1.3.0"
resolved "https://registry.yarnpkg.com/@shelex/allure-js-commons-browser/-/allure-js-commons-browser-1.3.0.tgz#dee9378678ebef57fd0793a3997146c785055529"
integrity sha512-wscwFaM/yZ72yXv0fnQGKLLecLVrDBJAsZ2PoyaMRi0E7E2Ix85XGbOjy0k+9e89SBskaF+Qq1B1IRGkKz39yA==
dependencies:
uuid "8.3.0"
uuid "8.3.2"

"@tootallnate/once@1":
version "1.1.2"
Expand Down Expand Up @@ -2765,6 +2765,13 @@ debug@4.1.1:
dependencies:
ms "^2.1.1"

debug@4.3.3:
version "4.3.3"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.3.tgz#04266e0b70a98d4462e6e288e38259213332b664"
integrity sha512-/zxw5+vh1Tfv+4Qn7a5nsbcJKPaSvCDhojn6FEl9vupwK2VCSDtEiEtqr8DFtzYFOdz63LBkxec7DYuc2jon6Q==
dependencies:
ms "2.1.2"

debug@^2.2.0, debug@^2.3.3:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
Expand Down Expand Up @@ -7454,11 +7461,6 @@ util@~0.10.1:
dependencies:
inherits "2.0.3"

uuid@8.3.0:
version "8.3.0"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.0.tgz#ab738085ca22dc9a8c92725e459b1d507df5d6ea"
integrity sha512-fX6Z5o4m6XsXBdli9g7DtWgAx+osMsRRZFKma1mIUsLCz6vRvv+pz5VNbyu9UEDzpMWulZfvpgb/cmDXVulYFQ==

uuid@8.3.2, uuid@^8.3.2:
version "8.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
Expand Down

0 comments on commit 5b42f75

Please sign in to comment.