Skip to content

Commit

Permalink
Merge pull request #72 from test-results-reporter/12-condition-suppor…
Browse files Browse the repository at this point in the history
…t-functions

feat: support functional conditions
  • Loading branch information
ASaiAnudeep authored Jul 29, 2022
2 parents 4d67aa1 + 63212c5 commit 21ea346
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/assets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/setup-node@v2
with:
node-version: '12.x'
- run: npm install
- run: npm install --only=prod
- run: npm run build
- uses: vimtor/action-zip@v1
with:
Expand Down
6 changes: 3 additions & 3 deletions src/extensions/hyperlinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function attachLinksToChat({ extension, payload, raw_links }) {

async function getLinks({ target, result, extension }) {
const raw_links = extension.inputs.links;
const raw_valid_links = getValidLinks(raw_links, result);
const raw_valid_links = await getValidLinks(raw_links, result);
for (let i = 0; i < raw_valid_links.length; i++) {
const url_function = raw_valid_links[i].url;
if (typeof url_function === 'function') {
Expand All @@ -60,11 +60,11 @@ async function getLinks({ target, result, extension }) {
return raw_valid_links;
}

function getValidLinks(links, result) {
async function getValidLinks(links, result) {
const valid_links = [];
for (const link of links) {
const condition = link.condition || default_options.condition;
if (checkCondition({ condition, result })) {
if (await checkCondition({ condition, result })) {
valid_links.push(link);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async function run(options) {
const extension_runner = getExtensionRunner(extension);
const extension_options = Object.assign({}, extension_runner.default_options, extension);
if (extension_options.hook === hook) {
if (checkCondition({ condition: extension_options.condition, result })) {
if (await checkCondition({ condition: extension_options.condition, result, target, extension })) {
extension.outputs = {};
options.extension = extension;
try {
Expand Down
16 changes: 10 additions & 6 deletions src/helpers/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,18 @@ function getResultText({ result }) {
/**
*
* @param {object} param0
* @param {string} param0.condition
* @param {string | Function} param0.condition
*/
function checkCondition({ condition, result }) {
const lower_condition = condition.toLowerCase();
if (ALLOWED_CONDITIONS.has(lower_condition)) {
return lower_condition.includes(result.status.toLowerCase());
async function checkCondition({ condition, result, target, extension }) {
if (typeof condition === 'function') {
return await condition({target, result, extension });
} else {
return eval(condition);
const lower_condition = condition.toLowerCase();
if (ALLOWED_CONDITIONS.has(lower_condition)) {
return lower_condition.includes(result.status.toLowerCase());
} else {
return eval(condition);
}
}
}

Expand Down
11 changes: 9 additions & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ import TestResult from 'test-results-parser/src/models/TestResult';

export type ExtensionName = 'report-portal-analysis' | 'hyperlinks' | 'mentions' | 'report-portal-history' | 'quick-chart-test-summary' | 'custom';
export type Hook = 'start' | 'end';
export type Condition = 'pass' | 'fail' | 'passOrFail';
export type TargetName = 'slack' | 'teams' | 'chat' | 'custom';
export type PublishReportType = 'test-summary' | 'test-summary-slim' | 'failure-details';

export interface ConditionFunctionContext {
target: Target;
extension?: Extension,
result: TestResult;
}
export type ConditionFunction = (ctx: ConditionFunctionContext) => boolean | Promise<boolean>;
export type Condition = 'pass' | 'fail' | 'passOrFail' | ConditionFunction;

/**
* Extensions
*/
Expand Down Expand Up @@ -73,7 +80,7 @@ export interface PercyAnalysisExtension extends Extension {

export interface CustomExtensionFunctionContext {
target: Target;
extension: HyperlinksExtension,
extension: Extension,
result: TestResult;
payload: any;
root_payload: any;
Expand Down
2 changes: 1 addition & 1 deletion src/targets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function getTargetRunner(target) {
async function run(target, result) {
const target_runner = getTargetRunner(target);
const target_options = Object.assign({}, target_runner.default_options, target);
if (checkCondition({ condition: target_options.condition, result })) {
if (await checkCondition({ condition: target_options.condition, result, target })) {
await target_runner.run({result, target});
}
}
Expand Down
54 changes: 53 additions & 1 deletion test/ext.hyperlink.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ describe('extensions - hyperlinks', () => {
assert.equal(ctx.target.name, 'chat');
assert.equal(ctx.extension.name, 'hyperlinks');
assert.equal(ctx.result.total, 4)
return 'some-url'
return 'some-url'
}
},
{
Expand Down Expand Up @@ -489,6 +489,58 @@ describe('extensions - hyperlinks', () => {
assert.equal(mock.getInteraction(id).exercised, true);
});

it('should send test-summary with links to teams - functional condition', async () => {
const id = mock.addInteraction('post test-summary with hyperlinks to teams - pass status');
await publish({
config: {
"reports": [
{
"targets": [
{
"name": "teams",
"inputs": {
"url": "http://localhost:9393/message"
},
"extensions": [
{
"name": "hyperlinks",
"condition": async ({ target, extension, result }) => {
assert.equal(target.name, 'teams');
assert.equal(extension.name, 'hyperlinks');
assert.equal(result.name, 'Default suite');
return true;
},
"inputs": {
"links": [
{
"text": "Pipeline",
"url": "some-url"
},
{
"text": "Video",
"url": "some-url"
}
]
}
}
]
}
],
"results": [
{
"type": "testng",
"files": [
"test/data/testng/single-suite.xml"
]
}
]
}
]
}
});
assert.equal(mock.getInteraction(id).exercised, true);
});

afterEach(() => {
mock.clearInteractions();
});
Expand Down
34 changes: 34 additions & 0 deletions test/targets.teams.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,40 @@ describe('targets - teams', () => {
assert.equal(mock.getInteraction(id).exercised, true);
});

it('should send test-summary with functional condition', async () => {
const id = mock.addInteraction('post test-summary to teams');
await publish({
config: {
"reports": [
{
"targets": [
{
"name": "teams",
"condition": ({target, result}) => {
assert.equal(target.name, 'teams');
assert.equal(result.name, 'Default suite');
return true;
},
"inputs": {
"url": "http://localhost:9393/message"
}
}
],
"results": [
{
"type": "testng",
"files": [
"test/data/testng/single-suite.xml"
]
}
]
}
]
}
});
assert.equal(mock.getInteraction(id).exercised, true);
});

afterEach(() => {
mock.clearInteractions();
});
Expand Down

0 comments on commit 21ea346

Please sign in to comment.