From 2d094ab9d323838a09d8bbe95281ac5a91231be0 Mon Sep 17 00:00:00 2001 From: Fu Hanxi Date: Fri, 26 Jan 2024 14:10:46 +0100 Subject: [PATCH] test: remove succeeded temp projects also rename `tmp_path` to `work_dirpath` since it's defined in pytest --- tools/ci/idf_pytest/tests/conftest.py | 15 +++++- .../ci/idf_pytest/tests/test_get_all_apps.py | 50 +++++++++---------- .../idf_pytest/tests/test_get_pytest_cases.py | 26 +++++----- 3 files changed, 51 insertions(+), 40 deletions(-) diff --git a/tools/ci/idf_pytest/tests/conftest.py b/tools/ci/idf_pytest/tests/conftest.py index b2244bd5b6..bd85b7f4df 100644 --- a/tools/ci/idf_pytest/tests/conftest.py +++ b/tools/ci/idf_pytest/tests/conftest.py @@ -1,8 +1,11 @@ # SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Apache-2.0 +import logging import os +import shutil import sys import tempfile +import typing as t from pathlib import Path import pytest @@ -53,7 +56,15 @@ void app_main(void) {} @pytest.fixture -def tmp_path() -> Path: +def work_dirpath() -> t.Generator[Path, None, None]: os.makedirs(os.path.join(IDF_PATH, 'pytest_embedded_log'), exist_ok=True) - return Path(tempfile.mkdtemp(prefix=os.path.join(IDF_PATH, 'pytest_embedded_log') + os.sep)) + p = Path(tempfile.mkdtemp(prefix=os.path.join(IDF_PATH, 'pytest_embedded_log') + os.sep)) + + try: + yield p + except Exception: + logging.critical('Test is failing, Please check the log in %s', p) + raise + else: + shutil.rmtree(p) diff --git a/tools/ci/idf_pytest/tests/test_get_all_apps.py b/tools/ci/idf_pytest/tests/test_get_all_apps.py index d4d53baee8..6996c4d5bb 100644 --- a/tools/ci/idf_pytest/tests/test_get_all_apps.py +++ b/tools/ci/idf_pytest/tests/test_get_all_apps.py @@ -8,19 +8,19 @@ from idf_pytest.script import SUPPORTED_TARGETS from conftest import create_project -def test_get_all_apps_non(tmp_path: Path) -> None: - create_project('foo', tmp_path) - create_project('bar', tmp_path) +def test_get_all_apps_non(work_dirpath: Path) -> None: + create_project('foo', work_dirpath) + create_project('bar', work_dirpath) - test_related_apps, non_test_related_apps = get_all_apps([str(tmp_path)]) + test_related_apps, non_test_related_apps = get_all_apps([str(work_dirpath)]) assert test_related_apps == set() assert len(non_test_related_apps) == 2 * len(SUPPORTED_TARGETS) -def test_get_all_apps_single_dut_test_script(tmp_path: Path) -> None: - create_project('foo', tmp_path) - with open(tmp_path / 'foo' / 'pytest_get_all_apps_single_dut_test_script.py', 'w') as fw: +def test_get_all_apps_single_dut_test_script(work_dirpath: Path) -> None: + create_project('foo', work_dirpath) + with open(work_dirpath / 'foo' / 'pytest_get_all_apps_single_dut_test_script.py', 'w') as fw: fw.write( """import pytest @@ -30,18 +30,18 @@ def test_foo(dut): pass """ ) - create_project('bar', tmp_path) + create_project('bar', work_dirpath) - test_related_apps, non_test_related_apps = get_all_apps([str(tmp_path)], target='all') + test_related_apps, non_test_related_apps = get_all_apps([str(work_dirpath)], target='all') assert len(test_related_apps) == 2 assert len(non_test_related_apps) == 2 * len(SUPPORTED_TARGETS) - 2 -def test_get_all_apps_multi_dut_with_markers_test_script(tmp_path: Path) -> None: - create_project('foo', tmp_path) +def test_get_all_apps_multi_dut_with_markers_test_script(work_dirpath: Path) -> None: + create_project('foo', work_dirpath) - (tmp_path / 'foo' / 'pytest_get_all_apps_multi_dut_with_markers_test_script.py').write_text( + (work_dirpath / 'foo' / 'pytest_get_all_apps_multi_dut_with_markers_test_script.py').write_text( """import pytest @pytest.mark.esp32 @@ -52,15 +52,15 @@ def test_foo(dut): encoding='utf-8', ) - test_related_apps, non_test_related_apps = get_all_apps([str(tmp_path)], target='all') + test_related_apps, non_test_related_apps = get_all_apps([str(work_dirpath)], target='all') assert len(test_related_apps) == 1 assert len(non_test_related_apps) == len(SUPPORTED_TARGETS) - 1 -def test_get_all_apps_multi_dut_test_script(tmp_path: Path) -> None: - create_project('foo', tmp_path) - with open(tmp_path / 'foo' / 'pytest_get_all_apps_multi_dut_test_script.py', 'w') as fw: +def test_get_all_apps_multi_dut_test_script(work_dirpath: Path) -> None: + create_project('foo', work_dirpath) + with open(work_dirpath / 'foo' / 'pytest_get_all_apps_multi_dut_test_script.py', 'w') as fw: fw.write( """import pytest @@ -75,17 +75,17 @@ def test_foo(dut): """ ) - test_related_apps, non_test_related_apps = get_all_apps([str(tmp_path)], target='all') + test_related_apps, non_test_related_apps = get_all_apps([str(work_dirpath)], target='all') assert len(test_related_apps) == 3 # 32, s2, s3 assert len(non_test_related_apps) == len(SUPPORTED_TARGETS) - 3 -def test_get_all_apps_modified_pytest_script(tmp_path: Path) -> None: - create_project('foo', tmp_path) - create_project('bar', tmp_path) +def test_get_all_apps_modified_pytest_script(work_dirpath: Path) -> None: + create_project('foo', work_dirpath) + create_project('bar', work_dirpath) - (tmp_path / 'pytest_get_all_apps_modified_pytest_script.py').write_text( + (work_dirpath / 'pytest_get_all_apps_modified_pytest_script.py').write_text( """import pytest import os @@ -100,20 +100,20 @@ def test_multi_foo_bar(dut): encoding='utf-8', ) - test_related_apps, non_test_related_apps = get_all_apps([str(tmp_path)], target='all') + test_related_apps, non_test_related_apps = get_all_apps([str(work_dirpath)], target='all') assert len(test_related_apps) == 2 # foo-esp32, bar-esp32 assert len(non_test_related_apps) == 2 * len(SUPPORTED_TARGETS) - 2 test_related_apps, non_test_related_apps = get_all_apps( - [str(tmp_path)], target='all', modified_files=[], modified_components=[] + [str(work_dirpath)], target='all', modified_files=[], modified_components=[] ) assert len(test_related_apps) == 0 assert len(non_test_related_apps) == 0 test_related_apps, non_test_related_apps = get_all_apps( - [str(tmp_path)], + [str(work_dirpath)], target='all', - modified_files=[str(tmp_path / 'pytest_get_all_apps_modified_pytest_script.py')], + modified_files=[str(work_dirpath / 'pytest_get_all_apps_modified_pytest_script.py')], modified_components=[], ) assert len(test_related_apps) == 2 diff --git a/tools/ci/idf_pytest/tests/test_get_pytest_cases.py b/tools/ci/idf_pytest/tests/test_get_pytest_cases.py index e221fef935..f74607194c 100644 --- a/tools/ci/idf_pytest/tests/test_get_pytest_cases.py +++ b/tools/ci/idf_pytest/tests/test_get_pytest_cases.py @@ -32,41 +32,41 @@ def test_foo_multi_with_marker(dut): ''' -def test_get_pytest_cases_single_specific(tmp_path: Path) -> None: - script = tmp_path / 'pytest_get_pytest_cases_single_specific.py' +def test_get_pytest_cases_single_specific(work_dirpath: Path) -> None: + script = work_dirpath / 'pytest_get_pytest_cases_single_specific.py' script.write_text(TEMPLATE_SCRIPT) - cases = get_pytest_cases([str(tmp_path)], 'esp32') + cases = get_pytest_cases([str(work_dirpath)], 'esp32') assert len(cases) == 1 assert cases[0].targets == ['esp32'] -def test_get_pytest_cases_multi_specific(tmp_path: Path) -> None: - script = tmp_path / 'pytest_get_pytest_cases_multi_specific.py' +def test_get_pytest_cases_multi_specific(work_dirpath: Path) -> None: + script = work_dirpath / 'pytest_get_pytest_cases_multi_specific.py' script.write_text(TEMPLATE_SCRIPT) - cases = get_pytest_cases([str(tmp_path)], 'esp32s2,esp32s2, esp32s3') + cases = get_pytest_cases([str(work_dirpath)], 'esp32s2,esp32s2, esp32s3') assert len(cases) == 1 assert cases[0].targets == ['esp32s2', 'esp32s2', 'esp32s3'] - cases = get_pytest_cases([str(tmp_path)], 'esp32s3,esp32s2,esp32s2') # order matters + cases = get_pytest_cases([str(work_dirpath)], 'esp32s3,esp32s2,esp32s2') # order matters assert len(cases) == 0 -def test_get_pytest_cases_multi_all(tmp_path: Path) -> None: - script = tmp_path / 'pytest_get_pytest_cases_multi_all.py' +def test_get_pytest_cases_multi_all(work_dirpath: Path) -> None: + script = work_dirpath / 'pytest_get_pytest_cases_multi_all.py' script.write_text(TEMPLATE_SCRIPT) - cases = get_pytest_cases([str(tmp_path)], CollectMode.MULTI_ALL_WITH_PARAM) + cases = get_pytest_cases([str(work_dirpath)], CollectMode.MULTI_ALL_WITH_PARAM) assert len(cases) == 2 assert cases[0].targets == ['esp32', 'esp32s2'] assert cases[1].targets == ['esp32s2', 'esp32s2', 'esp32s3'] -def test_get_pytest_cases_all(tmp_path: Path) -> None: - script = tmp_path / 'pytest_get_pytest_cases_all.py' +def test_get_pytest_cases_all(work_dirpath: Path) -> None: + script = work_dirpath / 'pytest_get_pytest_cases_all.py' script.write_text(TEMPLATE_SCRIPT) - cases = get_pytest_cases([str(tmp_path)], CollectMode.ALL) + cases = get_pytest_cases([str(work_dirpath)], CollectMode.ALL) assert len(cases) == 6 assert cases[0].targets == ['esp32', 'esp32s2']