Skip to content

Commit

Permalink
feat: switch path to path-browserify #142, add support for @badeball
Browse files Browse the repository at this point in the history
…cucumber preprocessor fork #139
  • Loading branch information
Oleksandr Shevtsov authored and Oleksandr Shevtsov committed Jun 27, 2022
1 parent e46b415 commit 8311178
Show file tree
Hide file tree
Showing 7 changed files with 1,836 additions and 1,766 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"dependencies": {
"@shelex/allure-js-commons-browser": "1.3.0",
"crypto-js": "4.1.1",
"debug": "4.3.3",
"debug": "4.3.4",
"path-browserify": "1.0.1",
"uuid": "8.3.2"
},
"devDependencies": {
Expand All @@ -52,8 +53,8 @@
"eslint-config-prettier": "8.3.0",
"eslint-plugin-chai-friendly": "0.7.2",
"eslint-plugin-cypress": "2.12.1",
"eslint-plugin-prettier": "4.0.0",
"prettier": "2.4.1",
"eslint-plugin-prettier": "4.1.0",
"prettier": "2.7.1",
"semantic-release": "17.4.7"
},
"cypress-cucumber-preprocessor": {
Expand Down
315 changes: 190 additions & 125 deletions reporter/allure-cypress/CucumberHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,146 +7,211 @@ module.exports = class CucumberHandler {
this.examplesStorage = [];
}

checkLinksInExamplesTable() {
if (globalThis && globalThis.testState) {
const { testState } = globalThis;
get isStateAvailable() {
return globalThis && globalThis.testState;
}

if (testState.currentScenario.keyword === 'Scenario Outline') {
logger.allure(`populating gherkin links from examples table`);
const scenario = testState.currentScenario;
get state() {
if (!this.isStateAvailable) {
return;
}
return globalThis.testState;
}

!this.examplesStorage.length &&
this.examplesStorage.push(...scenario.examples);
get feature() {
return this.isNewFormat
? this.state.gherkinDocument.feature
: this.state.feature;
}

const example =
this.examplesStorage.length && this.examplesStorage.pop();
get currentScenario() {
if (!this.isStateAvailable) {
return;
}
return (
this.state.currentScenario ||
this.state.gherkinDocument.feature.children.find((child) =>
this.state.pickle.astNodeIds.includes(child.scenario.id)
).scenario
);
}

if (example) {
const findCellIndex = (type) =>
example.tableHeader.cells.findIndex(
(cell) => cell.value === type
);
get isNewFormat() {
if (!this.isStateAvailable) {
return;
}
return Boolean(this.state.gherkinDocument);
}

const tmsCellIndex = findCellIndex('tms');
const issueCellIndex = findCellIndex('issue');
get outlineExampleIndex() {
if (this.isNewFormat) {
const [, exampleId] = this.state.pickle.astNodeIds;
if (!this.currentScenario.examples.length) {
return -1;
}
return this.currentScenario.examples[0].tableBody.findIndex(
(item) => item.id === exampleId
);
}
const num = parseInt(
exampleNumber.exec(this.currentScenario.name).pop()
);
if (!num) {
return -1;
}
return num - 1;
}

const exampleRowNumber = parseInt(
exampleNumber.exec(scenario.name).pop()
);
addTag(scenarioId, tag) {
const currentTags = this.currentScenario.tags || [];
if (this.isNewFormat) {
const indexOfChild = this.feature.children.findIndex(
(child) => child.scenario.id === scenarioId
);
if (indexOfChild === -1) {
return;
}
globalThis.testState.gherkinDocument.feature.children[
indexOfChild
].scenario.tags = [
...currentTags.filter((t) => !t.type && !t.type !== 'Tag'),
tag
];
return;
}
globalThis.testState.runScenarios[this.currentScenario.name].tags = [
...currentTags,
tag
];
}

if (!exampleRowNumber) {
return;
}
checkLinksInExamplesTable() {
if (!this.isStateAvailable) {
return;
}
if (this.currentScenario.keyword !== 'Scenario Outline') {
return;
}
logger.allure(`populating gherkin links from examples table`);
const scenario = this.currentScenario;

const exampleRowIndex = exampleRowNumber - 1;

const findTableCellValue = (headerIndex) =>
example.tableBody[exampleRowIndex].cells[headerIndex]
.value;

const addScenarioTag = (type, value) => {
const current =
globalThis.testState.runScenarios[scenario.name];

globalThis.testState.runScenarios[scenario.name].tags =
[
...current.tags,
{
type: 'Tag',
name: `@${type}("${value}")`
}
];
};

if (tmsCellIndex !== -1) {
const tmsId = findTableCellValue(tmsCellIndex);
logger.allure(`found tms link: %s`, tmsId);
addScenarioTag('tms', tmsId);
}
!this.examplesStorage.length &&
this.examplesStorage.push(...scenario.examples);

if (issueCellIndex !== -1) {
const issueId = findTableCellValue(issueCellIndex);
logger.allure(`found issue link: %s`, issueId);
addScenarioTag('issue', issueId);
}
}
const example =
this.examplesStorage.length && this.examplesStorage.pop();

if (example) {
const findCellIndex = (type) =>
example.tableHeader.cells.findIndex(
(cell) => cell.value === type
);

const tmsCellIndex = findCellIndex('tms');
const issueCellIndex = findCellIndex('issue');

const exampleRowIndex = this.outlineExampleIndex;
if (exampleRowIndex === -1) {
return;
}

const findTableCellValue = (headerIndex) =>
example.tableBody[exampleRowIndex].cells[headerIndex].value;

if (tmsCellIndex !== -1) {
const tmsId = findTableCellValue(tmsCellIndex);
logger.allure(`found tms link: %s`, tmsId);
this.addTag(this.currentScenario.id, {
type: 'Tag',
name: `@tms("${tmsId}")`
});
}

if (issueCellIndex !== -1) {
const issueId = findTableCellValue(issueCellIndex);
logger.allure(`found issue link: %s`, issueId);
this.addTag(this.currentScenario.id, {
type: 'Tag',
name: `@issue("${issueId}")`
});
}
}
}

// accept cucumber tags from cypress-cucumber-preprocessor as commands
checkTags() {
if (globalThis && globalThis.testState) {
logger.allure(`parsing gherkin tags`);
const { testState } = globalThis;
const { currentTest } = this.reporter;

// set bdd feature by default
currentTest.addLabel('feature', testState.feature.name);

/**
* tags set on test level has higher priority
* to not be overwritten by feature tags
*/
['feature', 'currentScenario'].forEach(function (type) {
logger.allure(`tags for %s`, type);
testState[type] &&
testState[type].tags
// check for labels
.filter(function ({ name }) {
const match = tagToLabel.exec(name);
if (match) {
const [, command, value] = match;
// feature and suite should be overwritten to avoid duplicates
if (['feature', 'suite'].includes(command)) {
const index =
currentTest.info.labels.findIndex(
(label) => label.name === command
);
currentTest.info.labels[index] = {
name: command,
value: value
};
} else {
currentTest.addLabel(command, value);
}
}
return !match;
})
// check for links
.filter(function ({ name }) {
const match = tagToLink.exec(name);
if (match) {
const [, command, name, matchUrl] = match;

const url = matchUrl || name;

const prefixBy = {
issue: Cypress.env('issuePrefix'),
tms: Cypress.env('tmsPrefix'),
link: null
};
const urlPrefix = prefixBy[command];

const pattern =
urlPrefix && urlPrefix.includes('*')
? urlPrefix
: `${urlPrefix}*`;
currentTest.addLink(
urlPrefix && pattern
? pattern.replace(/\*/g, url)
: url,
name,
command
);
}
return !match;
})
// add other tags
.forEach(function ({ name }) {
currentTest.addLabel('tag', name.replace('@', ''));
});
});
if (!this.isStateAvailable) {
return;
}
logger.allure(`parsing gherkin tags`);
const { currentTest } = this.reporter;

// set bdd feature by default
currentTest.addLabel('feature', this.feature.name);

/**
* tags set on test level has higher priority
* to not be overwritten by feature tags
*/

[this.feature, this.currentScenario].forEach(function (kind) {
if (!kind || !kind.tags || !kind.tags.length) {
return;
}

kind.tags
.filter(function ({ name }) {
const match = tagToLabel.exec(name);
if (match) {
const [, command, value] = match;
// feature and suite should be overwritten to avoid duplicates
if (['feature', 'suite'].includes(command)) {
const index = currentTest.info.labels.findIndex(
(label) => label.name === command
);
currentTest.info.labels[index] = {
name: command,
value: value
};
} else {
currentTest.addLabel(command, value);
}
}
return !match;
})
// check for links
.filter(function ({ name }) {
const match = tagToLink.exec(name);
if (match) {
const [, command, name, matchUrl] = match;

const url = matchUrl || name;

const prefixBy = {
issue: Cypress.env('issuePrefix'),
tms: Cypress.env('tmsPrefix'),
link: null
};
const urlPrefix = prefixBy[command];

const pattern =
urlPrefix && urlPrefix.includes('*')
? urlPrefix
: `${urlPrefix}*`;
currentTest.addLink(
urlPrefix && pattern
? pattern.replace(/\*/g, url)
: url,
name,
command
);
}
return !match;
})
// add other tags
.forEach(function ({ name }) {
currentTest.addLabel('tag', name.replace('@', ''));
});
});
}
};
2 changes: 1 addition & 1 deletion reporter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const {
EVENT_HOOK_END
} = Mocha.Runner.constants;

const path = require('path');
const path = require('path-browserify');
const {
AllureRuntime,
InMemoryAllureWriter,
Expand Down
2 changes: 1 addition & 1 deletion writer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const path = require('path');
const path = require('path-browserify');
const fs = require('fs');
const process = require('process');
const uuid = require('uuid');
Expand Down
2 changes: 1 addition & 1 deletion writer/attachments.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require('fs');
const path = require('path');
const path = require('path-browserify');
const uuid = require('uuid');
const logger = require('../reporter/debug');
const { createTest } = require('./write');
Expand Down
2 changes: 1 addition & 1 deletion writer/handleCrash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require('fs');
const path = require('path');
const path = require('path-browserify');
const uuid = require('uuid');
const logger = require('../reporter/debug');
const { createTest, createSuite } = require('./write');
Expand Down
Loading

0 comments on commit 8311178

Please sign in to comment.