From c9af266d2f87a2f44322a10aed34fec1abe33cea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jeffrey=20Garc=C3=ADa?= Date: Tue, 19 Mar 2024 23:14:20 +0100 Subject: [PATCH 1/3] Porting explosion/spaCy#12181 solution into weasel --- setup.cfg | 4 ++++ weasel/tests/cli/test_cli.py | 31 +++++++++++++++++++++++++++++++ weasel/util/git.py | 16 +++++++++++----- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/setup.cfg b/setup.cfg index 7055c0a..b43347d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -47,6 +47,10 @@ install_requires = console_scripts = weasel = weasel.cli:app +[tool:pytest] +markers = + issue: references specific issue + [mypy] ignore_missing_imports = True no_implicit_optional = True diff --git a/weasel/tests/cli/test_cli.py b/weasel/tests/cli/test_cli.py index 9e89534..bd9f59d 100644 --- a/weasel/tests/cli/test_cli.py +++ b/weasel/tests/cli/test_cli.py @@ -4,10 +4,14 @@ import pytest import srsly +from tempfile import TemporaryDirectory +from pathlib import Path + from weasel.cli.remote_storage import RemoteStorage from weasel.schemas import ProjectConfigSchema, validate from weasel.util import is_subpath_of, load_project_config, make_tempdir from weasel.util import validate_project_commands +from weasel.util import git_checkout def test_issue11235(): @@ -165,3 +169,30 @@ def test_local_remote_storage_pull_missing(): remote = RemoteStorage(d / "root", str(d / "remote")) assert remote.pull(filename, command_hash="aaaa") is None assert remote.pull(filename) is None + + +def test_project_git_dir_asset(): + with TemporaryDirectory() as d: + p = Path(d) + # Use a very small repo. + git_checkout( + "https://github.com/explosion/os-signpost.git", + "os_signpost", + p / "signpost", + branch="v0.0.3", + ) + assert os.path.isdir(p / "signpost") + + +@pytest.mark.issue(66) +def test_project_git_file_asset(): + with TemporaryDirectory() as d: + p = Path(d) + # Use a very small repo. + git_checkout( + "https://github.com/explosion/os-signpost.git", + "README.md", + p / "readme.md", + branch="v0.0.3", + ) + assert os.path.isfile(p / "readme.md") diff --git a/weasel/util/git.py b/weasel/util/git.py index d0a6ec3..3e452de 100644 --- a/weasel/util/git.py +++ b/weasel/util/git.py @@ -1,6 +1,8 @@ +import os import shutil from pathlib import Path from typing import Tuple +from tempfile import TemporaryDirectory from wasabi import msg @@ -32,16 +34,20 @@ def git_checkout( f"temporarily. To only download the files needed, make sure " f"you're using Git v2.22 or above." ) - with make_tempdir() as tmp_dir: - cmd = f"git -C {tmp_dir} clone {repo} . -b {branch}" + with TemporaryDirectory() as tmp_dir: + tmp_path = Path(tmp_dir) + cmd = f"git -C {tmp_path} clone {repo} . -b {branch}" run_command(cmd, capture=True) # We need Path(name) to make sure we also support subdirectories try: - source_path = tmp_dir / Path(subpath) - if not is_subpath_of(tmp_dir, source_path): + source_path = tmp_path / Path(subpath) + if not is_subpath_of(tmp_path, source_path): err = f"'{subpath}' is a path outside of the cloned repository." msg.fail(err, repo, exits=1) - shutil.copytree(str(source_path), str(dest)) + if os.path.isdir(source_path): + shutil.copytree(source_path, dest) + else: + shutil.copyfile(source_path, dest) except FileNotFoundError: err = f"Can't clone {subpath}. Make sure the directory exists in the repo (branch '{branch}')" msg.fail(err, repo, exits=1) From da86676418d733a40b70dd261801b99b2bddb70a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jeffrey=20Garc=C3=ADa?= Date: Wed, 20 Mar 2024 16:22:15 +0100 Subject: [PATCH 2/3] Running isort on changed files --- weasel/tests/cli/test_cli.py | 8 +++----- weasel/util/git.py | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/weasel/tests/cli/test_cli.py b/weasel/tests/cli/test_cli.py index bd9f59d..f0e3598 100644 --- a/weasel/tests/cli/test_cli.py +++ b/weasel/tests/cli/test_cli.py @@ -1,17 +1,15 @@ import os import time +from pathlib import Path +from tempfile import TemporaryDirectory import pytest import srsly -from tempfile import TemporaryDirectory -from pathlib import Path - from weasel.cli.remote_storage import RemoteStorage from weasel.schemas import ProjectConfigSchema, validate -from weasel.util import is_subpath_of, load_project_config, make_tempdir +from weasel.util import git_checkout, is_subpath_of, load_project_config, make_tempdir from weasel.util import validate_project_commands -from weasel.util import git_checkout def test_issue11235(): diff --git a/weasel/util/git.py b/weasel/util/git.py index 3e452de..195fbf5 100644 --- a/weasel/util/git.py +++ b/weasel/util/git.py @@ -1,8 +1,8 @@ import os import shutil from pathlib import Path -from typing import Tuple from tempfile import TemporaryDirectory +from typing import Tuple from wasabi import msg From 28ceb06a0f4ebccd1ec42682f20ab5b77999a356 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jeffrey=20Garc=C3=ADa?= Date: Wed, 20 Mar 2024 17:57:30 +0100 Subject: [PATCH 3/3] Replacing TemporaryDirectory with already implemented make_tempdir --- weasel/tests/cli/test_cli.py | 16 ++++++---------- weasel/util/git.py | 10 ++++------ 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/weasel/tests/cli/test_cli.py b/weasel/tests/cli/test_cli.py index f0e3598..79e1c81 100644 --- a/weasel/tests/cli/test_cli.py +++ b/weasel/tests/cli/test_cli.py @@ -1,7 +1,5 @@ import os import time -from pathlib import Path -from tempfile import TemporaryDirectory import pytest import srsly @@ -170,27 +168,25 @@ def test_local_remote_storage_pull_missing(): def test_project_git_dir_asset(): - with TemporaryDirectory() as d: - p = Path(d) + with make_tempdir() as d: # Use a very small repo. git_checkout( "https://github.com/explosion/os-signpost.git", "os_signpost", - p / "signpost", + d / "signpost", branch="v0.0.3", ) - assert os.path.isdir(p / "signpost") + assert os.path.isdir(d / "signpost") @pytest.mark.issue(66) def test_project_git_file_asset(): - with TemporaryDirectory() as d: - p = Path(d) + with make_tempdir() as d: # Use a very small repo. git_checkout( "https://github.com/explosion/os-signpost.git", "README.md", - p / "readme.md", + d / "readme.md", branch="v0.0.3", ) - assert os.path.isfile(p / "readme.md") + assert os.path.isfile(d / "readme.md") diff --git a/weasel/util/git.py b/weasel/util/git.py index 195fbf5..401d73a 100644 --- a/weasel/util/git.py +++ b/weasel/util/git.py @@ -1,7 +1,6 @@ import os import shutil from pathlib import Path -from tempfile import TemporaryDirectory from typing import Tuple from wasabi import msg @@ -34,14 +33,13 @@ def git_checkout( f"temporarily. To only download the files needed, make sure " f"you're using Git v2.22 or above." ) - with TemporaryDirectory() as tmp_dir: - tmp_path = Path(tmp_dir) - cmd = f"git -C {tmp_path} clone {repo} . -b {branch}" + with make_tempdir() as tmp_dir: + cmd = f"git -C {tmp_dir} clone {repo} . -b {branch}" run_command(cmd, capture=True) # We need Path(name) to make sure we also support subdirectories try: - source_path = tmp_path / Path(subpath) - if not is_subpath_of(tmp_path, source_path): + source_path = tmp_dir / Path(subpath) + if not is_subpath_of(tmp_dir, source_path): err = f"'{subpath}' is a path outside of the cloned repository." msg.fail(err, repo, exits=1) if os.path.isdir(source_path):