blob: b7beca27f7b4dfb3f3f5e52a2bb8c2e0968398d0 [file] [log] [blame]
# Copyright 2025 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.
"""Tests for kconfig_gen.py."""
import os
from pathlib import Path
import shutil
import tempfile
import unittest
import kconfig_gen
class KconfigGenTest(unittest.TestCase):
"""Tests for kconfig_gen.py."""
def setUp(self):
self.test_dir = tempfile.mkdtemp()
self.zephyr_root = Path(self.test_dir) / 'zephyr'
self.arch_root = self.zephyr_root
self.soc_root = self.zephyr_root
self.board_root = self.zephyr_root / 'boards'
os.environ['KCONFIG_BINARY_DIR'] = self.test_dir
os.environ['srctree'] = str(self.zephyr_root)
os.environ['BOARD'] = 'test_board'
# Simulate arch directories in the Zephyr tree.
(self.arch_root / 'arch' / 'arm').mkdir(parents=True, exist_ok=True)
(self.arch_root / 'arch' / 'x86').mkdir(parents=True, exist_ok=True)
# Simulate multiple layers of SoC definition directories.
(self.soc_root / 'soc' / 'st' / 'stm32').mkdir(
parents=True, exist_ok=True
)
(self.soc_root / 'soc' / 'st' / 'stm32' / 'soc.yml').touch()
(self.soc_root / 'soc' / 'st' / 'stm32' / 'stm32f7x').mkdir(
parents=True, exist_ok=True
)
(
self.soc_root / 'soc' / 'st' / 'stm32' / 'stm32f7x' / 'soc.yml'
).touch()
# Simulate a board directory.
(self.board_root / 'arm' / 'test_board').mkdir(
parents=True, exist_ok=True
)
def tearDown(self):
shutil.rmtree(self.test_dir)
def test_find_arch_source_directories(self):
arch_dirs = kconfig_gen.find_arch_source_directories([self.arch_root])
self.assertIn(str(self.arch_root / 'arch' / 'arm'), arch_dirs)
self.assertIn(str(self.arch_root / 'arch' / 'x86'), arch_dirs)
def test_find_soc_source_directories(self):
soc_dirs = [
str(d)
for d in kconfig_gen.find_soc_source_directories([self.soc_root])
]
self.assertIn(str(self.soc_root / 'soc' / 'st' / 'stm32'), soc_dirs)
self.assertIn(
str(self.soc_root / 'soc' / 'st' / 'stm32' / 'stm32f7x'), soc_dirs
)
def test_find_subdirectory(self):
board_dir = kconfig_gen.find_subdirectory(
str(self.board_root), 'test_board'
)
self.assertEqual(board_dir, str(self.board_root / 'arm' / 'test_board'))
def test_kconfig_gen(self):
arm_kconfig_dir = self.arch_root / 'arch' / 'arm'
(arm_kconfig_dir / 'Kconfig').write_text('config ARM\n')
kconfig_gen.kconfig_gen(
'arch', 'Kconfig', [str(arm_kconfig_dir)], 'Test Kconfig'
)
generated_file = Path(self.test_dir) / 'arch' / 'Kconfig'
self.assertTrue(generated_file.exists())
with open(generated_file, 'r') as f:
content = f.read()
self.assertIn('# Load Test Kconfig descriptions.', content)
self.assertIn(f'osource "{arm_kconfig_dir / "Kconfig"}"', content)
if __name__ == '__main__':
unittest.main()