Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simu committed Dec 30, 2024
1 parent 14b0a66 commit 7e68bcc
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 15 deletions.
48 changes: 44 additions & 4 deletions tests/test_cli_component.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from __future__ import annotations

import json
import os

from collections.abc import Iterable
from datetime import timedelta
from pathlib import Path
from typing import Type
from typing import Optional, Type
from unittest import mock

import pytest
Expand All @@ -17,6 +18,7 @@
import commodore.cli.component as component

from conftest import RunnerFunc
from test_component_template import call_component_new


@pytest.mark.parametrize("repo_dir", [False, True])
Expand Down Expand Up @@ -50,10 +52,41 @@ def _compile(cfg, path, alias, values, search_paths, output, name):
)


@pytest.mark.parametrize("template_version", [None, "main^"])
def test_update_component_cli(tmp_path, cli_runner, template_version):
cpath = tmp_path / "test-component"
call_component_new(tmp_path, cli_runner, output_dir=tmp_path)

template_arg = (
[f"--template-version={template_version}"]
if template_version is not None
else []
)

result = cli_runner(["component", "update", str(cpath)] + template_arg)

assert result.exit_code == 0
if template_version is not None:
with open(cpath / ".cruft.json", "r", encoding="utf-8") as cruftjson:
cruft = json.load(cruftjson)
assert cruft["checkout"] == template_version


@mock.patch.object(component, "sync_dependencies")
@pytest.mark.parametrize("ghtoken", [None, "ghp_fake-token"])
@pytest.mark.parametrize(
"ghtoken,template_version",
[
(None, None),
("ghp_fake-token", None),
("ghp_fake-token", "custom-template-version"),
],
)
def test_component_sync_cli(
mock_sync_dependencies, ghtoken, tmp_path: Path, cli_runner: RunnerFunc
mock_sync_dependencies,
ghtoken,
template_version,
tmp_path: Path,
cli_runner: RunnerFunc,
):
os.chdir(tmp_path)
if ghtoken is not None:
Expand All @@ -74,6 +107,7 @@ def sync_deps(
pr_batch_size: int,
github_pause: int,
filter: str,
tmpl_version: Optional[str],
):
assert config.github_token == ghtoken
assert deplist.absolute() == dep_list.absolute()
Expand All @@ -85,7 +119,13 @@ def sync_deps(
assert pr_batch_size == 10
assert github_pause == timedelta(seconds=120)
assert filter == ""
assert tmpl_version == template_version

mock_sync_dependencies.side_effect = sync_deps
result = cli_runner(["component", "sync", "deps.yaml"])
template_version_flag = (
[f"--template-version={template_version}"]
if template_version is not None
else []
)
result = cli_runner(["component", "sync", "deps.yaml"] + template_version_flag)
assert result.exit_code == (1 if ghtoken is None else 0)
48 changes: 44 additions & 4 deletions tests/test_cli_package.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from __future__ import annotations

import json
import os

from collections.abc import Iterable
from datetime import timedelta
from pathlib import Path
from typing import Type
from typing import Optional, Type
from unittest import mock

import pytest
Expand All @@ -16,12 +17,44 @@
from commodore.package.template import PackageTemplater

from conftest import RunnerFunc
from test_package_template import call_package_new


@pytest.mark.parametrize("template_version", [None, "main^"])
def test_update_package_cli(tmp_path, cli_runner, template_version):
ppath = tmp_path / "test-package"
call_package_new(tmp_path, cli_runner, output_dir=f"--output-dir={tmp_path}")

template_arg = (
[f"--template-version={template_version}"]
if template_version is not None
else []
)

result = cli_runner(["package", "update", str(ppath)] + template_arg)

assert result.exit_code == 0
if template_version is not None:
with open(ppath / ".cruft.json", "r", encoding="utf-8") as cruftjson:
cruft = json.load(cruftjson)
assert cruft["checkout"] == template_version


@mock.patch.object(package, "sync_dependencies")
@pytest.mark.parametrize("ghtoken", [None, "ghp_fake-token"])
@pytest.mark.parametrize(
"ghtoken,template_version",
[
(None, None),
("ghp_fake-token", None),
("ghp_fake-token", "custom-template-version"),
],
)
def test_package_sync_cli(
mock_sync_packages, ghtoken, tmp_path: Path, cli_runner: RunnerFunc
mock_sync_packages,
ghtoken,
template_version,
tmp_path: Path,
cli_runner: RunnerFunc,
):
os.chdir(tmp_path)
if ghtoken is not None:
Expand All @@ -42,6 +75,7 @@ def sync_pkgs(
pr_batch_size: int,
github_pause: int,
filter: str,
tmpl_version: Optional[str],
):
assert config.github_token == ghtoken
assert pkglist.absolute() == pkg_list.absolute()
Expand All @@ -53,8 +87,14 @@ def sync_pkgs(
assert pr_batch_size == 10
assert github_pause == timedelta(seconds=120)
assert filter == ""
assert tmpl_version == template_version

mock_sync_packages.side_effect = sync_pkgs
result = cli_runner(["package", "sync", "pkgs.yaml"])
template_version_flag = (
[f"--template-version={template_version}"]
if template_version is not None
else []
)
result = cli_runner(["package", "sync", "pkgs.yaml"] + template_version_flag)
print(result.stdout)
assert result.exit_code == (1 if ghtoken is None else 0)
19 changes: 12 additions & 7 deletions tests/test_dependency_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import json

from pathlib import Path
from typing import Union
from typing import Optional, Union
from unittest.mock import patch, MagicMock

import click
Expand Down Expand Up @@ -377,18 +377,21 @@ def test_sync_packages_package_list_parsing(


@pytest.mark.parametrize(
"dry_run,second_pkg,needs_update",
"dry_run,second_pkg,needs_update,template_version",
[
# no dry-run, no 2nd package, update required
(False, False, True),
(False, False, True, None),
# no dry-run, no 2nd package, no update required
(False, False, False),
(False, False, False, None),
# no dry-run, 2nd package, update required
(False, True, True),
(False, True, True, None),
# dry-run, no 2nd package, no update required
(True, False, False),
(True, False, False, None),
# dry-run, no 2nd package, update required
(True, False, True),
(True, False, True, None),
# no dry-run, no 2nd package, don't force update, custom version -- should
# require update but will force dry-run
(False, False, False, "main"),
],
)
@responses.activate
Expand All @@ -401,6 +404,7 @@ def test_sync_packages(
dry_run: bool,
second_pkg: bool,
needs_update: bool,
template_version: Optional[str],
):
config.github_token = "ghp_fake-token"
responses.add_passthru("https://github.com")
Expand Down Expand Up @@ -471,6 +475,7 @@ def _maybe_pause(updated: int, pr_batch_size: int, pause: datetime.timedelta):
PackageTemplater,
1,
datetime.timedelta(seconds=10),
template_version=template_version,
)

if needs_update and not dry_run and second_pkg:
Expand Down

0 comments on commit 7e68bcc

Please sign in to comment.