Skip to content

Commit bfb2905

Browse files
Fix test failures: add try-finally to UsethisConfig.set() and add SetupCFGManager to missing test contexts
Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/9ec216b2-e65e-4fec-aa2f-6addd5a627e1 Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
1 parent 5a62eaa commit bfb2905

3 files changed

Lines changed: 58 additions & 40 deletions

File tree

src/usethis/_config.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class UsethisConfig:
5959
project_dir: Path | None = None
6060

6161
@contextmanager
62-
def set( # noqa: PLR0913, PLR0915
62+
def set( # noqa: PLR0912, PLR0913, PLR0915
6363
self,
6464
*,
6565
offline: bool | None = None,
@@ -121,18 +121,20 @@ def set( # noqa: PLR0913, PLR0915
121121
if isinstance(project_dir, str):
122122
project_dir = Path(project_dir)
123123
self.project_dir = project_dir
124-
yield
125-
self.offline = old_offline
126-
self.quiet = old_quiet
127-
self.frozen = old_frozen
128-
self.alert_only = old_alert_only
129-
self.instruct_only = old_instruct_only
130-
self.backend = old_backend
131-
self.inferred_backend = old_inferred_backend
132-
self.build_backend = old_build_backend
133-
self.disable_pre_commit = old_disable_pre_commit
134-
self.subprocess_verbose = old_subprocess_verbose
135-
self.project_dir = old_project_dir
124+
try:
125+
yield
126+
finally:
127+
self.offline = old_offline
128+
self.quiet = old_quiet
129+
self.frozen = old_frozen
130+
self.alert_only = old_alert_only
131+
self.instruct_only = old_instruct_only
132+
self.backend = old_backend
133+
self.inferred_backend = old_inferred_backend
134+
self.build_backend = old_build_backend
135+
self.disable_pre_commit = old_disable_pre_commit
136+
self.subprocess_verbose = old_subprocess_verbose
137+
self.project_dir = old_project_dir
136138

137139
def cpd(self) -> Path:
138140
"""Return the current project directory."""

tests/usethis/_core/test_core_tool.py

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
FALLBACK_UV_VERSION,
3939
)
4040
from usethis._file.pyproject_toml.io_ import PyprojectTOMLManager
41+
from usethis._file.setup_cfg.io_ import SetupCFGManager
4142
from usethis._integrations.pre_commit.hooks import HOOK_GROUPS, get_hook_ids
4243
from usethis._integrations.pre_commit.yaml import PreCommitConfigYAMLManager
4344
from usethis._python.version import PythonVersion
@@ -71,7 +72,7 @@ class TestAdd:
7172
@pytest.mark.usefixtures("_vary_network_conn")
7273
def test_config(self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]):
7374
# Arrange
74-
with change_cwd(uv_init_dir), PyprojectTOMLManager():
75+
with change_cwd(uv_init_dir), PyprojectTOMLManager(), SetupCFGManager():
7576
add_deps_to_group([Dependency(name="codespell")], "dev")
7677
ensure_symlink_mode()
7778

@@ -485,7 +486,7 @@ def test_after_codespell(self, tmp_path: Path):
485486

486487
class TestRemove:
487488
def test_unused(self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]):
488-
with change_cwd(uv_init_dir), PyprojectTOMLManager():
489+
with change_cwd(uv_init_dir), PyprojectTOMLManager(), SetupCFGManager():
489490
# Act
490491
use_coverage_py(remove=True)
491492

@@ -554,7 +555,7 @@ class TestAdd:
554555
@pytest.mark.usefixtures("_vary_network_conn")
555556
def test_dependency_added(self, uv_init_dir: Path):
556557
# Act
557-
with change_cwd(uv_init_dir), PyprojectTOMLManager():
558+
with change_cwd(uv_init_dir), PyprojectTOMLManager(), SetupCFGManager():
558559
use_deptry()
559560

560561
# Assert
@@ -564,7 +565,7 @@ def test_dependency_added(self, uv_init_dir: Path):
564565
@pytest.mark.usefixtures("_vary_network_conn")
565566
def test_ignore_notebooks(self, uv_init_dir: Path):
566567
# Act
567-
with change_cwd(uv_init_dir), PyprojectTOMLManager():
568+
with change_cwd(uv_init_dir), PyprojectTOMLManager(), SetupCFGManager():
568569
use_deptry()
569570

570571
# Assert
@@ -580,7 +581,7 @@ def test_stdout(
580581
capfd: pytest.CaptureFixture[str],
581582
):
582583
# Act
583-
with change_cwd(uv_init_dir), PyprojectTOMLManager():
584+
with change_cwd(uv_init_dir), PyprojectTOMLManager(), SetupCFGManager():
584585
use_deptry()
585586

586587
# Assert
@@ -604,6 +605,7 @@ def test_stdout_unfrozen(
604605
with (
605606
change_cwd(uv_init_dir),
606607
PyprojectTOMLManager(),
608+
SetupCFGManager(),
607609
usethis_config.set(frozen=False),
608610
):
609611
use_deptry()
@@ -623,7 +625,7 @@ def test_run_deptry_fail(self, uv_init_dir: Path, uv_path: Path):
623625
f.write_text("import broken_dependency")
624626

625627
# Act
626-
with change_cwd(uv_init_dir), PyprojectTOMLManager():
628+
with change_cwd(uv_init_dir), PyprojectTOMLManager(), SetupCFGManager():
627629
use_deptry()
628630

629631
# Assert
@@ -643,6 +645,7 @@ def test_run_deptry_pass(self, uv_init_dir: Path):
643645
with (
644646
change_cwd(uv_init_dir),
645647
PyprojectTOMLManager(),
648+
SetupCFGManager(),
646649
usethis_config.set(frozen=False),
647650
):
648651
# Act
@@ -707,7 +710,7 @@ def test_pre_commit_after(
707710
class TestRemove:
708711
@pytest.mark.usefixtures("_vary_network_conn")
709712
def test_dep(self, uv_init_dir: Path):
710-
with change_cwd(uv_init_dir), PyprojectTOMLManager():
713+
with change_cwd(uv_init_dir), PyprojectTOMLManager(), SetupCFGManager():
711714
# Arrange
712715
add_deps_to_group([Dependency(name="deptry")], "dev")
713716

@@ -726,7 +729,7 @@ def test_use_deptry_removes_config(self, tmp_path: Path):
726729
""")
727730

728731
# Act
729-
with change_cwd(tmp_path), PyprojectTOMLManager():
732+
with change_cwd(tmp_path), PyprojectTOMLManager(), SetupCFGManager():
730733
use_deptry(remove=True)
731734

732735
# Assert
@@ -738,7 +741,7 @@ def test_roundtrip(self, uv_init_dir: Path):
738741
contents = (uv_init_dir / "pyproject.toml").read_text()
739742

740743
# Act
741-
with change_cwd(uv_init_dir), PyprojectTOMLManager():
744+
with change_cwd(uv_init_dir), PyprojectTOMLManager(), SetupCFGManager():
742745
use_deptry()
743746
use_deptry(remove=True)
744747

@@ -2247,12 +2250,13 @@ def test_added(self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]):
22472250
with (
22482251
change_cwd(uv_init_dir),
22492252
PyprojectTOMLManager(),
2253+
SetupCFGManager(),
22502254
usethis_config.set(quiet=True),
22512255
):
22522256
add_deps_to_group([Dependency(name="pyproject-fmt")], "dev")
22532257

22542258
# Act
2255-
with change_cwd(uv_init_dir), PyprojectTOMLManager():
2259+
with change_cwd(uv_init_dir), PyprojectTOMLManager(), SetupCFGManager():
22562260
use_pyproject_fmt()
22572261

22582262
# Assert
@@ -2276,7 +2280,7 @@ def test_added(self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]):
22762280
class TestDeps:
22772281
@pytest.mark.usefixtures("_vary_network_conn")
22782282
def test_added(self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]):
2279-
with change_cwd(uv_init_dir), PyprojectTOMLManager():
2283+
with change_cwd(uv_init_dir), PyprojectTOMLManager(), SetupCFGManager():
22802284
# Act
22812285
use_pyproject_fmt()
22822286

@@ -2299,7 +2303,7 @@ def test_skips_apply(
22992303
self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]
23002304
):
23012305
# Act
2302-
with change_cwd(uv_init_dir), PyprojectTOMLManager():
2306+
with change_cwd(uv_init_dir), PyprojectTOMLManager(), SetupCFGManager():
23032307
use_pyproject_fmt(no_apply=True)
23042308

23052309
# Assert
@@ -2352,7 +2356,7 @@ def test_config_file(self, uv_init_dir: Path):
23522356
)
23532357

23542358
# Act
2355-
with change_cwd(uv_init_dir), PyprojectTOMLManager():
2359+
with change_cwd(uv_init_dir), PyprojectTOMLManager(), SetupCFGManager():
23562360
use_pyproject_fmt(remove=True)
23572361

23582362
# Assert
@@ -2428,7 +2432,11 @@ def test_remove_with_precommit(
24282432
def test_remove_without_precommit(
24292433
self, uv_init_repo_dir: Path, capfd: pytest.CaptureFixture[str]
24302434
):
2431-
with change_cwd(uv_init_repo_dir), PyprojectTOMLManager():
2435+
with (
2436+
change_cwd(uv_init_repo_dir),
2437+
PyprojectTOMLManager(),
2438+
SetupCFGManager(),
2439+
):
24322440
# Arrange
24332441
with usethis_config.set(quiet=True):
24342442
use_pyproject_fmt()
@@ -2902,6 +2910,7 @@ def test_start_from_nothing_uv_backend(
29022910
with (
29032911
change_cwd(tmp_path),
29042912
PyprojectTOMLManager(),
2913+
SetupCFGManager(),
29052914
usethis_config.set(backend=BackendEnum.uv),
29062915
):
29072916
use_requirements_txt()
@@ -2926,6 +2935,7 @@ def test_start_from_nothing_none_backend(
29262935
with (
29272936
change_cwd(tmp_path),
29282937
PyprojectTOMLManager(),
2938+
SetupCFGManager(),
29292939
usethis_config.set(backend=BackendEnum.none),
29302940
):
29312941
use_requirements_txt()
@@ -2946,6 +2956,7 @@ def test_start_from_nothing_poetry_backend(
29462956
with (
29472957
change_cwd(tmp_path),
29482958
PyprojectTOMLManager(),
2959+
SetupCFGManager(),
29492960
usethis_config.set(backend=BackendEnum.poetry),
29502961
):
29512962
use_requirements_txt()
@@ -2965,6 +2976,7 @@ def test_start_from_uv_init(
29652976
with (
29662977
change_cwd(uv_init_dir),
29672978
PyprojectTOMLManager(),
2979+
SetupCFGManager(),
29682980
usethis_config.set(frozen=False),
29692981
):
29702982
use_requirements_txt()
@@ -2985,6 +2997,7 @@ def test_start_from_uv_locked(
29852997
with (
29862998
change_cwd(uv_init_dir),
29872999
PyprojectTOMLManager(),
3000+
SetupCFGManager(),
29883001
usethis_config.set(frozen=False),
29893002
):
29903003
# Arrange
@@ -3062,6 +3075,7 @@ def test_none_backend(
30623075
with (
30633076
change_cwd(tmp_path),
30643077
PyprojectTOMLManager(),
3078+
SetupCFGManager(),
30653079
usethis_config.set(backend=BackendEnum.auto),
30663080
):
30673081
# Act
@@ -3092,7 +3106,7 @@ def test_file_gone(self, tmp_path: Path):
30923106
(tmp_path / "requirements.txt").touch()
30933107

30943108
# Act
3095-
with change_cwd(tmp_path), PyprojectTOMLManager():
3109+
with change_cwd(tmp_path), PyprojectTOMLManager(), SetupCFGManager():
30963110
use_requirements_txt(remove=True)
30973111

30983112
# Assert
@@ -3103,7 +3117,7 @@ def test_requirements_dir(self, tmp_path: Path):
31033117
(tmp_path / "requirements.txt").mkdir()
31043118

31053119
# Act
3106-
with change_cwd(tmp_path), PyprojectTOMLManager():
3120+
with change_cwd(tmp_path), PyprojectTOMLManager(), SetupCFGManager():
31073121
use_requirements_txt(remove=True)
31083122

31093123
# Assert
@@ -3130,7 +3144,7 @@ def test_precommit_integration(self, tmp_path: Path):
31303144
assert "uv-export" not in content
31313145

31323146
def test_roundtrip(self, tmp_path: Path):
3133-
with change_cwd(tmp_path), PyprojectTOMLManager():
3147+
with change_cwd(tmp_path), PyprojectTOMLManager(), SetupCFGManager():
31343148
# Arrange
31353149
use_requirements_txt()
31363150

@@ -3156,6 +3170,7 @@ def test_custom_output_file_creates_correct_file(
31563170
with (
31573171
change_cwd(tmp_path),
31583172
PyprojectTOMLManager(),
3173+
SetupCFGManager(),
31593174
usethis_config.set(backend=BackendEnum.uv),
31603175
):
31613176
use_requirements_txt(output_file="constraints.txt")
@@ -3190,7 +3205,7 @@ def test_custom_output_file_remove(self, tmp_path: Path):
31903205
(tmp_path / "constraints.txt").touch()
31913206

31923207
# Act
3193-
with change_cwd(tmp_path), PyprojectTOMLManager():
3208+
with change_cwd(tmp_path), PyprojectTOMLManager(), SetupCFGManager():
31943209
use_requirements_txt(remove=True, output_file="constraints.txt")
31953210

31963211
# Assert
@@ -3203,6 +3218,7 @@ def test_default_output_file_is_requirements_txt(
32033218
with (
32043219
change_cwd(tmp_path),
32053220
PyprojectTOMLManager(),
3221+
SetupCFGManager(),
32063222
usethis_config.set(backend=BackendEnum.none),
32073223
):
32083224
use_requirements_txt()
@@ -3968,7 +3984,7 @@ def test_dependency_added(self, uv_init_dir: Path):
39683984
@pytest.mark.usefixtures("_vary_network_conn")
39693985
def test_config(self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]):
39703986
# Arrange
3971-
with change_cwd(uv_init_dir), PyprojectTOMLManager():
3987+
with change_cwd(uv_init_dir), PyprojectTOMLManager(), SetupCFGManager():
39723988
add_deps_to_group([Dependency(name="ty")], "dev")
39733989

39743990
capfd.readouterr()

tests/usethis/_tool/test_base.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ class TestIsUsed:
269269
def test_some_deps(self, uv_init_dir: Path):
270270
# Arrange
271271
tool = MyTool()
272-
with change_cwd(uv_init_dir), PyprojectTOMLManager():
272+
with change_cwd(uv_init_dir), PyprojectTOMLManager(), SetupCFGManager():
273273
add_deps_to_group(
274274
[
275275
Dependency(name="black"),
@@ -298,7 +298,7 @@ def test_files(self, uv_init_dir: Path):
298298
def test_dir(self, uv_init_dir: Path):
299299
# Arrange
300300
tool = MyTool()
301-
with change_cwd(uv_init_dir), PyprojectTOMLManager():
301+
with change_cwd(uv_init_dir), PyprojectTOMLManager(), SetupCFGManager():
302302
Path("mytool-config.yaml").mkdir()
303303

304304
# Act
@@ -310,7 +310,7 @@ def test_dir(self, uv_init_dir: Path):
310310
def test_pyproject(self, uv_init_dir: Path):
311311
# Arrange
312312
tool = MyTool()
313-
with change_cwd(uv_init_dir), PyprojectTOMLManager():
313+
with change_cwd(uv_init_dir), PyprojectTOMLManager(), SetupCFGManager():
314314
PyprojectTOMLManager().set_value(
315315
keys=["tool", "my_tool", "key"], value="value"
316316
)
@@ -326,7 +326,7 @@ def test_empty(self, uv_init_dir: Path):
326326
tool = MyTool()
327327

328328
# Act
329-
with change_cwd(uv_init_dir), PyprojectTOMLManager():
329+
with change_cwd(uv_init_dir), PyprojectTOMLManager(), SetupCFGManager():
330330
result = tool.is_used()
331331

332332
# Assert
@@ -336,7 +336,7 @@ def test_dev_deps(self, uv_init_dir: Path):
336336
# Arrange
337337
tool = MyTool()
338338

339-
with change_cwd(uv_init_dir), PyprojectTOMLManager():
339+
with change_cwd(uv_init_dir), PyprojectTOMLManager(), SetupCFGManager():
340340
add_deps_to_group(
341341
[
342342
Dependency(name="black"),
@@ -354,7 +354,7 @@ def test_test_deps(self, uv_init_dir: Path):
354354
# Arrange
355355
tool = MyTool()
356356

357-
with change_cwd(uv_init_dir), PyprojectTOMLManager():
357+
with change_cwd(uv_init_dir), PyprojectTOMLManager(), SetupCFGManager():
358358
add_deps_to_group(
359359
[
360360
Dependency(name="pytest"),
@@ -372,7 +372,7 @@ def test_not_extra_dev_deps(self, uv_init_dir: Path):
372372
# Arrange
373373
tool = MyTool()
374374

375-
with change_cwd(uv_init_dir), PyprojectTOMLManager():
375+
with change_cwd(uv_init_dir), PyprojectTOMLManager(), SetupCFGManager():
376376
add_deps_to_group(
377377
[
378378
Dependency(name="isort"),
@@ -504,7 +504,7 @@ def test_dev_deps(self, uv_init_dir: Path):
504504
# Arrange
505505
tool = MyTool()
506506

507-
with change_cwd(uv_init_dir), PyprojectTOMLManager():
507+
with change_cwd(uv_init_dir), PyprojectTOMLManager(), SetupCFGManager():
508508
add_deps_to_group(
509509
[
510510
Dependency(name="black"),

0 commit comments

Comments
 (0)