Skip to content

Commit

Permalink
Fixed removing trees on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
coordt committed May 5, 2024
1 parent 9d14ae4 commit 98aa8e1
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 11 deletions.
7 changes: 3 additions & 4 deletions cookie_composer/git_commands.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Functions for using git."""

import logging
import shutil
import subprocess
import tempfile
from contextlib import contextmanager
Expand All @@ -11,7 +10,7 @@
from git import GitCommandError, InvalidGitRepositoryError, NoSuchPathError, Repo

from cookie_composer.exceptions import GitError
from cookie_composer.utils import echo
from cookie_composer.utils import echo, remove_single_path

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -242,6 +241,6 @@ def temp_git_worktree_dir(
raise GitError(f"Could not create a worktree for {repo_path}") from e
finally:
# Clean up the temporary working directory.
shutil.rmtree(worktree_path)
shutil.rmtree(tmp_dir)
remove_single_path(worktree_path)
remove_single_path(tmp_dir)
repo.git.worktree("prune")
4 changes: 2 additions & 2 deletions cookie_composer/templates/git_repo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Utility functions for handling and fetching repo archives in git format."""

import logging
import shutil
from pathlib import Path
from typing import Optional

Expand All @@ -10,6 +9,7 @@

from cookie_composer.git_commands import checkout_ref, clone, get_repo
from cookie_composer.templates.types import Locality, TemplateFormat, TemplateRepo
from cookie_composer.utils import remove_single_path

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -80,6 +80,6 @@ def get_cached_remote(git_uri: str, cache_dir: Path, checkout: Optional[str] = N
repo = clone(git_uri, repo_dir)
if repo.head.is_detached and repo.head.object.hexsha != checkout:
logger.info("The cached repo is not on the expected checkout, deleting and re-cloning.")
shutil.rmtree(repo_dir)
remove_single_path(repo_dir)
repo = clone(git_uri, repo_dir)
return repo
7 changes: 4 additions & 3 deletions cookie_composer/templates/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""

import json
import shutil
import tempfile
from collections import OrderedDict
from contextlib import contextmanager
Expand All @@ -14,6 +13,8 @@
from pathlib import Path
from typing import Iterator, Optional

from cookie_composer.utils import remove_single_path


class Locality(str, Enum):
"""The locality of a template."""
Expand Down Expand Up @@ -121,7 +122,7 @@ def render_source(self, output_dir: Optional[Path] = None, commit: Optional[str]

zip_dir = extract_zipfile(self.cached_source, output_dir=output_dir, password=self.password)
yield zip_dir
shutil.rmtree(zip_dir)
remove_single_path(zip_dir)
elif self.format == TemplateFormat.PLAIN:
from cookie_composer.utils import temporary_copy

Expand All @@ -144,7 +145,7 @@ class Template:
def cleanup(self) -> None:
"""Remove the cached template if it is a Zipfile."""
if self.repo.format == TemplateFormat.ZIP and self.repo.cached_source.exists():
shutil.rmtree(self.repo.cached_source)
remove_single_path(self.repo.cached_source)

@property
def name(self) -> str:
Expand Down
7 changes: 6 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
"""Testing configuration."""

import shutil
import sys
from pathlib import Path

import pytest
from git import Actor, Repo

from cookie_composer.utils import remove_single_path

skip_if_windows = pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows yet")


@pytest.fixture(autouse=True)
def isolated_filesystem(monkeypatch, tmp_path):
Expand Down Expand Up @@ -62,7 +67,7 @@ def default_origin(tmp_path: Path) -> Repo:
tmp_repo.remotes.origin.push("remote-branch")
tmp_repo.remotes.origin.push("v1.0.0")

shutil.rmtree(tmp_repo_path)
remove_single_path(tmp_repo_path)
return origin


Expand Down
5 changes: 5 additions & 0 deletions tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Optional

from cookie_composer import authentication
from tests.conftest import skip_if_windows


class MockPath:
Expand Down Expand Up @@ -59,6 +60,7 @@ def create_mock_path(contents: str = "") -> MockPath:
return MockPath(contents=contents)


@skip_if_windows
def test_get_hosts_file(mocker):
"""Getting the host file should return the correct path."""
mocker.patch(
Expand All @@ -71,6 +73,7 @@ def test_get_hosts_file(mocker):
assert path.read_text() == "{}"


@skip_if_windows
def test_login_to_svc_with_no_hosts_file(mocker):
"""Login to the service with no hosts file."""
mocked_path = create_mock_path("")
Expand All @@ -94,6 +97,7 @@ def test_login_to_svc_with_no_hosts_file(mocker):
}


@skip_if_windows
def test_get_cached_token(mocker):
"""When the cached token exists, it gets returned."""
mocked_path = create_mock_path(
Expand All @@ -104,6 +108,7 @@ def test_get_cached_token(mocker):
assert authentication.get_cached_token("idon'texist") is None


@skip_if_windows
def test_add_auth_to_url(mocker):
"""A host with a cached token returns a new URL."""
mocked_path = create_mock_path(
Expand Down
3 changes: 2 additions & 1 deletion tests/test_commands/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from cookie_composer.commands import update
from cookie_composer.git_commands import checkout_branch, get_repo
from cookie_composer.templates.types import TemplateFormat, Locality
from cookie_composer.utils import remove_single_path


@pytest.fixture(scope="module")
Expand All @@ -36,7 +37,7 @@ def git_template(fixtures_path: Path, tmp_path_factory) -> dict:
origin = Repo.init(origin_path, bare=True)
tmp_repo.create_remote("origin", str(origin_path))
tmp_repo.remotes.origin.push("master")
shutil.rmtree(tmp_repo_path)
remove_single_path(tmp_repo_path)

template_updated_sha = origin.heads.master.commit.hexsha
template_initial_sha = origin.heads.master.commit.parents[0].hexsha
Expand Down

0 comments on commit 98aa8e1

Please sign in to comment.