Skip to content

Commit

Permalink
feat: suite metadata for cucumber (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
ASaiAnudeep authored Aug 25, 2024
1 parent a5e41b7 commit 0f471ae
Show file tree
Hide file tree
Showing 17 changed files with 685 additions and 35 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "testbeats",
"version": "2.1.0",
"version": "2.1.1",
"description": "Publish test results to Microsoft Teams, Google Chat, Slack and InfluxDB",
"main": "src/index.js",
"types": "./src/index.d.ts",
Expand Down Expand Up @@ -55,7 +55,7 @@
"pretty-ms": "^7.0.1",
"rosters": "0.0.1",
"sade": "^1.8.1",
"test-results-parser": "0.2.3"
"test-results-parser": "0.2.4"
},
"devDependencies": {
"c8": "^7.12.0",
Expand Down
118 changes: 118 additions & 0 deletions src/platforms/base.platform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
const { getPercentage, getPrettyDuration } = require('../helpers/helper')

class BasePlatform {

/**
* @param {string|number} text
*/
bold(text) {
throw new Error('Not Implemented');
}

break() {
throw new Error('Not Implemented');
}

/**
*
* @param {import('..').Target} target
* @param {import('test-results-parser').ITestSuite} suite
*/
getSuiteSummaryText(target, suite) {
const suite_title = this.getSuiteTitle(suite);
const suite_results_text = this.#getSuiteResultsText(suite);
const duration_text = this.#getSuiteDurationText(target, suite);

const texts = [
this.bold(suite_title),
this.break(),
this.break(),
suite_results_text,
this.break(),
duration_text,
];

const metadata_text = this.getSuiteMetaDataText(suite);

if (metadata_text) {
texts.push(this.break());
texts.push(this.break());
texts.push(metadata_text);
}

return texts.join('');
}

/**
*
* @param {import('test-results-parser').ITestSuite} suite
* @returns {string}
*/
getSuiteTitle(suite) {
const emoji = suite.status === 'PASS' ? '✅' : suite.total === suite.skipped ? '⏭️' : '❌';
return `${emoji} ${suite.name}`;
}

/**
*
* @param {import('test-results-parser').ITestSuite} suite
* @returns {string}
*/
#getSuiteResultsText(suite) {
const suite_results = this.getSuiteResults(suite);
return `${this.bold('Results')}: ${suite_results}`;
}

/**
*
* @param {import('test-results-parser').ITestSuite} suite
* @returns {string}
*/
getSuiteResults(suite) {
return `${suite.passed} / ${suite.total} Passed (${getPercentage(suite.passed, suite.total)}%)`;
}

/**
*
* @param {import('..').Target} target
* @param {import('test-results-parser').ITestSuite} suite
*/
#getSuiteDurationText(target, suite) {
const duration = this.getSuiteDuration(target, suite);
return `${this.bold('Duration')}: ${duration}`
}

/**
*
* @param {import('..').Target} target
* @param {import('test-results-parser').ITestSuite} suite
*/
getSuiteDuration(target, suite) {
return getPrettyDuration(suite.duration, target.inputs.duration);
}

/**
*
* @param {import('test-results-parser').ITestSuite} suite
* @returns {string}
*/
getSuiteMetaDataText(suite) {
if (!suite || !suite.metadata) {
return;
}

const texts = [];

if (suite.metadata.platform && suite.metadata.platform.name && suite.metadata.platform.version) {
texts.push(`${suite.metadata.platform.name} ${suite.metadata.platform.version}`)
}

if (suite.metadata.browser && suite.metadata.browser.name && suite.metadata.browser.version) {
texts.push(`${suite.metadata.browser.name} ${suite.metadata.browser.version}`)
}

return texts.join(' • ');
}
}

module.exports = { BasePlatform }
18 changes: 18 additions & 0 deletions src/platforms/chat.platform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { BasePlatform } = require("./base.platform");

class ChatPlatform extends BasePlatform {

/**
* @param {string|number} text
*/
bold(text) {
return `<b>${text}</b>`;
}

break() {
return '<br>';
}

}

module.exports = { ChatPlatform }
25 changes: 25 additions & 0 deletions src/platforms/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { TARGET } = require("../helpers/constants");
const { SlackPlatform } = require('./slack.platform');
const { TeamsPlatform } = require('./teams.platform');
const { ChatPlatform } = require('./chat.platform');

/**
*
* @param {string} name
*/
function getPlatform(name) {
switch (name) {
case TARGET.SLACK:
return new SlackPlatform();
case TARGET.TEAMS:
return new TeamsPlatform();
case TARGET.CHAT:
return new ChatPlatform();
default:
throw new Error('Invalid Platform');
}
}

module.exports = {
getPlatform
}
17 changes: 17 additions & 0 deletions src/platforms/slack.platform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { BasePlatform } = require("./base.platform");

class SlackPlatform extends BasePlatform {

/**
* @param {string|number} text
*/
bold(text) {
return `*${text}*`;
}

break() {
return '\n';
}
}

module.exports = { SlackPlatform }
16 changes: 16 additions & 0 deletions src/platforms/teams.platform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { BasePlatform } = require("./base.platform");

class TeamsPlatform extends BasePlatform {
/**
* @param {string|number} text
*/
bold(text) {
return `**${text}**`;
}

break() {
return '\n\n';
}
}

module.exports = { TeamsPlatform }
11 changes: 5 additions & 6 deletions src/targets/chat.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const request = require('phin-retry');
const { getTitleText, getResultText, truncate, getPrettyDuration } = require('../helpers/helper');
const extension_manager = require('../extensions');
const { HOOK, STATUS } = require('../helpers/constants');
const { HOOK, STATUS, TARGET } = require('../helpers/constants');
const PerformanceTestResult = require('performance-results-parser/src/models/PerformanceTestResult');
const { getValidMetrics, getMetricValuesText } = require('../helpers/performance');
const logger = require('../utils/logger');
const { getPlatform } = require('../platforms');

async function run({ result, target }) {
setTargetInputs(target);
Expand Down Expand Up @@ -100,11 +101,9 @@ function setSuiteBlock({ result, target, payload }) {
}

function getSuiteSummary({ target, suite }) {
const emoji = suite.status === 'PASS' ? '✅' : '❌';
const suite_title = `${emoji} ${suite.name}`;
const result_text = getResultText({ result: suite });
const duration_text = getPrettyDuration(suite.duration, target.inputs.duration);
return `<b>${suite_title}</b><br><br><b>Results</b>: ${result_text}<br><b>Duration</b>: ${duration_text}`;
const platform = getPlatform(TARGET.CHAT);
const text = platform.getSuiteSummaryText(target, suite);
return text;
}

function getFailureDetails(suite) {
Expand Down
13 changes: 4 additions & 9 deletions src/targets/slack.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const request = require('phin-retry');
const { getPercentage, truncate, getPrettyDuration } = require('../helpers/helper');
const extension_manager = require('../extensions');
const { HOOK, STATUS } = require('../helpers/constants');
const { HOOK, STATUS, TARGET } = require('../helpers/constants');
const logger = require('../utils/logger');

const PerformanceTestResult = require('performance-results-parser/src/models/PerformanceTestResult');
const { getValidMetrics, getMetricValuesText } = require('../helpers/performance');
const TestResult = require('test-results-parser/src/models/TestResult');
const { getPlatform } = require('../platforms');



Expand Down Expand Up @@ -109,9 +110,8 @@ function setSuiteBlock({ result, target, payload }) {
}

function getSuiteSummary({ target, suite }) {
let text = `*${getSuiteTitle(suite)}*\n`;
text += `\n*Results*: ${getResultText(suite)}`;
text += `\n*Duration*: ${getPrettyDuration(suite.duration, target.inputs.duration)}`;
const platform = getPlatform(TARGET.SLACK);
const text = platform.getSuiteSummaryText(target, suite);
return {
"type": "section",
"text": {
Expand All @@ -121,11 +121,6 @@ function getSuiteSummary({ target, suite }) {
};
}

function getSuiteTitle(suite) {
const emoji = suite.status === 'PASS' ? '✅' : suite.total === suite.skipped ? '⏭️' : '❌';
return `${emoji} ${suite.name}`;
}

function getFailureDetails(suite) {
let text = '';
const cases = suite.cases;
Expand Down
Loading

0 comments on commit 0f471ae

Please sign in to comment.