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,