Skip to content

Commit

Permalink
Update package sync tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simu committed Aug 24, 2022
1 parent 04390e2 commit d9777aa
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
16 changes: 13 additions & 3 deletions tests/test_cli_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os

from pathlib import Path
from typing import Any, Iterable
from typing import Any, Iterable, Type
from unittest import mock

import pytest
Expand All @@ -13,6 +13,8 @@

from commodore import cli
from commodore.config import Config
from commodore.package import Package
from commodore.package.template import PackageTemplater
from test_catalog import cluster_resp

from conftest import RunnerFunc
Expand Down Expand Up @@ -377,7 +379,7 @@ def test_catalog_compile_cli(
mock_login.assert_called()


@mock.patch.object(cli, "sync_packages")
@mock.patch.object(cli, "sync_dependencies")
@pytest.mark.parametrize("ghtoken", [None, "ghp_fake-token"])
def test_package_sync_cli(
mock_sync_packages, ghtoken, tmp_path: Path, cli_runner: RunnerFunc
Expand All @@ -391,13 +393,21 @@ def test_package_sync_cli(
yaml.safe_dump(["projectsyn/package-foo"], f)

def sync_pkgs(
config, pkglist: Path, dry_run: bool, pr_branch: str, pr_labels: Iterable[str]
config,
pkglist: Path,
dry_run: bool,
pr_branch: str,
pr_labels: Iterable[str],
deptype: Type,
templater: Type,
):
assert config.github_token == ghtoken
assert pkglist.absolute() == pkg_list.absolute()
assert not dry_run
assert pr_branch == "template-sync"
assert list(pr_labels) == []
assert deptype == Package
assert templater == PackageTemplater

mock_sync_packages.side_effect = sync_pkgs
result = cli_runner(["package", "sync", "pkgs.yaml"])
Expand Down
46 changes: 34 additions & 12 deletions tests/test_package_sync.py → tests/test_dependency_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
from commodore.config import Config
from commodore.gitrepo import GitRepo
from commodore.package import Package
from commodore.package import sync
from commodore.package.template import PackageTemplater

from commodore import dependency_syncer

DATA_DIR = Path(__file__).parent.absolute() / "testdata" / "github"

Expand Down Expand Up @@ -60,7 +62,7 @@ def test_ensure_branch(tmp_path: Path, config: Config, sync_branch: str):

assert any(h.name == "template-sync" for h in r.heads) == (sync_branch == "local")

sync.ensure_branch(p, "template-sync")
dependency_syncer.ensure_branch(p, "template-sync")

hs = [h for h in r.heads if h.name == "template-sync"]
assert len(hs) == 1
Expand Down Expand Up @@ -200,12 +202,14 @@ def test_ensure_pr(tmp_path: Path, config: Config, dry_run: bool, pr_exists: boo
config.github_token = "ghp_fake-token"
p = Package.clone(config, f"file://{tmp_path}/foo.git", "foo")
pname = "projectsyn/package-foo"
sync.ensure_branch(p, "template-sync")
dependency_syncer.ensure_branch(p, "template-sync")

gh = github.Github(config.github_token)
gr = gh.get_repo(pname)

msg = sync.ensure_pr(p, pname, gr, dry_run, "template-sync", ["template-sync"])
msg = dependency_syncer.ensure_pr(
p, pname, gr, dry_run, "template-sync", ["template-sync"]
)

cu = "update" if pr_exists else "create"

Expand Down Expand Up @@ -240,12 +244,12 @@ def test_ensure_pr_no_permission(tmp_path: Path, config: Config, pr_exists: bool
config.github_token = "ghp_fake-token"
p = Package.clone(config, f"file://{tmp_path}/foo.git", "foo")
pname = "projectsyn/package-foo"
sync.ensure_branch(p, "template-sync")
dependency_syncer.ensure_branch(p, "template-sync")

gh = github.Github(config.github_token)
gr = gh.get_repo(pname)

msg = sync.ensure_pr(p, pname, gr, False, "template-sync", [])
msg = dependency_syncer.ensure_pr(p, pname, gr, False, "template-sync", [])

cu = "update" if pr_exists else "create"
assert (
Expand Down Expand Up @@ -274,7 +278,15 @@ def test_sync_packages_package_list_parsing(
f.write(package_list_contents)

with pytest.raises(click.ClickException) as exc:
sync.sync_packages(config, pkg_list, False, "template-sync", [])
dependency_syncer.sync_dependencies(
config,
pkg_list,
False,
"template-sync",
[],
Package,
PackageTemplater,
)

if ghtoken is None:
assert str(exc.value) == "Can't continue, missing GitHub API token."
Expand Down Expand Up @@ -364,8 +376,14 @@ def test_sync_packages(
"commodore.dependency_templater.Templater.repo_url",
new_callable=lambda: remote_url,
):
sync.sync_packages(
config, pkg_list, dry_run, "template-sync", ["template-sync"]
dependency_syncer.sync_dependencies(
config,
pkg_list,
dry_run,
"template-sync",
["template-sync"],
Package,
PackageTemplater,
)

expected_call_count = 1
Expand Down Expand Up @@ -399,7 +417,9 @@ def test_sync_packages_skip(tmp_path: Path, config: Config, capsys):

pkg_list = create_pkg_list(tmp_path)

sync.sync_packages(config, pkg_list, True, "template-sync", [])
dependency_syncer.sync_dependencies(
config, pkg_list, True, "template-sync", [], Package, PackageTemplater
)

captured = capsys.readouterr()
assert (
Expand All @@ -420,7 +440,9 @@ def test_sync_packages_skip_missing(capsys, tmp_path: Path, config: Config):
status=404,
)

sync.sync_packages(config, pkg_list, True, "template-sync", [])
dependency_syncer.sync_dependencies(
config, pkg_list, True, "template-sync", [], Package, PackageTemplater
)

captured = capsys.readouterr()

Expand All @@ -444,4 +466,4 @@ def test_message_body(tmp_path: Path, raw_message: Union[str, bytes], expected:

c = git.Commit(r, binsha=b"\0" * 20, message=raw_message)

assert sync.message_body(c) == expected
assert dependency_syncer.message_body(c) == expected

0 comments on commit d9777aa

Please sign in to comment.