Tools: Rewrite build system unit tests to python - cmake libraries and Kconfig

This commit is contained in:
Marek Fiala
2023-02-08 19:27:01 +01:00
parent 51772f4fb5
commit b4446e1748
9 changed files with 349 additions and 62 deletions

View File

@@ -1,12 +1,30 @@
# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0
import logging
import os
import re
import shutil
import stat
import subprocess
import sys
import textwrap
from pathlib import Path
from typing import List
import pytest
from _pytest.monkeypatch import MonkeyPatch
from test_build_system_helpers import IdfPyFunc, get_snapshot, replace_in_file
from test_build_system_helpers import IdfPyFunc, find_python, get_snapshot, replace_in_file, run_idf_py
def get_subdirs_absolute_paths(path: Path) -> List[str]:
"""
Get a list of files with absolute path in a given `path` folder
"""
return [
'{}/{}'.format(dir_path, file_name)
for dir_path, _, file_names in os.walk(path)
for file_name in file_names
]
@pytest.mark.usefixtures('test_app_copy')
@@ -88,3 +106,55 @@ def test_efuse_symmary_cmake_functions(
def test_custom_build_folder(test_app_copy: Path, idf_py: IdfPyFunc) -> None:
idf_py('-BBuiLDdiR', 'build')
assert (test_app_copy / 'BuiLDdiR').is_dir()
def test_python_clean(idf_py: IdfPyFunc) -> None:
logging.info('Cleaning Python bytecode')
idf_path = Path(os.environ['IDF_PATH'])
abs_paths = get_subdirs_absolute_paths(idf_path)
abs_paths_suffix = [path for path in abs_paths if path.endswith(('.pyc', '.pyo'))]
assert len(abs_paths_suffix) != 0
idf_py('python-clean')
abs_paths = get_subdirs_absolute_paths(idf_path)
abs_paths_suffix = [path for path in abs_paths if path.endswith(('.pyc', '.pyo'))]
assert len(abs_paths_suffix) == 0
@pytest.mark.usefixtures('test_app_copy')
def test_partition_table(idf_py: IdfPyFunc) -> None:
logging.info('Displays partition table when executing target partition_table')
output = idf_py('partition-table')
assert re.search('# ESP-IDF.+Partition Table', output.stdout)
@pytest.mark.skipif(sys.platform == 'win32', reason='Windows does not support executing bash script')
def test_python_interpreter_unix(test_app_copy: Path) -> None:
logging.info("Make sure idf.py never runs '/usr/bin/env python' or similar")
env_dict = dict(**os.environ)
python = find_python(env_dict['PATH'])
(test_app_copy / 'python').write_text(textwrap.dedent("""#!/bin/sh
echo "idf.py has executed '/usr/bin/env python' or similar"
exit 1
"""))
st = os.stat(test_app_copy / 'python')
# equivalent to 'chmod +x ./python'
os.chmod((test_app_copy / 'python'), st.st_mode | stat.S_IEXEC)
env_dict['PATH'] = str(test_app_copy) + os.pathsep + env_dict['PATH']
# python is loaded from env:$PATH, but since false interpreter is provided there, python needs to be specified as argument
# if idf.py is reconfigured during it's execution, it would load a false interpreter
run_idf_py('reconfigure', env=env_dict, python=python)
@pytest.mark.skipif(sys.platform != 'win32', reason='Linux does not support executing .exe files')
def test_python_interpreter_win(test_app_copy: Path) -> None:
logging.info('Make sure idf.py never runs false python interpreter')
env_dict = dict(**os.environ)
python = find_python(env_dict['PATH'])
# on windows python interpreter has compiled code '.exe' format, so this false file can be empty
(test_app_copy / 'python.exe').write_text('')
env_dict['PATH'] = str(test_app_copy) + os.pathsep + env_dict['PATH']
# python is loaded from env:$PATH, but since false interpreter is provided there, python needs to be specified as argument
# if idf.py is reconfigured during it's execution, it would load a false interpreter
run_idf_py('reconfigure', env=env_dict, python=python)