Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
coordt committed Oct 15, 2023
1 parent 0fefdbc commit 5d3c51f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 43 deletions.
46 changes: 9 additions & 37 deletions tests/templates/test_zipfile_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ def test_unzip_local_file(mocker, fixtures_path: Path, tmp_path: Path):
"cookie_composer.templates.zipfile_repo.prompt_and_delete", return_value=True, autospec=True
)
zipfile_path = fixtures_path.joinpath("fake-repo-tmpl.zip")
output_dir = zipfile_repo.unzip(str(zipfile_path), is_remote=False, cache_dir=tmp_path)

assert str(output_dir).startswith(tempfile.gettempdir())
output_dir = zipfile_repo.cache_source(str(zipfile_path), is_remote=False, cache_dir=tmp_path)
assert str(output_dir).startswith(str(fixtures_path))
assert not mock_prompt_and_delete.called


Expand All @@ -53,60 +52,33 @@ def test_unzip_local_protected_file(mocker, fixtures_path: Path, tmp_path: Path)
"cookie_composer.templates.zipfile_repo.prompt_and_delete", return_value=True, autospec=True
)
zipfile_path = fixtures_path.joinpath("protected-fake-repo-tmpl.zip")
output_dir = zipfile_repo.unzip(str(zipfile_path), is_remote=False, cache_dir=tmp_path, password="sekrit")
output_dir = zipfile_repo.cache_source(str(zipfile_path), is_remote=False, cache_dir=tmp_path)

assert str(output_dir).startswith(tempfile.gettempdir())
assert str(output_dir).startswith(str(fixtures_path))
assert not mock_prompt_and_delete.called


def test_extract_protected_local_file_environment_password(fixtures_path: Path):
"""In `extract_zipfile()`, the environment can be used to provide a repo password."""
zipfile_path = fixtures_path.joinpath("protected-fake-repo-tmpl.zip")
output_dir = zipfile_repo.extract_zipfile(zipfile_path, no_input=True, password="sekrit")
output_dir = zipfile_repo.extract_zipfile(zipfile_path, password="sekrit")

assert str(output_dir).startswith(tempfile.gettempdir())


def test_extract_protected_local_file_bad_environment_password(mocker, fixtures_path: Path):
"""In `extract_zipfile()`, an error occurs if the environment has a bad password."""
mock_prompt_and_delete = mocker.patch(
"cookie_composer.templates.zipfile_repo.read_repo_password", return_value="still-wrong"
)
zipfile_path = fixtures_path.joinpath("protected-fake-repo-tmpl.zip")
with pytest.raises(InvalidZipPasswordError):
zipfile_repo.extract_zipfile(
zipfile_path,
no_input=False,
password="not-the-right-password",
)
zipfile_repo.extract_zipfile(zipfile_path, password="not-the-right-password")


def test_extract_protected_local_file_user_password_with_noinput(fixtures_path: Path):
"""Can't unpack a password-protected repo in no_input mode."""

zipfile_path = fixtures_path.joinpath("protected-fake-repo-tmpl.zip")
with pytest.raises(InvalidZipPasswordError):
zipfile_repo.extract_zipfile(
zipfile_path,
no_input=True,
password=None,
)


def test_extract_protected_local_file_user_password(mocker, fixtures_path: Path):
"""A password-protected local file reference can be unzipped."""
mock_read_repo_password = mocker.patch(
"cookie_composer.templates.zipfile_repo.read_repo_password", return_value="sekrit"
)
zipfile_path = fixtures_path.joinpath("protected-fake-repo-tmpl.zip")
output_dir = zipfile_repo.extract_zipfile(
zipfile_path,
no_input=False,
password=None,
)

assert str(output_dir).startswith(tempfile.gettempdir())
assert mock_read_repo_password.called
zipfile_repo.extract_zipfile(zipfile_path, password=None)


def test_validate_empty_zip_file(fixtures_path: Path):
Expand Down Expand Up @@ -286,8 +258,8 @@ def test_template_repo_from_zipfile(fixtures_path: Path, tmp_path: Path):

assert template_repo.source == str(zipfile_path)
assert template_repo.cached_source.exists()
assert template_repo.cached_source.is_dir()
assert template_repo.cached_source.name == "fake-repo-tmpl"
assert template_repo.cached_source.is_file()
assert template_repo.cached_source.name == "fake-repo-tmpl.zip"
assert template_repo.format == "zip"
assert template_repo.locality == Locality.LOCAL
assert template_repo.checkout is None
Expand Down
5 changes: 4 additions & 1 deletion tests/test_commands/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def test_update_command(git_project: dict, mocker):
mocker.patch("cookie_composer.templates.source.identify_repo", return_value=(TemplateFormat.GIT, Locality.REMOTE))
repo = get_repo(git_project["project_path"])
assert repo.active_branch.name == "master"

current_items = {item.name for item in os.scandir(git_project["project_path"])}
assert current_items == {
".composition.yaml",
Expand Down Expand Up @@ -159,9 +160,11 @@ def test_update_command(git_project: dict, mocker):
assert repo.active_branch.commit.hexsha == previous_sha


def test_update_command_no_update_required(git_project: dict, capsys):
def test_update_command_no_update_required(git_project: dict, mocker, capsys):
"""Should output a message and do nothing."""
mocker.patch("cookie_composer.templates.source.identify_repo", return_value=(TemplateFormat.GIT, Locality.REMOTE))
repo = get_repo(git_project["project_path"])
assert repo.active_branch.name == "master"

# rewrite the composition to change the commit to the latest of the template
rendered_comp_path = Path(git_project["project_path"] / ".composition.yaml")
Expand Down
27 changes: 26 additions & 1 deletion tests/test_git_commands.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Tests for cookie_composer.git_commands."""
from pathlib import Path
from typing import Optional

import pytest
from git import Actor, Repo
from pytest import param
from git import Repo

from cookie_composer import git_commands
from cookie_composer.exceptions import GitError
Expand Down Expand Up @@ -84,3 +86,26 @@ def test_clone_existing_repo(default_repo):
repo_path = Path(default_repo.working_tree_dir)
repo = git_commands.clone("git://someplace/else.git", repo_path)
assert repo.working_tree_dir == str(repo_path)


@pytest.mark.parametrize(
["checkout", "commit", "expected_ref"],
[
param(None, None, "master", id="default"),
param("remote-branch", None, "remote-branch", id="checkout branch"),
param("v1.0.0", None, "v1.0.0", id="checkout tag"),
param(None, "HEAD~1", "HEAD~1", id="checkout commit"),
],
)
def test_temp_git_worktree_dir(
default_origin: Repo, tmp_path: Path, checkout: Optional[str], commit: Optional[str], expected_ref: str
):
"""Should return the path to the worktree and check out the appropriate ref."""

dest_path = tmp_path / "dest"
with git_commands.temp_git_worktree_dir(
Path(default_origin.working_dir), dest_path, branch=checkout, commit=commit
) as wtree_dir:
assert wtree_dir == dest_path
r = git_commands.get_repo(wtree_dir)
assert r.head.commit.hexsha == default_origin.commit(expected_ref).hexsha
9 changes: 5 additions & 4 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest
from pytest import param

from cookie_composer.composition import get_context_for_layer
from cookie_composer import utils
from cookie_composer.io import read_rendered_composition

Expand All @@ -12,18 +13,18 @@ def test_get_context_for_layer(fixtures_path: Path):
"""Return a context for a given layer."""
rendered_comp = read_rendered_composition(fixtures_path / "rendered_composition.yaml")

result1 = utils.get_context_for_layer(rendered_comp, 0)
result1 = get_context_for_layer(rendered_comp, 0)
assert result1 == rendered_comp.layers[0].rendered_context

result2 = utils.get_context_for_layer(rendered_comp, 1)
result2 = get_context_for_layer(rendered_comp, 1)
assert "project_slug" in result2
assert len(result2) == len(result1) + 1

result3 = utils.get_context_for_layer(rendered_comp, 2)
result3 = get_context_for_layer(rendered_comp, 2)
assert "_docs_requirements" in result3
assert len(result3) == len(result2) + 1

result4 = utils.get_context_for_layer(rendered_comp)
result4 = get_context_for_layer(rendered_comp)
assert result4 == result3


Expand Down

0 comments on commit 5d3c51f

Please sign in to comment.