Skip to content

Commit 87a1cc9

Browse files
authored
Bugfix/489 fix precommit targets (#492)
* Fix nox tasks for .pre-commit-config.yaml * Add test to verify generated pyproject.toml passes basic poetry inspection * Tests should be in one directory type; if it differs for an existing project this can be modified * Bump to exasol-toolbox 1.6.0 * Add isolated test to run pre-commit hooks to ensure they work * Update dependencies * Test clean up * Switch author_full_name and author_email to be default * Add comment to make clear why env is separately set * Add cookiecutter as a dev dependency in the PTB * Remove install as pre-commit included in PTB dependencies
1 parent 74e5352 commit 87a1cc9

File tree

10 files changed

+368
-115
lines changed

10 files changed

+368
-115
lines changed

.pre-commit-config.yaml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,6 @@ repos:
1111
entry: poetry run -- nox -s project:fix
1212
stages: [ pre-commit ]
1313

14-
- repo: https://github.com/pre-commit/pre-commit-hooks
15-
rev: v4.4.0
16-
hooks:
17-
- id: check-yaml
18-
stages: [ pre-commit ]
19-
- id: end-of-file-fixer
20-
stages: [ pre-commit ]
21-
- id: trailing-whitespace
22-
stages: [ pre-commit ]
23-
2414
- repo: local
2515
hooks:
2616
- id: type-check
@@ -40,3 +30,13 @@ repos:
4030
language: system
4131
entry: poetry run -- nox -s lint:code
4232
stages: [ pre-push ]
33+
34+
- repo: https://github.com/pre-commit/pre-commit-hooks
35+
rev: v5.0.0
36+
hooks:
37+
- id: check-yaml
38+
stages: [ pre-commit ]
39+
- id: end-of-file-fixer
40+
stages: [ pre-commit ]
41+
- id: trailing-whitespace
42+
stages: [ pre-commit ]

doc/changes/unreleased.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
# Unreleased
2+
3+
## Bugfixes
4+
5+
* #489: Fixed .pre-commit-config.yaml to use existing nox tasks

poetry.lock

Lines changed: 221 additions & 94 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

project-template/cookiecutter.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"pypi_package_name": "exasol-{{cookiecutter.repo_name}}",
66
"import_package": "exasol.{{cookiecutter.package_name}}",
77
"description": "",
8-
"author_full_name": "",
9-
"author_email": "",
8+
"author_full_name": "Exasol AG",
9+
"author_email": "opensource@exasol.com",
1010
"project_short_tag": "",
1111
"python_version_min": "3.9",
1212
"license_year": "{% now 'utc', '%Y' %}",
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
default_stages: [ commit ]
1+
default_stages: [ pre-commit, pre-push ]
22
repos:
33

44
- repo: local
@@ -8,7 +8,8 @@ repos:
88
types: [ python ]
99
pass_filenames: false
1010
language: system
11-
entry: poetry run -- nox -s fix
11+
entry: poetry run -- nox -s project:fix
12+
stages: [ pre-commit ]
1213

1314
- repo: local
1415
hooks:
@@ -17,7 +18,8 @@ repos:
1718
types: [ python ]
1819
pass_filenames: false
1920
language: system
20-
entry: poetry run -- nox -s type-check
21+
entry: poetry run -- nox -s lint:typing
22+
stages: [ pre-push ]
2123

2224
- repo: local
2325
hooks:
@@ -26,11 +28,15 @@ repos:
2628
types: [ python ]
2729
pass_filenames: false
2830
language: system
29-
entry: poetry run -- nox -s lint
31+
entry: poetry run -- nox -s lint:code
32+
stages: [ pre-push ]
3033

3134
- repo: https://github.com/pre-commit/pre-commit-hooks
32-
rev: v4.4.0
35+
rev: v5.0.0
3336
hooks:
3437
- id: check-yaml
38+
stages: [ pre-commit ]
3539
- id: end-of-file-fixer
36-
- id: trailing-whitespace
40+
stages: [ pre-commit ]
41+
- id: trailing-whitespace
42+
stages: [ pre-commit ]

project-template/{{cookiecutter.repo_name}}/pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ homepage = "https://github.com/exasol/{{cookiecutter.repo_name}}"
2424
[tool.poetry.dependencies]
2525

2626
[tool.poetry.group.dev.dependencies]
27-
exasol-toolbox = "^1.1.0"
27+
exasol-toolbox = "^1.6.0"
2828

2929
[build-system]
3030
requires = ["poetry-core>=2.0.0"]
@@ -69,7 +69,6 @@ ignore_errors = true
6969

7070
[[tool.mypy.overrides]]
7171
module = [
72-
"tests.*",
7372
"test.*",
7473
]
7574
ignore_errors = true

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ typer = { extras = ["all"], version = ">=0.7.0" }
7070

7171
[tool.poetry.group.dev.dependencies]
7272
autoimport = "^1.4.0"
73+
cookiecutter = "^2.6.0"
7374

7475
[build-system]
7576
requires = ["poetry-core>=2.0.0"]
@@ -121,4 +122,4 @@ sphinx-multiversion = 'exasol.toolbox.sphinx.multiversion:main'
121122
[tool.sonar]
122123
projectKey = "com.exasol:python-toolbox"
123124
hostUrl = "https://sonarcloud.io"
124-
organization = "exasol"
125+
organization = "exasol"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import subprocess
2+
3+
import pytest
4+
5+
from noxconfig import Config
6+
7+
8+
@pytest.fixture(scope="session")
9+
def cwd(tmp_path_factory):
10+
return tmp_path_factory.mktemp("project_template_test")
11+
12+
13+
@pytest.fixture(scope="session")
14+
def new_project(cwd):
15+
project_name = "project"
16+
repo_name = "repo"
17+
package_name = "package"
18+
19+
subprocess.run(
20+
["cookiecutter", Config.root / "project-template", "-o", cwd, "--no-input",
21+
f"project_name={project_name}", f"repo_name={repo_name}",
22+
f"package_name={package_name}",
23+
], capture_output=True, check=True)
24+
25+
return cwd / repo_name
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import subprocess
2+
3+
4+
def test_poetry_check_passes(new_project):
5+
"""
6+
If this tests fails, this indicates that the `pyproject.toml` is not compatible
7+
with the PTB's default poetry version. Note, that this checks only known poetry
8+
attributes, so there could be keys, like `project-abc = 127`, that are present, but,
9+
as they do not have a meaning for poetry, they are ignored.
10+
"""
11+
output = subprocess.run(["poetry", "check"], cwd=new_project,
12+
capture_output=True, text=True)
13+
14+
assert output.stderr == ""
15+
assert output.stdout == "All set!\n"
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import subprocess
2+
from pathlib import Path
3+
4+
import pytest
5+
6+
7+
@pytest.fixture(scope="module")
8+
def poetry_path() -> str:
9+
result = subprocess.run(["which", "poetry"], capture_output=True, text=True)
10+
poetry_path = result.stdout.strip()
11+
return poetry_path
12+
13+
14+
@pytest.fixture(scope="module")
15+
def git_path() -> str:
16+
result = subprocess.run(["which", "git"], capture_output=True, text=True)
17+
git_path = result.stdout.strip()
18+
return git_path
19+
20+
21+
@pytest.fixture(scope="module")
22+
def run_command(poetry_path, git_path, new_project):
23+
"""
24+
Run subprocess command with captured output and a limited environment (env).
25+
26+
We restrict the environment as different systems & tools (i.e. PyCharm) include
27+
environment variables which may supersede the ones provided here. In such cases,
28+
this can lead to a breaking alteration in the PTB poetry environment. Thus,
29+
we provide the minimum information needed to execute the pre-commit command.
30+
"""
31+
32+
def _run_command_fixture(command, **kwargs):
33+
defaults = {
34+
"capture_output": True,
35+
"check": True,
36+
"cwd": new_project,
37+
"env": {"PATH": f"{Path(git_path).parent}:{Path(poetry_path).parent}"},
38+
"text": True,
39+
40+
}
41+
config = {**defaults, **kwargs}
42+
43+
return subprocess.run(command, **config)
44+
45+
return _run_command_fixture
46+
47+
48+
@pytest.fixture(scope="module")
49+
def pre_commit(run_command, new_project, poetry_path):
50+
run_command(command=["git", "init"])
51+
run_command([poetry_path, "install"])
52+
run_command([poetry_path, "run", "--", "pre-commit", "install"])
53+
54+
55+
class TestPreCommitConfig:
56+
@staticmethod
57+
def _command(poetry_path: str, stage: str) -> list[str]:
58+
return [poetry_path, "run", "--", "pre-commit", "run", "--hook-stage", stage,
59+
"--files",
60+
"exasol/package/version.py"]
61+
62+
def test_stage_pre_commit(self, pre_commit, poetry_path, run_command):
63+
command = self._command(poetry_path, "pre-commit")
64+
output = run_command(command=command, check=False)
65+
66+
assert "Failed" not in output.stdout
67+
assert "Passed" in output.stdout
68+
assert output.returncode == 0
69+
70+
def test_stage_pre_push(self, pre_commit, poetry_path, run_command):
71+
command = self._command(poetry_path, "pre-push")
72+
output = run_command(command=command, check=False)
73+
74+
assert "Failed" not in output.stdout
75+
assert "Passed" in output.stdout
76+
assert output.returncode == 0

0 commit comments

Comments
 (0)