| # 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. |
| """Test API for enviroment.""" |
| |
| import re |
| |
| from recipe_engine import recipe_test_api |
| |
| |
| class EnvironmentTestApi(recipe_test_api.RecipeTestApi): |
| """Test API for Enviroment.""" |
| |
| def __init__(self, *args, **kwargs): |
| super(EnvironmentTestApi, self).__init__(*args, **kwargs) |
| self._cargo_package_file = 'config/./cargo_packages.txt' |
| self._cipd_package_file = 'config/cipd_packages.json' |
| self._virtualenv_requirement = 'config/requirements.txt' |
| self._virtualenv_setup_py_root = 'python/.' |
| |
| def _convert_path(self, path, sep): |
| parts = ['[START_DIR]'] |
| parts.extend(x for x in path.split('/') if x != '.') |
| return sep.join(parts) |
| |
| def cipd_test_data(self, sep): |
| path = self._convert_path(self._cipd_package_file, sep) |
| # Using re instead of os.path because this needs to handle both Windows and |
| # Linux paths. |
| name = re.search(r'^.*[/\\](.*?)(?:\.json)', path).group(1) |
| |
| return self.step_data( |
| 'environment.setup cipd.{}.read {}'.format(name, path), |
| self.m.file.read_json([ |
| { |
| 'path': 'cipd{sep}path{sep}${{platform}}'.format(sep=sep), |
| 'tags': ['version:42'] |
| }, |
| ])) |
| |
| def virtualenv_test_data(self, sep): |
| root = self._convert_path(self._virtualenv_setup_py_root, sep) |
| |
| return self.step_data( |
| 'environment.setup python.find_python_packages.{root}'.format( |
| root=root), |
| stdout=self.m.raw_io.output('pw_cli/py/setup.py\n' |
| 'pw_presubmit/py/setup.py\n')) |
| |
| def cargo_test_data(self, sep): |
| path = self._convert_path(self._cargo_package_file, sep) |
| |
| return self.step_data( |
| 'environment.setup cargo.read {path}'.format(path=path), |
| self.m.file.read_text(''' |
| # comment followed by empty line then a package with a version |
| |
| package 1.2.3 |
| ''')) |
| |
| def test_data(self, with_cargo=False, windows=False): |
| sep = '\\' if windows else '/' |
| result = self.cipd_test_data(sep) + self.virtualenv_test_data(sep) |
| if with_cargo: |
| result += self.cargo_test_data(sep) |
| return result |
| |
| def properties(self, with_cargo=False, minimal=True): |
| props = { |
| 'cipd_package_files': [], |
| 'root_variable_name': 'FOO_ROOT', |
| 'virtualenv_requirements': [], |
| 'virtualenv_setup_py_roots': [], |
| 'cargo_package_files': [], |
| } |
| |
| if not minimal: |
| props['cipd_package_files'].append('config/cipd_packages.json') |
| props['virtualenv_requirements'].append('config/requirements.txt') |
| props['virtualenv_setup_py_roots'].append('python/.') |
| |
| if with_cargo: |
| props['cargo_package_files'].append('config/./cargo_packages.txt') |
| |
| return {'$pigweed/environment': props} |