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 <ehmaldonado@chromium.org>
Commit-Queue: Saagar Sanghavi <saagarsanghavi@google.com>
changes/39/2327839/9
Saagar Sanghavi 5 years ago committed by LUCI CQ
parent 343f63643b
commit 98b332f2db

@ -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:

@ -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,

Loading…
Cancel
Save