PRESUBMIT: check if DEPS file is an existing file rather than path.
Bug: 382703190
Change-Id: I6fca194462fe20a4bd4dcd63a531970d91360c06
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6081832
Reviewed-by: Andrew Grieve <agrieve@chromium.org>
Commit-Queue: Andrew Grieve <agrieve@chromium.org>
Auto-Submit: Joanna Wang <jojwang@chromium.org>
Reviewed-by: Josip Sokcevic <sokcevic@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1394373}
diff --git a/PRESUBMIT_test_mocks.py b/PRESUBMIT_test_mocks.py
index e8872a6..5b3fccf0 100644
--- a/PRESUBMIT_test_mocks.py
+++ b/PRESUBMIT_test_mocks.py
@@ -73,7 +73,20 @@
self.fnmatch = fnmatch
self.json = json
self.re = re
- self.os_path = os.path
+
+ # We want os_path.exists() and os_path.isfile() to work for files
+ # that are both in the filesystem and mock files we have added
+ # via InitFiles().
+ # By setting os_path to a copy of os.path rather than directly we
+ # can not only have os_path.exists() be a combined output for fake
+ # files and real files in the filesystem.
+ import importlib.util
+ SPEC_OS_PATH = importlib.util.find_spec('os.path')
+ os_path1 = importlib.util.module_from_spec(SPEC_OS_PATH)
+ SPEC_OS_PATH.loader.exec_module(os_path1)
+ sys.modules['os_path1'] = os_path1
+ self.os_path = os_path1
+
self.platform = sys.platform
self.python_executable = sys.executable
self.python3_executable = sys.executable
@@ -107,13 +120,22 @@
if not os.path.isabs(path):
path = os.path.join(self.presubmit_local_path, path)
path = os.path.normpath(path)
- return path in files_that_exist
+ return path in files_that_exist or any(
+ f.startswith(path)
+ for f in files_that_exist) or os.path.exists(path)
+
+ def mock_isfile(path):
+ if not os.path.isabs(path):
+ path = os.path.join(self.presubmit_local_path, path)
+ path = os.path.normpath(path)
+ return path in files_that_exist or os.path.isfile(path)
def mock_glob(pattern, *args, **kwargs):
- return fnmatch.filter(files_that_exist, pattern)
+ return fnmatch.filter(files_that_exist, pattern)
# Do not stub these in the constructor to not break existing tests.
self.os_path.exists = mock_exists
+ self.os_path.isfile = mock_isfile
self.glob = mock_glob
def AffectedFiles(self, file_filter=None, include_deletes=True):