Skip to content

Commit

Permalink
feat(tests): Add GH action for autotesting #16 (#73)
Browse files Browse the repository at this point in the history
Automatic testing of code changes before merging to the main branch did
not exist.

closes #16
  • Loading branch information
imAsparky committed Sep 14, 2021
1 parent c12e2f5 commit a49b767
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 28 deletions.
8 changes: 6 additions & 2 deletions README.rst
Expand Up @@ -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 <https://www.conventionalcommits.org/en/v1.0.0/>`_
specification custom commit message are now built into the
cookiecutter-p3-package project.
#. Choose to use a `Conventional Commits <https://www.conventionalcommits.org/en/v1.0.0/>`_
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.
Expand Down
6 changes: 5 additions & 1 deletion cookiecutter.json
Expand Up @@ -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"
]
}
46 changes: 36 additions & 10 deletions docs/source/prompts.rst
Expand Up @@ -61,21 +61,26 @@ Options
The following package configuration options set up different features for your
project.

**use_pytest**
Whether to use `pytest <https://docs.pytest.org/en/latest/>`_.

**add_pyup_badge**
Whether to include a `pyup <https://github.com/pyupio/pyup>`_ badge.
*default = n*

Whether to include a `pyup <https://github.com/pyupio/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 <https://www.conventionalcommits.org/en/v1.0.0/>`_
specification.
Expand All @@ -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 <https://github.com/imAsparky/cookiecutter-py3-package/issues/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.

Expand All @@ -113,6 +137,8 @@ project.
for more information on generating secrets and repository security.

**open_source_license**
*default = MIT*

Choose a `license <https://choosealicense.com/>`_. Options:

1. MIT License,
Expand Down
3 changes: 3 additions & 0 deletions hooks/post_gen_project.py
Expand Up @@ -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")
31 changes: 31 additions & 0 deletions tests/test_bake_project.py
Expand Up @@ -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.
Expand All @@ -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:
Expand Down
@@ -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
36 changes: 21 additions & 15 deletions {{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

0 comments on commit a49b767

Please sign in to comment.