Skip to content

Commit

Permalink
Remove test reliance on unmaintained pytest-git (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep authored Jul 30, 2024
1 parent 5844892 commit d87bb16
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 29 deletions.
31 changes: 19 additions & 12 deletions scripts/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,33 @@ dependencies = [
[project.optional-dependencies]
test = [
"pytest",
"pytest-git",
"pytest-network",
"pytest-socket",
"pytest-responsemock",
]

[project.scripts]
send-cruft-prs = "scverse_template_scripts.cruft_prs:cli"
make-rich-output = "scverse_template_scripts.make_rich_output:main"

[tool.hatch.build.targets.wheel]
packages = ["src/testing", "src/scverse_template_scripts"]

[tool.hatch.version]
source = "vcs"
fallback-version = "0.0"

[tool.hatch.envs.default]
python = "3.11"

[tool.hatch.envs.test]
[tool.hatch.envs.hatch-test]
features = ["test"]
[tool.hatch.envs.test.scripts]
run = "pytest -v {args}"

[tool.pytest.ini_options]
addopts = [
"--import-mode=importlib",
"-pgit",
"-pnetwork", "--disable-network",
"-presponsemock"
"--disable-socket",
"-presponsemock",
"-ptesting.scverse_template_scripts._pytest"
]
filterwarnings = [
"error",
Expand Down Expand Up @@ -93,20 +93,27 @@ select = [
"RUF",
"S",
"T",
"TCH",
"TID",
"UP",
"W",
"YTT",
]
ignore = [
"S101", # assert should be allowed
"S603", # subprocess with shell=False should be allowed
"S311", # we don’t need cryptographically secure RNG
"S101", # assert should be allowed
"S603", # subprocess with shell=False should be allowed
"S311", # we don’t need cryptographically secure RNG
"ISC001", # conflicts with formatter
]
unfixable = ["RUF001"] # never “fix” “confusables”

[tool.ruff.lint.isort]
known-first-party = ["scverse_template_scripts"]
known-first-party = ["scverse_template_scripts", "testing.scverse_template_scripts"]
required-imports = ["from __future__ import annotations"]

[tool.ruff.lint.flake8-type-checking]
exempt-modules = []
strict = true

[tool.ruff.lint.per-file-ignores]
"tests/*.py" = [
Expand Down
11 changes: 8 additions & 3 deletions scripts/src/scverse_template_scripts/backoff.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from __future__ import annotations

import random
import time
from collections.abc import Callable
from typing import TypeVar
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from collections.abc import Callable
from typing import TypeVar

T = TypeVar("T")
T = TypeVar("T")


def retry_with_backoff(
Expand Down
27 changes: 19 additions & 8 deletions scripts/src/scverse_template_scripts/cruft_prs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,38 @@
Uses `template-repos.yml` from `scverse/ecosystem-packages`.
"""

from __future__ import annotations

import math
import os
import sys
from collections.abc import Generator
from dataclasses import InitVar, dataclass, field
from logging import basicConfig, getLogger
from pathlib import Path
from subprocess import CompletedProcess, run
from subprocess import run
from tempfile import TemporaryDirectory
from typing import IO, ClassVar, LiteralString, NotRequired, TypedDict, cast
from typing import TYPE_CHECKING, ClassVar, TypedDict, cast

import typer
from furl import furl
from git.exc import GitCommandError
from git.repo import Repo
from git.util import Actor
from github import ContentFile, Github, UnknownObjectException
from github.GitRelease import GitRelease as GHRelease
from github.NamedUser import NamedUser
from github.PullRequest import PullRequest
from github.Repository import Repository as GHRepo
from yaml import safe_load

from .backoff import retry_with_backoff

if TYPE_CHECKING:
from collections.abc import Generator
from subprocess import CompletedProcess
from typing import IO, LiteralString, NotRequired

from github.GitRelease import GitRelease as GHRelease
from github.NamedUser import NamedUser
from github.PullRequest import PullRequest
from github.Repository import Repository as GHRepo

log = getLogger(__name__)

PR_BODY_TEMPLATE = """\
Expand Down Expand Up @@ -211,7 +218,11 @@ def get_fork(con: GitHubConnection, repo: GHRepo) -> GHRepo:
If the fork already exists it is reused.
"""
fork = repo.create_fork()
return retry_with_backoff(lambda: con.gh.get_repo(fork.id), retries=n_retries, exc_cls=UnknownObjectException)
return retry_with_backoff(
lambda: con.gh.get_repo(fork.id),
retries=n_retries,
exc_cls=UnknownObjectException,
)


def make_pr(con: GitHubConnection, release: GHRelease, repo_url: str) -> None:
Expand Down
2 changes: 2 additions & 0 deletions scripts/src/scverse_template_scripts/make_rich_output.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from rich.console import Console
from rich.markdown import Markdown

Expand Down
17 changes: 17 additions & 0 deletions scripts/src/testing/scverse_template_scripts/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Temporary directory and git fixtures"""

from __future__ import annotations

from typing import TYPE_CHECKING

from git import Repo

if TYPE_CHECKING:
from pathlib import Path


class GitRepo:
def __init__(self, workspace: Path) -> None:
self.workspace = workspace
self.api = Repo.init(self.workspace)
self.uri = f"file://{self.workspace}"
15 changes: 15 additions & 0 deletions scripts/src/testing/scverse_template_scripts/_pytest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Temporary directory and git fixtures"""

from __future__ import annotations

import pytest

from . import GitRepo


@pytest.fixture
def git_repo(
tmp_path_factory: pytest.TempPathFactory,
) -> GitRepo:
tmp_path = tmp_path_factory.mktemp("git_repo")
return GitRepo(tmp_path)
15 changes: 9 additions & 6 deletions scripts/tests/test_cruft.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
from __future__ import annotations

import json
from dataclasses import dataclass
from pathlib import Path
from typing import cast
from warnings import catch_warnings, filterwarnings
from typing import TYPE_CHECKING, cast
from warnings import catch_warnings

import pytest
from git import Commit, Diff
from github.GitRelease import GitRelease as GHRelease
from github.Repository import Repository as GHRepo
from pytest_git import GitRepo

from scverse_template_scripts.cruft_prs import PR, GitHubConnection, cruft_update

if TYPE_CHECKING:
from pathlib import Path

from testing.scverse_template_scripts import GitRepo


@dataclass
class MockGHRepo:
Expand All @@ -32,8 +37,6 @@ class MockRelease:
def con(response_mock) -> GitHubConnection:
resp = json.dumps({"login": "scverse-bot"})
with catch_warnings():
# https://github.com/idlesign/pytest-responsemock/issues/7
filterwarnings("ignore", category=ResourceWarning)
with response_mock(f"GET https://api.github.com:443/users/scverse-bot -> 200 :{resp}"):
return GitHubConnection("scverse-bot")

Expand Down

0 comments on commit d87bb16

Please sign in to comment.