From a49b767f9baba25f091f5e948458dd995fe83bef Mon Sep 17 00:00:00 2001 From: mark sevelj <31756570+imAsparky@users.noreply.github.com> Date: Tue, 14 Sep 2021 08:55:13 +0800 Subject: [PATCH] feat(tests): Add GH action for autotesting #16 (#73) Automatic testing of code changes before merging to the main branch did not exist. closes #16 --- README.rst | 8 +++- cookiecutter.json | 6 ++- docs/source/prompts.rst | 46 +++++++++++++++---- hooks/post_gen_project.py | 3 ++ tests/test_bake_project.py | 31 +++++++++++++ .../.github/workflows/test_contribution.yaml | 36 +++++++++++++++ {{cookiecutter.project_slug}}/tox.ini | 36 +++++++++------ 7 files changed, 138 insertions(+), 28 deletions(-) create mode 100644 {{cookiecutter.project_slug}}/.github/workflows/test_contribution.yaml diff --git a/README.rst b/README.rst index c879f16..a153ad9 100644 --- a/README.rst +++ b/README.rst @@ -52,17 +52,21 @@ Cookiecutter_ template for a Python package. #. Links from the original fork have been updated to point to this repository. #. Support for Travis has been removed. -#. Stale Similar Cookiecutter Templates have been removed. +#. Stale "Similar Cookiecutter Templates" have been removed. #. `Conventional Commits `_ specification custom commit message are now built into the cookiecutter-p3-package project. #. Choose to use a `Conventional Commits `_ specification custom commits message in your built package. +#. Added an optional GitHub action to generate a package CHANGELOG automatically. +#. Added an optional GitHub action to run your tox package test suite when a + pull request to the main branch starts. +#. Tox configuration for your package now includes an OS and Python test matrix. + OS includes Linux, macOS and Windows. Python 3.6 - 3.9. Features -------- -* Testing setup with ``unittest`` and ``python setup.py test`` or ``pytest``. * Tox_ testing: Setup to easily test for Python 3.6, 3.7, 3.8 and 3.9. * Sphinx_ docs: Documentation ready for generation with, for example, `Read the Docs`_. * bump2version_: Pre-configured version bumping with a single command. diff --git a/cookiecutter.json b/cookiecutter.json index efc909c..3f10ada 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -14,7 +14,11 @@ "command_line_interface": ["Click", "Argparse", "No command-line interface"], "create_author_file": "y", "create_conventional_commits_edit_message": "y", + "create_repo_auto_test_workflow": "y", "create_auto_CHANGELOG": "y", "github_access_token": ["secrets.GITHUB_TOKEN", "secrets.CHANGELOG_UPDATE"], - "open_source_license": ["MIT license", "BSD license", "ISC license", "Apache Software License 2.0", "GNU General Public License v3", "Not open source"] + "open_source_license": ["MIT license", "BSD license", "ISC license", "Apache Software License 2.0", "GNU General Public License v3", "Not open source"], + "_copy_without_render": [ + ".github/workflows/test_contribution.yaml" + ] } diff --git a/docs/source/prompts.rst b/docs/source/prompts.rst index f4adcfe..838a941 100644 --- a/docs/source/prompts.rst +++ b/docs/source/prompts.rst @@ -61,21 +61,26 @@ Options The following package configuration options set up different features for your project. -**use_pytest** - Whether to use `pytest `_. - **add_pyup_badge** - Whether to include a `pyup `_ badge. + *default = n* + + Whether to include a `pyup `_ badge. **command_line_interface** - Whether to create a console script using Click. Console script entry point - will match the project_slug. - Options: ['Click', 'Argparse', 'No command-line interface'] + *default = Click* + + Whether to create a console script using Click. Console script entry point + will match the project_slug. + Options: ['Click', 'Argparse', 'No command-line interface'] **create_author_file** - Whether to create an authors file. + *default = y* + + Whether to create an authors file. **create_conventional_commits_edit_message** + *default = y* + Whether to use a commit message that helps you adhere to the `Conventional Commits `_ specification. @@ -93,11 +98,30 @@ project. git config --local commit.template .github/.git-commit-template.txt +**create_repo_auto_test_workflow** + *default = y* + + A GitHub action workflow will run your test suite using tox and pytest + when creating a pull request to the main branch. + +.. todo:: + + Create a tutorial to demonstrate GitHub protected branches configuration + to make the best use of the create_repo_auto_test_workflow feature. + + See `Issue 72 `_. + + + **create_auto_CHANGELOG** - create_auto_CHANGELOG will use GitHub actions to generate a changelog using - a cron job, scheduled daily. + *default = y* + + create_auto_CHANGELOG will use GitHub actions to generate a changelog using + a cron job, scheduled daily. **github_access_token** + *default = secrets.GITHUB_TOKEN* + For new or small repositories, select `secrets.GITHUB_TOKEN`. This is adequate for most small packages. @@ -113,6 +137,8 @@ project. for more information on generating secrets and repository security. **open_source_license** + *default = MIT* + Choose a `license `_. Options: 1. MIT License, diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index c5e7ddb..1a578b8 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -32,3 +32,6 @@ def remove_file(filepath): if "{{ cookiecutter.create_auto_CHANGELOG }}" == "y": remove_file("HISTORY.rst") + + if "{{ cookiecutter.create_repo_auto_test_workflow }}" != "y": + remove_file(".github/workflows/test_contribution.yaml") diff --git a/tests/test_bake_project.py b/tests/test_bake_project.py index d251055..3e18fe7 100644 --- a/tests/test_bake_project.py +++ b/tests/test_bake_project.py @@ -377,6 +377,34 @@ def test_bake_without_conventional_commits_message(cookies): assert ".git-commit-template.txt" not in git_without_files +def test_bake_with_repo_automatic_testing_suite(cookies): + """ + Test cookiecutter created the package with repo automatic testing. + """ + with bake_in_temp_dir( + cookies, extra_context={"create_repo_auto_test_workflow": "y"} + ) as result: + + test_workflow_with_files = [ + f.basename for f in result.project.join(".github/workflows").listdir() + ] + assert "test_contribution.yaml" in test_workflow_with_files + + +def test_bake_without_repo_automatic_testing_suite(cookies): + """ + Test cookiecutter created the package without repo automatic testing. + """ + with bake_in_temp_dir( + cookies, extra_context={"create_repo_auto_test_workflow": "n"} + ) as result: + + test_workflow_without_files = [ + f.basename for f in result.project.join(".github/workflows").listdir() + ] + assert "test_contribution.yaml" not in test_workflow_without_files + + def test_bake_with_automatic_CHANGELOG(cookies): """ Test cookiecutter created the package with a auto changelog generation. @@ -396,6 +424,9 @@ def test_bake_with_automatic_CHANGELOG(cookies): def test_bake_without_automatic_CHANGELOG(cookies): + """ + Test cookiecutter created the package without auto changelog generation. + """ with bake_in_temp_dir( cookies, extra_context={"create_auto_CHANGELOG": "n"} ) as result: diff --git a/{{cookiecutter.project_slug}}/.github/workflows/test_contribution.yaml b/{{cookiecutter.project_slug}}/.github/workflows/test_contribution.yaml new file mode 100644 index 0000000..34c9e86 --- /dev/null +++ b/{{cookiecutter.project_slug}}/.github/workflows/test_contribution.yaml @@ -0,0 +1,36 @@ +name: Test Contributions + +on: + pull_request: + branches: [main] + + # push: + # branches: [main] + + workflow_dispatch: + +jobs: + test_contribs: + strategy: + matrix: + python-version: ["3.6", "3.7", "3.8", "3.9"] + os: [macos-latest, ubuntu-latest, windows-latest] + + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install tox tox-gh-actions + + - name: Test with tox + run: tox diff --git a/{{cookiecutter.project_slug}}/tox.ini b/{{cookiecutter.project_slug}}/tox.ini index bad564f..9418392 100644 --- a/{{cookiecutter.project_slug}}/tox.ini +++ b/{{cookiecutter.project_slug}}/tox.ini @@ -1,29 +1,35 @@ [tox] -envlist = py36, py37, py38, flake8 +envlist = py36, py37,py38, py39, py38 pypy, docs +skipsdist = true +skip_missing_interpreters = true -[travis] +[testenv:docs] +basepython=python +changedir=docs/source +deps= -r{toxinidir}/docs/requirements.txt +commands= + sphinx-build -b html -d {envtmpdir}/doctrees . {envtmpdir}/html + +[gh-actions] python = + pypy-3.8: pypy3 + 3.9: py39 3.8: py38 3.7: py37 3.6: py36 -[testenv:flake8] -basepython = python -deps = flake8 -commands = flake8 {{ cookiecutter.project_slug }} tests +[gh-actions:env] +PLATFORM = + ubuntu-latest: linux + macos-latest: macos + windows-latest: windows + [testenv] setenv = PYTHONPATH = {toxinidir} -{% if cookiecutter.use_pytest == 'y' -%} deps = -r{toxinidir}/requirements_dev.txt -; If you want to make tox run the tests with the same versions, create a -; requirements.txt with the pinned versions and uncomment the following line: -; -r{toxinidir}/requirements.txt commands = - pip install -U pip - pytest --basetemp={envtmpdir} -{% else %} -commands = python setup.py test -{%- endif %} + python -m pip install --upgrade pip + pytest