Skip to content

Commit

Permalink
added tox to the cookiecutter repo (#22)
Browse files Browse the repository at this point in the history
* added tox to the cookiecutter repo
  • Loading branch information
Florian Maas authored Apr 13, 2022
1 parent 27bed36 commit 314b810
Show file tree
Hide file tree
Showing 20 changed files with 141 additions and 51 deletions.
24 changes: 23 additions & 1 deletion .github/workflows/on-merge-to-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,26 @@ jobs:
uses: ./.github/workflows/setup-poetry-env

- name: Run checks
uses: ./.github/workflows/run-checks
uses: ./.github/workflows/run-checks

tox:
runs-on: ubuntu-latest
needs: Quality
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10']
steps:

- name: Check out
uses: actions/checkout@v2

- name: Set up the environment
uses: ./.github/workflows/setup-poetry-env
with:
python-version: ${{ matrix.python-version }}

- name: Test with tox
run: |
source .venv/bin/activate
poetry add tox-gh-actions
tox
2 changes: 1 addition & 1 deletion .github/workflows/on-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
needs: Quality
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9']
python-version: ['3.7', '3.8', '3.9', '3.10']
steps:

- name: Check out
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/on-release-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
needs: Quality
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9']
python-version: ['3.7', '3.8', '3.9', '3.10']
steps:
- name: Check out
uses: actions/checkout@v2
Expand Down
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ Features
+----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------+
| **Formatting** | Adds code formatting with `isort <https://github.com/PyCQA/isort>`_ and `black <https://pypi.org/project/black/>`_. |
+----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------+
| **Pyenv** | Automatically adds a ``.python-version`` file for `pyenv <https://github.com/pyenv/pyenv>`_ support. |
+----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------+
| **CI/CD with Github actions** | Adds Github actions that run the formatting checks and unittests for pull requests and when merged to `main`. |
+----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------+
| **Release to Pypi** | Release to `Pypi <https://pypi.org>`_ by creating a new release on Github. |
Expand All @@ -72,6 +70,8 @@ Features
+----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------+
| **Documentation with Sphinx** | Automatically add documentation to your project and its code with `Sphinx <https://www.sphinx-doc.org/>`_. |
+----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------+
| **Tox testing** | Setup and CI/CD integration to easily test for different Python versions with `Tox <https://tox.wiki/>`_. |
+----------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------+

Quickstart
------------
Expand Down
3 changes: 1 addition & 2 deletions cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
"project_name": "example-project",
"project_slug": "{{cookiecutter.project_name|lower|replace('-', '_')}}",
"project_description": "This is a template repository for Python projects that use Poetry for their dependency management.",
"python_version": "3.9.7",
"include_pyenv_local_file": ["y","n"],
"include_github_actions": ["y","n"],
"publish_to": ["pypi", "artifactory", "none"],
"sphinx_docs": ["y", "n"],
"tox": ["y","n"],
"open_source_license": ["MIT license", "BSD license", "ISC license", "Apache Software License 2.0", "GNU General Public License v3", "Not open source"]
}
2 changes: 1 addition & 1 deletion docs/feature_details.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Feature Details
features/makefile
features/pytest
features/formatting
features/pyenv
features/cicd
features/releasing
features/sphinx
features/tox
21 changes: 0 additions & 21 deletions docs/features/pyenv.rst

This file was deleted.

13 changes: 13 additions & 0 deletions docs/features/tox.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
==================
Tox
==================

The project uses `Tox <https://tox.wiki/en/latest/>`_ to test compatibility with multiple Python versions. By default, the project is tested
with Python ``3.7``, ``3.8``, and ``3.9``. Testing is done automatically in the CI/CD pipeline on every pull request, merge to main, and on each release.

If you want to add more Python versions you can simply add them to ``tox.ini`` and to the separate workflows in ``.github``.





6 changes: 0 additions & 6 deletions docs/prompt_arguments.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ a prompt will start which enables you to configure your repository. The prompt v
**project_description**
A short description of your project.

**python_version**
The python version of your project.

**include_pyenv_local_file**
``"y"`` or ``"n"``. Adds a ``.python-version`` file to the root directory for pyenv.

**include_github_actions**
``"y"`` or ``"n"``. Adds a ``.github`` directory with various actions and workflows to setup the environment and run code formatting checks and unittests.

Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Select a version and install it with
pyenv install -v 3.9.7
Replacing ``3.9.7`` with a version of your choosing. For the rest of this tutorial, replace ``3.9.7`` with the version you have chosen here.
Replacing ``3.9.7`` with a version of your choosing.

It is also recommended to run

Expand Down
6 changes: 3 additions & 3 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def remove_dir(filepath):

if __name__ == "__main__":

if "{{cookiecutter.include_pyenv_local_file}}" != "y":
remove_file(".python-version")

if "{{cookiecutter.include_github_actions}}" != "y":
remove_dir(".github")

if "{{cookiecutter.tox}}" != "y":
remove_file("tox.ini")
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[tox]
skipsdist = true
envlist = py37, py38, py39
envlist = py37, py38, py39, py310

[gh-actions]
python =
3.7: py37
3.8: py38
3.9: py39
3.10: py310

[testenv]
passenv = PYTHON_VERSION
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,25 @@ jobs:

- name: Run checks
uses: ./.github/workflows/run-checks

{% if cookiecutter.tox== "y" %}
tox:
runs-on: ubuntu-latest
needs: Quality
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10']
steps:
- name: Check out
uses: actions/checkout@v2

- name: Set up the environment
uses: ./.github/workflows/setup-poetry-env
with:
python-version: {% raw %}${{ matrix.python-version }}{% endraw %}

- name: Test with tox
run: |
source .venv/bin/activate
poetry add tox-gh-actions
tox
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,25 @@ jobs:

- name: Run checks
uses: ./.github/workflows/run-checks
{% if cookiecutter.tox== "y" %}
tox:
runs-on: ubuntu-latest
needs: Quality
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10']
steps:
- name: Check out
uses: actions/checkout@v2

- name: Set up the environment
uses: ./.github/workflows/setup-poetry-env
with:
python-version: {% raw %}${{ matrix.python-version }}{% endraw %}

- name: Test with tox
run: |
source .venv/bin/activate
poetry add tox-gh-actions
tox
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,30 @@ jobs:

- name: Run checks
uses: ./.github/workflows/run-checks
{% if cookiecutter.tox== "y" %}
tox:
runs-on: ubuntu-latest
needs: Quality
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10']
steps:
- name: Check out
uses: actions/checkout@v2

- name: Set up the environment
uses: ./.github/workflows/setup-poetry-env
with:
python-version: {% raw %}${{ matrix.python-version }}{% endraw %}

- name: Test with tox
run: |
source .venv/bin/activate
poetry add tox-gh-actions
tox
{% endif %}
release:
needs: Quality
needs: {% if cookiecutter.tox== "y" %}Tox{% else %}Quality{% endif %}
runs-on: ubuntu-latest
steps:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

name: "setup-poetry-env"
description: "Composite action to setup the Python and poetry environment."
inputs:
python-version:
required: false
description: "The python version to use"
default: 3.9.7
runs:
using: "composite"
steps:
Expand All @@ -14,7 +19,7 @@ runs:
- name: Set up python
uses: actions/setup-python@v2
with:
python-version: {{cookiecutter.python_version}}
python-version: {% raw %}${{ inputs.python-version }}{% endraw %}
#----------------------------------------------
# ----- install & configure poetry -----
#----------------------------------------------
Expand All @@ -28,14 +33,12 @@ runs:
#----------------------------------------------
# load cached venv if cache exists
#----------------------------------------------
{% raw -%}
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v2
with:
path: .venv
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
{%- endraw %}
key: {% raw %}venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}{% endraw %}

#----------------------------------------------
# install dependencies if cache does not exist
Expand All @@ -52,4 +55,4 @@ runs:
shell: bash
- name: Activate environment
run: source .venv/bin/activate
shell: bash
shell: bash
1 change: 0 additions & 1 deletion {{cookiecutter.project_name}}/.python-version

This file was deleted.

2 changes: 0 additions & 2 deletions {{cookiecutter.project_name}}/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
PYTHON_VERSION := {{cookiecutter.python_version}}

install: ## Install the poetry environment
@echo "🚀 Creating virtual environment using pyenv and poetry"
@poetry install
Expand Down
3 changes: 2 additions & 1 deletion {{cookiecutter.project_name}}/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description = "{{cookiecutter.project_description}}"
authors = ["{{cookiecutter.author}} <f{{cookiecutter.email}}>"]

[tool.poetry.dependencies]
python = "{{cookiecutter.python_version}}"
python = ">=3.7,<4.0"


[tool.poetry.dev-dependencies]
Expand Down Expand Up @@ -36,6 +36,7 @@ exclude = '''
| \.venv
| build
| dist
| \.tox
))
'''

Expand Down
17 changes: 17 additions & 0 deletions {{cookiecutter.project_name}}/tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[tox]
skipsdist = true
envlist = py37, py38, py39, py310

[gh-actions]
python =
3.7: py37
3.8: py38
3.9: py39
3.10: py310

[testenv]
passenv = PYTHON_VERSION
whitelist_externals = poetry
commands =
poetry install -v
pytest --doctest-modules tests

0 comments on commit 314b810

Please sign in to comment.