Skip to content

wip: restructure testing #1196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

### changed

- refine activation logic and add unittest for the relevant cases instead of trying to speedrun setuptools
- refine activation logic and add unittest for the relevant cases instead of trying to speedrun setuptools

## v9.1.1

Expand Down
26 changes: 23 additions & 3 deletions testing/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from .wd_wrapper import WorkDir


def pytest_configure() -> None:
def pytest_configure(config: pytest.Config) -> None:
# 2009-02-13T23:31:30+00:00
os.environ["SOURCE_DATE_EPOCH"] = "1234567890"
os.environ["SETUPTOOLS_SCM_DEBUG"] = "1"
Expand All @@ -42,10 +42,10 @@ def pytest_report_header() -> list[str]:
# Replace everything up to and including site-packages with site::
parts = path.split("site-packages", 1)
if len(parts) > 1:
path = "site:." + parts[1]
path = "site::" + parts[1]
elif path and str(Path.cwd()) in path:
# Replace current working directory with CWD::
path = path.replace(str(Path.cwd()), "CWD:.")
path = path.replace(str(Path.cwd()), "CWD::")
res.append(f"{pkg} version {pkg_version} from {path}")
return res

Expand Down Expand Up @@ -88,8 +88,28 @@ def debug_mode() -> Iterator[DebugMode]:
yield debug_mode


def setup_git_wd(wd: WorkDir, monkeypatch: pytest.MonkeyPatch | None = None) -> WorkDir:
"""Set up a WorkDir with git initialized and configured for testing.

Note: This is a compatibility wrapper. Consider using wd.setup_git() directly.
"""
return wd.setup_git(monkeypatch)


def setup_hg_wd(wd: WorkDir) -> WorkDir:
"""Set up a WorkDir with mercurial initialized and configured for testing.

Note: This is a compatibility wrapper. Consider using wd.setup_hg() directly.
"""
return wd.setup_hg()


@pytest.fixture
def wd(tmp_path: Path) -> WorkDir:
"""Base WorkDir fixture that returns an unconfigured working directory.

Individual test modules should override this fixture to set up specific SCM configurations.
"""
target_wd = tmp_path.resolve() / "wd"
target_wd.mkdir()
return WorkDir(target_wd)
Expand Down
33 changes: 7 additions & 26 deletions testing/test_better_root_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,13 @@
from setuptools_scm._get_version_impl import _version_missing
from testing.wd_wrapper import WorkDir


def setup_git_repo(wd: WorkDir) -> WorkDir:
"""Set up a git repository for testing."""
wd("git init")
wd("git config user.email [email protected]")
wd('git config user.name "a test"')
wd.add_command = "git add ."
wd.commit_command = "git commit -m test-{reason}"
return wd


def setup_hg_repo(wd: WorkDir) -> WorkDir:
"""Set up a mercurial repository for testing."""
try:
wd("hg init")
wd.add_command = "hg add ."
wd.commit_command = 'hg commit -m test-{reason} -u test -d "0 0"'
return wd
except Exception:
pytest.skip("hg not available")
# No longer need to import setup functions - using WorkDir methods directly


def test_find_scm_in_parents_finds_git(wd: WorkDir) -> None:
"""Test that _find_scm_in_parents correctly finds git repositories in parent directories."""
# Set up git repo in root
setup_git_repo(wd)
wd.setup_git()

# Create a subdirectory structure
subdir = wd.cwd / "subproject" / "nested"
Expand All @@ -57,7 +38,7 @@ def test_find_scm_in_parents_finds_git(wd: WorkDir) -> None:
def test_find_scm_in_parents_finds_hg(wd: WorkDir) -> None:
"""Test that _find_scm_in_parents correctly finds mercurial repositories in parent directories."""
# Set up hg repo in root
setup_hg_repo(wd)
wd.setup_hg()

# Create a subdirectory structure
subdir = wd.cwd / "subproject" / "nested"
Expand Down Expand Up @@ -85,7 +66,7 @@ def test_find_scm_in_parents_returns_none(wd: WorkDir) -> None:
def test_version_missing_with_scm_in_parent(wd: WorkDir) -> None:
"""Test that _version_missing provides helpful error message when SCM is found in parent."""
# Set up git repo in root
setup_git_repo(wd)
wd.setup_git()

# Create a subdirectory structure
subdir = wd.cwd / "subproject" / "nested"
Expand Down Expand Up @@ -130,7 +111,7 @@ def test_version_missing_no_scm_found(wd: WorkDir) -> None:
def test_version_missing_with_relative_to_set(wd: WorkDir) -> None:
"""Test that when relative_to is set, we don't search parents for error messages."""
# Set up git repo in root
setup_git_repo(wd)
wd.setup_git()

# Create a subdirectory structure
subdir = wd.cwd / "subproject" / "nested"
Expand Down Expand Up @@ -161,7 +142,7 @@ def test_search_parent_directories_works_as_suggested(
) -> None:
"""Test that the suggested search_parent_directories=True solution actually works."""
# Set up git repo
setup_git_repo(wd)
wd.setup_git()
wd.commit_testfile() # Make sure there's a commit for version detection

# Create a subdirectory
Expand All @@ -182,7 +163,7 @@ def test_integration_better_error_from_nested_directory(
) -> None:
"""Integration test: get_version from nested directory should give helpful error."""
# Set up git repo
setup_git_repo(wd)
wd.setup_git()

# Create a subdirectory
subdir = wd.cwd / "subproject"
Expand Down
21 changes: 3 additions & 18 deletions testing/test_file_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,9 @@ def inwd(
) -> WorkDir:
param: str = request.param # type: ignore[attr-defined]
if param == "git":
try:
wd("git init")
except OSError:
pytest.skip("git executable not found")
wd("git config user.email [email protected]")
wd('git config user.name "a test"')
wd.add_command = "git add ."
wd.commit_command = "git commit -m test-{reason}"
wd.setup_git(monkeypatch)
elif param == "hg":
try:
wd("hg init")
except OSError:
pytest.skip("hg executable not found")
wd.add_command = "hg add ."
wd.commit_command = 'hg commit -m test-{reason} -u test -d "0 0"'
wd.setup_hg()
(wd.cwd / "file1").touch()
adir = wd.cwd / "adir"
adir.mkdir()
Expand Down Expand Up @@ -249,10 +237,7 @@ def test_archive(

@pytest.fixture
def hg_wd(wd: WorkDir, monkeypatch: pytest.MonkeyPatch) -> WorkDir:
try:
wd("hg init")
except OSError:
pytest.skip("hg executable not found")
wd.setup_hg()
(wd.cwd / "file").touch()
wd("hg add file")
monkeypatch.chdir(wd.cwd)
Expand Down
23 changes: 5 additions & 18 deletions testing/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,14 @@
from .conftest import DebugMode
from .wd_wrapper import WorkDir

pytestmark = pytest.mark.skipif(
not has_command("git", warn=False), reason="git executable not found"
)


def setup_git_wd(wd: WorkDir, monkeypatch: pytest.MonkeyPatch | None = None) -> WorkDir:
"""Set up a WorkDir with git initialized and configured for testing."""
if monkeypatch:
monkeypatch.delenv("HOME", raising=False)
wd("git init")
wd("git config user.email [email protected]")
wd('git config user.name "a test"')
wd.add_command = "git add ."
wd.commit_command = "git commit -m test-{reason}"
return wd
# Note: Git availability is now checked in WorkDir.setup_git() method


@pytest.fixture(name="wd")
@pytest.fixture
def wd(wd: WorkDir, monkeypatch: pytest.MonkeyPatch, debug_mode: DebugMode) -> WorkDir:
"""Set up git for git-specific tests."""
debug_mode.disable()
setup_git_wd(wd, monkeypatch)
wd.setup_git(monkeypatch)
debug_mode.enable()
return wd

Expand Down Expand Up @@ -685,7 +672,7 @@ def test_fail_on_missing_submodules_with_uninitialized_submodules(
# Create a test repository with a .gitmodules file but no actual submodule
test_repo = tmp_path / "test_repo"
test_repo.mkdir()
test_wd = setup_git_wd(WorkDir(test_repo))
test_wd = WorkDir(test_repo).setup_git()

# Create a fake .gitmodules file (this simulates what happens after cloning without --recurse-submodules)
gitmodules_content = """[submodule "external"]
Expand Down
11 changes: 4 additions & 7 deletions testing/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,11 @@
c = Configuration()


# Module-level fixture for git SCM setup
@pytest.fixture
def wd(wd: WorkDir) -> WorkDir:
wd("git init")
wd("git config user.email [email protected]")
wd('git config user.name "a test"')
wd.add_command = "git add ."
wd.commit_command = "git commit -m test-{reason}"
return wd
def wd(wd: WorkDir, monkeypatch: pytest.MonkeyPatch) -> WorkDir:
"""Set up git for integration tests."""
return wd.setup_git(monkeypatch)


def test_pyproject_support(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
Expand Down
6 changes: 1 addition & 5 deletions testing/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ def test_main() -> None:

@pytest.fixture
def repo(wd: WorkDir) -> WorkDir:
wd("git init")
wd("git config user.email user@host")
wd("git config user.name user")
wd.add_command = "git add ."
wd.commit_command = "git commit -m test-{reason}"
wd.setup_git()

wd.write("README.rst", "My example")
wd.add_and_commit()
Expand Down
11 changes: 3 additions & 8 deletions testing/test_mercurial.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,18 @@

from setuptools_scm import Configuration
from setuptools_scm._run_cmd import CommandNotFoundError
from setuptools_scm._run_cmd import has_command
from setuptools_scm.hg import archival_to_version
from setuptools_scm.hg import parse
from setuptools_scm.version import format_version
from testing.wd_wrapper import WorkDir

pytestmark = pytest.mark.skipif(
not has_command("hg", warn=False), reason="hg executable not found"
)
# Note: Mercurial availability is now checked in WorkDir.setup_hg() method


@pytest.fixture
def wd(wd: WorkDir) -> WorkDir:
wd("hg init")
wd.add_command = "hg add ."
wd.commit_command = 'hg commit -m test-{reason} -u test -d "0 0"'
return wd
"""Set up mercurial for hg-specific tests."""
return wd.setup_hg()


archival_mapping = {
Expand Down
Loading
Loading