Skip to content

Commit

Permalink
Add GHA workflow to run tox (#36)
Browse files Browse the repository at this point in the history
Signed-off-by: NilashishC <[email protected]>
  • Loading branch information
NilashishC authored Nov 16, 2023
1 parent b7312ca commit a16c037
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,15 @@ subschema
subspec
substring
superfences
sysargs
templatable
templated
templating
testcol
testhost
testname
testns
testorg
testpaths
tmplt
topbar
Expand Down
3 changes: 3 additions & 0 deletions .config/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pytest
pytest-xdist
tox
67 changes: 67 additions & 0 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
name: tox

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

on:
pull_request:
schedule:
- cron: "0 0 * * *"

jobs:
pre:
name: pre
runs-on: ubuntu-22.04
outputs:
matrix: ${{ steps.generate_matrix.outputs.matrix }}
steps:
- name: Determine matrix
id: generate_matrix
uses: coactions/dynamic-matrix@v1
with:
min_python: "3.9"
max_python: "3.11"
default_python: "3.11"
other_names: lint

tox:
name: ${{ matrix.name }} / python ${{ matrix.python_version }}
runs-on: ubuntu-20.04
needs: pre
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.pre.outputs.matrix) }}

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # needed by setuptools-scm

- name: Set up Python ${{ matrix.python_version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python_version }}

- name: Install tox
run: python3 -m pip install --upgrade "tox>=4.0.2"

- name: Log Python info (${{ matrix.python_version }})
run: |
command -v python
python --version --version
python3 -m pip freeze --all
- name: "tox -e ${{ matrix.passed_name }}"
continue-on-error: ${{ matrix.devel || false }}
run: python3 -m tox -e ${{ matrix.passed_name }}

tox_passed:
needs: tox
runs-on: ubuntu-latest
steps:
- run: >-
python -c "assert set([
'${{ needs.tox.result }}',
]) == {'success'}"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ build/

*.mypy_cache/

*.tox/

__pycache__/
3 changes: 3 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ repos:
- jinja2
- jsonschema
- black
- pytest
- tox

- repo: https://github.com/pre-commit/mirrors-mypy.git
rev: v1.6.1
Expand All @@ -91,6 +93,7 @@ repos:
- black
- types-jsonschema
- types-pyyaml
- pytest
args:
- src
- tests
Expand Down
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
"[jsonc]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"python.analysis.typeCheckingMode": "basic"
"python.analysis.typeCheckingMode": "basic",
"python.testing.pytestArgs": ["tests"],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ convention = "pep257"

[tool.setuptools.dynamic]
dependencies = {file = "requirements.txt"}
optional-dependencies.test = {file = [".config/requirements-test.txt"]}

[tool.setuptools_scm]
local_scheme = "no-local-version"
Expand Down
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
testpaths = tests
1 change: 1 addition & 0 deletions tests/units/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Unit tests for ansible-creator."""
6 changes: 6 additions & 0 deletions tests/units/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""conftest."""
import os


os.environ["HOME"] = "/home/ansible"
os.environ["DEV_WORKSPACE"] = "collections/ansible_collections"
89 changes: 89 additions & 0 deletions tests/units/test_basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"""Basic unit tests for ansible-creator."""
from pathlib import Path

import pytest

from ansible_creator.cli import Cli
from ansible_creator.config import Config
from ansible_creator.utils import expand_path


def test_expand_path() -> None:
"""Test expand_path utils."""
assert (
expand_path("~/$DEV_WORKSPACE/namespace/collection")
== "/home/ansible/collections/ansible_collections/namespace/collection"
)


@pytest.mark.parametrize(
argnames=["sysargs", "expected"],
argvalues=[
[
["ansible-creator", "init", "testorg.testcol"],
{
"subcommand": "init",
"no_ansi": False,
"log_file": str(Path.cwd() / "ansible-creator.log"),
"log_level": "notset",
"log_append": "true",
"json": False,
"verbose": 0,
"collection": "testorg.testcol",
"init_path": "./",
"force": False,
},
],
[
[
"ansible-creator",
"init",
"testorg.testcol",
"--init-path=/home/ansible",
"-vvv",
"--json",
"--no-ansi",
"--la=false",
"--lf=test.log",
"--ll=debug",
"--force",
],
{
"subcommand": "init",
"no_ansi": True,
"log_file": "test.log",
"log_level": "debug",
"log_append": "false",
"json": True,
"verbose": 3,
"collection": "testorg.testcol",
"init_path": "/home/ansible",
"force": True,
},
],
],
)
def test_cli_parser(monkeypatch, sysargs, expected) -> None: # noqa: ANN001
"""Test CLI args parsing."""
monkeypatch.setattr("sys.argv", sysargs)
assert vars(Cli().parse_args()) == expected


def test_configuration_class() -> None:
"""Test Config() dataclass post_init."""
cli_args: dict = {
"creator_version": "0.0.1",
"json": True,
"log_append": True,
"log_file": "./ansible-creator.log",
"log_level": "debug",
"no_ansi": False,
"subcommand": "init",
"verbose": 2,
"collection": "testorg.testcol",
"init_path": "$HOME",
}
app_config = Config(**cli_args)
assert app_config.namespace == "testorg"
assert app_config.collection_name == "testcol"
assert app_config.init_path == "/home/ansible"
26 changes: 26 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[tox]
requires =
tox>=4.2
env_list =
py
lint
no_package = true
skip_missing_interpreters = true
work_dir = {env:TOX_WORK_DIR:.tox}

[testenv:py]
description = Run pytest under {basepython} ({envpython})
deps =
-e .[test]
commands =
python --version
pip list
pytest {posargs:tests}

[testenv:lint]
description = Enforce quality standards under {basepython} ({envpython})
skip_install = true
deps =
pre-commit
commands =
pre-commit run --show-diff-on-failure --all-files

0 comments on commit a16c037

Please sign in to comment.