Package: pytest-gitconfig
pytest-gitconfigProvide a gitconfig sandbox for testingGetting startedInstallpytest-gitconfig:# pippipinstallpytest-gitconfig# pipenvpipenvinstallpytest-gitconfig# PDMpdmaddpytest-gitconfigThen thedefault_gitconfigsession fixture will be automatically loaded for the session
providing isolation from gloval user defined values.If you want to customize or depend on itfrom__future__importannotationsfromtypingimportTYPE_CHECKINGifTYPE_CHECKING:frompytest_gitconfigimportGitConfig@pytest.fixturedefdefault_git_user_name()->str:return"John Doe"@pytest.fixture(scope="session",autouse=True)deffixture_depending_on_default_gitconfig(default_gitconfig:GitConfig)->Whatever:# You can set values, the following statements are equivalentsdefault_gitconfig.set({"some":{key:value}})# nested dicts formdefault_gitconfig.set({"some.key":value})# dict with dotted keys form# Or read themdata=default_gitconfig.get("some.key")data=default_gitconfig.get("some.key","fallback")# If you need the path to the gitconfig fileassertstr(default_gitconfig)==str(default_gitconfig.path)returnwhateverNote that thedefault_gitconfigfixture being session-scoped (avoiding the performance hit of creating a gitconfig file for each test),
set values are persistent for the whole session and should be defined once preferably in yourconftest.py.
But if you need to temporarily override some value, you can use theoverride()context manager which is accepting the same parameters asset().This allows to override it directly during a test:from__future__importannotationsfromtypingimportTYPE_CHECKING,IteratorifTYPE_CHECKING:frompytest_gitconfigimportGitConfigdeftest_something(default_gitconfig):withgitconfig.override({"other.key":value}):# Do something depending on those overridden valuesBut to test some value in some specific tests, its best to rely ongitconfigwhich is providing a function-scoped GitConfig:from__future__importannotationsfromtypingimportTYPE_CHECKINGifTYPE_CHECKING:frompytest_gitconfigimportGitConfigdeftest_something(gitconfig:GitConfig):gitconfig.set({"other.key":value})# Only valid for this test# Do something depending on those overridden valuesA classical setup being:default to using the session-scopeddefault_gitconfigto ensure isolation.some specific test cases relying on some specific settings set on thegitconfigfunction-scoped fixtureThis has the following benefits:session isolation is done only oncetest having specific settings does not impact other teststest having specific settings can be run in parallelProvided fixturesFunction-scopedgitconfig -> pytest_gitconfig.GitConfigThis is the main fixture which is creating a new and clean git config file for the tested function.It inherit fromdefault_gitconfig(meaning that all values set ondefault_gitconfigwill be set ongitconfig)It works by monkeypatching theGIT_CONFIG_GLOBALenvironment variable.
So, if you rely on this in a context whereos.environis ignored, you should patch it yourself using this fixture.git_user_name -> str | NoneProvide the initialuser.namesetting forgitconfig.
IfNone,user.namewill inherit its value fromdefault_config,
so most probably fromdefault_git_user_nameif not overridden.git_user_email -> str | NoneProvide the initialuser.emailsetting forgitconfig.
IfNone,user.emailwill inherit its value fromdefault_config,
so most probably fromdefault_git_user_emailif not overridden.git_init_default_branch -> str | NoneProvide the initialinit.defaultBranchsetting forgitconfig.
IfNone,init.defaultBranchwill inherit its value fromdefault_config,
so most probablydefault_git_init_default_branchif not overridden.Session-scopeddefalt_gitconfig -> pytest_gitconfig.GitConfigThis is the main fixture which is creating a new and clean git config file for the test session.
It is loaded automatically if you havepytest-gitconfiginstalled.By default, it will set 3 settings:user.nameuser.emailinit.defaultBranchIt works by monkeypatching theGIT_CONFIG_GLOBALenvironment variable.
So, if you rely on this in a context whereos.environis ignored, you should patch it yourself using this fixture.default_git_user_name -> strProvide the initialuser.namesetting. By defaultpytest_gitconfig.DEFAULT_GIT_USER_NAME.
Override to provide a different initial value.default_git_user_email -> strProvide the initialuser.emailsetting. By defaultpytest_gitconfig.DEFAULT_GIT_USER_EMAIL.
Override to provide a different initial value.default_git_init_default_branch -> strProvide the initialinit.defaultBranchsetting. By defaultpytest_gitconfig.DEFAULT_GIT_BRANCH(main).
Override to provide a different initial value.sessionpatch -> pytest.MonkeyPatchApytest.MonkeyPatchsession instance.APIpytest_gitconfig.GitConfigAn object materializing a givengitconfigfile.set(self, data: Mapping[str, Any])Write somegitconfigvalues.
It accepts adictof parsed data sections as dict or dotted-key-values.get(self, key: str, default: Any = _UNSET)Get a setting given its dotted key. Get a 2nd default value. RaiseKeyErrorif config does not exists anddefaultis not providedoverride(self, data: Mapping[str, Any]) -> Iterator[GitConfig]A context manager setting the values and restoring them on exit.
Accept the same format a theset()method.