|
| 1 | +""" |
| 2 | +Tests for better error messages when relative_to is not set. |
| 3 | +
|
| 4 | +This addresses the issue #279 where error messages should be more helpful |
| 5 | +when setuptools-scm fails to detect a version but a repository exists |
| 6 | +in a parent directory. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from setuptools_scm import Configuration |
| 14 | +from setuptools_scm import get_version |
| 15 | +from setuptools_scm._get_version_impl import _find_scm_in_parents |
| 16 | +from setuptools_scm._get_version_impl import _version_missing |
| 17 | +from testing.wd_wrapper import WorkDir |
| 18 | + |
| 19 | + |
| 20 | +def setup_git_repo(wd: WorkDir) -> WorkDir: |
| 21 | + """Set up a git repository for testing.""" |
| 22 | + wd("git init") |
| 23 | + wd( "git config user.email [email protected]") |
| 24 | + wd('git config user.name "a test"') |
| 25 | + wd.add_command = "git add ." |
| 26 | + wd.commit_command = "git commit -m test-{reason}" |
| 27 | + return wd |
| 28 | + |
| 29 | + |
| 30 | +def setup_hg_repo(wd: WorkDir) -> WorkDir: |
| 31 | + """Set up a mercurial repository for testing.""" |
| 32 | + try: |
| 33 | + wd("hg init") |
| 34 | + wd( "hg config --local ui.username 'test <[email protected]>'") |
| 35 | + wd.add_command = "hg add ." |
| 36 | + wd.commit_command = "hg commit -m test-{reason}" |
| 37 | + return wd |
| 38 | + except Exception: |
| 39 | + pytest.skip("hg not available") |
| 40 | + |
| 41 | + |
| 42 | +def test_find_scm_in_parents_finds_git(wd: WorkDir) -> None: |
| 43 | + """Test that _find_scm_in_parents correctly finds git repositories in parent directories.""" |
| 44 | + # Set up git repo in root |
| 45 | + setup_git_repo(wd) |
| 46 | + |
| 47 | + # Create a subdirectory structure |
| 48 | + subdir = wd.cwd / "subproject" / "nested" |
| 49 | + subdir.mkdir(parents=True) |
| 50 | + |
| 51 | + # Test from the nested subdirectory |
| 52 | + config = Configuration(root=str(subdir)) |
| 53 | + found_scm = _find_scm_in_parents(config) |
| 54 | + |
| 55 | + assert found_scm == wd.cwd |
| 56 | + |
| 57 | + |
| 58 | +def test_find_scm_in_parents_finds_hg(wd: WorkDir) -> None: |
| 59 | + """Test that _find_scm_in_parents correctly finds mercurial repositories in parent directories.""" |
| 60 | + # Set up hg repo in root |
| 61 | + setup_hg_repo(wd) |
| 62 | + |
| 63 | + # Create a subdirectory structure |
| 64 | + subdir = wd.cwd / "subproject" / "nested" |
| 65 | + subdir.mkdir(parents=True) |
| 66 | + |
| 67 | + # Test from the nested subdirectory |
| 68 | + config = Configuration(root=str(subdir)) |
| 69 | + found_scm = _find_scm_in_parents(config) |
| 70 | + |
| 71 | + assert found_scm == wd.cwd |
| 72 | + |
| 73 | + |
| 74 | +def test_find_scm_in_parents_returns_none(wd: WorkDir) -> None: |
| 75 | + """Test that _find_scm_in_parents returns None when no SCM repository is found.""" |
| 76 | + # Don't initialize any SCM, just create subdirectories |
| 77 | + subdir = wd.cwd / "project" / "nested" |
| 78 | + subdir.mkdir(parents=True) |
| 79 | + |
| 80 | + config = Configuration(root=str(subdir)) |
| 81 | + found_scm = _find_scm_in_parents(config) |
| 82 | + |
| 83 | + assert found_scm is None |
| 84 | + |
| 85 | + |
| 86 | +def test_version_missing_with_scm_in_parent(wd: WorkDir) -> None: |
| 87 | + """Test that _version_missing provides helpful error message when SCM is found in parent.""" |
| 88 | + # Set up git repo in root |
| 89 | + setup_git_repo(wd) |
| 90 | + |
| 91 | + # Create a subdirectory structure |
| 92 | + subdir = wd.cwd / "subproject" / "nested" |
| 93 | + subdir.mkdir(parents=True) |
| 94 | + |
| 95 | + # Test error message when relative_to is not set |
| 96 | + config = Configuration(root=str(subdir), relative_to=None) |
| 97 | + |
| 98 | + with pytest.raises(LookupError) as exc_info: |
| 99 | + _version_missing(config) |
| 100 | + |
| 101 | + error_message = str(exc_info.value) |
| 102 | + |
| 103 | + # Check that the error message mentions the parent repository |
| 104 | + assert f"repository was found in a parent directory: {wd.cwd}" in error_message |
| 105 | + assert "relative_to" in error_message |
| 106 | + assert "search_parent_directories = true" in error_message |
| 107 | + assert "setuptools_scm.get_version(relative_to=__file__)" in error_message |
| 108 | + |
| 109 | + |
| 110 | +def test_version_missing_no_scm_found(wd: WorkDir) -> None: |
| 111 | + """Test that _version_missing provides standard error message when no SCM is found anywhere.""" |
| 112 | + # Don't initialize any SCM, just create subdirectories |
| 113 | + subdir = wd.cwd / "project" / "nested" |
| 114 | + subdir.mkdir(parents=True) |
| 115 | + |
| 116 | + config = Configuration(root=str(subdir), relative_to=None) |
| 117 | + |
| 118 | + with pytest.raises(LookupError) as exc_info: |
| 119 | + _version_missing(config) |
| 120 | + |
| 121 | + error_message = str(exc_info.value) |
| 122 | + |
| 123 | + # Check that it falls back to the standard error message |
| 124 | + assert ( |
| 125 | + "Make sure you're either building from a fully intact git repository" |
| 126 | + in error_message |
| 127 | + ) |
| 128 | + assert "repository was found in a parent directory" not in error_message |
| 129 | + |
| 130 | + |
| 131 | +def test_version_missing_with_relative_to_set(wd: WorkDir) -> None: |
| 132 | + """Test that when relative_to is set, we don't search parents for error messages.""" |
| 133 | + # Set up git repo in root |
| 134 | + setup_git_repo(wd) |
| 135 | + |
| 136 | + # Create a subdirectory structure |
| 137 | + subdir = wd.cwd / "subproject" / "nested" |
| 138 | + subdir.mkdir(parents=True) |
| 139 | + |
| 140 | + # Create a dummy file to use as relative_to |
| 141 | + dummy_file = subdir / "setup.py" |
| 142 | + dummy_file.write_text("# dummy file") |
| 143 | + |
| 144 | + # Test error message when relative_to IS set |
| 145 | + config = Configuration(root=str(subdir), relative_to=str(dummy_file)) |
| 146 | + |
| 147 | + with pytest.raises(LookupError) as exc_info: |
| 148 | + _version_missing(config) |
| 149 | + |
| 150 | + error_message = str(exc_info.value) |
| 151 | + |
| 152 | + # Should not mention parent directory when relative_to is set |
| 153 | + assert "repository was found in a parent directory" not in error_message |
| 154 | + assert ( |
| 155 | + "Make sure you're either building from a fully intact git repository" |
| 156 | + in error_message |
| 157 | + ) |
| 158 | + |
| 159 | + |
| 160 | +def test_search_parent_directories_works_as_suggested( |
| 161 | + wd: WorkDir, monkeypatch: pytest.MonkeyPatch |
| 162 | +) -> None: |
| 163 | + """Test that the suggested search_parent_directories=True solution actually works.""" |
| 164 | + # Set up git repo |
| 165 | + setup_git_repo(wd) |
| 166 | + wd.commit_testfile() # Make sure there's a commit for version detection |
| 167 | + |
| 168 | + # Create a subdirectory |
| 169 | + subdir = wd.cwd / "subproject" |
| 170 | + subdir.mkdir() |
| 171 | + |
| 172 | + # Change to the subdirectory |
| 173 | + monkeypatch.chdir(subdir) |
| 174 | + |
| 175 | + # This should work with search_parent_directories=True |
| 176 | + version = get_version(search_parent_directories=True) |
| 177 | + assert version is not None |
| 178 | + assert "0.1.dev" in version |
| 179 | + |
| 180 | + |
| 181 | +def test_integration_better_error_from_nested_directory( |
| 182 | + wd: WorkDir, monkeypatch: pytest.MonkeyPatch |
| 183 | +) -> None: |
| 184 | + """Integration test: get_version from nested directory should give helpful error.""" |
| 185 | + # Set up git repo |
| 186 | + setup_git_repo(wd) |
| 187 | + |
| 188 | + # Create a subdirectory |
| 189 | + subdir = wd.cwd / "subproject" |
| 190 | + subdir.mkdir() |
| 191 | + |
| 192 | + # Change to the subdirectory |
| 193 | + monkeypatch.chdir(subdir) |
| 194 | + |
| 195 | + # Try to get version without any configuration |
| 196 | + with pytest.raises(LookupError) as exc_info: |
| 197 | + get_version() |
| 198 | + |
| 199 | + error_message = str(exc_info.value) |
| 200 | + |
| 201 | + # Should suggest helpful solutions |
| 202 | + assert f"repository was found in a parent directory: {wd.cwd}" in error_message |
| 203 | + assert "search_parent_directories = true" in error_message |
0 commit comments