Commit 5df957f8 authored by Dan Beam's avatar Dan Beam Committed by Commit Bot

Add presubmit to check that folks are adding optimized .svg files

Bug: 954683

Change-Id: Id7b8d1da4a69c809de4c2cb943e49c61f77990d9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1552569
Commit-Queue: Dan Beam <dbeam@chromium.org>
Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#653629}
parent 650f7430
...@@ -100,6 +100,20 @@ def RunOptimizeWebUiTests(input_api, output_api): ...@@ -100,6 +100,20 @@ def RunOptimizeWebUiTests(input_api, output_api):
return input_api.canned_checks.RunUnitTests(input_api, output_api, tests) return input_api.canned_checks.RunUnitTests(input_api, output_api, tests)
def _CheckSvgsOptimized(input_api, output_api):
results = []
try:
import sys
old_sys_path = sys.path[:]
cwd = input_api.PresubmitLocalPath()
sys.path += [input_api.os_path.join(cwd, '..', '..', '..', 'tools')]
from resources import svgo_presubmit
results += svgo_presubmit.CheckOptimized(input_api, output_api)
finally:
sys.path = old_sys_path
return results
def _CheckWebDevStyle(input_api, output_api): def _CheckWebDevStyle(input_api, output_api):
results = [] results = []
...@@ -108,8 +122,8 @@ def _CheckWebDevStyle(input_api, output_api): ...@@ -108,8 +122,8 @@ def _CheckWebDevStyle(input_api, output_api):
old_sys_path = sys.path[:] old_sys_path = sys.path[:]
cwd = input_api.PresubmitLocalPath() cwd = input_api.PresubmitLocalPath()
sys.path += [input_api.os_path.join(cwd, '..', '..', '..', 'tools')] sys.path += [input_api.os_path.join(cwd, '..', '..', '..', 'tools')]
import web_dev_style.presubmit_support from web_dev_style import presubmit_support
results += web_dev_style.presubmit_support.CheckStyle(input_api, output_api) results += presubmit_support.CheckStyle(input_api, output_api)
finally: finally:
sys.path = old_sys_path sys.path = old_sys_path
...@@ -126,6 +140,7 @@ def _CheckChangeOnUploadOrCommit(input_api, output_api): ...@@ -126,6 +140,7 @@ def _CheckChangeOnUploadOrCommit(input_api, output_api):
affected_files = [input_api.os_path.basename(f.LocalPath()) for f in affected] affected_files = [input_api.os_path.basename(f.LocalPath()) for f in affected]
if webui_sources.intersection(set(affected_files)): if webui_sources.intersection(set(affected_files)):
results += RunOptimizeWebUiTests(input_api, output_api) results += RunOptimizeWebUiTests(input_api, output_api)
results += _CheckSvgsOptimized(input_api, output_api)
results += _CheckWebDevStyle(input_api, output_api) results += _CheckWebDevStyle(input_api, output_api)
results += input_api.canned_checks.CheckPatchFormatted(input_api, output_api, results += input_api.canned_checks.CheckPatchFormatted(input_api, output_api,
check_js=True) check_js=True)
......
agrieve@chromium.org agrieve@chromium.org
per-file *svgo*=dbeam@chromium.org
per-file PRESUBMIT.py=dbeam@chromium.org
per-file filter_resource_whitelist.*=estevenson@chromium.org per-file filter_resource_whitelist.*=estevenson@chromium.org
per-file generate_resource_whitelist.*=estevenson@chromium.org per-file generate_resource_whitelist.*=estevenson@chromium.org
# Copyright 2019 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.
def CheckChangeOnUpload(*args):
return _CommonChecks(*args)
def CheckChangeOnCommit(*args):
return _CommonChecks(*args)
def _CommonChecks(input_api, output_api):
cwd = input_api.PresubmitLocalPath()
path = input_api.os_path
files = [path.basename(f.LocalPath()) for f in input_api.AffectedFiles()]
if any(f for f in files if f.startswith('svgo_presubmit')):
tests = [path.join(cwd, 'svgo_presubmit_test.py')]
return input_api.canned_checks.RunUnitTests(input_api, output_api, tests)
return []
#!/usr/bin/env python
# Copyright 2019 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.
def Run(os_path=None, args=None):
_HERE_PATH = os_path.dirname(os_path.realpath(__file__))
_SRC_PATH = os_path.normpath(os_path.join(_HERE_PATH, '..', '..'))
import sys
old_sys_path = sys.path[:]
sys.path.append(os_path.join(_SRC_PATH, 'third_party', 'node'))
try:
import node, node_modules
finally:
sys.path = old_sys_path
return node.RunNode([node_modules.PathToSvgo()] + args)
if __name__ == '__main__':
import os
import sys
print Run(os_path=os.path, args=sys.argv[1:])
# Copyright 2019 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.
def CheckOptimized(input_api, output_api):
file_filter = lambda f: f.LocalPath().endswith('.svg')
svgs = input_api.AffectedFiles(file_filter=file_filter, include_deletes=False)
if not svgs:
return []
import svgo
unoptimized = []
for f in svgs:
output = svgo.Run(input_api.os_path, ['-o', '-', f.AbsoluteLocalPath()])
if output.strip() != '\n'.join(f.NewContents()).strip():
unoptimized.append(f.LocalPath())
if unoptimized:
instructions = 'Run tools/resources/svgo.py on these files to optimize:'
msg = '\n '.join([instructions] + unoptimized)
return [output_api.PresubmitNotifyResult(msg)]
return []
#!/usr/bin/env python
# Copyright 2017 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 svgo_presubmit
import sys
import tempfile
import unittest
_HERE_PATH = os.path.dirname(__file__)
sys.path.append(os.path.join(_HERE_PATH, '..', '..'))
from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi, MockFile
_OPTIMIZED_SVG = '''
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="#757575"><path d="M10 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V8c0-1.1-.9-2-2-2h-8l-2-2z"/></svg>
'''.strip()
_UNOPTIMIZED_SVG = '''
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#757575">
<path d="M10 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V8c0-1.1-.9-2-2-2h-8l-2-2z"></path>
</svg>
'''
class SvgPresubmitTest(unittest.TestCase):
def tearDown(self):
os.remove(self._tmp_file)
def check_contents(self, file_contents):
tmp_args = {'suffix': '.svg', 'dir': _HERE_PATH, 'delete': False}
with tempfile.NamedTemporaryFile(**tmp_args) as f:
self._tmp_file = f.name
f.write(file_contents)
input_api = MockInputApi()
input_api.files = [MockFile(os.path.abspath(self._tmp_file), file_contents.splitlines())]
input_api.presubmit_local_path = _HERE_PATH
return svgo_presubmit.CheckOptimized(input_api, MockOutputApi())
def testUnoptimizedSvg(self):
results = self.check_contents(_UNOPTIMIZED_SVG)
self.assertEquals(len(results), 1)
self.assertTrue(results[0].type == 'notify')
self.assertTrue('svgo' in results[0].message)
def testOptimizedSvg(self):
self.assertEquals(len(self.check_contents(_OPTIMIZED_SVG)), 0)
if __name__ == '__main__':
unittest.main()
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
import os
def CheckChangeOnUpload(input_api, output_api): def CheckChangeOnUpload(input_api, output_api):
return _CommonChecks(input_api, output_api) return _CommonChecks(input_api, output_api)
...@@ -50,11 +49,22 @@ translation from the place using the shared code. For an example: see ...@@ -50,11 +49,22 @@ translation from the place using the shared code. For an example: see
<cr-dialog>#closeText (http://bit.ly/2eLEsqh).""")] <cr-dialog>#closeText (http://bit.ly/2eLEsqh).""")]
def _CommonChecks(input_api, output_api): def _CheckSvgsOptimized(input_api, output_api):
results = []
try:
import sys
old_sys_path = sys.path[:]
cwd = input_api.PresubmitLocalPath()
sys.path += [input_api.os_path.join(cwd, '..', '..', '..', 'tools')]
from resources import svgo_presubmit
results += svgo_presubmit.CheckOptimized(input_api, output_api)
finally:
sys.path = old_sys_path
return results
def _CheckWebDevStyle(input_api, output_api):
results = [] results = []
results += _CheckForTranslations(input_api, output_api)
results += input_api.canned_checks.CheckPatchFormatted(input_api, output_api,
check_js=True)
try: try:
import sys import sys
old_sys_path = sys.path[:] old_sys_path = sys.path[:]
...@@ -67,3 +77,13 @@ def _CommonChecks(input_api, output_api): ...@@ -67,3 +77,13 @@ def _CommonChecks(input_api, output_api):
finally: finally:
sys.path = old_sys_path sys.path = old_sys_path
return results return results
def _CommonChecks(input_api, output_api):
results = []
results += _CheckForTranslations(input_api, output_api)
results += _CheckSvgsOptimized(input_api, output_api)
results += _CheckWebDevStyle(input_api, output_api)
results += input_api.canned_checks.CheckPatchFormatted(input_api, output_api,
check_js=True)
return results
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