@@ -45,9 +45,7 @@ def get_pre_commit_repos(self) -> list[LocalRepo | UriRepo]:
4545 ]
4646
4747 def get_pyproject_configs (self ) -> list [PyProjectConfig ]:
48- return [
49- PyProjectConfig (id_keys = ["tool" , self .name ], main_contents = {"key" : "value" })
50- ]
48+ return [PyProjectConfig (id_keys = ["tool" , self .name ], value = {"key" : "value" })]
5149
5250 def get_associated_ruff_rules (self ) -> list [str ]:
5351 return ["MYRULE" ]
@@ -117,9 +115,7 @@ def test_default(self):
117115 def test_specific (self ):
118116 tool = MyTool ()
119117 assert tool .get_pyproject_configs () == [
120- PyProjectConfig (
121- id_keys = ["tool" , "my_tool" ], main_contents = {"key" : "value" }
122- )
118+ PyProjectConfig (id_keys = ["tool" , "my_tool" ], value = {"key" : "value" })
123119 ]
124120
125121 class TestGetAssociatedRuffRules :
@@ -545,3 +541,103 @@ def get_pre_commit_repos(self) -> list[LocalRepo | UriRepo]:
545541 # Assert
546542 assert (tmp_path / ".pre-commit-config.yaml" ).exists ()
547543 assert get_hook_names () == [_PLACEHOLDER_ID ]
544+
545+ class TestAddPyprojectConfigs :
546+ def test_no_config (self , tmp_path : Path ):
547+ # Arrange
548+ class NoConfigTool (Tool ):
549+ @property
550+ def name (self ) -> str :
551+ return "no_config_tool"
552+
553+ def get_pyproject_configs (self ) -> list [PyProjectConfig ]:
554+ return []
555+
556+ nc_tool = NoConfigTool ()
557+
558+ # Act
559+ with change_cwd (tmp_path ):
560+ nc_tool .add_pyproject_configs ()
561+
562+ # Assert
563+ assert not (tmp_path / "pyproject.toml" ).exists ()
564+
565+ def test_empty (self , tmp_path : Path , capfd : pytest .CaptureFixture [str ]):
566+ # Arrange
567+ class ThisTool (Tool ):
568+ @property
569+ def name (self ) -> str :
570+ return "mytool"
571+
572+ def get_pyproject_configs (self ) -> list [PyProjectConfig ]:
573+ return [
574+ PyProjectConfig (
575+ id_keys = ["tool" , "mytool" ],
576+ value = {"key" : "value" },
577+ ),
578+ ]
579+
580+ (tmp_path / "pyproject.toml" ).write_text ("" )
581+
582+ # Act
583+ with change_cwd (tmp_path ):
584+ ThisTool ().add_pyproject_configs ()
585+
586+ # Assert
587+ assert (
588+ (tmp_path / "pyproject.toml" ).read_text ()
589+ == """\
590+ [tool.mytool]
591+ key = "value"
592+ """
593+ )
594+ out , err = capfd .readouterr ()
595+ assert not err
596+ assert out == "✔ Adding mytool config to 'pyproject.toml'.\n "
597+
598+ def test_differing_sections (
599+ self , tmp_path : Path , capfd : pytest .CaptureFixture [str ]
600+ ):
601+ # https://github.com/nathanjmcdougall/usethis-python/issues/184
602+
603+ # Arrange
604+ class ThisTool (Tool ):
605+ @property
606+ def name (self ) -> str :
607+ return "mytool"
608+
609+ def get_pyproject_configs (self ) -> list [PyProjectConfig ]:
610+ return [
611+ PyProjectConfig (
612+ id_keys = ["tool" , "mytool" , "name" ],
613+ value = "Modular Design" ,
614+ ),
615+ PyProjectConfig (
616+ id_keys = ["tool" , "mytool" , "root_packages" ],
617+ value = ["example" ],
618+ ),
619+ ]
620+
621+ (tmp_path / "pyproject.toml" ).write_text (
622+ """\
623+ [tool.mytool]
624+ name = "Modular Design"
625+ """
626+ )
627+
628+ # Act
629+ with change_cwd (tmp_path ):
630+ ThisTool ().add_pyproject_configs ()
631+
632+ # Assert
633+ assert (
634+ (tmp_path / "pyproject.toml" ).read_text ()
635+ == """\
636+ [tool.mytool]
637+ name = "Modular Design"
638+ root_packages = ["example"]
639+ """
640+ )
641+ out , err = capfd .readouterr ()
642+ assert not err
643+ assert out == "✔ Adding mytool config to 'pyproject.toml'.\n "
0 commit comments