Speed up presubmit by caching code-owners check

Currently, presubmit queries the gerrit server every run to check if the
code-owners plugin is enabled.

This CL caches that result in a /tmp file per repository.

This saves as much as 1.5s per `git cl presubmit`.

Change-Id: I1a3631074d1bcbb1a254caa6654fd8434f069aa2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/7227749
Commit-Queue: Josiah Kiehl <kiehl@google.com>
Reviewed-by: Yiwei Zhang <yiwzhang@google.com>
This commit is contained in:
Josiah Kiehl
2025-12-09 10:50:31 -08:00
committed by LUCI CQ
parent f520a9305e
commit 82130f468a
7 changed files with 371 additions and 12 deletions

View File

@@ -419,4 +419,31 @@ class GClientUtilsTest(trial_dir.TestCase):
if __name__ == '__main__':
unittest.main()
class SafeReplaceTest(unittest.TestCase):
def setUp(self):
self.mock_os = mock.patch('gclient_utils.os').start()
self.addCleanup(mock.patch.stopall)
def testReplace(self):
gclient_utils.safe_replace('old', 'new')
self.mock_os.replace.assert_called_with('old', 'new')
def testReplaceRetry(self):
self.mock_os.replace.side_effect = [OSError, OSError, None]
with mock.patch('time.sleep') as mock_sleep:
gclient_utils.safe_replace('old', 'new')
self.assertEqual(self.mock_os.replace.call_count, 3)
self.assertEqual(mock_sleep.call_count, 2)
def testReplaceFailure(self):
self.mock_os.replace.side_effect = OSError
with mock.patch('time.sleep'):
with self.assertRaises(OSError):
gclient_utils.safe_replace('old', 'new')
# vim: ts=2:sw=2:tw=80:et:

163
tests/gerrit_cache_test.py Executable file
View File

@@ -0,0 +1,163 @@
#!/usr/bin/env vpython3
# Copyright (c) 2025 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.
import os
import re
import shutil
import sys
import tempfile
import unittest
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, ROOT_DIR)
import gerrit_cache
import unittest.mock as mock
class AtomicFileWriterTest(unittest.TestCase):
def setUp(self):
self.test_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.test_dir)
def testSuccess(self):
path = os.path.join(self.test_dir, 'test_file')
with gerrit_cache._AtomicFileWriter(path, 'w') as f:
f.write('content')
self.assertTrue(os.path.exists(path))
with open(path, 'r') as f:
self.assertEqual(f.read(), 'content')
def testFailure(self):
path = os.path.join(self.test_dir, 'test_file')
try:
with gerrit_cache._AtomicFileWriter(path, 'w') as f:
f.write('content')
raise Exception('oops')
except Exception:
pass
self.assertFalse(os.path.exists(path))
def testOverwrite(self):
path = os.path.join(self.test_dir, 'test_file')
with open(path, 'w') as f:
f.write('old')
with gerrit_cache._AtomicFileWriter(path, 'w') as f:
f.write('new')
with open(path, 'r') as f:
self.assertEqual(f.read(), 'new')
def testOverwriteFailure(self):
path = os.path.join(self.test_dir, 'test_file')
with open(path, 'w') as f:
f.write('old')
try:
with gerrit_cache._AtomicFileWriter(path, 'w') as f:
f.write('new')
raise Exception('oops')
except Exception:
pass
with open(path, 'r') as f:
self.assertEqual(f.read(), 'old')
class GerritCacheTest(unittest.TestCase):
def setUp(self):
self.test_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.test_dir)
self.mock_git = mock.patch('gerrit_cache.scm.GIT').start()
self.addCleanup(mock.patch.stopall)
self.mock_git.GetConfig.return_value = None
def testGetSet(self):
cache = gerrit_cache.GerritCache(self.test_dir)
cache.set('key', 'value')
self.assertEqual(cache.get('key'), 'value')
# Verify persistence
cache2 = gerrit_cache.GerritCache(self.test_dir)
self.assertEqual(cache2.get('key'), 'value')
def testGetSetBoolean(self):
cache = gerrit_cache.GerritCache(self.test_dir)
cache.setBoolean('bool_key', True)
self.assertTrue(cache.getBoolean('bool_key'))
cache2 = gerrit_cache.GerritCache(self.test_dir)
self.assertTrue(cache2.getBoolean('bool_key'))
def testGetMissing(self):
cache = gerrit_cache.GerritCache(self.test_dir)
self.assertIsNone(cache.get('missing'))
def testCachePathFromEnv(self):
with mock.patch.dict(
os.environ, {
'DEPOT_TOOLS_GERRIT_CACHE_PATH':
os.path.join(self.test_dir, 'env_cache.json')
}):
cache = gerrit_cache.GerritCache(self.test_dir)
cache.set('key', 'env_value')
self.assertEqual(cache.get('key'), 'env_value')
self.assertTrue(
os.path.exists(os.path.join(self.test_dir, 'env_cache.json')))
def testCachePathFromGitConfig(self):
config_path = os.path.join(self.test_dir, 'config_cache.json')
self.mock_git.GetConfig.return_value = config_path
cache = gerrit_cache.GerritCache(self.test_dir)
self.assertEqual(cache.cache_path, config_path)
def testCachePathCreation(self):
# When no env var and no config, it should create a deterministic temp file and set config
cache = gerrit_cache.GerritCache(self.test_dir)
sanitized_path = re.sub(r'[^a-zA-Z0-9]', '_',
os.path.abspath(self.test_dir))
expected_filename = 'depot_tools_gerrit_cache_%s.json' % sanitized_path
self.assertTrue(cache.cache_path.endswith(expected_filename))
self.assertTrue(cache.cache_path.startswith(tempfile.gettempdir()))
self.mock_git.SetConfig.assert_called_with(
self.test_dir, 'depot-tools.gerrit-cache-path', cache.cache_path)
def testCachePathCreationLinux(self):
root_dir = '/usr/local/google/home/user/repo'
with mock.patch('os.path.abspath', return_value=root_dir):
cache = gerrit_cache.GerritCache(root_dir)
sanitized_path = re.sub(r'[^a-zA-Z0-9]', '_', root_dir)
expected_filename = 'depot_tools_gerrit_cache_%s.json' % sanitized_path
self.assertTrue(cache.cache_path.endswith(expected_filename))
def testCachePathCreationWindows(self):
root_dir = r'C:\Users\user\repo'
# On Linux, os.path.abspath('C:\Users\user\repo') will prepend current cwd.
with mock.patch('os.path.abspath', return_value=root_dir):
cache = gerrit_cache.GerritCache(root_dir)
sanitized_path = re.sub(r'[^a-zA-Z0-9]', '_', root_dir)
expected_filename = 'depot_tools_gerrit_cache_%s.json' % sanitized_path
self.assertTrue(cache.cache_path.endswith(expected_filename))
def testCorruptCache(self):
cache = gerrit_cache.GerritCache(self.test_dir)
# Write garbage to the cache file
with open(cache.cache_path, 'w') as f:
f.write('this is not json')
# Should not crash, just return None
self.assertIsNone(cache.get('key'))
cache.set('key', 'value')
self.assertEqual(cache.get('key'), 'value')
if __name__ == "__main__":
unittest.main()