blob: 5391a1f39a848dd646d1b2461b1f1d1ff7bec8b5 [file]
# Copyright 2026 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""Tests for static_checks readability labels."""
from __future__ import annotations
from collections.abc import Iterator
from typing import Any
from recipe_engine import post_process, recipe_api, recipe_test_api
from PB.recipe_engine import result as result_pb
from PB.recipes.pigweed.static_checks import InputProperties
from RECIPE_MODULES.pigweed.raw_result import api as raw_result_api
DEPS = [
'pigweed/checkout',
'pigweed/raw_result',
'pigweed/static_checks',
'recipe_engine/cv',
'recipe_engine/properties',
]
PROPERTIES = InputProperties
@raw_result_api.wrap_run_steps
def RunSteps(
api: recipe_api.RecipeScriptApi, props: InputProperties
) -> result_pb.RawResult | None:
api.static_checks(props)
return None
def GenTests(
api: recipe_test_api.RecipeTestApi,
) -> Iterator[recipe_test_api.TestData]:
def test(
name: str,
*args: Any,
msg: str = '',
status: str = 'SUCCESS',
**kwargs: Any,
) -> recipe_test_api.TestData:
final_props = {
'readability': [
{
'trivial_label': 'Readability-Trivial',
'file_extensions': (
'.c .cc .cpp .cxx .C .h .hh .hpp .hxx .H'.split()
),
'line_number_threshold': 10,
},
],
}
votes = kwargs.pop('votes', {'Readability-Trivial': None})
return api.test(
f'readability.{name}',
api.properties(**final_props),
api.static_checks.change_details(
msg=msg,
votes=votes,
**kwargs,
),
api.cv(api.cv.FULL_RUN),
*args,
api.post_process(post_process.DropExpectation),
status=status,
)
def assert_changed_files(num: int) -> recipe_test_api.TestData:
return api.post_check(
post_process.MustRun,
f'Readability-Trivial.changed files {num}',
)
def assert_changed_lines(num: int) -> recipe_test_api.TestData:
return api.post_check(
post_process.MustRun,
f'Readability-Trivial.changed lines {num}',
)
def assert_set() -> recipe_test_api.TestData:
return api.post_check(
post_process.MustRun,
'Readability-Trivial.set label',
)
def assert_not_set() -> recipe_test_api.TestData:
return api.post_check(
post_process.DoesNotRun,
'Readability-Trivial.set label',
)
yield test(
'no_label',
api.checkout.try_test_data(),
assert_not_set(),
votes={},
)
yield test(
'no_cpp_changes',
api.checkout.try_test_data(),
assert_changed_files(0),
assert_changed_lines(0),
assert_not_set(),
)
yield test(
'inserted_nontrivial',
api.checkout.try_test_data(),
assert_changed_files(1),
assert_changed_lines(15),
assert_not_set(),
files={'foo.cpp': {'lines_inserted': 15}},
)
yield test(
'inserted_trivial',
api.checkout.try_test_data(),
assert_changed_files(1),
assert_changed_lines(5),
assert_set(),
files={'foo.cpp': {'lines_inserted': 5}},
)
yield test(
'removed_nontrivial',
api.checkout.try_test_data(),
assert_changed_files(1),
assert_changed_lines(15),
assert_not_set(),
files={'foo.cpp': {'lines_deleted': 15}},
)
yield test(
'removed_trivial',
api.checkout.try_test_data(),
assert_changed_files(1),
assert_changed_lines(5),
assert_set(),
files={'foo.cpp': {'lines_deleted': 5}},
)
yield test(
'mixed_nontrivial',
api.checkout.try_test_data(),
assert_changed_files(1),
assert_changed_lines(10),
assert_not_set(),
files={'foo.cpp': {'lines_inserted': 5, 'lines_deleted': 5}},
)
yield test(
'mixed_trivial',
api.checkout.try_test_data(),
assert_changed_files(1),
assert_changed_lines(9),
assert_set(),
files={'foo.cpp': {'lines_inserted': 5, 'lines_deleted': 4}},
)
yield test(
'multiple_nontrivial',
api.checkout.try_test_data(),
assert_changed_files(5),
assert_changed_lines(10),
assert_not_set(),
files={
'foo.cpp': {'lines_inserted': 1, 'lines_deleted': 1},
'foo.hpp': {'lines_inserted': 1, 'lines_deleted': 1},
'foo.cc': {'lines_inserted': 1, 'lines_deleted': 1},
'foo.hh': {'lines_inserted': 1, 'lines_deleted': 1},
'foo.c': {'lines_inserted': 1, 'lines_deleted': 1},
},
)
yield test(
'multiple_trivial',
api.checkout.try_test_data(),
assert_changed_files(4),
assert_changed_lines(8),
assert_set(),
files={
'foo.cpp': {'lines_inserted': 1, 'lines_deleted': 1},
'foo.hpp': {'lines_inserted': 1, 'lines_deleted': 1},
'foo.cc': {'lines_inserted': 1, 'lines_deleted': 1},
'foo.hh': {'lines_inserted': 1, 'lines_deleted': 1},
},
)