blob: e727de82704ee30a96fbe66acf95ed421e9953c0 [file] [log] [blame]
# Copyright 2020 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.
from __future__ import annotations
from recipe_engine import post_process, recipe_test_api
class RollUtilTestApi(recipe_test_api.RecipeTestApi):
def properties(
self,
labels_to_set=None,
labels_to_wait_on=None,
commit_divider=None,
footer=None,
):
props = {}
for label, value in (labels_to_set or {}).items():
props.setdefault('labels_to_set', [])
props['labels_to_set'].append({'label': label, 'value': value})
if labels_to_wait_on:
props['labels_to_wait_on'] = list(labels_to_wait_on)
if commit_divider:
props['commit_divider'] = commit_divider
if footer:
if isinstance(footer, str):
footer = [footer]
props['footer'] = footer
return self.m.properties(**{'$pigweed/roll_util': props})
def commit(
self,
commit_hash,
message=None,
author='author@example.com',
name=None,
):
if not message:
message = """
module: Short commit summary
Longer commit details.
Bug: 123
Change-Id: I{commit_hash}
Reviewed-On: https://pigweed-review.googlesource.com/c/project/+/{commit_hash:.5}
""".strip().format(
commit_hash=commit_hash
)
if name is None:
name = author.split('@')[0]
return '\n'.join((commit_hash, name, author, message))
def format_prefix(self, p):
if not p:
return p
return f"{p.rstrip('.')}."
def commit_data(self, name, *commits, prefix=''):
prefix = f'{prefix.rstrip(".")}.' if prefix else ''
name = f'{name.rstrip(".")}.' if name else ''
return self.step_data(
f'{prefix}{name}git log',
stdout=self.m.raw_io.output_text('\0'.join(commits)),
)
def cancelled(self):
return self.post_process(post_process.MustRunRE, '.*cancelling roll.*')
def not_cancelled(self):
return self.post_process(
post_process.DoesNotRunRE, '.*cancelling roll.*'
)
def noop_roll(self, prefix='', name='get roll direction'):
prefix = self.format_prefix(prefix)
return self.step_data(
f'{prefix}{name}.is forward', retcode=0
) + self.step_data(f'{prefix}{name}.is backward', retcode=0)
def forward_roll(self, prefix='', name='get roll direction'):
prefix = self.format_prefix(prefix)
return self.step_data(
f'{prefix}{name}.is forward', retcode=0
) + self.step_data(f'{prefix}{name}.is backward', retcode=1)
def backward_roll(self, prefix='', name='get roll direction'):
prefix = self.format_prefix(prefix)
return self.step_data(
f'{prefix}{name}.is forward', retcode=1
) + self.step_data(f'{prefix}{name}.is backward', retcode=0)
def rebased_roll(self, prefix='', name='get roll direction'):
prefix = self.format_prefix(prefix)
return self.step_data(
f'{prefix}{name}.is forward', retcode=1
) + self.step_data(f'{prefix}{name}.is backward', retcode=1)