Skip to content

Commit

Permalink
Merge pull request #11 from generalui/test-python
Browse files Browse the repository at this point in the history
[test-python] Allow a coverage prefix and min coverage threshold
  • Loading branch information
jonryser authored Nov 1, 2024
2 parents 3f1459a + 0166f97 commit 666b133
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
33 changes: 24 additions & 9 deletions .github/actions/test-python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ The action accepts the following input:
- `checkout-code`:
- __Description__: Whether or not to checkout the code.
- __Default__: 'yes'
- __Required__: No

- `coverage-prefix`:
- __Description__: A prefix to use for the coverage artifact to help avoid collisions with other coverage artifacts in the same job.
- __Default__: ''
- __Required__: No

- `global-index-url`:
- __Description__: The base URL of the Python Package Index (default <https://pypi.org/simple>).
Expand All @@ -41,18 +47,23 @@ The action accepts the following input:

- `python-version`:
- __Description__: The version of Python to use. Defaults to a specific version if not specified.
- __Required__: No
- __Default__: '3.11.7'
- __Required__: No

- `min-coverage`:
- __Description__: The minimum coverage to require for testing to pass.
- __Default__: 0
- __Required__: No

- `retention-days`:
- __Description__: The number of days to keep artifacts.
- __Required__: No
- __Default__: 31
- __Required__: No

- `run-before-tests`:
- __Description__: A shell command to run before tests.
- __Required__: No
- __Default__: ''
- __Required__: No

- `search-index`:
- __Description__: The search index to use for PIP.
Expand All @@ -62,17 +73,17 @@ The action accepts the following input:

- `should-run-tests`:
- __Description__: Whether or not to run tests. Set this to anything other than "yes" to skip tests.
- __Required__: No
- __Default__: 'yes'
- __Required__: No

- `tox-version`:
- __Description__: The version of Tox to use for testing. If not specified, `tox` will not be used and `pytest` will be called directly.
- __Required__: No

- `upload-coverage`:
- __Description__: Whether or not to upload coverage as an artifact. Set this to anything other than "yes" to skip uploading.
- __Required__: No
- __Default__: 'yes'
- __Required__: No

## Usage

Expand All @@ -83,11 +94,13 @@ To use the "Test Python" action in your workflow, include it as a step:
uses: generalui/github-workflow-accelerators/.github/actions/test-python@1.0.0-test-python
with:
branch: ${{ github.ref_name }}
global-index-url: 'http://sakura.ohgod.ai:3141/eo/stable/+simple/'
global-trusted-host: 'sakura.ohgod.ai'
coverage-prefix: ${{ github.run_id }}
global-index-url: 'http://mypydevhost.com:3210/stable/+simple/'
global-trusted-host: 'mypydevhost.com'
min-coverage: 80
python-version: '3.11.7'
run-before-tests: 'echo "Hello World"'
search-index: 'http://sakura.ohgod.ai:3141/eo/stable'
search-index: 'http://mypydevhost.com:3210/stable'
should-run-tests: 'yes'
tox-version: '4.12.1'
upload-coverage: 'yes'
Expand All @@ -108,6 +121,7 @@ To use the "Test Python" action in your workflow, include it as a step:
1) __Install Dependencies__:
- If tests are to be run this installs the specified version of `tox`.
If the `tox` version is NOT provided, it installs the test and app dependencies directly.
If the `requirements-dev.txt` file does not exist, it will use the `requirements-test.txt` file instead.

1) __Run Before Tests__:
- Runs the specified shell command before tests.
Expand All @@ -116,7 +130,7 @@ To use the "Test Python" action in your workflow, include it as a step:
1) __Test__:
- If tests are to be run this runs tests using `tox` (if the `tox` version is provided).
Otherwise it runs tests using `pytest`.
Once tests are complete, it uploads the coverage report.
If the `min-coverage` input is greater than 0, it will fail the tests if the coverage is less than the specified minimum.

1) __Get the coverage file__:
- Copies the coverage information to a folder named after the branch, if tests are to be run and coverage is to be uploaded.
Expand All @@ -131,6 +145,7 @@ To use the "Test Python" action in your workflow, include it as a step:

- Ensure that the Python version specified is compatible with your project.
- If using `tox`, ensure the version specified is compatible with your project.
- The action is opinionated and expects the test dependencies to be in a `requirements-dev.txt` or `requirement-test.txt` file.
- The action is customizable to skip tests or coverage uploading by adjusting the `should-run-tests` and `upload-coverage` inputs respectively.
- Coverage reports are retained as artifacts for historical comparison and analysis.

Expand Down
15 changes: 12 additions & 3 deletions .github/actions/test-python/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ inputs:
checkout-code:
default: 'yes'
description: Whether or not to checkout the code.
coverage-prefix:
default: ''
description: A prefix to use for the coverage artifact to help avoid collisions with other coverage artifacts in the same job.
global-index-url:
default: ''
description: The base URL of the Python Package Index (default https://pypi.org/simple). This should point to a repository compliant with PEP 503 (the simple repository API) or a local directory laid out in the same format. If none is passed, the index URL will not be updated.
global-trusted-host:
default: ''
description: The host of the global trusted host to use for PIP. This will mark this host or host:port pair as trusted, even though it does not have valid or any HTTPS. If none is passed, the trusted host will not be updated.
min-coverage:
default: 0
description: The minimum coverage to require for testing to pass.
python-version:
default: 3.11.7
description: The version of Python to use (defaults to 3.11.7)
Expand Down Expand Up @@ -101,22 +107,25 @@ runs:
# Run test coverage using as many cores as are available.
tox run -e coverage-py${major_minor}
else
pytest --cov --cov-report html -n auto
pytest --cov --cov-report html -n auto ${{ inputs.min-coverage > 0 && "--cov-fail-under=${{ inputs.min-coverage }}" }}
fi
shell: bash

- name: Get the coverage file.
if: inputs.should-run-tests == 'yes' && inputs.upload-coverage == 'yes'
run: |
coverage_branch=${${{ inputs.branch }}//+([\"\:<>|\*\?\\\/])/-}
coverage_dir=coverage-${coverage_branch}
coverage_prefix=${{ inputs.coverage-prefix != '' && "${{ inputs.coverage-prefix }}-" }}
coverage_dir=${coverage_prefix}coverage-${coverage_branch}
mkdir -p ${coverage_dir} && sudo cp -r coverage ${coverage_dir}
echo "coverage_branch=${coverage_branch}" >> $GITHUB_OUTPUT
echo "coverage_prefix=${coverage_prefix}" >> $GITHUB_OUTPUT
echo "coverage_dir=${coverage_dir}" >> $GITHUB_OUTPUT
echo "::group::Coverage Data"
echo 'branch: '${coverage_branch}
echo 'prefix: '${coverage_prefix}
echo 'dir: '${coverage_dir}
echo "::endgroup::"
shell: bash
Expand All @@ -126,7 +135,7 @@ runs:
if: inputs.should-run-tests == 'yes' && inputs.upload-coverage == 'yes'
uses: actions/upload-artifact@v4
with:
name: ${{ steps.coverage.outputs.coverage_branch }}-test-coverage
name: ${{ steps.coverage.outputs.coverage_prefix }}${{ steps.coverage.outputs.coverage_branch }}-test-coverage
path: ${{ steps.coverage.outputs.coverage_dir }}
retention-days: ${{ inputs.retention-days }}

Expand Down
2 changes: 1 addition & 1 deletion .github/actions/test-python/project.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "test-python",
"version": "1.0.0"
"version": "1.0.1"
}
2 changes: 2 additions & 0 deletions .vscode/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"datetime",
"dockerized",
"dnsfetch",
"endgroup",
"generalui",
"genui",
"guid",
Expand All @@ -17,6 +18,7 @@
"metacharacter",
"mkdir",
"queryable",
"pytest",
"referer",
"reactjs",
"repo",
Expand Down

0 comments on commit 666b133

Please sign in to comment.