Skip to content

Commit

Permalink
PR Feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Bernát Gábor <bgabor8@bloomberg.net>
  • Loading branch information
gaborbernat committed Nov 26, 2024
1 parent b7df639 commit 4613919
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 38 deletions.
2 changes: 1 addition & 1 deletion docs/changelog/2803.feature.rst
Original file line number Diff line number Diff line change
@@ -1 +1 @@
write CACHEDIR.TAG file on creation - by "user:`neilramsay`
Write CACHEDIR.TAG file on creation - by "user:`neilramsay`.
12 changes: 2 additions & 10 deletions src/virtualenv/create/creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,22 +164,14 @@ def run(self):
self.setup_ignore_vcs()

def add_cachedir_tag(self):
"""
Add a Cache Directory Tag file "CACHEDIR.TAG".
The CACHEDIR.TAG file is used by various tools to mark
a directory as cache, so that it can be handled differently.
Some backup tools look for this file to exclude the directory.
See https://bford.info/cachedir/ for more details.
"""
"""Generate a file indicating that this is not meant to be backed up."""
cachedir_tag_file = self.dest / "CACHEDIR.TAG"
if not cachedir_tag_file.exists():
cachedir_tag_text = textwrap.dedent("""
Signature: 8a477f597d28d172789f06886806bc55
# This file is a cache directory tag created by Python virtualenv.
# For information about cache directory tags, see:
# http://www.brynosaurus.com/cachedir/
# https://bford.info/cachedir/
""").strip()
cachedir_tag_file.write_text(cachedir_tag_text, encoding="utf-8")

Expand Down
33 changes: 14 additions & 19 deletions tests/integration/test_cachedir_tag.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
from __future__ import annotations

import shutil
import subprocess
import sys
from subprocess import check_output, run
from typing import TYPE_CHECKING

import pytest

from virtualenv import cli_run

if TYPE_CHECKING:
from pathlib import Path

@pytest.fixture(scope="session")
def tar_test_env(tmp_path_factory):
base_path = tmp_path_factory.mktemp("tar-cachedir-test")
cli_run(["--activators", "", "--without-pip", str(base_path / ".venv")])
yield base_path
shutil.rmtree(str(base_path))
# gtar => gnu-tar on macOS
TAR = next((target for target in ("gtar", "tar") if shutil.which(target)), None)


def compatible_is_tar_present() -> bool:
try:
tar_result = subprocess.run(args=["tar", "--help"], capture_output=True, encoding="utf-8")
return tar_result.stdout.find("--exclude-caches") > -1
except FileNotFoundError:
return False
return TAR and "--exclude-caches" in check_output(args=[TAR, "--help"], text=True)


@pytest.mark.skipif(sys.platform == "win32", reason="Windows does not have tar")
@pytest.mark.skipif(not compatible_is_tar_present(), reason="Compatible tar is not installed")
def test_cachedir_tag_ignored_by_tag(tar_test_env): # noqa: ARG001
tar_result = subprocess.run(
args=["tar", "--create", "--file", "/dev/null", "--exclude-caches", "--verbose", ".venv"],
capture_output=True,
encoding="utf-8",
)
def test_cachedir_tag_ignored_by_tag(tmp_path: Path) -> None:
venv = tmp_path / ".venv"
cli_run(["--activators", "", "--without-pip", str(venv)])

args = [TAR, "--create", "--file", "/dev/null", "--exclude-caches", "--verbose", venv.name]
tar_result = run(args=args, capture_output=True, text=True, cwd=tmp_path)
assert tar_result.stdout == ".venv/\n.venv/CACHEDIR.TAG\n"
assert tar_result.stderr == "tar: .venv/: contains a cache directory tag CACHEDIR.TAG; contents not dumped\n"
assert tar_result.stderr == f"{TAR}: .venv/: contains a cache directory tag CACHEDIR.TAG; contents not dumped\n"
15 changes: 7 additions & 8 deletions tests/unit/create/test_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,25 +227,24 @@ def list_to_str(iterable):
def test_create_cachedir_tag(tmp_path):
cachedir_tag_file = tmp_path / "CACHEDIR.TAG"
cli_run([str(tmp_path), "--without-pip", "--activators", ""])
assert (
cachedir_tag_file.read_text(encoding="utf-8")
== textwrap.dedent("""

expected = """
Signature: 8a477f597d28d172789f06886806bc55
# This file is a cache directory tag created by Python virtualenv.
# For information about cache directory tags, see:
# http://www.brynosaurus.com/cachedir/
""").strip()
)
# https://bford.info/cachedir/
"""
assert cachedir_tag_file.read_text(encoding="utf-8") == textwrap.dedent(expected).strip()


def test_create_cachedir_tag_exists(tmp_path):
def test_create_cachedir_tag_exists(tmp_path: Path) -> None:
cachedir_tag_file = tmp_path / "CACHEDIR.TAG"
cachedir_tag_file.write_text("magic", encoding="utf-8")
cli_run([str(tmp_path), "--without-pip", "--activators", ""])
assert cachedir_tag_file.read_text(encoding="utf-8") == "magic"


def test_create_cachedir_tag_exists_override(tmp_path):
def test_create_cachedir_tag_exists_override(tmp_path: Path) -> None:
cachedir_tag_file = tmp_path / "CACHEDIR.TAG"
cachedir_tag_file.write_text("magic", encoding="utf-8")
cli_run([str(tmp_path), "--without-pip", "--activators", ""])
Expand Down

0 comments on commit 4613919

Please sign in to comment.