Skip to content

Commit

Permalink
fix: Update default files regex to make sure nested YAML files aren't…
Browse files Browse the repository at this point in the history
… included (#66)

* Add test to make sure tested regex matches the actual spec

* Update default files regex to make sure nested YAML files aren't included
  • Loading branch information
mattkram authored May 10, 2024
1 parent 4712cbc commit 6d0e2bd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
name: generate-renovate-annotations
description: Generate Renovate tags in environment.yml files
entry: generate-renovate-annotations
files: environment.*\.ya?ml
files: environment[\w-]*\.ya?ml
args: []
language: python
37 changes: 37 additions & 0 deletions tests/test_generate_renovate_annotations.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import re
import subprocess
from pathlib import Path
from textwrap import dedent
Expand All @@ -14,6 +15,8 @@
setup_conda_environment,
)

DEFAULT_FILES_REGEX_STRING = r"environment[\w-]*\.ya?ml"

ENVIRONMENT_YAML = dedent("""\
channels:
- defaults
Expand All @@ -31,6 +34,40 @@
""")


@pytest.fixture()
def repo_root() -> Path:
return Path(__file__).parents[1]


def test_ensure_default_files_regex_in_pre_commit_hooks_yaml_matches_tested(repo_root):
"""This test ensures that the regex tested below is the same as what is in .pre-commit-hooks.yaml."""
pre_commit_hooks_path = repo_root / ".pre-commit-hooks.yaml"
hooks = yaml.safe_load(pre_commit_hooks_path.read_text())

hook_map = {h["id"]: h for h in hooks}
hook_spec = hook_map["generate-renovate-annotations"]
files_regex = hook_spec["files"]

assert files_regex == DEFAULT_FILES_REGEX_STRING


@pytest.mark.parametrize(
"string, expected_match",
[
("requirements.txt", False),
("environment.yml", True),
("environment-dev.yml", True),
("environment.yaml", True),
("environment-dev.yaml", True),
("path/to/environment.yml", True),
("infra/environments/base.yml", False),
],
)
def test_default_files_regex(string, expected_match):
match = re.search(DEFAULT_FILES_REGEX_STRING, string)
assert bool(match) is expected_match


@pytest.fixture()
def environment_yaml():
return yaml.safe_load(ENVIRONMENT_YAML)
Expand Down

1 comment on commit 6d0e2bd

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
src/anaconda_pre_commit_hooks
   __init__.py00100% 
   add_renovate_annotations.py1151884%46–49, 60–62, 107, 141, 150, 165, 218, 224–229
tests
   test_generate_renovate_annotations.py610100% 
TOTAL1761889% 

Please sign in to comment.