Skip to content

Commit 7429734

Browse files
add(tests): Add unit and e2e suite with CI coverage gate and badge (#164)
* add: unit e2e test suite, ci workflow and coverage badge * add: explanatory comment to write permissions * update: test coverage to match real use cases and known bug count * add: known-bug missing file * add: correct badge
1 parent 5f3d0fc commit 7429734

19 files changed

Lines changed: 1540 additions & 146 deletions

‎.github/workflows/ci.yml‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
branches: [master]
6+
7+
concurrency:
8+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
9+
cancel-in-progress: true
10+
11+
permissions:
12+
contents: read
13+
14+
jobs:
15+
test:
16+
name: tests and coverage
17+
runs-on: cx-public-ubuntu-x64
18+
timeout-minutes: 10
19+
steps:
20+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
21+
with:
22+
persist-credentials: false
23+
- uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
24+
with:
25+
node-version: 22
26+
# Runner cannot reach registry.npmjs.org; install through the echohq registry, as in test_action.yaml
27+
- name: Install dependencies through echohq registry
28+
env:
29+
ECHO_LIBRARIES_ACCESS_KEY: ${{ secrets.ECHO_LIBRARIES_ACCESS_KEY }}
30+
run: |
31+
NPMRC="${RUNNER_TEMP}/npmrc"
32+
umask 077
33+
printf 'registry=https://npm.echohq.com/\n//npm.echohq.com/:_authToken=%s\n' "${ECHO_LIBRARIES_ACCESS_KEY}" > "${NPMRC}"
34+
npm ci --ignore-scripts --userconfig "${NPMRC}"
35+
rm -f "${NPMRC}"
36+
- name: Run tests with coverage
37+
run: npm run test:coverage
38+
39+
shellcheck:
40+
name: shellcheck entrypoint
41+
runs-on: cx-public-ubuntu-x64
42+
timeout-minutes: 5
43+
steps:
44+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
45+
with:
46+
persist-credentials: false
47+
# entrypoint.sh runs under busybox ash in the action image
48+
- name: Lint entrypoint.sh
49+
run: docker run --rm -v "${PWD}:/mnt:ro" koalaman/shellcheck@sha256:2097951f02e735b613f4a34de20c40f937a6c8f18ecb170612c88c34517221fb --shell=busybox --severity=error entrypoint.sh # v0.10.0
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: coverage-badge
2+
3+
on:
4+
push:
5+
branches: [master]
6+
7+
concurrency:
8+
group: ${{ github.workflow }}
9+
cancel-in-progress: false
10+
11+
permissions:
12+
contents: read
13+
14+
jobs:
15+
coverage:
16+
name: generate-coverage
17+
runs-on: cx-public-ubuntu-x64
18+
timeout-minutes: 10
19+
steps:
20+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
21+
with:
22+
persist-credentials: false
23+
- uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
24+
with:
25+
node-version: 22
26+
# Runner cannot reach registry.npmjs.org; install through the echohq registry, as in test_action.yaml
27+
- name: Install dependencies through echohq registry
28+
env:
29+
ECHO_LIBRARIES_ACCESS_KEY: ${{ secrets.ECHO_LIBRARIES_ACCESS_KEY }}
30+
run: |
31+
NPMRC="${RUNNER_TEMP}/npmrc"
32+
umask 077
33+
printf 'registry=https://npm.echohq.com/\n//npm.echohq.com/:_authToken=%s\n' "${ECHO_LIBRARIES_ACCESS_KEY}" > "${NPMRC}"
34+
npm ci --ignore-scripts --userconfig "${NPMRC}"
35+
rm -f "${NPMRC}"
36+
- name: Run tests with coverage
37+
run: npm run test:coverage
38+
- name: Generate badge
39+
run: node scripts/coverage-badge.js coverage/lcov.info coverage/badge.json
40+
- name: Upload coverage badge Artifact
41+
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
42+
with:
43+
name: coverage-badge
44+
path: coverage/badge.json
45+
46+
publish:
47+
name: publish-coverage
48+
needs: coverage
49+
runs-on: cx-public-ubuntu-x64
50+
timeout-minutes: 5
51+
permissions:
52+
contents: write # for git push to the badges branch
53+
steps:
54+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
55+
with:
56+
ref: badges
57+
persist-credentials: true
58+
- name: Download Badge json
59+
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
60+
with:
61+
name: coverage-badge
62+
path: latest-coverage
63+
- name: Push badge to the badges branch
64+
run: |
65+
git config user.name "github-actions[bot]"
66+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
67+
mv latest-coverage/badge.json coverage.json
68+
git add coverage.json
69+
if ! git status | grep "nothing to commit"; then
70+
git commit -m 'chore(tests): updating test coverage badge'
71+
git push origin badges
72+
fi

‎.gitignore‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
2-
.vscode
2+
.vscode
3+
coverage

‎README.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[![License: GPL-3.0](https://img.shields.io/badge/License-GPL3.0-yellow.svg)](https://www.gnu.org/licenses)
44
[![Latest Release](https://img.shields.io/github/v/release/checkmarx/kics-github-action)](https://github.com/checkmarx/kics-github-action/releases)
55
[![Open Issues](https://img.shields.io/github/issues-raw/checkmarx/kics-github-action)](https://github.com/checkmarx/kics-github-action/issues)
6+
[![Coverage](https://img.shields.io/endpoint?url=https%3A%2F%2Fraw.githubusercontent.com%2FCheckmarx%2Fkics-github-action%2Fbadges%2Fcoverage.json)](https://github.com/Checkmarx/kics-github-action/actions/workflows/coverage-badge.yml)
67

78
- [KICS GitHub Action](#kics-github-action)
89
- [Integrate KICS into your GitHub workflows](#integrate-kics-into-your-github-workflows)
@@ -20,6 +21,7 @@
2021
- [Uploading SARIF report](#uploading-sarif-report)
2122
- [Using configuration file](#using-configuration-file)
2223
- [How To Contribute](#how-to-contribute)
24+
- [Running the tests](#running-the-tests)
2325
- [License](#license)
2426

2527
## Integrate KICS into your GitHub workflows
@@ -448,6 +450,25 @@ jobs:
448450

449451
We welcome [issues](https://github.com/checkmarx/kics-github-action/issues) to and [pull requests](https://github.com/checkmarx/kics-github-action/pulls) against this repository!
450452

453+
### Running the tests
454+
455+
Requires Node.js 22 or newer.
456+
457+
```sh
458+
npm ci
459+
npm test # unit and end-to-end tests
460+
npm run test:coverage # same, plus a coverage report and the coverage gate used by CI
461+
```
462+
463+
Tests live under `test/`:
464+
465+
- `test/unit` - the PR comment and job summary logic against a fake GitHub API
466+
- `test/e2e` - the action run as a workflow would run it: `src/main.js` as a process, `entrypoint.sh` against a fake `kics`, and the `action.yml` contract
467+
- `test/helpers` and `test/fixtures` - the fake GitHub API, KICS report builders and process runners
468+
469+
Tests marked with `knownBug` document known bugs: they are reported as `todo` and are expected to fail until the bug is fixed.
470+
They are skipped in the coverage run, so coverage only counts verified behaviour.
471+
451472
# License
452473

453474
KICS Github Action

‎package-lock.json‎

Lines changed: 1 addition & 138 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)