1- import os
21import shutil
32from collections .abc import Callable , Generator
43
@@ -28,7 +27,7 @@ def clear_cached_config(app: Application) -> None:
2827
2928# Initialize a dummy repo in a temporary directory for the tests to use
3029@pytest .fixture
31- def temp_repo (app : Application , temp_dir : Path , set_system_git_author : None ) -> Generator [Path , None , None ]: # noqa: ARG001
30+ def temp_repo (app : Application , temp_dir : Path , set_git_author : None ) -> Generator [Path , None , None ]: # noqa: ARG001
3231 git : Git = app .tools .git
3332 repo_path = temp_dir / "dummy-repo"
3433 repo_path .mkdir () # Don't do exist_ok, the directory should not exist
@@ -42,52 +41,22 @@ def temp_repo(app: Application, temp_dir: Path, set_system_git_author: None) ->
4241
4342
4443@pytest .fixture
45- def set_system_git_author (app : Application ) -> Generator [None , None , None ]:
46- # Make sure the config is set to "system" mode, so that the author details are not inherited from the user config
47- old_config = app .config_file .data ["tools" ]["git" ]["author_details" ]
48- app .config_file .data ["tools" ]["git" ]["author_details" ] = "system"
49- app .config_file .save ()
50- clear_cached_config (app )
44+ def set_git_author (app : Application ) -> Generator [None , None , None ]:
45+ """
46+ Set the git author name and email to "Test Runner" and "test.runner@example.com" respectively.
47+ This is done by setting the values in the config file.
48+ Any commits made by `dda` will use these values, but any calls to `git config` will still return the global git config values.
49+ """
50+ old_name = app .config_file .data ["tools" ]["git" ]["author_name" ]
51+ old_email = app .config_file .data ["tools" ]["git" ]["author_email" ]
52+ app .config_file .data ["tools" ]["git" ]["author_name" ] = "Test Runner"
53+ app .config_file .data ["tools" ]["git" ]["author_email" ] = "test.runner@example.com"
5154
52- old_env_author = os .environ .pop (Git .AUTHOR_NAME_ENV_VAR , default = None )
53- old_env_email = os .environ .pop (Git .AUTHOR_EMAIL_ENV_VAR , default = None )
54- old_author_name = app .tools .git .capture (["config" , "--get" , "user.name" ], check = False )
55- old_author_email = app .tools .git .capture (["config" , "--get" , "user.email" ], check = False )
56- app .tools .git .run (["config" , "--global" , "user.name" , "Test Runner" ])
57- app .tools .git .run (["config" , "--global" , "user.email" , "test.runner@example.com" ])
58- yield
59-
60- app .tools .git .run (["config" , "--global" , "--unset" , "user.name" ])
61- app .tools .git .run (["config" , "--global" , "--unset" , "user.email" ])
62- if old_author_name :
63- app .tools .git .run (["config" , "--global" , "user.name" , old_author_name .strip ()])
64- if old_author_email :
65- app .tools .git .run (["config" , "--global" , "user.email" , old_author_email .strip ()])
66- if old_env_author :
67- os .environ [Git .AUTHOR_NAME_ENV_VAR ] = old_env_author .strip ()
68- if old_env_email :
69- os .environ [Git .AUTHOR_EMAIL_ENV_VAR ] = old_env_email .strip ()
70-
71- app .config_file .data ["tools" ]["git" ]["author_details" ] = old_config
72- app .config_file .save ()
73- clear_cached_config (app )
74-
75-
76- @pytest .fixture
77- def set_inherit_git_author (app : Application ) -> Generator [None , None , None ]:
78- old_config = app .config_file .data ["tools" ]["git" ]["author_details" ]
79- app .config_file .data ["tools" ]["git" ]["author_details" ] = "inherit"
80-
81- old_name = app .config_file .data ["user" ]["name" ]
82- old_emails = app .config_file .data ["user" ]["emails" ]
83- app .config_file .data ["user" ]["name" ] = "Test Runner"
84- app .config_file .data ["user" ]["emails" ] = ["test.runner@example.com" ]
8555 app .config_file .save ()
8656 clear_cached_config (app )
8757 yield
88- app .config_file .data ["tools" ]["git" ]["author_details" ] = old_config
89- app .config_file .data ["user" ]["name" ] = old_name
90- app .config_file .data ["user" ]["emails" ] = old_emails
58+ app .config_file .data ["tools" ]["git" ]["author_name" ] = old_name
59+ app .config_file .data ["tools" ]["git" ]["author_email" ] = old_email
9160 app .config_file .save ()
9261 clear_cached_config (app )
9362
0 commit comments