Commit 8ad69166 authored by garzonou's avatar garzonou Committed by Commit Bot

add presubmit test of relative path inclusion for headers in //ios/web_view/public

Change-Id: I4cb1539a93be286bdaaef436d94da036d5aa86d2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2011629
Commit-Queue: Jiajun Ou <garzonou@google.com>
Reviewed-by: default avatarHiroshi Ichikawa <ichikawa@chromium.org>
Reviewed-by: default avatarOlivier Robin <olivierrobin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#752622}
parent f8d01800
# Copyright 2020 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Presubmit script for ios/web_view.
See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
for more details about the presubmit API built into depot_tools.
"""
import os
INCLUSION_PREFIXES = ('#import "', '#include "')
def _CheckAbsolutePathInclusionInPublicHeaders(input_api, output_api):
"""Checks if all affected headers under //ios/web_view/public only include
the headers in the same directory by using relative path inclusions.
Because only these headers will be exported to the client side code,
and path above public/ will be changed, the clients will not find the
headers that are not in that public directory, and relative path
inclusions should be used.
"""
error_items = [] # [(file_path, lineno, corrected_inclusion_path)]
normpath = os.path.normpath
public_dir = normpath('%s/public/' % input_api.PresubmitLocalPath())
files_under_public_dir = list(filter(
lambda f: normpath(f.AbsoluteLocalPath()).startswith(public_dir),
input_api.change.AffectedFiles()))
for f in files_under_public_dir:
_, ext = os.path.splitext(f.LocalPath())
if ext != '.h':
continue
for idx, line in enumerate(f.NewContents()):
lineno = idx + 1
if line.startswith(INCLUSION_PREFIXES) and '/' in line:
error_items.append((f.AbsoluteLocalPath(),
lineno,
line[line.rfind('/')+1:-1])) # :-1 to exclude "
if len(error_items) == 0:
return []
plural_suffix = '' if len(error_items) == 1 else 's'
error_message = '\n'.join([
'Found header file%(plural)s with absolute path inclusion%(plural)s '
'in //ios/web_view/public.\n'
'You can only include header files in //ios/web_view/public (no '
'subdirectory) using relative path inclusions in the following '
'file%(plural)s:\n' % {'plural': plural_suffix}
])
error_message += '\n'.join([
' %(file_path)s [line %(lineno)d]:\n'
' Do you mean "%(corrected)s"?' % {
'file_path': i[0], 'lineno': i[1], 'corrected': i[2]
} for i in error_items
]) + '\n'
return [output_api.PresubmitError(error_message)]
def CheckChangeOnCommit(input_api, output_api):
results = []
results.extend(
_CheckAbsolutePathInclusionInPublicHeaders(input_api, output_api))
return results
# Copyright 2020 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import os
import sys
import unittest
import PRESUBMIT
# append the path of src/ to sys.path to import PRESUBMIT_test_mocks
SRC_IOS_WEB_VIEW_PATH = os.path.dirname(os.path.abspath(__file__))
SRC_PATH = os.path.dirname(os.path.dirname(SRC_IOS_WEB_VIEW_PATH))
sys.path.append(SRC_PATH)
import PRESUBMIT_test_mocks
class InclusionPathCheckerTest(unittest.TestCase):
"""Test the _CheckAbsolutePathInclusionInPublicHeaders presubmit check."""
def testInclusionPathChecker(self):
bads = [
('#import "ios/web_view/aaa_imported.h"', 'ios/web_view/public/aaa.h'),
('#include "ios/web_view/eee_imported.h"', 'ios/web_view/public/eee.h'),
('#include "base/logging.h"', 'ios/web_view/public/fff.h'),
('#import "ios/web_view/public/ggg_imported.h"',
'ios/web_view/public/ggg.h'),
('#import "subdirectory/hhh_imported.h"', 'ios/web_view/public/hhh.h'),
]
goods = [
('#import "ios/web_view/bbb_imported.h"', 'ios/web_view/shell/bbb.h'),
('#import "ccc_imported.h"', 'ios/web_view/public/ccc.h'),
('#import <UIKit/UIKit.h>', 'ios/web_view/public/ddd.h'),
]
normal_code = '''
/**
* Some random comments here.
* Write #include "base/logging.h" to use logging functions.
*/
int main() {
double a = 1.0 / 2.0;
const char* str = "Hello, World!"; // a string to print
printf(str);
}'''
bads = [((code + normal_code).split('\n'),
SRC_PATH + '/' + path) for code, path in bads]
goods = [((code + normal_code).split('\n'),
SRC_PATH + '/' + path) for code, path in goods]
mock_input = PRESUBMIT_test_mocks.MockInputApi()
mock_input.presubmit_local_path = SRC_IOS_WEB_VIEW_PATH
mock_input.change = PRESUBMIT_test_mocks.MockChange([
PRESUBMIT_test_mocks.MockFile(file_path, code)
for code, file_path in (bads + goods)])
mock_output = PRESUBMIT_test_mocks.MockOutputApi()
errors = PRESUBMIT._CheckAbsolutePathInclusionInPublicHeaders(mock_input,
mock_output)
self.assertEqual(len(errors), 1)
self.assertEqual('error', errors[0].type)
self.assertTrue('with absolute path inclusion' in errors[0].message)
for _, file_path in bads:
self.assertTrue(file_path in errors[0].message)
for _, file_path in goods:
self.assertFalse(file_path in errors[0].message)
if __name__ == '__main__':
unittest.main()
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