Commit 98b332f2 authored by Saagar Sanghavi's avatar Saagar Sanghavi Committed by LUCI CQ

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/+/2327839Reviewed-by: 's avatarEdward Lesmes <ehmaldonado@chromium.org>
Commit-Queue: Saagar Sanghavi <saagarsanghavi@google.com>
parent 343f6364
...@@ -1529,10 +1529,10 @@ class PresubmitExecuter(object): ...@@ -1529,10 +1529,10 @@ class PresubmitExecuter(object):
Return: Return:
A list of result objects, empty if no problems. A list of result objects, empty if no problems.
""" """
# Change to the presubmit file's directory to support local imports. # Change to the presubmit file's directory to support local imports.
main_path = os.getcwd() 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. # Load the presubmit script into context.
input_api = InputApi(self.change, presubmit_path, self.committing, input_api = InputApi(self.change, presubmit_path, self.committing,
...@@ -1560,8 +1560,10 @@ class PresubmitExecuter(object): ...@@ -1560,8 +1560,10 @@ class PresubmitExecuter(object):
# TODO (crbug.com/1106943): Dive into each of the individual checks # 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 # 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, '/') rel_path = rel_path.replace(os.path.sep, '/')
with rdb_wrapper.setup_rdb(function_name, rel_path) as my_status: with rdb_wrapper.setup_rdb(function_name, rel_path) as my_status:
......
...@@ -964,25 +964,27 @@ def CheckChangeOnCommit(input_api, output_api): ...@@ -964,25 +964,27 @@ def CheckChangeOnCommit(input_api, output_api):
presubmit.main( presubmit.main(
['--root', self.fake_root_dir, 'random_file.txt', '--post_upload'])) ['--root', self.fake_root_dir, 'random_file.txt', '--post_upload']))
@mock.patch( @mock.patch('presubmit_support.ListRelevantPresubmitFiles')
'presubmit_support.ListRelevantPresubmitFiles',
return_value=['PRESUBMIT.py'])
def testMainUnversioned(self, *_mocks): def testMainUnversioned(self, *_mocks):
gclient_utils.FileRead.return_value = '' gclient_utils.FileRead.return_value = ''
scm.determine_scm.return_value = None scm.determine_scm.return_value = None
presubmit.ListRelevantPresubmitFiles.return_value = [
os.path.join(self.fake_root_dir, 'PRESUBMIT.py')
]
self.assertEqual( self.assertEqual(
0, 0,
presubmit.main(['--root', self.fake_root_dir, 'random_file.txt'])) presubmit.main(['--root', self.fake_root_dir, 'random_file.txt']))
@mock.patch( @mock.patch('presubmit_support.ListRelevantPresubmitFiles')
'presubmit_support.ListRelevantPresubmitFiles',
return_value=['PRESUBMIT.py'])
def testMainUnversionedChecksFail(self, *_mocks): def testMainUnversionedChecksFail(self, *_mocks):
gclient_utils.FileRead.return_value = ( gclient_utils.FileRead.return_value = (
'def CheckChangeOnUpload(input_api, output_api):\n' 'def CheckChangeOnUpload(input_api, output_api):\n'
' return [output_api.PresubmitError("!!")]\n') ' return [output_api.PresubmitError("!!")]\n')
scm.determine_scm.return_value = None scm.determine_scm.return_value = None
presubmit.ListRelevantPresubmitFiles.return_value = [
os.path.join(self.fake_root_dir, 'PRESUBMIT.py')
]
self.assertEqual( self.assertEqual(
1, 1,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment