Skip to content

Commit

Permalink
Format/lint with black
Browse files Browse the repository at this point in the history
  • Loading branch information
tdurk93 committed Dec 11, 2023
1 parent adc0012 commit ec67c0a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 17 deletions.
10 changes: 8 additions & 2 deletions secureli/abstractions/pre_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,17 @@ def check_for_hook_updates(

repos_to_update: dict[str, RevisionPair] = {}
for repo_config in config.repos:
repo_config_dict = repo_config.__dict__ | {"repo": repo_config.url} # PreCommitSettings uses "url" instead of "repo", so we need to copy that value over
repo_config_dict = repo_config.__dict__ | {
"repo": repo_config.url
} # PreCommitSettings uses "url" instead of "repo", so we need to copy that value over
old_rev_info = HookRepoRevInfo.from_config(repo_config_dict)
# if the revision currently specified in .pre-commit-config.yaml looks like a full git SHA
# (40-character hex string), then set freeze to True
freeze = bool(git_commit_sha_pattern.fullmatch(repo_config.rev)) if freeze is None else freeze
freeze = (
bool(git_commit_sha_pattern.fullmatch(repo_config.rev))
if freeze is None
else freeze
)
new_rev_info = old_rev_info.update(tags_only=tags_only, freeze=freeze)
revisions = RevisionPair(oldRev=old_rev_info.rev, newRev=new_rev_info.rev)
if revisions.oldRev != revisions.newRev:
Expand Down
63 changes: 48 additions & 15 deletions tests/abstractions/test_pre_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
test_folder_path = Path("does-not-matter")
example_git_sha = "a" * 40


@pytest.fixture()
def settings_dict() -> dict:
return PreCommitSettings(
Expand Down Expand Up @@ -305,14 +306,22 @@ def test_pre_commit_config_file_is_deserialized_correctly(
assert pre_commit_config.repos[0].hooks[0].id == "detect-secrets"


@pytest.mark.parametrize(argnames=["rev", "rev_is_sha"], argvalues=[("tag1", False), (example_git_sha, True)])
@pytest.mark.parametrize(
argnames=["rev", "rev_is_sha"], argvalues=[("tag1", False), (example_git_sha, True)]
)
def test_check_for_hook_updates_infers_freeze_param_when_not_provided(
pre_commit: PreCommitAbstraction,
rev: str,
rev_is_sha: bool,
):
with um.patch("secureli.abstractions.pre_commit.HookRepoRevInfo.from_config") as mock_hook_repo_rev_info:
pre_commit_config_repo = PreCommitRepo(repo="http://example-repo.com/", rev=rev, hooks=[PreCommitHook(id="hook-id")])
with um.patch(
"secureli.abstractions.pre_commit.HookRepoRevInfo.from_config"
) as mock_hook_repo_rev_info:
pre_commit_config_repo = PreCommitRepo(
repo="http://example-repo.com/",
rev=rev,
hooks=[PreCommitHook(id="hook-id")],
)
pre_commit_config = PreCommitSettings(repos=[pre_commit_config_repo])
rev_info_mock = MagicMock(rev=pre_commit_config_repo.rev)
mock_hook_repo_rev_info.return_value = rev_info_mock
Expand All @@ -322,14 +331,20 @@ def test_check_for_hook_updates_infers_freeze_param_when_not_provided(


def test_check_for_hook_updates_respects_freeze_param_when_false(
pre_commit: PreCommitAbstraction,
pre_commit: PreCommitAbstraction,
):
"""
When freeze is explicitly provided, the rev_info.update() method respect that value
regardless of whether the existing rev is a tag or a commit hash.
"""
with um.patch("secureli.abstractions.pre_commit.HookRepoRevInfo.from_config") as mock_hook_repo_rev_info:
pre_commit_config_repo = PreCommitRepo(repo="http://example-repo.com/", rev=example_git_sha, hooks=[PreCommitHook(id="hook-id")])
with um.patch(
"secureli.abstractions.pre_commit.HookRepoRevInfo.from_config"
) as mock_hook_repo_rev_info:
pre_commit_config_repo = PreCommitRepo(
repo="http://example-repo.com/",
rev=example_git_sha,
hooks=[PreCommitHook(id="hook-id")],
)
pre_commit_config = PreCommitSettings(repos=[pre_commit_config_repo])
rev_info_mock = MagicMock(rev=pre_commit_config_repo.rev)
mock_hook_repo_rev_info.return_value = rev_info_mock
Expand All @@ -338,12 +353,17 @@ def test_check_for_hook_updates_respects_freeze_param_when_false(
rev_info_mock.update.assert_called_with(tags_only=True, freeze=False)



def test_check_for_hook_updates_respects_freeze_param_when_true(
pre_commit: PreCommitAbstraction,
pre_commit: PreCommitAbstraction,
):
with um.patch("secureli.abstractions.pre_commit.HookRepoRevInfo.from_config") as mock_hook_repo_rev_info:
pre_commit_config_repo = PreCommitRepo(repo="http://example-repo.com/", rev="tag1", hooks=[PreCommitHook(id="hook-id")])
with um.patch(
"secureli.abstractions.pre_commit.HookRepoRevInfo.from_config"
) as mock_hook_repo_rev_info:
pre_commit_config_repo = PreCommitRepo(
repo="http://example-repo.com/",
rev="tag1",
hooks=[PreCommitHook(id="hook-id")],
)
pre_commit_config = PreCommitSettings(repos=[pre_commit_config_repo])
rev_info_mock = MagicMock(rev=pre_commit_config_repo.rev)
mock_hook_repo_rev_info.return_value = rev_info_mock
Expand All @@ -353,19 +373,32 @@ def test_check_for_hook_updates_respects_freeze_param_when_true(


def test_check_for_hook_updates_returns_repos_with_new_revs(
pre_commit: PreCommitAbstraction,
pre_commit: PreCommitAbstraction,
):
with um.patch("secureli.abstractions.pre_commit.HookRepoRevInfo") as mock_hook_repo_rev_info:
with um.patch(
"secureli.abstractions.pre_commit.HookRepoRevInfo"
) as mock_hook_repo_rev_info:
repo_urls = ["http://example-repo.com/", "http://example-repo-2.com/"]
old_rev = "tag1"
repo_1_new_rev = "tag2"
pre_commit_config = PreCommitSettings(repos=[PreCommitRepo(repo=repo_url, rev=old_rev, hooks=[PreCommitHook(id="hook-id")]) for repo_url in repo_urls])
pre_commit_config = PreCommitSettings(
repos=[
PreCommitRepo(
repo=repo_url, rev=old_rev, hooks=[PreCommitHook(id="hook-id")]
)
for repo_url in repo_urls
]
)
repo_1_old_rev_mock = MagicMock(rev=old_rev, repo=repo_urls[0])
repo_1_new_rev_mock = MagicMock(rev=repo_1_new_rev, repo=repo_urls[0])
repo_2_old_rev_mock = MagicMock(rev=old_rev, repo=repo_urls[1])
mock_hook_repo_rev_info.from_config = MagicMock(side_effect=[repo_1_old_rev_mock, repo_2_old_rev_mock])
mock_hook_repo_rev_info.from_config = MagicMock(
side_effect=[repo_1_old_rev_mock, repo_2_old_rev_mock]
)
repo_1_old_rev_mock.update.return_value = repo_1_new_rev_mock
repo_2_old_rev_mock.update.return_value = repo_2_old_rev_mock # this update should return the same rev info
repo_2_old_rev_mock.update.return_value = (
repo_2_old_rev_mock # this update should return the same rev info
)
updated_repos = pre_commit.check_for_hook_updates(pre_commit_config)
assert len(updated_repos) == 1 # only the first repo should be returned
assert updated_repos[repo_urls[0]].oldRev == "tag1"
Expand Down

0 comments on commit ec67c0a

Please sign in to comment.