77import pytest
88
99from setuptools_scm ._cli import main
10+ from setuptools_scm ._integration .pyproject_reading import PyProjectData
1011
1112from .conftest import DebugMode
12- from .test_git import wd as wd_fixture # noqa: F401 (evil fixture reuse)
1313from .wd_wrapper import WorkDir
1414
15+
16+ @pytest .fixture
17+ def wd (wd : WorkDir , monkeypatch : pytest .MonkeyPatch , debug_mode : DebugMode ) -> WorkDir :
18+ """Set up git for CLI tests."""
19+ debug_mode .disable ()
20+ wd .setup_git (monkeypatch )
21+ debug_mode .enable ()
22+ return wd
23+
24+
1525PYPROJECT_TOML = "pyproject.toml"
1626PYPROJECT_SIMPLE = "[tool.setuptools_scm]"
1727PYPROJECT_ROOT = '[tool.setuptools_scm]\n root=".."'
1828
29+ # PyProjectData constants for testing
30+ PYPROJECT_DATA_SIMPLE = PyProjectData .for_testing (section_present = True )
31+ PYPROJECT_DATA_WITH_PROJECT = PyProjectData .for_testing (
32+ section_present = True , project_present = True , project_name = "test"
33+ )
34+
1935
20- def get_output (args : list [str ]) -> str :
36+ def _create_version_file_pyproject_data () -> PyProjectData :
37+ """Create PyProjectData with version_file configuration for testing."""
38+ data = PyProjectData .for_testing (
39+ section_present = True , project_present = True , project_name = "test"
40+ )
41+ data .section ["version_file" ] = "ver.py"
42+ return data
43+
44+
45+ def get_output (
46+ args : list [str ], * , _given_pyproject_data : PyProjectData | None = None
47+ ) -> str :
2148 with redirect_stdout (io .StringIO ()) as out :
22- main (args )
49+ main (args , _given_pyproject_data = _given_pyproject_data )
2350 return out .getvalue ()
2451
2552
@@ -59,24 +86,20 @@ def test_cli_force_version_files(
5986) -> None :
6087 debug_mode .disable ()
6188 wd .commit_testfile ()
62- wd .write (
63- PYPROJECT_TOML ,
64- """
65- [project]
66- name = "test"
67- [tool.setuptools_scm]
68- version_file = "ver.py"
69- """ ,
70- )
7189 monkeypatch .chdir (wd .cwd )
7290
7391 version_file = wd .cwd .joinpath ("ver.py" )
7492 assert not version_file .exists ()
7593
76- get_output ([])
94+ # Create pyproject data with version_file configuration
95+ pyproject_data = _create_version_file_pyproject_data ()
96+
97+ get_output ([], _given_pyproject_data = pyproject_data )
7798 assert not version_file .exists ()
7899
79- output = get_output (["--force-write-version-files" ])
100+ output = get_output (
101+ ["--force-write-version-files" ], _given_pyproject_data = pyproject_data
102+ )
80103 assert version_file .exists ()
81104
82105 assert output [:5 ] in version_file .read_text ("utf-8" )
@@ -87,13 +110,16 @@ def test_cli_create_archival_file_stable(
87110) -> None :
88111 """Test creating stable .git_archival.txt file."""
89112 wd .commit_testfile ()
90- wd .write (PYPROJECT_TOML , PYPROJECT_SIMPLE )
91113 monkeypatch .chdir (wd .cwd )
92114
93115 archival_file = wd .cwd / ".git_archival.txt"
94116 assert not archival_file .exists ()
95117
96- result = main (["create-archival-file" , "--stable" ])
118+ # Use injected pyproject data instead of creating a file
119+ pyproject_data = PYPROJECT_DATA_SIMPLE
120+ result = main (
121+ ["create-archival-file" , "--stable" ], _given_pyproject_data = pyproject_data
122+ )
97123 assert result == 0
98124 assert archival_file .exists ()
99125
@@ -115,13 +141,16 @@ def test_cli_create_archival_file_full(
115141) -> None :
116142 """Test creating full .git_archival.txt file with branch information."""
117143 wd .commit_testfile ()
118- wd .write (PYPROJECT_TOML , PYPROJECT_SIMPLE )
119144 monkeypatch .chdir (wd .cwd )
120145
121146 archival_file = wd .cwd / ".git_archival.txt"
122147 assert not archival_file .exists ()
123148
124- result = main (["create-archival-file" , "--full" ])
149+ # Use injected pyproject data instead of creating a file
150+ pyproject_data = PYPROJECT_DATA_SIMPLE
151+ result = main (
152+ ["create-archival-file" , "--full" ], _given_pyproject_data = pyproject_data
153+ )
125154 assert result == 0
126155 assert archival_file .exists ()
127156
@@ -144,15 +173,18 @@ def test_cli_create_archival_file_exists_no_force(
144173 wd : WorkDir , monkeypatch : pytest .MonkeyPatch
145174) -> None :
146175 """Test that existing .git_archival.txt file prevents creation without --force."""
176+ wd .setup_git (monkeypatch )
147177 wd .commit_testfile ()
148- wd .write (PYPROJECT_TOML , PYPROJECT_SIMPLE )
149178 monkeypatch .chdir (wd .cwd )
150179
151180 archival_file = wd .cwd / ".git_archival.txt"
152181 archival_file .write_text ("existing content" , encoding = "utf-8" )
153182
154183 # Should fail without --force
155- result = main (["create-archival-file" , "--stable" ])
184+ pyproject_data = PYPROJECT_DATA_SIMPLE
185+ result = main (
186+ ["create-archival-file" , "--stable" ], _given_pyproject_data = pyproject_data
187+ )
156188 assert result == 1
157189
158190 # Content should be unchanged
@@ -163,15 +195,19 @@ def test_cli_create_archival_file_exists_with_force(
163195 wd : WorkDir , monkeypatch : pytest .MonkeyPatch
164196) -> None :
165197 """Test that --force overwrites existing .git_archival.txt file."""
198+ wd .setup_git (monkeypatch )
166199 wd .commit_testfile ()
167- wd .write (PYPROJECT_TOML , PYPROJECT_SIMPLE )
168200 monkeypatch .chdir (wd .cwd )
169201
170202 archival_file = wd .cwd / ".git_archival.txt"
171203 archival_file .write_text ("existing content" , encoding = "utf-8" )
172204
173205 # Should succeed with --force
174- result = main (["create-archival-file" , "--stable" , "--force" ])
206+ pyproject_data = PYPROJECT_DATA_SIMPLE
207+ result = main (
208+ ["create-archival-file" , "--stable" , "--force" ],
209+ _given_pyproject_data = pyproject_data ,
210+ )
175211 assert result == 0
176212
177213 # Content should be updated
@@ -184,41 +220,48 @@ def test_cli_create_archival_file_requires_stable_or_full(
184220 wd : WorkDir , monkeypatch : pytest .MonkeyPatch
185221) -> None :
186222 """Test that create-archival-file requires either --stable or --full."""
223+ wd .setup_git (monkeypatch )
187224 wd .commit_testfile ()
188- wd .write (PYPROJECT_TOML , PYPROJECT_SIMPLE )
189225 monkeypatch .chdir (wd .cwd )
190226
191227 # Should fail without --stable or --full
228+ pyproject_data = PYPROJECT_DATA_SIMPLE
192229 with pytest .raises (SystemExit ):
193- main (["create-archival-file" ])
230+ main (["create-archival-file" ], _given_pyproject_data = pyproject_data )
194231
195232
196233def test_cli_create_archival_file_mutually_exclusive (
197234 wd : WorkDir , monkeypatch : pytest .MonkeyPatch
198235) -> None :
199236 """Test that --stable and --full are mutually exclusive."""
237+ wd .setup_git (monkeypatch )
200238 wd .commit_testfile ()
201- wd .write (PYPROJECT_TOML , PYPROJECT_SIMPLE )
202239 monkeypatch .chdir (wd .cwd )
203240
204241 # Should fail with both --stable and --full
242+ pyproject_data = PYPROJECT_DATA_SIMPLE
205243 with pytest .raises (SystemExit ):
206- main (["create-archival-file" , "--stable" , "--full" ])
244+ main (
245+ ["create-archival-file" , "--stable" , "--full" ],
246+ _given_pyproject_data = pyproject_data ,
247+ )
207248
208249
209250def test_cli_create_archival_file_existing_gitattributes (
210251 wd : WorkDir , monkeypatch : pytest .MonkeyPatch
211252) -> None :
212253 """Test behavior when .gitattributes already has export-subst configuration."""
213254 wd .commit_testfile ()
214- wd .write (PYPROJECT_TOML , PYPROJECT_SIMPLE )
215255 monkeypatch .chdir (wd .cwd )
216256
217257 # Create .gitattributes with export-subst configuration
218258 gitattributes_file = wd .cwd / ".gitattributes"
219259 gitattributes_file .write_text (".git_archival.txt export-subst\n " , encoding = "utf-8" )
220260
221- result = main (["create-archival-file" , "--stable" ])
261+ pyproject_data = PYPROJECT_DATA_SIMPLE
262+ result = main (
263+ ["create-archival-file" , "--stable" ], _given_pyproject_data = pyproject_data
264+ )
222265 assert result == 0
223266
224267 archival_file = wd .cwd / ".git_archival.txt"
@@ -230,10 +273,12 @@ def test_cli_create_archival_file_no_gitattributes(
230273) -> None :
231274 """Test behavior when .gitattributes doesn't exist or lacks export-subst."""
232275 wd .commit_testfile ()
233- wd .write (PYPROJECT_TOML , PYPROJECT_SIMPLE )
234276 monkeypatch .chdir (wd .cwd )
235277
236- result = main (["create-archival-file" , "--stable" ])
278+ pyproject_data = PYPROJECT_DATA_SIMPLE
279+ result = main (
280+ ["create-archival-file" , "--stable" ], _given_pyproject_data = pyproject_data
281+ )
237282 assert result == 0
238283
239284 archival_file = wd .cwd / ".git_archival.txt"
0 commit comments