| # Copyright 2024 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 PB.recipe_modules.pigweed.submodule_roll.tests.full import InputProperties |
| from PB.recipe_modules.pigweed.submodule_roll.submodule import SubmoduleEntry |
| |
| DEPS = [ |
| "pigweed/checkout", |
| "pigweed/roll_util", |
| "pigweed/submodule_roll", |
| "recipe_engine/file", |
| "recipe_engine/properties", |
| "recipe_engine/step", |
| ] |
| |
| PROPERTIES = InputProperties |
| |
| |
| def RunSteps( # pylint: disable=invalid-name |
| api: recipe_api.RecipeScriptApi, |
| props: InputProperties, |
| ): |
| checkout = api.checkout.fake_context() |
| |
| rolls = api.submodule_roll.update(checkout, props.submodules) |
| |
| if not rolls: |
| with api.step.nest('nothing to roll, exiting'): |
| return |
| |
| |
| def GenTests(api) -> Generator[recipe_test_api.TestData, None, None]: |
| """Create tests.""" |
| |
| def _url(x): |
| if x.startswith(('https://', 'sso://', '.')): |
| return x |
| return 'https://foo.googlesource.com/' + x |
| |
| def submodules(*subs): |
| res = [] |
| for sub in subs: |
| if isinstance(sub, str): |
| res.append(SubmoduleEntry(path=sub)) |
| elif isinstance(sub, dict): |
| res.append(SubmoduleEntry(**sub)) |
| else: |
| raise ValueError(repr(sub)) # pragma: no cover |
| return res |
| |
| def properties(submodules, **kwargs): |
| props = InputProperties(**kwargs) |
| props.submodules.extend(submodules) |
| return api.properties(props) |
| |
| def gitmodules(**submodules): |
| branches = {} |
| for k, v in submodules.items(): |
| if k.endswith('_branch'): |
| branches[k.replace('_branch', '')] = v |
| |
| for x in branches: |
| del submodules[f'{x}_branch'] |
| |
| text = [] |
| for k, v in submodules.items(): |
| text.append( |
| '[submodule "{0}"]\n\tpath = {0}\n\turl = {1}\n'.format( |
| k, _url(v) |
| ) |
| ) |
| if k in branches: |
| text.append(f'\tbranch = {branches[k]}\n') |
| |
| return api.step_data( |
| 'read .gitmodules', api.file.read_text(''.join(text)) |
| ) |
| |
| def commit_data(name, **kwargs): |
| return api.roll_util.commit_data( |
| name, |
| api.roll_util.commit('a' * 40, 'foo\nbar\n\nChange-Id: I1111'), |
| **kwargs, |
| ) |
| |
| yield api.test( |
| 'success', |
| properties(submodules('a1', 'b2')), |
| commit_data('a1', prefix=''), |
| commit_data('b2', prefix=''), |
| gitmodules(a1='sso://foo/a1', b2='sso://foo/b2'), |
| api.roll_util.forward_roll('a1'), |
| api.roll_util.forward_roll('b2'), |
| ) |
| |
| yield api.test( |
| 'partial_noop', |
| properties(submodules('a1', 'b2')), |
| commit_data('a1', prefix=''), |
| gitmodules(a1='sso://foo/a1', b2='sso://foo/b2'), |
| api.roll_util.forward_roll('a1'), |
| api.roll_util.noop_roll('b2'), |
| ) |
| |
| yield api.test( |
| 'noop', |
| properties(submodules('a1', {'path': 'b2'})), |
| gitmodules(a1='a1', b2='b2', b2_branch='branch'), |
| api.roll_util.noop_roll('a1'), |
| api.roll_util.noop_roll('b2'), |
| ) |
| |
| yield api.test( |
| 'missing', |
| properties(submodules('a1', 'b2')), |
| gitmodules(a1='sso://foo/a1'), |
| status='FAILURE', |
| ) |