From 03434d28f434c813ee84debb9cb3c3d905105218 Mon Sep 17 00:00:00 2001 From: Allen Li Date: Fri, 26 Jul 2024 19:23:54 +0000 Subject: [PATCH] [git_auth] Add git_auth tests Bug: b/351024645 Change-Id: Ie8808de63d09c4a89187f81a5782e09fe73c8b0f Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5698748 Commit-Queue: Allen Li Reviewed-by: Yiwei Zhang Reviewed-by: Robbie Iannucci --- scm.py | 17 ++++- tests/git_auth_test.py | 144 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+), 2 deletions(-) create mode 100755 tests/git_auth_test.py diff --git a/scm.py b/scm.py index 645674b936..9877f99890 100644 --- a/scm.py +++ b/scm.py @@ -180,9 +180,9 @@ class CachedGitConfigState(object): If pattern is None, this returns all config items. """ if pattern is None: - pred = lambda _: True + pred = lambda _: True else: - pred = re.compile(pattern).match + pred = re.compile(pattern).match for name, values in sorted(self._maybe_load_config().items()): if pred(name): for value in values: @@ -431,6 +431,19 @@ class GIT(object): cls._CONFIG_CACHE[key] = ret return ret + @classmethod + def _dump_config_state(cls) -> Dict[str, GitFlatConfigData]: + """Dump internal config state. + + Used for testing. This will NOT work properly in non-test + contexts as it relies on internal caches. + """ + with cls._CONFIG_CACHE_LOCK: + state = {} + for key, val in cls._CONFIG_CACHE.items(): + state[str(key)] = val._maybe_load_config() + return state + @staticmethod def ApplyEnvVars(kwargs): env = kwargs.pop('env', None) or os.environ.copy() diff --git a/tests/git_auth_test.py b/tests/git_auth_test.py new file mode 100755 index 0000000000..f554317d7d --- /dev/null +++ b/tests/git_auth_test.py @@ -0,0 +1,144 @@ +#!/usr/bin/env vpython3 +# coding=utf-8 +# Copyright (c) 2012 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. +"""Unit tests for git_cl.py.""" + +import logging +import os +import sys +import unittest + +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +import git_auth +import scm +import scm_mock + + +class TestConfigChanger(unittest.TestCase): + + def setUp(self): + scm_mock.GIT(self) + + def test_apply_new_auth(self): + git_auth.ConfigChanger( + mode=git_auth.ConfigMode.NEW_AUTH, + remote_url= + 'https://chromium.googlesource.com/chromium/tools/depot_tools.git', + ).apply('/some/fake/dir') + want = { + '/some/fake/dir': { + 'credential.https://chromium.googlesource.com/.helper': + ['', 'luci'], + 'http.cookieFile': [''], + }, + } + self.assertEqual(scm.GIT._dump_config_state(), want) + + def test_apply_new_auth_sso(self): + git_auth.ConfigChanger( + mode=git_auth.ConfigMode.NEW_AUTH_SSO, + remote_url= + 'https://chromium.googlesource.com/chromium/tools/depot_tools.git', + ).apply('/some/fake/dir') + want = { + '/some/fake/dir': { + 'protocol.sso.allow': ['always'], + 'url.sso://chromium/.insteadOf': + ['https://chromium.googlesource.com/'], + 'http.cookieFile': [''], + }, + } + self.assertEqual(scm.GIT._dump_config_state(), want) + + def test_apply_no_auth(self): + git_auth.ConfigChanger( + mode=git_auth.ConfigMode.NO_AUTH, + remote_url= + 'https://chromium.googlesource.com/chromium/tools/depot_tools.git', + ).apply('/some/fake/dir') + want = { + '/some/fake/dir': {}, + } + self.assertEqual(scm.GIT._dump_config_state(), want) + + def test_apply_chain_sso_new(self): + git_auth.ConfigChanger( + mode=git_auth.ConfigMode.NEW_AUTH_SSO, + remote_url= + 'https://chromium.googlesource.com/chromium/tools/depot_tools.git', + ).apply('/some/fake/dir') + git_auth.ConfigChanger( + mode=git_auth.ConfigMode.NEW_AUTH, + remote_url= + 'https://chromium.googlesource.com/chromium/tools/depot_tools.git', + ).apply('/some/fake/dir') + want = { + '/some/fake/dir': { + 'credential.https://chromium.googlesource.com/.helper': + ['', 'luci'], + 'http.cookieFile': [''], + }, + } + self.assertEqual(scm.GIT._dump_config_state(), want) + + def test_apply_chain_new_sso(self): + git_auth.ConfigChanger( + mode=git_auth.ConfigMode.NEW_AUTH, + remote_url= + 'https://chromium.googlesource.com/chromium/tools/depot_tools.git', + ).apply('/some/fake/dir') + git_auth.ConfigChanger( + mode=git_auth.ConfigMode.NEW_AUTH_SSO, + remote_url= + 'https://chromium.googlesource.com/chromium/tools/depot_tools.git', + ).apply('/some/fake/dir') + want = { + '/some/fake/dir': { + 'protocol.sso.allow': ['always'], + 'url.sso://chromium/.insteadOf': + ['https://chromium.googlesource.com/'], + 'http.cookieFile': [''], + }, + } + self.assertEqual(scm.GIT._dump_config_state(), want) + + def test_apply_chain_new_no(self): + git_auth.ConfigChanger( + mode=git_auth.ConfigMode.NEW_AUTH, + remote_url= + 'https://chromium.googlesource.com/chromium/tools/depot_tools.git', + ).apply('/some/fake/dir') + git_auth.ConfigChanger( + mode=git_auth.ConfigMode.NO_AUTH, + remote_url= + 'https://chromium.googlesource.com/chromium/tools/depot_tools.git', + ).apply('/some/fake/dir') + want = { + '/some/fake/dir': {}, + } + self.assertEqual(scm.GIT._dump_config_state(), want) + + def test_apply_chain_sso_no(self): + git_auth.ConfigChanger( + mode=git_auth.ConfigMode.NEW_AUTH_SSO, + remote_url= + 'https://chromium.googlesource.com/chromium/tools/depot_tools.git', + ).apply('/some/fake/dir') + git_auth.ConfigChanger( + mode=git_auth.ConfigMode.NO_AUTH, + remote_url= + 'https://chromium.googlesource.com/chromium/tools/depot_tools.git', + ).apply('/some/fake/dir') + want = { + '/some/fake/dir': {}, + } + self.assertEqual(scm.GIT._dump_config_state(), want) + + +if __name__ == '__main__': + logging.basicConfig( + level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) + unittest.main()