| # Copyright 2021 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. |
| """Recipe for running a specified script from.""" |
| |
| from __future__ import annotations |
| |
| import re |
| from collections.abc import Iterator |
| |
| from recipe_engine import ( |
| config_types, |
| post_process, |
| recipe_api, |
| recipe_test_api, |
| ) |
| |
| from PB.recipes.pigweed.run_script import InputProperties |
| |
| from RECIPE_MODULES.pigweed.raw_result import api as raw_result_api |
| |
| DEPS = [ |
| 'pigweed/checkout', |
| 'pigweed/environment', |
| 'pigweed/limited_cq', |
| 'pigweed/raw_result', |
| 'recipe_engine/context', |
| 'recipe_engine/file', |
| 'recipe_engine/path', |
| 'recipe_engine/properties', |
| 'recipe_engine/step', |
| ] |
| |
| PROPERTIES = InputProperties |
| |
| |
| @raw_result_api.wrap_run_steps |
| def RunSteps(api: recipe_api.RecipeScriptApi, props: InputProperties) -> None: |
| checkout: api.checkout.CheckoutContext = api.checkout( |
| props.checkout_options |
| ) |
| |
| if res := api.limited_cq.exit_early_if_not_approved(checkout.changes): |
| return res # pragma: no cover |
| |
| env: api.environment.Environment = api.environment.init( |
| checkout, props.environment_options |
| ) |
| |
| cmd: list[str | config_types.Path] = [checkout.root / props.script] |
| |
| with env(): |
| for arg in props.arguments: |
| |
| def repl(match: re.Match) -> str: |
| return getattr(env, match.group(1)) |
| |
| arg: str = re.sub(r'\$([\w]+)', repl, arg) |
| arg: str = re.sub(r'\$\{([\w]+)\}', repl, arg) |
| cmd.append(arg) |
| |
| props_out = api.path.mkdtemp() / 'output_properties.json' |
| api.path.mock_add_file(props_out) |
| env = {'PROPERTIES_OUT': props_out} |
| if props.cas_digest_in: |
| env['CAS_DIGEST_IN'] = props.cas_digest_in |
| |
| with api.context(cwd=checkout.root, env=env): |
| api.step(f'run {props.script}', cmd) |
| |
| if api.path.isfile(props_out): |
| output_props = api.file.read_json( |
| 'read output properties', |
| props_out, |
| test_data={'cas': 'hash-hash-hash-hash'}, |
| ) |
| |
| if output_props: |
| pres = api.step.empty('output properties').presentation |
| pres.properties = output_props |
| |
| |
| def GenTests(api) -> Iterator[recipe_test_api.TestData]: |
| def properties(**kwargs): |
| props = InputProperties(**kwargs) |
| props.checkout_options.CopyFrom(api.checkout.git_options()) |
| return api.properties(props) |
| |
| yield api.test( |
| 'run_script', |
| properties( |
| script='foo/bar/run-tests.sh', |
| arguments='foo bar'.split(), |
| cas_digest_in='hashy-hashy-hashy', |
| ), |
| api.post_process(post_process.DropExpectation, 'checkout pigweed'), |
| ) |
| |
| yield api.test( |
| 'variable_substitution', |
| properties( |
| script='run-tests.sh', |
| arguments='without=$PW_TEST_VAR with=${PW_TEST_VAR}'.split(), |
| ), |
| api.post_process(post_process.DropExpectation, 'checkout pigweed'), |
| ) |