From 98b332f2dbbd25add6b0904cc9d01608ceeaa52d Mon Sep 17 00:00:00 2001 From: Saagar Sanghavi Date: Fri, 31 Jul 2020 17:19:15 +0000 Subject: [PATCH] Fixed relative path to work when presubmit run from any directory Bug: 1111829 Change-Id: I75c616f4fa9c16c19e732e43ba740bb67f3eb827 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2327839 Reviewed-by: Edward Lesmes Commit-Queue: Saagar Sanghavi --- presubmit_support.py | 8 +++++--- tests/presubmit_unittest.py | 14 ++++++++------ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/presubmit_support.py b/presubmit_support.py index b64a7bc9a..cd2eb7b08 100755 --- a/presubmit_support.py +++ b/presubmit_support.py @@ -1529,10 +1529,10 @@ class PresubmitExecuter(object): Return: A list of result objects, empty if no problems. """ - # Change to the presubmit file's directory to support local imports. main_path = os.getcwd() - os.chdir(os.path.dirname(presubmit_path)) + presubmit_dir = os.path.dirname(presubmit_path) + os.chdir(presubmit_dir) # Load the presubmit script into context. input_api = InputApi(self.change, presubmit_path, self.committing, @@ -1560,8 +1560,10 @@ class PresubmitExecuter(object): # TODO (crbug.com/1106943): Dive into each of the individual checks - rel_path = os.path.relpath(os.getcwd(), main_path) + # Get path of presubmit directory relative to repository root. # Always use forward slashes, so that path is same in *nix and Windows + root = input_api.change.RepositoryRoot() + rel_path = os.path.relpath(presubmit_dir, root) rel_path = rel_path.replace(os.path.sep, '/') with rdb_wrapper.setup_rdb(function_name, rel_path) as my_status: diff --git a/tests/presubmit_unittest.py b/tests/presubmit_unittest.py index 7e2ffad95..e21c957a6 100755 --- a/tests/presubmit_unittest.py +++ b/tests/presubmit_unittest.py @@ -964,25 +964,27 @@ def CheckChangeOnCommit(input_api, output_api): presubmit.main( ['--root', self.fake_root_dir, 'random_file.txt', '--post_upload'])) - @mock.patch( - 'presubmit_support.ListRelevantPresubmitFiles', - return_value=['PRESUBMIT.py']) + @mock.patch('presubmit_support.ListRelevantPresubmitFiles') def testMainUnversioned(self, *_mocks): gclient_utils.FileRead.return_value = '' scm.determine_scm.return_value = None + presubmit.ListRelevantPresubmitFiles.return_value = [ + os.path.join(self.fake_root_dir, 'PRESUBMIT.py') + ] self.assertEqual( 0, presubmit.main(['--root', self.fake_root_dir, 'random_file.txt'])) - @mock.patch( - 'presubmit_support.ListRelevantPresubmitFiles', - return_value=['PRESUBMIT.py']) + @mock.patch('presubmit_support.ListRelevantPresubmitFiles') def testMainUnversionedChecksFail(self, *_mocks): gclient_utils.FileRead.return_value = ( 'def CheckChangeOnUpload(input_api, output_api):\n' ' return [output_api.PresubmitError("!!")]\n') scm.determine_scm.return_value = None + presubmit.ListRelevantPresubmitFiles.return_value = [ + os.path.join(self.fake_root_dir, 'PRESUBMIT.py') + ] self.assertEqual( 1,