Skip to content

Commit

Permalink
feat(pre-commit): Add pre-commit to package #93 (#99)
Browse files Browse the repository at this point in the history
* Pre-commit added to reduce the effort for developers to check their
  work.
* I have added missing checks to test_bake_with_defaults.
* I have fixed some logic in the post_gen.py file about deleting the
  CHANGELOG.md file and added a test for that.

closes #93
  • Loading branch information
imAsparky committed Sep 16, 2021
1 parent 322ac5b commit 5ec4067
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 10 deletions.
6 changes: 4 additions & 2 deletions cookiecutter.json
Expand Up @@ -15,14 +15,16 @@
"create_author_file": "y",
"create_conventional_commits_edit_message": "y",
"create_repo_auto_test_workflow": "y",
"create_auto_CHANGELOG": "y",
"create_auto_CHANGELOG": "n",
"github_CHANGELOG_access_token": ["secrets.GITHUB_TOKEN", "secrets.CHANGELOG_UPDATE"],
"use_GH_action_semantic_version": "y",
"use_pre_commit": "y",
"use_GH_custom_issue_templates": "y",
"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",
".github/workflows/semantic_release.yaml"
".github/workflows/semantic_release.yaml",
".pre-commit-config.yaml"

]
}
13 changes: 12 additions & 1 deletion hooks/post_gen_project.py
Expand Up @@ -28,7 +28,6 @@ def remove_file(filepath):
remove_file(".github/.git-commit-template.txt")

if "{{ cookiecutter.create_auto_CHANGELOG }}" != "y":
remove_file("CHANGELOG.md")
remove_file(".github/workflows/update-changelog.yaml")

if "{{ cookiecutter.create_auto_CHANGELOG }}" == "y":
Expand All @@ -41,6 +40,15 @@ def remove_file(filepath):
remove_file(".github/workflows/semantic_release.yaml")
remove_file(".github/semantic.yaml")

if "{{ cookiecutter.use_GH_action_semantic_version }}" == "y":
remove_file("HISTORY.rst")

if (
"{{ cookiecutter.use_GH_action_semantic_version }}" != "y"
and "{{ cookiecutter.create_auto_CHANGELOG }}" != "y"
):
remove_file("CHANGELOG.md")

if "{{ cookiecutter.use_GH_custom_issue_templates }}" != "y":
remove_file(".github/ISSUE_TEMPLATE/bug-report.md")
remove_file(".github/ISSUE_TEMPLATE/chore.md")
Expand All @@ -49,3 +57,6 @@ def remove_file(filepath):

if "{{ cookiecutter.use_GH_custom_issue_templates }}" == "y":
remove_file(".github/ISSUE_TEMPLATE.md")

if "{{ cookiecutter.use_pre_commit }}" != "y":
remove_file(".pre-commit-config.yaml")
68 changes: 65 additions & 3 deletions tests/test_bake_project.py
Expand Up @@ -95,6 +95,33 @@ def test_bake_with_defaults(cookies):
assert "python_3_package_boilerplate" in found_toplevel_files
assert "tox.ini" in found_toplevel_files
assert "tests" in found_toplevel_files
assert "CHANGELOG.md" in found_toplevel_files
assert "LICENSE" in found_toplevel_files
assert "AUTHORS.rst" in found_toplevel_files
assert "History.rst" not in found_toplevel_files
# assert ".pre-commit-config.yaml" in found_toplevel_files

found_git_workflows = [
f.basename for f in result.project.join(".github/workflows").listdir()
]
assert "semantic_release.yaml" in found_git_workflows
assert "test_contribution.yaml" in found_git_workflows
assert "update-changelog.yaml" not in found_git_workflows

found_git_templates = [
f.basename for f in result.project.join(".github/ISSUE_TEMPLATE").listdir()
]

assert "bug-report.md" in found_git_templates
assert "chore.md" in found_git_templates
assert "documentation-request.md" in found_git_templates
assert "feature-request.md" in found_git_templates

found_git_files = [f.basename for f in result.project.join(".github").listdir()]

assert ".git-commit-template.txt" in found_git_files
assert "semantic.yaml" in found_git_files
assert "ISSUE_TEMPLATE.md" not in found_git_files


def test_bake_and_run_tests(cookies):
Expand Down Expand Up @@ -431,9 +458,6 @@ def test_bake_without_automatic_CHANGELOG(cookies):
cookies, extra_context={"create_auto_CHANGELOG": "n"}
) as result:

change_log_without_files = [f.basename for f in result.project.listdir()]
assert "CHANGELOG.md" not in change_log_without_files

auto_workflow_without_files = [
f.basename for f in result.project.join(".github/workflows").listdir()
]
Expand Down Expand Up @@ -478,6 +502,24 @@ def test_bake_without_auto_semantic_version(cookies):
assert "semantic_release.yaml" not in sem_ver_workflow_without_files


def test_bake_without_automatic_CHANGELOG_and_semantic_version(cookies):
"""
Test cookiecutter created the package without Changelog & Semantic Version.
"""
with bake_in_temp_dir(
cookies,
extra_context={
"create_auto_CHANGELOG": "n",
"use_GH_action_semantic_version": "n",
},
) as result:

auto_changelog_semantic_without_files = [
f.basename for f in result.project.listdir()
]
assert "CHANGELOG.md" not in auto_changelog_semantic_without_files


def test_bake_with_custom_issue_templates(cookies):
"""
Test cookiecutter created the package with custom issue templates.
Expand Down Expand Up @@ -522,3 +564,23 @@ def test_bake_without_custom_issue_templates(cookies):
f.basename for f in result.project.join(".github/").listdir()
]
assert "ISSUE_TEMPLATE.md" in standard_issue_template


def test_bake_with_pre_commit(cookies):
"""
Test cookiecutter created the package with pre-commit.
"""
with bake_in_temp_dir(cookies, extra_context={"use_pre_commit": "y"}) as result:

pre_commit_with_files = [f.basename for f in result.project.listdir()]
assert ".pre-commit-config.yaml" in pre_commit_with_files


# def test_bake_without_pre_commit(cookies):
# """
# Test cookiecutter created the package without pre-commit.
# """
# with bake_in_temp_dir(cookies, extra_context={"use_pre_commit": "n"}) as result:

# pre_commit_without_files = [f.basename for f in result.project.listdir()]
# assert ".pre-commit-config.yaml" in pre_commit_without_files
85 changes: 85 additions & 0 deletions {{cookiecutter.project_slug}}/.pre-commit-config.yaml
@@ -0,0 +1,85 @@
default_stages: [commit]
fail_fast: true
exclude: ".conf.py|^tests/|^{{cookiecutter.project_slug}}/"
# {{cookiecutter.project_slug}} has been excluded until I start adapting
# that area.

ci:
autofix_commit_msg: |
chore(pre-commit): Auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
autofix_prs: true
autoupdate_commit_msg: "chore(pre-commit): Weekly pre-commit autoupdate"
autoupdate_schedule: weekly
skip: []
submodules: false

repos:
# - repo: https://github.com/Lucas-C/pre-commit-hooks
# rev: v1.1.9
# hooks:
# - id: forbid-crlf
# - id: remove-crlf

# - repo: https://github.com/Lucas-C/pre-commit-hooks-safety
# rev: v1.2.1
# hooks:
# - id: python-safety-dependencies-check

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-merge-conflict
- id: check-yaml

- repo: https://github.com/pycqa/pylint
rev: v2.10.2
hooks:
- id: pylint

- repo: https://github.com/psf/black
rev: 21.8b0
hooks:
- id: black

- repo: https://github.com/PyCQA/bandit
rev: 1.7.0
hooks:
- id: bandit
args: ["--ini=tox.ini"]

- repo: https://github.com/pycqa/flake8
rev: 3.9.2
hooks:
- id: flake8
additional_dependencies: [flake8-typing-imports==1.10.0]

- repo: https://github.com/PyCQA/isort
rev: 5.9.3
hooks:
- id: isort

- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.9.0
hooks:
- id: rst-directive-colons
# - id: rst-backticks
- id: rst-inline-touching-normal
- id: python-no-log-warn
- id: python-no-eval
- id: text-unicode-replacement-char

# {%- if cookiecutter.use_mypy == 'y' %}
# - repo: https://github.com/pre-commit/mirrors-mypy
# rev: v0.901
# hooks:
# - id: mypy
# exclude: tests/
# {% if cookiecutter.command_line_interface|lower == 'click' -%}
# additional_dependencies:
# - types-click
# {%- endif %}
# {%- endif %}
12 changes: 8 additions & 4 deletions {{cookiecutter.project_slug}}/tox.ini
@@ -1,7 +1,13 @@
[tox]
envlist = py36, py37,py38, py39, py38 pypy, docs
skipsdist = true
skip_missing_interpreters = true
envlist =
py36
py37
py38
py39
py38 pypy
py39-docs

[testenv:docs]
basepython=python
Expand All @@ -24,12 +30,10 @@ PLATFORM =
macos-latest: macos
windows-latest: windows


[testenv]
setenv =
PYTHONPATH = {toxinidir}
deps =
-r{toxinidir}/requirements_dev.txt
commands =
python -m pip install --upgrade pip
pytest
pytest -v {posargs:tests}

0 comments on commit 5ec4067

Please sign in to comment.