Skip to content

Commit

Permalink
Add option to not fail if no tests were found (#2)
Browse files Browse the repository at this point in the history
Adds an option to not fail the check if there are no test results. This can be if the file was not found or if there are actually no tests.
  • Loading branch information
tahirmt authored Mar 12, 2021
1 parent ac00dbe commit d740c3b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ If you are looking for surefire reporting check out the amazing plugin by [ScaCa

### Inputs

| **Input** | **Description** |
|----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `github_token` | **Required**. Usually in form of `github_token: ${{ secrets.GITHUB_TOKEN }}`. |
| `report_paths` | **Required**. [Glob](https://github.com/actions/toolkit/tree/master/packages/glob) expression to junit report paths. The default is `**/junit-reports/TEST-*.xml`. |
| `check_name` | Optional. Check name to use when creating a check run. The default is `Test Report`. |
| `commit` | Optional. The commit SHA to update the status. This is useful when you run it with `workflow_run`. |
| **Input** | **Description** |
|-----------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `github_token` | **Required**. Usually in form of `github_token: ${{ secrets.GITHUB_TOKEN }}`. |
| `report_paths` | **Required**. [Glob](https://github.com/actions/toolkit/tree/master/packages/glob) expression to junit report paths. The default is `**/junit-reports/TEST-*.xml`. |
| `check_name` | Optional. Check name to use when creating a check run. The default is `Test Report`. |
| `commit` | Optional. The commit SHA to update the status. This is useful when you run it with `workflow_run`. |
| `fails_if_no_test_results` | Optional. Default `true`. When this is false and there are no tests, the check will pass. |

### Example usage

Expand Down
3 changes: 2 additions & 1 deletion action.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const IDENTIFIER = "23edae2f-afea-4264-b56f-bc94eb0d4ea1"

const action = async () => {
const reportPaths = core.getInput('report_paths');
const failsIfNoTestResults = core.getInput('fails_if_no_test_results') == 'true';
core.info(`Going to parse results form ${reportPaths}`);
const githubToken = core.getInput('github_token');
const name = core.getInput('check_name');
Expand All @@ -21,7 +22,7 @@ const action = async () => {

const pullRequest = github.context.payload.pull_request;
const link = pullRequest && pullRequest.html_url || github.context.ref;
const conclusion = foundResults && annotations.length === 0 ? 'success' : 'failure';
const conclusion = (foundResults || !failsIfNoTestResults) && annotations.length === 0 ? 'success' : 'failure';
const status = 'completed';
const head_sha = commit || pullRequest && pullRequest.head.sha || github.context.sha;
core.info(
Expand Down
14 changes: 13 additions & 1 deletion action.test.fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,16 @@ const nothingFound = {
}
};

module.exports = { finishedWithFailures, finishedSuccess, nothingFound, masterSuccess };
const nothingFoundSuccess = {
name: 'Test Report',
head_sha: 'sha123',
status: 'completed',
conclusion: 'success',
output: {
title: 'No test results found!',
summary: '',
annotations: []
}
};

module.exports = { finishedWithFailures, finishedSuccess, nothingFound, nothingFoundSuccess, masterSuccess };
20 changes: 19 additions & 1 deletion action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
finishedWithFailures,
finishedSuccess,
nothingFound,
nothingFoundSuccess,
masterSuccess
} = require('./action.test.fixtures');

Expand Down Expand Up @@ -43,7 +44,8 @@ describe('action should work', () => {
inputs = {
report_paths: '**/surefire-reports/TEST-*.xml',
github_token: 'GITHUB_TOKEN',
check_name: 'Test Report'
check_name: 'Test Report',
fails_if_no_test_results: 'true'
};
});

Expand Down Expand Up @@ -95,6 +97,22 @@ describe('action should work', () => {
expect(request).toStrictEqual(nothingFound);
});

it('should send success if no test results were found but fails_if_no_test_results is false', async () => {
inputs.report_paths = '**/xxx/*.xml';
inputs.fails_if_no_test_results = 'false';
let request = null;
const scope = nock('https://api.github.com')
.post('/repos/mikepenz/action-junit-report/check-runs', body => {
request = body;
return body;
})
.reply(200, {});
await action();
scope.done();

expect(request).toStrictEqual(nothingFoundSuccess);
});

it('should send reports to sha if no pr detected', async () => {
inputs.report_paths = '**/surefire-reports/TEST-*AllOkTest.xml';
github.context.payload.pull_request = undefined;
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ inputs:
description: 'junit xml report paths in glob format'
required: false
default: '**/junit-reports/TEST-*.xml'
fails_if_no_test_results:
description: 'should the action fail if no tests results are found'
required: false
default: 'true'
check_name:
description: 'check name for test reports'
required: false
Expand Down
3 changes: 2 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8576,6 +8576,7 @@ const IDENTIFIER = "23edae2f-afea-4264-b56f-bc94eb0d4ea1"

const action = async () => {
const reportPaths = core.getInput('report_paths');
const failsIfNoTestResults = core.getInput('fails_if_no_test_results') == 'true';
core.info(`Going to parse results form ${reportPaths}`);
const githubToken = core.getInput('github_token');
const name = core.getInput('check_name');
Expand All @@ -8590,7 +8591,7 @@ const action = async () => {

const pullRequest = github.context.payload.pull_request;
const link = pullRequest && pullRequest.html_url || github.context.ref;
const conclusion = foundResults && annotations.length === 0 ? 'success' : 'failure';
const conclusion = (foundResults || !failsIfNoTestResults) && annotations.length === 0 ? 'success' : 'failure';
const status = 'completed';
const head_sha = commit || pullRequest && pullRequest.head.sha || github.context.sha;
core.info(
Expand Down

0 comments on commit d740c3b

Please sign in to comment.