[scm] Mock global git config scope globally.

This will be useful for writing tests which rely on shared 'global'
config between different working directories.

This also adds support for mocking 'system' (global, immutable) and
'workspace' (local, mutable). The workspace scope I think is a bit iffy
though, given how `git cl` is actually built - currently scm.GIT doesn't
really know about clone vs. workspace, and afaik no config adjustements
actually apply to the workspace scope.

Adds tests for mocked git config implementation, including bug fixes
to the current implementation revealed by the tests.

R=ayatane, yiwzhang

Change-Id: Ia56d2a81d8df6ae75d9f8d0497be0d67bdc03651
Bug: 355505750
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5759163
Reviewed-by: Yiwei Zhang <yiwzhang@google.com>
Commit-Queue: Robbie Iannucci <iannucci@chromium.org>
This commit is contained in:
Robert Iannucci
2024-08-05 22:59:27 +00:00
committed by LUCI CQ
parent cdf5599e67
commit a3fb9bad66
3 changed files with 454 additions and 99 deletions

View File

@@ -4,6 +4,7 @@
import os
import sys
import threading
from typing import Dict, List, Optional
from unittest import mock
@@ -29,21 +30,24 @@ def GIT(test: unittest.TestCase,
NOTE: The dependency on git_new_branch.create_new_branch seems pretty
circular - this functionality should probably move to scm.GIT?
"""
# TODO - remove `config` - have callers just directly call SetConfig with
# whatever config state they need.
# TODO - add `system_config` - this will be configuration which exists at
# the 'system installation' level and is immutable.
_branchref = [branchref or 'refs/heads/main']
initial_state = {}
if config is not None:
initial_state['local'] = config
global_lock = threading.Lock()
global_state = {}
def _newBranch(branchref):
_branchref[0] = branchref
patches: List[mock._patch] = [
mock.patch(
'scm.GIT._new_config_state',
side_effect=lambda root: scm.GitConfigStateTest(initial_state)),
mock.patch('scm.GIT.GetBranchRef',
side_effect=lambda _root: _branchref[0]),
mock.patch('scm.GIT._new_config_state',
side_effect=lambda _: scm.GitConfigStateTest(
global_lock, global_state, local_state=config)),
mock.patch('scm.GIT.GetBranchRef', side_effect=lambda _: _branchref[0]),
mock.patch('git_new_branch.create_new_branch', side_effect=_newBranch)
]