blob: c62035b7e47ab0c49a0151dc1374cf68cac66e72 [file] [log] [blame]
# Copyright 2020 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Recipe for running Tricium analyzers."""
import functools
import collections
from recipe_engine.post_process import MustRun
from PB.recipes.fuchsia.tricium.tricium import InputProperties
DEPS = [
"fuchsia/buildbucket_util",
"fuchsia/git",
"fuchsia/git_checkout",
"fuchsia/tricium_analyze",
"recipe_engine/cipd",
"recipe_engine/context",
"recipe_engine/path",
"recipe_engine/properties",
"recipe_engine/raw_io",
"recipe_engine/step",
]
PROPERTIES = InputProperties
# We'll skip running checks on any files for which this git attribute is
# explicitly unset. We *will* run checks on all files for which this attribute
# is unspecified, or explicitly set.
TRICIUM_GIT_ATTR = "tricium"
# Namedtuple to simulate a class generated by jiri checkout because flutter is
# not using jiri.
_CheckoutResult = collections.namedtuple(
'_CheckoutResult', ['root_dir', 'release_version']
)
REMOTE='https://flutter.googlesource.com/recipes'
def RunSteps(api, props):
with api.context(infra_steps=True):
checkout_path = api.path['start_dir'].join('recipes')
api.git_checkout(REMOTE, path=checkout_path)
# tricium is expecting a dictionary as a checkout.
checkout = _CheckoutResult(checkout_path, '')
# If specified, download CIPD packages.
if props.cipd_packages:
with api.step.nest("ensure_packages"):
with api.context(infra_steps=True):
cipd_dir = api.path['start_dir'].join("cipd")
pkgs = api.cipd.EnsureFile()
for package in props.cipd_packages:
pkgs.add_package(
package.name, package.version, subdir=package.subdir
)
api.cipd.ensure(cipd_dir, pkgs)
api.tricium_analyze.yapf = cipd_dir.join("yapf")
api.tricium_analyze.check_commit_message()
with api.context(cwd=checkout_path):
api.step('git log', ['git', 'log', '--oneline', '-n', '3'])
paths = api.git.get_changed_files(
"get changed files",
deleted=False,
# If we include submodules then the output will include the
# directory paths of those submodules, but tricium_analyze operates
# on files, not directories. If we ever want to analyze changes to
# files within submodules, we can instead do a submodule-aware diff
# to get the paths to changed files within the submodule.
ignore_submodules=True,
)
api.tricium_analyze.checkout = checkout
api.tricium_analyze(
paths,
enabled_analyzers=props.analyses,
enabled_luci_analyzers=props.luci_analyzers,
)
def GenTests(api):
DIFF = """diff --git a/{0} b/{0}
index e684c1e..a76a10e 100644
--- a/{0}
+++ b/{0}
@@ -1,2 +1,4 @@
+ foo
+ bar
"""
def changed_files_data(files):
return api.git.get_changed_files("get changed files", files)
def change_diff_data(filename):
return api.step_data(
f"analyze {filename}.get change diff",
api.raw_io.stream_output_text(DIFF.format(filename)),
)
def formatted_diff_data(filename):
return api.step_data(
f"analyze {filename}.get formatted diff",
api.raw_io.stream_output_text(DIFF.format(filename)),
)
def properties(cipd_packages=(), **kwargs):
defaults = dict(
manifest="flower",
remote="https://fuchsia.googlesource.com/integration",
)
# Using CIPD packages implies that we don't need to do a build, and
# vice versa.
if cipd_packages:
defaults["cipd_packages"] = cipd_packages
defaults.update(kwargs)
return api.properties(**defaults)
yield api.test(
'basic',
api.buildbucket_util.test("default", tryjob=True),
properties(
analyses=["yapf"],
cipd_packages=[
{
"name": "fuchsia/go/${platform}",
"version": "integration",
"subdir": "go",
}
],
),
changed_files_data(["engine.py"]),
change_diff_data("engine.py"),
formatted_diff_data("engine.py")
)