-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Windows for pre-commit hook usage (#165)
Remove any OS-specific commands in order to support Windows. Several CI test jobs have been added to run all pre-commit hooks in both Linux and Windows environments, furthermore all pytest jobs have been extended to include Windows environments. The expanded test-space have revealed some weaknesses regarding path-handling, which have been fixed through the usage of pathlib.PurePosixPath. This ensures the user inputs should still match what is explained in the documentation for paths, namely that one should use POSIX (Linux) type path strings (with forward slashes (/) and excluding drives and similar). Finally, any Linux-specific commands used through invoke.context.run(), which calls the OS shell, have been removed in favor of a Pythonic approach. Note: It is still necessary that the git command can be run in the given shell where pre-commit is run. Ensure changes from testing `ci-cd setver` hooks is OK: Change target version to 0.0.0, which should never be an actual version used, meaning there is no single point this test job should suddenly not incur a "change" in the base files.
- Loading branch information
Showing
7 changed files
with
227 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
repos: | ||
- repo: . | ||
rev: HEAD | ||
hooks: | ||
- id: docs-api-reference | ||
args: | ||
- "--package-dir=ci_cd" | ||
- "--debug" | ||
- id: docs-landing-page | ||
- id: update-pyproject | ||
|
||
- repo: local | ||
hooks: | ||
- id: set-version | ||
name: Set package version | ||
entry: "ci-cd setver" | ||
language: python | ||
files: "" | ||
exclude: ^$ | ||
types: [] | ||
types_or: [] | ||
exclude_types: [] | ||
always_run: false | ||
fail_fast: false | ||
verbose: false | ||
pass_filenames: false | ||
require_serial: false | ||
description: "Sets the specified version of specified Python package." | ||
language_version: default | ||
minimum_pre_commit_version: "2.16.0" | ||
args: | ||
- "--package-dir=ci_cd" | ||
- "--version=0.0.0" | ||
- "--test" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/usr/bin/env python3 | ||
"""Run pre-commit hooks on all files in the repository. | ||
File used to test running the hooks in the CI/CD pipeline independently of the shell. | ||
""" | ||
from __future__ import annotations | ||
|
||
# import platform | ||
import subprocess # nosec | ||
import sys | ||
|
||
SUCCESSFUL_FAILURES_MAPPING = { | ||
"docs-api-reference": "The following files have been changed/added/removed:", | ||
"docs-landing-page": "The landing page has been updated.", | ||
"update-pyproject": "Successfully updated the following dependencies:", | ||
"set-version": "Bumped version for ci_cd to 0.0.0.", | ||
} | ||
|
||
|
||
def main(hook: str, options: list[str]) -> None: | ||
"""Run pre-commit hooks on all files in the repository.""" | ||
run_pre_commit = ( | ||
"pre-commit run -c .github/utils/.pre-commit-config_testing.yaml " | ||
"--all-files --verbose" | ||
) | ||
|
||
result = subprocess.run( | ||
f"{run_pre_commit} {' '.join(_ for _ in options)} {hook}", | ||
check=False, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.STDOUT, | ||
shell=True, # nosec | ||
) | ||
|
||
if result.returncode != 0: | ||
if SUCCESSFUL_FAILURES_MAPPING[hook] in result.stdout.decode(): | ||
print(f"Successfully failed {hook} hook.\n\n", flush=True) | ||
print(result.stdout.decode(), flush=True) | ||
else: | ||
sys.exit(result.stdout.decode()) | ||
print(f"Successfully ran {hook} hook.\n\n", flush=True) | ||
print(result.stdout.decode(), flush=True) | ||
|
||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) < 2: | ||
raise sys.exit("Missing arguments") | ||
|
||
# "Parse" arguments | ||
# The first argument should be the hook name | ||
if sys.argv[1] not in SUCCESSFUL_FAILURES_MAPPING: | ||
raise sys.exit( | ||
f"Invalid hook name: {sys.argv[1]}\n" | ||
"The hook name should be the first argument. Any number of hook options " | ||
"can then follow." | ||
) | ||
|
||
try: | ||
main( | ||
hook=sys.argv[1], | ||
options=sys.argv[2:] if len(sys.argv) > 2 else [], | ||
) | ||
except Exception as exc: # pylint: disable=broad-except | ||
sys.exit(str(exc)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters