Commit 560c6455 authored by Christopher Lam's avatar Christopher Lam Committed by Commit Bot

[cros_colors] Add a PRESUBMIT to find removed colors.

This CL adds a PRESUBMIT to ui/chromeos/colors that greps through the
codebase for generated CSS variables for any removed colors.

Bug: 1018654
Change-Id: I011a035a6d8c28e913ebdc31f8b3f2ea88e38952
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2315370
Commit-Queue: calamity <calamity@chromium.org>
Reviewed-by: default avatarGiovanni Ortuño Urquidi <ortuno@chromium.org>
Reviewed-by: default avatarJiewei Qian  <qjw@chromium.org>
Cr-Commit-Position: refs/heads/master@{#792584}
parent 4d33990a
...@@ -7,14 +7,33 @@ See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts ...@@ -7,14 +7,33 @@ See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
for more details about the presubmit API built into depot_tools. for more details about the presubmit API built into depot_tools.
""" """
WHITELIST = [r'.+_test.py$'] TEST_ALLOWLIST = [r'.+_test.py$']
STYLE_VAR_GEN_INPUTS = [
r'^tools[\\\/]style_variable_generator[\\\/].+\.json5$'
]
def _CommonChecks(input_api, output_api):
results = input_api.canned_checks.RunUnitTestsInDirectory(
input_api, output_api, '.', allowlist=TEST_ALLOWLIST)
try:
import sys
old_sys_path = sys.path[:]
sys.path += [
input_api.os_path.join(input_api.change.RepositoryRoot(), 'tools')
]
import style_variable_generator.presubmit_support
results += (
style_variable_generator.presubmit_support.FindDeletedCSSVariables(
input_api, output_api, STYLE_VAR_GEN_INPUTS))
finally:
sys.path = old_sys_path
return results
def CheckChangeOnUpload(input_api, output_api): def CheckChangeOnUpload(input_api, output_api):
return input_api.canned_checks.RunUnitTestsInDirectory( return _CommonChecks(input_api, output_api)
input_api, output_api, '.', whitelist=WHITELIST)
def CheckChangeOnCommit(input_api, output_api): def CheckChangeOnCommit(input_api, output_api):
return input_api.canned_checks.RunUnitTestsInDirectory( return _CommonChecks(input_api, output_api)
input_api, output_api, '.', whitelist=WHITELIST)
...@@ -73,6 +73,10 @@ class BaseGenerator: ...@@ -73,6 +73,10 @@ class BaseGenerator:
globals to render their output. globals to render their output.
''' '''
@staticmethod
def GetName():
return None
def __init__(self): def __init__(self):
self.out_file_path = None self.out_file_path = None
# A map of input filepaths to their context object. # A map of input filepaths to their context object.
...@@ -139,6 +143,8 @@ class BaseGenerator: ...@@ -139,6 +143,8 @@ class BaseGenerator:
# rolled. # rolled.
data = json5.loads(json_string, data = json5.loads(json_string,
object_pairs_hook=collections.OrderedDict) object_pairs_hook=collections.OrderedDict)
if generator_name is None:
generator_name = self.GetName()
generator_context = data.get('options', {}).get(generator_name, None) generator_context = data.get('options', {}).get(generator_name, None)
self.in_file_to_context[in_file] = generator_context self.in_file_to_context[in_file] = generator_context
......
...@@ -9,21 +9,13 @@ import os ...@@ -9,21 +9,13 @@ import os
import subprocess import subprocess
import sys import sys
from css_generator import CSSStyleGenerator from css_generator import CSSStyleGenerator
from presubmit_support import RunGit
def RunGit(command):
"""Run a git subcommand, returning its output."""
command = ['git'] + command
proc = subprocess.Popen(command, stdout=subprocess.PIPE)
out = proc.communicate()[0].strip()
return out
# TODO(calamity): extend this checker to find unused C++ variables # TODO(calamity): extend this checker to find unused C++ variables
def FindInvalidCSSVariables(json_string, input_file, git_runner=RunGit): def FindInvalidCSSVariables(json_string, input_file, git_runner=RunGit):
style_generator = CSSStyleGenerator() style_generator = CSSStyleGenerator()
style_generator.AddJSONToModel(json_string, style_generator.GetName(), style_generator.AddJSONToModel(json_string, in_file=input_file)
input_file)
context = style_generator.in_file_to_context[input_file] context = style_generator.in_file_to_context[input_file]
if (not context or 'prefix' not in context): if (not context or 'prefix' not in context):
......
# 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 argparse
import os
import subprocess
import sys
from css_generator import CSSStyleGenerator
def BuildGrepQuery(deleted_names):
# Query is built as \--var-1|--var-2|... The first backslash is necessary to
# prevent --var-1 from being read as an argument. The pipes make a big OR
# query.
return '\\' + '|'.join(deleted_names)
def RunGit(command):
"""Run a git subcommand, returning its output."""
command = ['git'] + command
proc = subprocess.Popen(command, stdout=subprocess.PIPE)
out = proc.communicate()[0].strip()
return out
def FindDeletedCSSVariables(input_api, output_api, input_file_filter):
files = input_api.AffectedFiles(
file_filter=lambda f: input_api.FilterSourceFile(
f, files_to_check=input_file_filter))
def get_css_var_names_for_contents(contents_function):
style_generator = CSSStyleGenerator()
for f in files:
style_generator.AddJSONToModel('\n'.join(contents_function(f)),
in_file=f.LocalPath())
return style_generator.GetCSSVarNames()
old_names = get_css_var_names_for_contents(lambda f: f.OldContents())
new_names = get_css_var_names_for_contents(lambda f: f.NewContents())
deleted_names = old_names.difference(new_names)
if not deleted_names:
return []
# Use --full-name and -n for formatting, -E for extended regexp, and :/
# as pathspec for grepping across entire repository (assumes git > 1.9.1)
problems = RunGit([
'grep', '--full-name', '-En',
BuildGrepQuery(deleted_names), '--', ':/'
]).splitlines()
if not problems:
return []
return [
output_api.PresubmitPromptWarning(
'style_variable_generator variables were deleted but usages of ' +
'generated CSS variables were found in the codebase:',
items=problems)
]
# 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.
STYLE_VAR_GEN_INPUTS = [r'^ui[\\/]chromeos[\\/]colors[\\/].+\.json5$']
def _CommonChecks(input_api, output_api):
results = []
try:
import sys
old_sys_path = sys.path[:]
sys.path += [
input_api.os_path.join(input_api.change.RepositoryRoot(), 'tools')
]
import style_variable_generator.presubmit_support
results += (
style_variable_generator.presubmit_support.FindDeletedCSSVariables(
input_api, output_api, STYLE_VAR_GEN_INPUTS))
finally:
sys.path = old_sys_path
return results
def CheckChangeOnUpload(input_api, output_api):
return _CommonChecks(input_api, output_api)
def CheckChangeOnCommit(input_api, output_api):
return _CommonChecks(input_api, output_api)
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