1#!/usr/bin/env python 2# Copyright 2024 The Pigweed Authors 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); you may not 5# use this file except in compliance with the License. You may obtain a copy of 6# the License at 7# 8# https://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13# License for the specific language governing permissions and limitations under 14# the License. 15"""Tests for pw_zephyr's manifest subcommand""" 16import pathlib 17 18import unittest 19import os 20import shutil 21import subprocess 22import tempfile 23from pw_env_setup_zephyr import zephyr 24 25 26class ZephyrManifestTest(unittest.TestCase): 27 """Tests the manifest subcommand for Zephyr""" 28 29 def setUp(self) -> None: 30 super().setUp() 31 self._pw_dir = tempfile.mkdtemp() 32 self._pw_path = pathlib.Path(self._pw_dir) 33 self._zephyr_path = ( 34 self._pw_path / 'environment' / 'packages' / 'zephyr' 35 ) 36 self.init_git_dir(self._pw_path, 'pigweed') 37 38 os.environ['PW_ROOT'] = str(self._pw_dir) 39 40 def tearDown(self) -> None: 41 super().tearDown() 42 shutil.rmtree(self._pw_dir) 43 44 def _init_zephyr(self) -> None: 45 self._zephyr_path.mkdir(parents=True) 46 self.init_git_dir(self._zephyr_path, 'zephyr') 47 48 @staticmethod 49 def git_get_revision(path: pathlib.Path) -> str: 50 result = subprocess.run( 51 ['git', 'log', '--pretty=format:"%H"', '-n', '1'], 52 stdout=subprocess.PIPE, 53 cwd=path, 54 ) 55 assert result.returncode == 0 56 return result.stdout.decode('utf-8').strip().strip('"') 57 58 @staticmethod 59 def init_git_dir(path: pathlib.Path, name: str) -> None: 60 subprocess.check_call(['git', 'init'], cwd=path) 61 subprocess.check_call( 62 ['git', 'remote', 'add', 'origin', f'http://fake.path/{name}'], 63 cwd=path, 64 ) 65 subprocess.check_call( 66 ['git', 'commit', '--allow-empty', '-m', '"Test commit"'], cwd=path 67 ) 68 69 def test_pigweed_only_manifest(self) -> None: 70 yaml = zephyr.generate_manifest() 71 assert yaml.get('manifest') is not None 72 assert yaml['manifest'].get('remotes') is not None 73 remotes: list = yaml['manifest']['remotes'] 74 assert len(remotes) == 1 75 assert remotes[0].get('name') == 'pigweed' 76 assert remotes[0].get('url-base') == 'http://fake.path/pigweed' 77 assert yaml['manifest'].get('projects') is not None 78 projects: list = yaml['manifest']['projects'] 79 assert len(projects) == 1 80 assert projects[0].get('name') == 'pigweed' 81 assert projects[0].get('remote') == 'pigweed' 82 assert projects[0].get('revision') == self.git_get_revision( 83 self._pw_path 84 ) 85 assert projects[0].get('path') == str( 86 self._pw_path.relative_to( 87 os.path.commonpath([self._pw_path, os.getcwd()]) 88 ) 89 ) 90 assert projects[0].get('import') is False 91 92 def test_pigweed_and_zephyr_manifest(self) -> None: 93 yaml = zephyr.generate_manifest() 94 assert yaml.get('manifest') is not None 95 assert yaml['manifest'].get('remotes') is not None 96 remotes: list = yaml['manifest']['remotes'] 97 assert len(remotes) == 2 98 zephyr_remote = [it for it in remotes if it.get('name') == 'zephyr'][0] 99 assert zephyr_remote.get('url-base') == 'http://fake.path/zephyr' 100 assert yaml['manifest'].get('projects') is not None 101 projects: list = yaml['manifest']['projects'] 102 zephyr_project = [it for it in projects if it.get('name') == 'zephyr'][ 103 0 104 ] 105 assert zephyr_project.get('remote') == 'zephyr' 106 assert zephyr_project.get('revision') == self.git_get_revision( 107 self._zephyr_path 108 ) 109 assert zephyr_project.get('path') == str( 110 self._zephyr_path.relative_to( 111 os.path.commonpath([self._zephyr_path, os.getcwd()]) 112 ) 113 ) 114 assert zephyr_project.get('import') is True 115