prekmoi 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- prekmoi-0.1.0/LICENSE +21 -0
- prekmoi-0.1.0/PKG-INFO +122 -0
- prekmoi-0.1.0/README.md +92 -0
- prekmoi-0.1.0/pyproject.toml +84 -0
- prekmoi-0.1.0/setup.cfg +4 -0
- prekmoi-0.1.0/src/prekmoi/__init__.py +3 -0
- prekmoi-0.1.0/src/prekmoi/hook.py +66 -0
- prekmoi-0.1.0/src/prekmoi.egg-info/PKG-INFO +122 -0
- prekmoi-0.1.0/src/prekmoi.egg-info/SOURCES.txt +12 -0
- prekmoi-0.1.0/src/prekmoi.egg-info/dependency_links.txt +1 -0
- prekmoi-0.1.0/src/prekmoi.egg-info/entry_points.txt +2 -0
- prekmoi-0.1.0/src/prekmoi.egg-info/requires.txt +9 -0
- prekmoi-0.1.0/src/prekmoi.egg-info/top_level.txt +1 -0
- prekmoi-0.1.0/tests/test_hook.py +152 -0
prekmoi-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 BALDOeFIERO
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
prekmoi-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: prekmoi
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A pre-commit hook to validate Zsh syntax in Chezmoi templates and standalone Zsh scripts.
|
|
5
|
+
Author: BALDOeFIERO
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/baldoefiero/prekmoi
|
|
8
|
+
Project-URL: Repository, https://github.com/baldoefiero/prekmoi
|
|
9
|
+
Project-URL: Issues, https://github.com/baldoefiero/prekmoi/issues
|
|
10
|
+
Keywords: chezmoi,prek,pre-commit,zsh,dotfiles
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Environment :: Console
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
18
|
+
Classifier: Topic :: System :: Systems Administration
|
|
19
|
+
Requires-Python: >=3.11
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: pytest<10,>=9.0; extra == "dev"
|
|
24
|
+
Requires-Dist: pytest-mock<4,>=3.17; extra == "dev"
|
|
25
|
+
Requires-Dist: pytest-cov<8,>=7.0; extra == "dev"
|
|
26
|
+
Requires-Dist: pylint<5,>=3.3; extra == "dev"
|
|
27
|
+
Provides-Extra: docs
|
|
28
|
+
Requires-Dist: mkdocs-material<10,>=9.6; extra == "docs"
|
|
29
|
+
Dynamic: license-file
|
|
30
|
+
|
|
31
|
+
# prekmoi
|
|
32
|
+
|
|
33
|
+
**prekmoi** is a [prek](https://prek.j178.dev/) hook that validates the Zsh syntax in
|
|
34
|
+
[Chezmoi](https://chezmoi.io) templates and standalone Zsh scripts.
|
|
35
|
+
|
|
36
|
+
It automatically selects supported files, renders Chezmoi templates when necessary, and checks the
|
|
37
|
+
resulting Zsh syntax without executing the scripts.
|
|
38
|
+
|
|
39
|
+
**Contents**
|
|
40
|
+
|
|
41
|
+
- [Features](#features)
|
|
42
|
+
- [Requirements](#requirements)
|
|
43
|
+
- [Installation](#installation)
|
|
44
|
+
- [How It Works](#how-it-works)
|
|
45
|
+
- [Bug Reports](#bug-reports)
|
|
46
|
+
- [License](#license)
|
|
47
|
+
|
|
48
|
+
## Features
|
|
49
|
+
|
|
50
|
+
- Validates Chezmoi Zsh templates after rendering.
|
|
51
|
+
- Validates standalone Zsh scripts without rendering.
|
|
52
|
+
- Uses `chezmoi execute-template` for template rendering.
|
|
53
|
+
- Uses `zsh -n` for syntax validation.
|
|
54
|
+
- Skips unsupported files automatically through `prek`.
|
|
55
|
+
- Does not execute validated scripts.
|
|
56
|
+
|
|
57
|
+
## Requirements
|
|
58
|
+
|
|
59
|
+
- Python 3.11+
|
|
60
|
+
- [zsh](https://www.zsh.org/)
|
|
61
|
+
- [Chezmoi](https://chezmoi.io)
|
|
62
|
+
- [prek](https://prek.j178.dev/)
|
|
63
|
+
|
|
64
|
+
## Installation
|
|
65
|
+
|
|
66
|
+
Add the following to your `.pre-commit-config.yaml` file.
|
|
67
|
+
|
|
68
|
+
```yaml
|
|
69
|
+
repos:
|
|
70
|
+
- repo: https://github.com/baldoefiero/prekmoi
|
|
71
|
+
rev: v0.1.0
|
|
72
|
+
hooks:
|
|
73
|
+
- id: prekmoi
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## How It Works
|
|
77
|
+
|
|
78
|
+
prekmoi uses `prek` to select files based on their paths and names.
|
|
79
|
+
|
|
80
|
+
**Chezmoi templates**
|
|
81
|
+
|
|
82
|
+
Files matching a supported prefix and ending in `.tmpl` are rendered using
|
|
83
|
+
`chezmoi execute-template`.
|
|
84
|
+
|
|
85
|
+
The rendered output is then passed to `zsh -n` for syntax validation.
|
|
86
|
+
|
|
87
|
+
If rendering fails, the file is considered invalid and syntax validation is skipped.
|
|
88
|
+
|
|
89
|
+
**Zsh scripts**
|
|
90
|
+
|
|
91
|
+
Files selected by `prek` that do not end in `.tmpl` are passed directly to `zsh -n` without
|
|
92
|
+
template rendering.
|
|
93
|
+
|
|
94
|
+
**Supported prefixes**
|
|
95
|
+
|
|
96
|
+
The following filename prefixes are supported:
|
|
97
|
+
|
|
98
|
+
| Prefix |
|
|
99
|
+
| :--- |
|
|
100
|
+
| dot_zshrc |
|
|
101
|
+
| dot_zshenv |
|
|
102
|
+
| dot_zprofile |
|
|
103
|
+
| dot_zlogin |
|
|
104
|
+
| dot_zlogout |
|
|
105
|
+
| run_ |
|
|
106
|
+
|
|
107
|
+
Files that do not match the selection rules are ignored by `prek`.
|
|
108
|
+
|
|
109
|
+
## Bug Reports
|
|
110
|
+
|
|
111
|
+
Found a bug or something not working as expected?
|
|
112
|
+
|
|
113
|
+
Please [open an issue](https://github.com/baldoefiero/prekmoi/issues) and include:
|
|
114
|
+
|
|
115
|
+
- A description of the problem
|
|
116
|
+
- Steps to reproduce it
|
|
117
|
+
- Relevant error messages or output
|
|
118
|
+
- You operating system and the versions of Python, Zsh, Chezmoi and prek.
|
|
119
|
+
|
|
120
|
+
## License
|
|
121
|
+
|
|
122
|
+
prekmoi is distributed under the [MIT License](LICENSE).
|
prekmoi-0.1.0/README.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# prekmoi
|
|
2
|
+
|
|
3
|
+
**prekmoi** is a [prek](https://prek.j178.dev/) hook that validates the Zsh syntax in
|
|
4
|
+
[Chezmoi](https://chezmoi.io) templates and standalone Zsh scripts.
|
|
5
|
+
|
|
6
|
+
It automatically selects supported files, renders Chezmoi templates when necessary, and checks the
|
|
7
|
+
resulting Zsh syntax without executing the scripts.
|
|
8
|
+
|
|
9
|
+
**Contents**
|
|
10
|
+
|
|
11
|
+
- [Features](#features)
|
|
12
|
+
- [Requirements](#requirements)
|
|
13
|
+
- [Installation](#installation)
|
|
14
|
+
- [How It Works](#how-it-works)
|
|
15
|
+
- [Bug Reports](#bug-reports)
|
|
16
|
+
- [License](#license)
|
|
17
|
+
|
|
18
|
+
## Features
|
|
19
|
+
|
|
20
|
+
- Validates Chezmoi Zsh templates after rendering.
|
|
21
|
+
- Validates standalone Zsh scripts without rendering.
|
|
22
|
+
- Uses `chezmoi execute-template` for template rendering.
|
|
23
|
+
- Uses `zsh -n` for syntax validation.
|
|
24
|
+
- Skips unsupported files automatically through `prek`.
|
|
25
|
+
- Does not execute validated scripts.
|
|
26
|
+
|
|
27
|
+
## Requirements
|
|
28
|
+
|
|
29
|
+
- Python 3.11+
|
|
30
|
+
- [zsh](https://www.zsh.org/)
|
|
31
|
+
- [Chezmoi](https://chezmoi.io)
|
|
32
|
+
- [prek](https://prek.j178.dev/)
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
Add the following to your `.pre-commit-config.yaml` file.
|
|
37
|
+
|
|
38
|
+
```yaml
|
|
39
|
+
repos:
|
|
40
|
+
- repo: https://github.com/baldoefiero/prekmoi
|
|
41
|
+
rev: v0.1.0
|
|
42
|
+
hooks:
|
|
43
|
+
- id: prekmoi
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## How It Works
|
|
47
|
+
|
|
48
|
+
prekmoi uses `prek` to select files based on their paths and names.
|
|
49
|
+
|
|
50
|
+
**Chezmoi templates**
|
|
51
|
+
|
|
52
|
+
Files matching a supported prefix and ending in `.tmpl` are rendered using
|
|
53
|
+
`chezmoi execute-template`.
|
|
54
|
+
|
|
55
|
+
The rendered output is then passed to `zsh -n` for syntax validation.
|
|
56
|
+
|
|
57
|
+
If rendering fails, the file is considered invalid and syntax validation is skipped.
|
|
58
|
+
|
|
59
|
+
**Zsh scripts**
|
|
60
|
+
|
|
61
|
+
Files selected by `prek` that do not end in `.tmpl` are passed directly to `zsh -n` without
|
|
62
|
+
template rendering.
|
|
63
|
+
|
|
64
|
+
**Supported prefixes**
|
|
65
|
+
|
|
66
|
+
The following filename prefixes are supported:
|
|
67
|
+
|
|
68
|
+
| Prefix |
|
|
69
|
+
| :--- |
|
|
70
|
+
| dot_zshrc |
|
|
71
|
+
| dot_zshenv |
|
|
72
|
+
| dot_zprofile |
|
|
73
|
+
| dot_zlogin |
|
|
74
|
+
| dot_zlogout |
|
|
75
|
+
| run_ |
|
|
76
|
+
|
|
77
|
+
Files that do not match the selection rules are ignored by `prek`.
|
|
78
|
+
|
|
79
|
+
## Bug Reports
|
|
80
|
+
|
|
81
|
+
Found a bug or something not working as expected?
|
|
82
|
+
|
|
83
|
+
Please [open an issue](https://github.com/baldoefiero/prekmoi/issues) and include:
|
|
84
|
+
|
|
85
|
+
- A description of the problem
|
|
86
|
+
- Steps to reproduce it
|
|
87
|
+
- Relevant error messages or output
|
|
88
|
+
- You operating system and the versions of Python, Zsh, Chezmoi and prek.
|
|
89
|
+
|
|
90
|
+
## License
|
|
91
|
+
|
|
92
|
+
prekmoi is distributed under the [MIT License](LICENSE).
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77.0.3"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "prekmoi"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A pre-commit hook to validate Zsh syntax in Chezmoi templates and standalone Zsh scripts."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [
|
|
14
|
+
{ name = "BALDOeFIERO" },
|
|
15
|
+
]
|
|
16
|
+
keywords = [
|
|
17
|
+
"chezmoi",
|
|
18
|
+
"prek",
|
|
19
|
+
"pre-commit",
|
|
20
|
+
"zsh",
|
|
21
|
+
"dotfiles",
|
|
22
|
+
]
|
|
23
|
+
classifiers = [
|
|
24
|
+
"Development Status :: 3 - Alpha",
|
|
25
|
+
"Environment :: Console",
|
|
26
|
+
"Intended Audience :: Developers",
|
|
27
|
+
"Operating System :: OS Independent",
|
|
28
|
+
"Programming Language :: Python :: 3",
|
|
29
|
+
"Programming Language :: Python :: 3.11",
|
|
30
|
+
"Topic :: Software Development :: Quality Assurance",
|
|
31
|
+
"Topic :: System :: Systems Administration",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
dependencies = []
|
|
35
|
+
|
|
36
|
+
[project.urls]
|
|
37
|
+
Homepage = "https://github.com/baldoefiero/prekmoi"
|
|
38
|
+
Repository = "https://github.com/baldoefiero/prekmoi"
|
|
39
|
+
Issues = "https://github.com/baldoefiero/prekmoi/issues"
|
|
40
|
+
|
|
41
|
+
[project.scripts]
|
|
42
|
+
prekmoi = "prekmoi.hook:main"
|
|
43
|
+
|
|
44
|
+
[project.optional-dependencies]
|
|
45
|
+
dev = [
|
|
46
|
+
"pytest>=9.0,<10",
|
|
47
|
+
"pytest-mock>=3.17,<4",
|
|
48
|
+
"pytest-cov>=7.0,<8",
|
|
49
|
+
"pylint>=3.3,<5",
|
|
50
|
+
]
|
|
51
|
+
docs = [
|
|
52
|
+
"mkdocs-material>=9.6,<10",
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
[tool.setuptools]
|
|
56
|
+
package-dir = {"" = "src"}
|
|
57
|
+
|
|
58
|
+
[tool.setuptools.packages.find]
|
|
59
|
+
where = ["src"]
|
|
60
|
+
include = ["prekmoi*"]
|
|
61
|
+
|
|
62
|
+
[tool.pytest.ini_options]
|
|
63
|
+
testpaths = ["tests"]
|
|
64
|
+
pythonpath = ["src"]
|
|
65
|
+
strict_config = true
|
|
66
|
+
strict_markers = true
|
|
67
|
+
addopts = [
|
|
68
|
+
"-ra",
|
|
69
|
+
"--cov",
|
|
70
|
+
"--cov-report=term-missing",
|
|
71
|
+
]
|
|
72
|
+
|
|
73
|
+
[tool.pylint.main]
|
|
74
|
+
py-version = "3.11"
|
|
75
|
+
source-roots = ["src", "tests"]
|
|
76
|
+
ignore-paths = ["tests/fixtures/.*"]
|
|
77
|
+
|
|
78
|
+
[tool.coverage.run]
|
|
79
|
+
branch = true
|
|
80
|
+
source = ["prekmoi"]
|
|
81
|
+
|
|
82
|
+
[tool.coverage.report]
|
|
83
|
+
fail_under = 100
|
|
84
|
+
show_missing = true
|
prekmoi-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""Pre-commit hook for validating Chezmoi Zsh templates or Zsh scripts."""
|
|
2
|
+
|
|
3
|
+
import subprocess # nosec B404: required to execute chezmoi and zsh.
|
|
4
|
+
import sys
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
TEMPLATE_SUFFIX = ".tmpl"
|
|
9
|
+
CHEZMOI_COMMAND_RENDER = ["chezmoi", "execute-template"]
|
|
10
|
+
ZSH_SYNTAX_CHECK = ["zsh", "-n"]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def validate_zsh_syntax(path: str) -> bool:
|
|
14
|
+
"""Check if file name in `path` has a valid `zsh` syntax.
|
|
15
|
+
|
|
16
|
+
Templates are rendered with `chezmoi` first, then the output is validated.
|
|
17
|
+
A template that fails to render is considered invalid.
|
|
18
|
+
|
|
19
|
+
Args
|
|
20
|
+
----
|
|
21
|
+
path (str): path of the file to check.
|
|
22
|
+
|
|
23
|
+
Returns
|
|
24
|
+
-------
|
|
25
|
+
bool
|
|
26
|
+
True if `path` file name has a valid `zsh` syntax; otherwise False.
|
|
27
|
+
"""
|
|
28
|
+
with Path(path).open("rb") as file:
|
|
29
|
+
if Path(path).suffix == TEMPLATE_SUFFIX:
|
|
30
|
+
rendered = subprocess.run(
|
|
31
|
+
CHEZMOI_COMMAND_RENDER, stdin=file, capture_output=True, check=False
|
|
32
|
+
) # nosec B603: arguments are passed without a shell.
|
|
33
|
+
if rendered.returncode != 0:
|
|
34
|
+
return False
|
|
35
|
+
source = rendered.stdout
|
|
36
|
+
else:
|
|
37
|
+
source = file.read()
|
|
38
|
+
|
|
39
|
+
result = subprocess.run(
|
|
40
|
+
ZSH_SYNTAX_CHECK, input=source, capture_output=True, check=False
|
|
41
|
+
) # nosec B603: arguments are passed without a shell.
|
|
42
|
+
return result.returncode == 0
|
|
43
|
+
|
|
44
|
+
def main() -> int:
|
|
45
|
+
"""Validate shell scripts and templates received from pre-commit.
|
|
46
|
+
|
|
47
|
+
Prints `PASS` or `FAIL` for each validated file; files that are neither templates nor scripts
|
|
48
|
+
are skipped.
|
|
49
|
+
|
|
50
|
+
Returns
|
|
51
|
+
-------
|
|
52
|
+
int
|
|
53
|
+
1 if validation fails otherwise 0.
|
|
54
|
+
"""
|
|
55
|
+
failed = False
|
|
56
|
+
for p in sys.argv[1:]:
|
|
57
|
+
if validate_zsh_syntax(p):
|
|
58
|
+
print(f"PASS: {p}")
|
|
59
|
+
else:
|
|
60
|
+
print(f"FAIL: {p}")
|
|
61
|
+
failed = True
|
|
62
|
+
return int(failed)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
if __name__ == "__main__":
|
|
66
|
+
sys.exit(main())
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: prekmoi
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A pre-commit hook to validate Zsh syntax in Chezmoi templates and standalone Zsh scripts.
|
|
5
|
+
Author: BALDOeFIERO
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/baldoefiero/prekmoi
|
|
8
|
+
Project-URL: Repository, https://github.com/baldoefiero/prekmoi
|
|
9
|
+
Project-URL: Issues, https://github.com/baldoefiero/prekmoi/issues
|
|
10
|
+
Keywords: chezmoi,prek,pre-commit,zsh,dotfiles
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Environment :: Console
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
18
|
+
Classifier: Topic :: System :: Systems Administration
|
|
19
|
+
Requires-Python: >=3.11
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: pytest<10,>=9.0; extra == "dev"
|
|
24
|
+
Requires-Dist: pytest-mock<4,>=3.17; extra == "dev"
|
|
25
|
+
Requires-Dist: pytest-cov<8,>=7.0; extra == "dev"
|
|
26
|
+
Requires-Dist: pylint<5,>=3.3; extra == "dev"
|
|
27
|
+
Provides-Extra: docs
|
|
28
|
+
Requires-Dist: mkdocs-material<10,>=9.6; extra == "docs"
|
|
29
|
+
Dynamic: license-file
|
|
30
|
+
|
|
31
|
+
# prekmoi
|
|
32
|
+
|
|
33
|
+
**prekmoi** is a [prek](https://prek.j178.dev/) hook that validates the Zsh syntax in
|
|
34
|
+
[Chezmoi](https://chezmoi.io) templates and standalone Zsh scripts.
|
|
35
|
+
|
|
36
|
+
It automatically selects supported files, renders Chezmoi templates when necessary, and checks the
|
|
37
|
+
resulting Zsh syntax without executing the scripts.
|
|
38
|
+
|
|
39
|
+
**Contents**
|
|
40
|
+
|
|
41
|
+
- [Features](#features)
|
|
42
|
+
- [Requirements](#requirements)
|
|
43
|
+
- [Installation](#installation)
|
|
44
|
+
- [How It Works](#how-it-works)
|
|
45
|
+
- [Bug Reports](#bug-reports)
|
|
46
|
+
- [License](#license)
|
|
47
|
+
|
|
48
|
+
## Features
|
|
49
|
+
|
|
50
|
+
- Validates Chezmoi Zsh templates after rendering.
|
|
51
|
+
- Validates standalone Zsh scripts without rendering.
|
|
52
|
+
- Uses `chezmoi execute-template` for template rendering.
|
|
53
|
+
- Uses `zsh -n` for syntax validation.
|
|
54
|
+
- Skips unsupported files automatically through `prek`.
|
|
55
|
+
- Does not execute validated scripts.
|
|
56
|
+
|
|
57
|
+
## Requirements
|
|
58
|
+
|
|
59
|
+
- Python 3.11+
|
|
60
|
+
- [zsh](https://www.zsh.org/)
|
|
61
|
+
- [Chezmoi](https://chezmoi.io)
|
|
62
|
+
- [prek](https://prek.j178.dev/)
|
|
63
|
+
|
|
64
|
+
## Installation
|
|
65
|
+
|
|
66
|
+
Add the following to your `.pre-commit-config.yaml` file.
|
|
67
|
+
|
|
68
|
+
```yaml
|
|
69
|
+
repos:
|
|
70
|
+
- repo: https://github.com/baldoefiero/prekmoi
|
|
71
|
+
rev: v0.1.0
|
|
72
|
+
hooks:
|
|
73
|
+
- id: prekmoi
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## How It Works
|
|
77
|
+
|
|
78
|
+
prekmoi uses `prek` to select files based on their paths and names.
|
|
79
|
+
|
|
80
|
+
**Chezmoi templates**
|
|
81
|
+
|
|
82
|
+
Files matching a supported prefix and ending in `.tmpl` are rendered using
|
|
83
|
+
`chezmoi execute-template`.
|
|
84
|
+
|
|
85
|
+
The rendered output is then passed to `zsh -n` for syntax validation.
|
|
86
|
+
|
|
87
|
+
If rendering fails, the file is considered invalid and syntax validation is skipped.
|
|
88
|
+
|
|
89
|
+
**Zsh scripts**
|
|
90
|
+
|
|
91
|
+
Files selected by `prek` that do not end in `.tmpl` are passed directly to `zsh -n` without
|
|
92
|
+
template rendering.
|
|
93
|
+
|
|
94
|
+
**Supported prefixes**
|
|
95
|
+
|
|
96
|
+
The following filename prefixes are supported:
|
|
97
|
+
|
|
98
|
+
| Prefix |
|
|
99
|
+
| :--- |
|
|
100
|
+
| dot_zshrc |
|
|
101
|
+
| dot_zshenv |
|
|
102
|
+
| dot_zprofile |
|
|
103
|
+
| dot_zlogin |
|
|
104
|
+
| dot_zlogout |
|
|
105
|
+
| run_ |
|
|
106
|
+
|
|
107
|
+
Files that do not match the selection rules are ignored by `prek`.
|
|
108
|
+
|
|
109
|
+
## Bug Reports
|
|
110
|
+
|
|
111
|
+
Found a bug or something not working as expected?
|
|
112
|
+
|
|
113
|
+
Please [open an issue](https://github.com/baldoefiero/prekmoi/issues) and include:
|
|
114
|
+
|
|
115
|
+
- A description of the problem
|
|
116
|
+
- Steps to reproduce it
|
|
117
|
+
- Relevant error messages or output
|
|
118
|
+
- You operating system and the versions of Python, Zsh, Chezmoi and prek.
|
|
119
|
+
|
|
120
|
+
## License
|
|
121
|
+
|
|
122
|
+
prekmoi is distributed under the [MIT License](LICENSE).
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/prekmoi/__init__.py
|
|
5
|
+
src/prekmoi/hook.py
|
|
6
|
+
src/prekmoi.egg-info/PKG-INFO
|
|
7
|
+
src/prekmoi.egg-info/SOURCES.txt
|
|
8
|
+
src/prekmoi.egg-info/dependency_links.txt
|
|
9
|
+
src/prekmoi.egg-info/entry_points.txt
|
|
10
|
+
src/prekmoi.egg-info/requires.txt
|
|
11
|
+
src/prekmoi.egg-info/top_level.txt
|
|
12
|
+
tests/test_hook.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
prekmoi
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"""Tests for the prekmoi hook."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
import shutil
|
|
5
|
+
import runpy
|
|
6
|
+
import sys
|
|
7
|
+
import subprocess
|
|
8
|
+
from subprocess import CompletedProcess
|
|
9
|
+
from typing import TYPE_CHECKING, Protocol, cast
|
|
10
|
+
import pytest
|
|
11
|
+
from prekmoi.hook import main, validate_zsh_syntax
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from collections.abc import Callable
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
from typing import Any
|
|
18
|
+
from pytest_mock import MockerFixture
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
VALID_ZSH = "echo hello\n"
|
|
22
|
+
INVALID_ZSH = "if true; then\n echo hello\n" # missing `fi`
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class FakeChezmoi(Protocol): # pylint: disable=too-few-public-methods
|
|
26
|
+
"""Callable installing a fake `chezmoi` render command."""
|
|
27
|
+
|
|
28
|
+
def __call__(self, stdout: bytes = b"", returncode: int = 0) -> None: ...
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
requires_zsh = pytest.mark.skipif(
|
|
32
|
+
shutil.which("zsh") is None, reason="zsh is not installed"
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
VALIDITY = [
|
|
37
|
+
pytest.param(VALID_ZSH, True, id="valid"),
|
|
38
|
+
pytest.param(INVALID_ZSH, False, id="invalid"),
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@pytest.fixture(name="fake_chezmoi")
|
|
43
|
+
def fake_chezmoi_fixture(mocker: MockerFixture) -> FakeChezmoi:
|
|
44
|
+
"""Fake only the chezmoi command; every other command (zsh) runs for real."""
|
|
45
|
+
real_run = cast("Callable[..., CompletedProcess[bytes]]", subprocess.run)
|
|
46
|
+
|
|
47
|
+
def install(stdout: bytes = b"", returncode: int = 0, stderr: bytes = b"") -> None:
|
|
48
|
+
def run(command: list[str], **kwargs: Any) -> CompletedProcess[bytes]:
|
|
49
|
+
if command[0] == "chezmoi":
|
|
50
|
+
return CompletedProcess(command, returncode, stdout, stderr)
|
|
51
|
+
return real_run(command, **kwargs) # pylint: disable=subprocess-run-check
|
|
52
|
+
mocker.patch("prekmoi.hook.subprocess.run", side_effect=run)
|
|
53
|
+
return install
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@requires_zsh
|
|
57
|
+
@pytest.mark.parametrize(("content", "expected"), VALIDITY)
|
|
58
|
+
def test_validate_script(
|
|
59
|
+
make_file: Callable[[str, str], Path], content: str, expected: bool
|
|
60
|
+
) -> None:
|
|
61
|
+
"""Test that a script is syntax-checked by zsh as it is."""
|
|
62
|
+
path = make_file("dot_zshrc", content)
|
|
63
|
+
assert validate_zsh_syntax(str(path)) is expected
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@requires_zsh
|
|
67
|
+
@pytest.mark.parametrize(("rendered", "expected"), VALIDITY)
|
|
68
|
+
def test_validate_template_checks_rendered_output(
|
|
69
|
+
make_file: Callable[[str, str], Path],
|
|
70
|
+
fake_chezmoi: Callable[..., None],
|
|
71
|
+
rendered: str,
|
|
72
|
+
expected: bool,
|
|
73
|
+
) -> None:
|
|
74
|
+
"""Test that a template is syntax-checked after rendering, not before."""
|
|
75
|
+
path = make_file("dot_zshrc.tmpl", "{{ .anything }}") # source is irrelevant
|
|
76
|
+
fake_chezmoi(stdout=rendered.encode())
|
|
77
|
+
|
|
78
|
+
assert validate_zsh_syntax(str(path)) is expected
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def test_validate_template_render_failure_returns_false(
|
|
82
|
+
make_file: Callable[[str, str], Path],
|
|
83
|
+
fake_chezmoi: Callable[..., None],
|
|
84
|
+
) -> None:
|
|
85
|
+
"""Test that a template failing to render is invalid and does not raise."""
|
|
86
|
+
path = make_file("dot_zshrc.tmpl", "{{ broken")
|
|
87
|
+
fake_chezmoi(returncode=1)
|
|
88
|
+
|
|
89
|
+
assert validate_zsh_syntax(str(path)) is False
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@requires_zsh
|
|
93
|
+
def test_main_reports_pass_and_fail(
|
|
94
|
+
make_file: Callable[[str, str], Path],
|
|
95
|
+
mocker: MockerFixture,
|
|
96
|
+
capsys: pytest.CaptureFixture[str],
|
|
97
|
+
) -> None:
|
|
98
|
+
"""Test that `main` reports each validated file and skips unsupported ones."""
|
|
99
|
+
good = make_file("dot_zshrc", VALID_ZSH)
|
|
100
|
+
bad = make_file("dot_zprofile", INVALID_ZSH)
|
|
101
|
+
mocker.patch("sys.argv", ["prekmoi", str(good), str(bad)])
|
|
102
|
+
|
|
103
|
+
assert main() == 1
|
|
104
|
+
|
|
105
|
+
out = capsys.readouterr().out
|
|
106
|
+
assert f"PASS: {good}" in out
|
|
107
|
+
assert f"FAIL: {bad}" in out
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
@requires_zsh
|
|
111
|
+
def test_main_exit_code_zero_when_all_valid(
|
|
112
|
+
make_file: Callable[[str, str], Path], mocker: MockerFixture
|
|
113
|
+
) -> None:
|
|
114
|
+
"""Test that `main` returns 0 when every validated file is valid."""
|
|
115
|
+
good = make_file("dot_zshrc", VALID_ZSH)
|
|
116
|
+
mocker.patch("sys.argv", ["prekmoi", str(good)])
|
|
117
|
+
|
|
118
|
+
assert main() == 0
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def test_main_without_files_returns_zero(mocker: MockerFixture) -> None:
|
|
122
|
+
"""Test that `main` succeeds when pre-commit passes no files."""
|
|
123
|
+
mocker.patch("sys.argv", ["prekmoi"])
|
|
124
|
+
|
|
125
|
+
assert main() == 0
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
@requires_zsh
|
|
129
|
+
@pytest.mark.parametrize(
|
|
130
|
+
("content", "exit_code"),
|
|
131
|
+
[
|
|
132
|
+
pytest.param(VALID_ZSH, 0, id="valid"),
|
|
133
|
+
pytest.param(INVALID_ZSH, 1, id="invalid"),
|
|
134
|
+
],
|
|
135
|
+
)
|
|
136
|
+
def test_run_as_script_exits_with_main_status(
|
|
137
|
+
make_file: Callable[[str, str], Path],
|
|
138
|
+
mocker: MockerFixture,
|
|
139
|
+
content: str,
|
|
140
|
+
exit_code: int
|
|
141
|
+
) -> None:
|
|
142
|
+
"""Test that running the module as script exists with the `main` status."""
|
|
143
|
+
path = make_file("dot_zshrc", content)
|
|
144
|
+
mocker.patch("sys.argv", ["prekmoi", str(path)])
|
|
145
|
+
# Re-execute the module from scratch, avoiding runpy's "found in sys.modules" warning.
|
|
146
|
+
mocker.patch.dict("sys.modules")
|
|
147
|
+
del sys.modules["prekmoi.hook"]
|
|
148
|
+
|
|
149
|
+
with pytest.raises(SystemExit) as exc_info:
|
|
150
|
+
runpy.run_module("prekmoi.hook", run_name="__main__")
|
|
151
|
+
|
|
152
|
+
assert exc_info.value.code == exit_code
|