Skip to content

Commit

Permalink
test: added mock idf related files.
Browse files Browse the repository at this point in the history
  • Loading branch information
horw committed Jan 14, 2025
1 parent 4ad3854 commit 1f785e8
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 31 deletions.
12 changes: 12 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ def copy_fixtures(testdir: Testdir):
yield


@pytest.fixture()
def copy_mock_esp_idf(testdir: Testdir):
esp_idf = os.path.join(os.path.dirname(__file__), 'tests', 'esp-idf')
for item in os.listdir(esp_idf):
if os.path.isfile(os.path.join(esp_idf, item)):
shutil.copy(os.path.join(esp_idf, item), os.path.join(str(testdir.tmpdir), item))
else:
shutil.copytree(os.path.join(esp_idf, item), os.path.join(str(testdir.tmpdir), item))

yield


@pytest.fixture(autouse=True)
def cache_file_remove(cache_dir):
yield
Expand Down
86 changes: 55 additions & 31 deletions pytest-embedded-idf/tests/test_idf.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,50 +988,74 @@ def test_python_case(dut):
for testcase in junit_report[1:]:
assert testcase.attrib['is_unity_case'] == '1' # Other test cases


def test_skip_if_soc(testdir):
EMBEDDED_SERVICES = ['--embedded-services', 'esp,idf']
def test_esp_bool_parser_returned_values(testdir, copy_mock_esp_idf, monkeypatch): # noqa: ARG001
monkeypatch.setenv('IDF_PATH', str(testdir))
from esp_bool_parser import SOC_HEADERS, SUPPORTED_TARGETS
assert SOC_HEADERS == {
'esp32': {'SOC_A': 0, 'SOC_B': 1, 'SOC_C': 0},
'esp32s2': {'SOC_A': 0, 'SOC_B': 0, 'SOC_C': 0},
'esp32c3': {'SOC_A': 1, 'SOC_B': 1, 'SOC_C': 1},
'esp32s3': {'SOC_A': 1, 'SOC_B': 0, 'SOC_C': 1},
'esp32c2': {'SOC_A': 0, 'SOC_B': 1, 'SOC_C': 0},
'esp32c6': {'SOC_A': 1, 'SOC_B': 0, 'SOC_C': 0},
'esp32h2': {'SOC_A': 0, 'SOC_B': 1, 'SOC_C': 1},
'esp32p4': {'SOC_A': 0, 'SOC_B': 0, 'SOC_C': 1},
'linux': {},
'esp32c5': {'SOC_A': 1, 'SOC_B': 1, 'SOC_C': 0},
'esp32c61': {'SOC_A': 0, 'SOC_B': 0, 'SOC_C': 1},
'esp32h21': {'SOC_A': 0, 'SOC_B': 0, 'SOC_C': 0}
}
assert SUPPORTED_TARGETS == ['esp32', 'esp32s2', 'esp32c3', 'esp32s3', 'esp32c2', 'esp32c6', 'esp32h2', 'esp32p4']


def test_skip_if_soc(testdir, copy_mock_esp_idf, monkeypatch): # noqa: ARG001
monkeypatch.setenv('IDF_PATH', str(testdir))
from esp_bool_parser import SOC_HEADERS, SUPPORTED_TARGETS

def run_test_for_condition(condition, condition_func):
to_skip = sum([1 for t in SUPPORTED_TARGETS if condition_func(SOC_HEADERS[t])])
to_pass = len(SUPPORTED_TARGETS) - to_skip
testdir.makepyfile(f"""
import pytest
from esp_bool_parser.constants import SUPPORTED_TARGETS
@pytest.mark.skip_if_soc("{condition}")
@pytest.mark.parametrize('target', SUPPORTED_TARGETS, indirect=True)
def test_skip_if_for_condition():
pass
""")

result = testdir.runpytest('-s', '--embedded-services', 'esp,idf')
result.assert_outcomes(passed=to_pass, skipped=to_skip)


for c, cf in [
('SOC_A == 1', lambda h: h['SOC_A'] == 1),
('SOC_A == 1 or SOC_B == 1', lambda h: h['SOC_A'] == 1 or h['SOC_B'] == 1),
('SOC_A == 1 and SOC_B == 1', lambda h: h['SOC_A'] == 1 and h['SOC_B'] == 1),
('SOC_A == 1 or SOC_B == 1 and SOC_C == 1', lambda h: h['SOC_A'] == 1 or (h['SOC_B'] == 1 and h['SOC_C'] == 1)),
('SOC_A == 1 and SOC_B == 0 or SOC_C == 1 ', lambda h: (h['SOC_A'] == 1 and h['SOC_B'] == 0) or h['SOC_C'] == 1), # noqa: E501
]:
run_test_for_condition(c, cf)


def test_skip_if_soc_target_in_args(testdir, copy_mock_esp_idf, monkeypatch): # noqa: ARG001
monkeypatch.setenv('IDF_PATH', str(testdir))

def run_pytest_with_target(target):
count = len(target.split('|'))
return testdir.runpytest(*EMBEDDED_SERVICES, '--target', target, '--count', count)

testdir.makepyfile("""
import pytest
from esp_bool_parser.constants import SUPPORTED_TARGETS
@pytest.mark.skip_if_soc("SOC_ULP_LP_UART_SUPPORTED == 1")
@pytest.mark.parametrize('target', ['esp32', 'esp32s3', 'esp32c6'], indirect=True)
def test_lp_uart_wakeup():
pass
@pytest.mark.skip_if_soc("SOC_BLE_SUPPORTED == 1")
@pytest.mark.parametrize('target', SUPPORTED_TARGETS, indirect=True)
def test_ble():
pass
@pytest.mark.skip_if_soc("SOC_ADC_SAMPLE_FREQ_THRES_HIGH == 83333")
@pytest.mark.parametrize('target', SUPPORTED_TARGETS, indirect=True)
def test_adc():
pass
""")

result = testdir.runpytest('-s', *EMBEDDED_SERVICES)
result.assert_outcomes(passed=14, failed=0, skipped=5)
return testdir.runpytest( '--embedded-services', 'esp,idf', '--target', target, '--count', count)

testdir.makepyfile("""
import pytest
@pytest.mark.skip_if_soc("SOC_ULP_LP_UART_SUPPORTED == 1")
@pytest.mark.skip_if_soc("SOC_A == 1")
def test_from_args():
pass
""")

results = [
(run_pytest_with_target('esp32'), {'passed': 0, 'failed': 0, 'skipped': 1}),
(run_pytest_with_target('esp32c5'), {'passed': 1, 'failed': 0, 'skipped': 0}),
(run_pytest_with_target('auto'), {'passed': 1, 'failed': 0, 'skipped': 0}),
(run_pytest_with_target('esp32|esp32'), {'passed': 1, 'failed': 0, 'skipped': 0}),
]
Expand Down
3 changes: 3 additions & 0 deletions tests/esp-idf/components/soc/esp32/include/soc/soc_caps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define SOC_A 0
#define SOC_B 1
#define SOC_C 0
3 changes: 3 additions & 0 deletions tests/esp-idf/components/soc/esp32c2/include/soc/soc_caps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define SOC_A 0
#define SOC_B 1
#define SOC_C 0
3 changes: 3 additions & 0 deletions tests/esp-idf/components/soc/esp32c3/include/soc/soc_caps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define SOC_A 1
#define SOC_B 1
#define SOC_C 1
3 changes: 3 additions & 0 deletions tests/esp-idf/components/soc/esp32c5/include/soc/soc_caps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define SOC_A 1
#define SOC_B 1
#define SOC_C 0
3 changes: 3 additions & 0 deletions tests/esp-idf/components/soc/esp32c6/include/soc/soc_caps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define SOC_A 1
#define SOC_B 0
#define SOC_C 0
3 changes: 3 additions & 0 deletions tests/esp-idf/components/soc/esp32c61/include/soc/soc_caps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define SOC_A 0
#define SOC_B 0
#define SOC_C 1
3 changes: 3 additions & 0 deletions tests/esp-idf/components/soc/esp32h2/include/soc/soc_caps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define SOC_A 0
#define SOC_B 1
#define SOC_C 1
3 changes: 3 additions & 0 deletions tests/esp-idf/components/soc/esp32h21/include/soc/soc_caps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define SOC_A 0
#define SOC_B 0
#define SOC_C 0
3 changes: 3 additions & 0 deletions tests/esp-idf/components/soc/esp32p4/include/soc/soc_caps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define SOC_A 0
#define SOC_B 0
#define SOC_C 1
3 changes: 3 additions & 0 deletions tests/esp-idf/components/soc/esp32s2/include/soc/soc_caps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define SOC_A 0
#define SOC_B 0
#define SOC_C 0
3 changes: 3 additions & 0 deletions tests/esp-idf/components/soc/esp32s3/include/soc/soc_caps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define SOC_A 1
#define SOC_B 0
#define SOC_C 1
5 changes: 5 additions & 0 deletions tests/esp-idf/tools/cmake/version.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set(IDF_VERSION_MAJOR 5)
set(IDF_VERSION_MINOR 5)
set(IDF_VERSION_PATCH 0)

set(ENV{IDF_VERSION} "${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}.${IDF_VERSION_PATCH}")
5 changes: 5 additions & 0 deletions tests/esp-idf/tools/idf_py_actions/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0

SUPPORTED_TARGETS = ['esp32', 'esp32s2', 'esp32c3', 'esp32s3', 'esp32c2', 'esp32c6', 'esp32h2', 'esp32p4']
PREVIEW_TARGETS = ['linux', 'esp32c5', 'esp32c61', 'esp32h21']

0 comments on commit 1f785e8

Please sign in to comment.