Skip to content

Commit

Permalink
Merge pull request #38 from microbiomedata/37-configure-gha-to-run-bl…
Browse files Browse the repository at this point in the history
…ack-and-pytest-in-more-scenarios

Use individual GHA workflows to run tests (`pytest`) and check code format (`black`)
  • Loading branch information
eecavanna authored Jan 27, 2025
2 parents 05574ea + c3bef76 commit 2d2953c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
17 changes: 14 additions & 3 deletions .github/workflows/build-and-publish-package-to-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@ on:
types: [ published ]

# Note: Since the building and publishing are done in separate jobs,
# we use GitHub's artifact persistent system to give the later
# we use GitHub's artifact persistence system to give the later
# job access to files generated by the earlier job.
jobs:
# Use existing workflows.
# Reference: https://docs.github.com/en/actions/using-workflows/reusing-workflows#calling-a-reusable-workflow
check-source-code-format:
name: Check source code format
uses: ./.github/workflows/check-source-code-format.yml
run-tests:
name: Run tests
uses: ./.github/workflows/run-tests.yml
build:
name: Build package
# This job depends upon other jobs succeeding.
# Reference: https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions#jobsjob_idneeds
needs:
- check-source-code-format
- run-tests
runs-on: ubuntu-latest
steps:
- name: Check out commit # Docs: https://github.com/actions/checkout
Expand All @@ -25,8 +38,6 @@ jobs:
uses: snok/install-poetry@v1
- name: Install dependencies # Docs: https://python-poetry.org/docs/cli/#install
run: poetry install --no-interaction
- name: Run tests # TODO: Consider extracting this and preceding steps into a separate job and/or workflow
run: poetry run pytest -v
- name: Update package version # Docs: https://python-poetry.org/docs/cli/#version
run: poetry version ${{ github.ref_name }}
- name: Build package # Docs: https://python-poetry.org/docs/cli/#build
Expand Down
28 changes: 28 additions & 0 deletions .github/workflows/check-source-code-format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# GitHub Actions workflow that checks the format of the source code.
# Reference: https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions
name: Check source code format

on:
pull_request: { branches: [ main ] }
push: { branches: [ main ] }
workflow_dispatch: { }

jobs:
check-source-code-format:
name: Check source code format
runs-on: ubuntu-latest
steps:
- name: Check out commit # Docs: https://github.com/actions/checkout
uses: actions/checkout@v4
- name: Set up Python # Docs: https://github.com/actions/setup-python
uses: actions/setup-python@v5
with:
# Specify a Python version that satisfies the `tool.poetry.dependencies.python`
# version requirement specified in `pyproject.toml`.
python-version: '3.9'
- name: Install Poetry # Docs: https://github.com/snok/install-poetry
uses: snok/install-poetry@v1
- name: Install dependencies # Docs: https://python-poetry.org/docs/cli/#install
run: poetry install --no-interaction
- name: Check source code format
run: poetry run black --check .
29 changes: 29 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# GitHub Actions workflow that runs tests.
# Reference: https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions
name: Run tests

on:
pull_request: { branches: [ main ] }
push: { branches: [ main ] }
workflow_dispatch: { }

jobs:
test:
name: Run tests
runs-on: ubuntu-latest
steps:
- name: Check out commit # Docs: https://github.com/actions/checkout
uses: actions/checkout@v4
- name: Set up Python # Docs: https://github.com/actions/setup-python
uses: actions/setup-python@v5
with:
# Specify a Python version that satisfies the `tool.poetry.dependencies.python`
# version requirement specified in `pyproject.toml`.
python-version: '3.9'
- name: Install Poetry # Docs: https://github.com/snok/install-poetry
uses: snok/install-poetry@v1
- name: Install dependencies # Docs: https://python-poetry.org/docs/cli/#install
run: poetry install --no-interaction
- name: Run tests
run: |
poetry run pytest -vv --color=yes
13 changes: 12 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,18 @@ def test_graph_command_with_custom_output_path():

def test_graph_command_help():
"""Test the `graph` command with the --help flag."""
runner = CliRunner()

# Note: For this test, we define an environment variable (in the context of the `CliRunner`) named `TERM` having
# a value `"unknown"`. When Rich "sees" that environment variable, Rich will refrain from coloring its output.
# The reason we influence Rich in this way here is that, when we don't, this test fails when run via GitHub
# Actions. I think it has to do with the fact that GitHub Actions Runners do not allocate a TTY.
#
# References:
# - https://click.palletsprojects.com/en/stable/api/#click.testing.CliRunner (re: the `env` kwarg)
# - https://rich.readthedocs.io/en/stable/console.html#environment-variables (re: the `TERM` environment variable)
# - https://github.com/actions/runner/issues/241 (re: GHA Runners not allocating a TTY)
#
runner = CliRunner(env=dict(TERM="unknown"))
result = runner.invoke(app, ["graph", "--help"])

assert result.exit_code == 0, f"Unexpected exit code: {result.exit_code}"
Expand Down

0 comments on commit 2d2953c

Please sign in to comment.