Add web dev style guide presubmit checks and --fix support.

This commit is contained in:
Aleksey Khoroshilov
2022-12-16 14:32:20 +07:00
parent f5442c1ecf
commit 7c8709db0d
7 changed files with 109 additions and 2 deletions
+19
View File
@@ -6,6 +6,8 @@
import os
import sys
import override_utils
USE_PYTHON3 = True
PRESUBMIT_VERSION = '2.0.0'
@@ -124,3 +126,20 @@ def CheckLicense(input_api, output_api):
output_api.PresubmitPromptWarning(expected_license_message,
items=bad_files))
return result
def CheckWebDevStyle(input_api, output_api):
results = []
try:
old_sys_path = sys.path[:]
cwd = input_api.PresubmitLocalPath()
sys.path += [input_api.os_path.join(cwd, '..', 'tools')]
# pylint: disable=import-error, import-outside-toplevel
from web_dev_style import presubmit_support
with override_utils.override_scope_variable(output_api,
'PresubmitPromptWarning',
output_api.PresubmitError):
results += presubmit_support.CheckStyle(input_api, output_api)
finally:
sys.path = old_sys_path
return results
+3
View File
@@ -710,6 +710,9 @@ const util = {
if (options.verbose) {
args.push(...Array(options.verbose).fill('--verbose'))
}
if (options.fix) {
cmd_options.env.PRESUBMIT_FIX = '1'
}
util.run(cmd, args, cmd_options)
},
+1
View File
@@ -277,6 +277,7 @@ program
.option('--files <file list>',
'semicolon-separated list files to run presubmit on')
.option('--verbose [arg]', 'pass --verbose 2 for more debugging info', JSON.parse)
.option('--fix', 'try to fix found issues automatically')
.action(util.presubmit)
program
@@ -0,0 +1,24 @@
# Copyright (c) 2022 The Brave Authors. All rights reserved.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at https://mozilla.org/MPL/2.0/.
import os
import brave_node
import override_utils
@override_utils.override_function(globals())
def Run(_original_function, **kwargs):
node_args = [
brave_node.PathInNodeModules('eslint', 'bin', 'eslint'),
'--quiet',
'--resolve-plugins-relative-to',
brave_node.PathInNodeModules(),
]
if os.environ.get('PRESUBMIT_FIX') == '1':
node_args += ['--fix']
node_args += kwargs['args']
return brave_node.RunNode(node_args)
@@ -0,0 +1,12 @@
diff --git a/tools/web_dev_style/eslint.py b/tools/web_dev_style/eslint.py
index 18be4f4b076888f18538323096e84d52a8659a5c..e29cc0173d69cb9ff5d972b7c210bf25714b5049 100755
--- a/tools/web_dev_style/eslint.py
+++ b/tools/web_dev_style/eslint.py
@@ -26,6 +26,7 @@ def Run(os_path=None, args=None):
] + args)
+from import_inline import inline_file_from_src; inline_file_from_src("brave/chromium_src/tools/web_dev_style/eslint.py", globals(), locals())
if __name__ == '__main__':
import os
import sys
+35
View File
@@ -0,0 +1,35 @@
#!/usr/bin/env python3
# Copyright (c) 2022 The Brave Authors. All rights reserved.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at https://mozilla.org/MPL/2.0/.
import subprocess
import sys
import os
NODE_MODULES = os.path.join(os.path.dirname(__file__), '..', 'node_modules')
def PathInNodeModules(*args):
return os.path.join(NODE_MODULES, *args)
def RunNode(cmd_parts):
cmd = ['node'] + cmd_parts
process = subprocess.Popen(cmd,
cwd=os.getcwd(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
stdout, stderr = process.communicate()
if process.returncode != 0:
err = stderr if len(stderr) > 0 else stdout
raise RuntimeError('Command \'%s\' failed\n%s' % (' '.join(cmd), err))
return stdout
if __name__ == '__main__':
RunNode(sys.argv[1:])
+15 -2
View File
@@ -9,6 +9,7 @@
import copy
import inspect
import os
import re
import sys
@@ -129,7 +130,18 @@ def override_canned_checks(canned_checks):
'bypass_warnings': False,
'check_python': True,
}
result = original_check(input_api, output_api, **kwargs)
# pylint: disable=import-outside-toplevel
import git_cl
def RunGitWithCode(original_function, args, **kwargs):
if input_api.PRESUBMIT_FIX and '--dry-run' in args:
args.remove('--dry-run')
return original_function(args, **kwargs)
with override_utils.override_scope_function(git_cl, RunGitWithCode):
result = original_check(input_api, output_api, **kwargs)
# If presubmit generates "Please run git cl format --js" message, we
# should replace the command with "npm run format -- --js". The
# order of these replacements ensure we do this properly.
@@ -169,9 +181,10 @@ def override_canned_checks(canned_checks):
# Overrides canned checks and installs per-check file filter.
def modify_input_api(input_api):
input_api.DEFAULT_FILES_TO_SKIP += (*config['default_files_to_skip'], )
input_api.PRESUBMIT_FIX = os.environ.get('PRESUBMIT_FIX') == '1'
override_canned_checks(input_api.canned_checks)
setup_per_check_file_filter(input_api)
input_api.DEFAULT_FILES_TO_SKIP += (*config['default_files_to_skip'], )
# Disables checks or forces presubmit errors for checks listed in the config.