Merge branch 'host-pch' into esp32-pio-pch

This commit is contained in:
J. Nick Koston
2026-09-02 11:18:09 +02:00
191 changed files with 5173 additions and 570 deletions
+147
View File
@@ -0,0 +1,147 @@
"""Unit tests for the ESP_LOG-needs-braces lint rule in script/ci-custom.py.
The rule flags an if/else/for/while whose only body is an unbraced ESP_LOG*() call (which becomes an
empty statement -- and a -Wempty-body warning -- once the log level compiles the macro out). These
tests pin the comment/string/raw-string masker, the accepted control-statement shapes, and the
NOLINT escape hatch at both placements a contributor would try.
"""
import importlib.util
from pathlib import Path
import sys
SCRIPT_DIR = (Path(__file__).parent / ".." / ".." / "script").resolve()
sys.path.insert(0, str(SCRIPT_DIR))
_spec = importlib.util.spec_from_file_location("ci_custom", SCRIPT_DIR / "ci-custom.py")
ci_custom = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(ci_custom)
mask = ci_custom._mask_cpp_comments_strings
def _lint(content: str) -> list:
return ci_custom.lint_esp_log_needs_braces("test.cpp", content)
# --- masker ---
def test_mask_preserves_length_newlines_and_real_parens() -> None:
src = 'foo("bar") + baz();\nqux();\n'
masked = mask(src)
assert len(masked) == len(src)
assert masked.count("\n") == src.count("\n")
assert masked.count("(") == src.count("(") # real parens survive for balancing
def test_mask_blanks_line_and_block_comments() -> None:
assert "ESP_LOGD" not in mask("a; // if (x) ESP_LOGD(t);\n")
assert "ESP_LOGD" not in mask("a; /* if (x) ESP_LOGD(t); */ b;\n")
def test_mask_blanks_string_literals() -> None:
assert "if" not in mask('x = "if (y) ESP_LOGD";\n')
def test_mask_handles_raw_string_without_desync() -> None:
# A raw string full of quotes/parens must be consumed as one unit; code after it stays intact.
src = 's.print(R"(<a href="x">)");\nreturn;\n'
masked = mask(src)
assert "href" not in masked
assert "return;" in masked # not swallowed by a desynced string scan
# --- rule: flags real violations ---
def test_flags_unbraced_if_next_line() -> None:
assert _lint("if (x)\n ESP_LOGD(t);\n")
def test_flags_unbraced_same_line() -> None:
assert _lint("if (x) ESP_LOGW(t);\n")
def test_flags_c_style_for() -> None:
assert _lint("for (int i = 0; i < n; i++)\n ESP_LOGD(t, i);\n")
def test_flags_range_for_and_else() -> None:
assert _lint("for (auto &x : v)\n ESP_LOGCONFIG(t);\n")
assert _lint("else\n ESP_LOGE(t);\n")
def test_flags_for_header_with_nested_call() -> None:
assert _lint("for (auto it = v.begin(); it != v.end(); ++it)\n ESP_LOGD(t);\n")
def test_for_header_does_not_reach_into_a_later_statement() -> None:
# The 'for' header is bounded to its own statement, so it cannot swallow the loop body and latch
# onto a later ')'. Without that, the '#if' line below is reported as an unbraced body even though
# the '#' preprocessor check should skip it.
assert not _lint(
"for (int i = 0; i < n; i++)\n arr[i] = 0;\n#if defined(USE_X)\n ESP_LOGD(t);\n#endif\n"
)
def test_violation_after_a_for_loop_is_reported_at_its_own_line() -> None:
errors = _lint(
"for (int i = 0; i < n; i++)\n sum += a[i];\nif (verbose)\n ESP_LOGD(t, sum);\n"
)
lines = [line for line, _col, _msg in errors]
assert lines == [3] # the 'if', not the 'for' on line 1
def test_flags_lowercase_esph_log_family() -> None:
# core/log.h defines esph_log_*() alongside ESP_LOG*(); both expand to nothing below their level.
assert _lint('if (x)\n esph_log_config(t, "m");\n')
assert _lint('if (err != ESP_OK)\n esph_log_e(t, "m");\n')
def test_digit_separator_does_not_disable_the_rest_of_the_file() -> None:
# A "'" digit separator must not be read as a char-literal opener, which blanked everything after.
assert _lint("uint32_t x = 1'000;\nif (y)\n ESP_LOGD(t);\n")
def test_mask_still_blanks_real_char_literals() -> None:
assert "ESP_LOGD" not in mask("char c = '\"'; // if (x) ESP_LOGD(t);\n")
assert not _lint("char sep = ';';\nif (x) {\n ESP_LOGD(t);\n}\n")
def test_flags_multiline_log_body() -> None:
assert _lint('if (x)\n ESP_LOGD(t, "%d %d",\n a, b);\n')
def test_raw_string_before_violation_still_caught() -> None:
# Regression for the masker desyncing on a raw string and disabling the check for the rest.
assert _lint('s.print(R"(<a href="x">)");\nif (y)\n ESP_LOGD(t);\n')
# --- rule: ignores non-violations ---
def test_ignores_braced_body() -> None:
assert not _lint("if (x) {\n ESP_LOGD(t);\n}\n")
def test_ignores_commented_out_code() -> None:
assert not _lint("// if (x) ESP_LOGD(t);\n")
def test_ignores_preprocessor_else() -> None:
assert not _lint("#else\n ESP_LOGCONFIG(t);\n#endif\n")
def test_ignores_non_log_body() -> None:
assert not _lint("if (x)\n return false;\n")
# --- NOLINT escape hatch, both placements ---
def test_nolint_at_end_of_log_line_suppresses() -> None:
assert not _lint("if (x)\n ESP_LOGD(t); // NOLINT\n")
def test_nolint_on_control_line_suppresses() -> None:
assert not _lint("if (x) // NOLINT\n ESP_LOGD(t);\n")
+115 -22
View File
@@ -165,9 +165,14 @@ def test_main_all_tests_should_run(
patch.object(determine_jobs, "_is_clang_tidy_full_scan", return_value=False),
patch.object(
determine_jobs,
"_all_integration_test_files",
"all_integration_test_files",
return_value=fake_test_files,
),
patch.object(
determine_jobs,
"load_integration_durations",
return_value=dict.fromkeys(fake_test_files, 200.0),
),
patch.object(
determine_jobs,
"get_changed_components",
@@ -203,24 +208,12 @@ def test_main_all_tests_should_run(
output = json.loads(captured.out)
assert output["integration_tests"] is True
# run_all=True expands to the full glob and pre-buckets into 3 parts.
# Each bucket's `tests` is a JSON list of file paths.
assert output["integration_run_all"] is True
# run_all=True expands to the full glob; balance and naming are pinned
# by the unit tests, main() only needs to round-trip the structure
assert isinstance(output["integration_test_buckets"], list)
assert len(output["integration_test_buckets"]) == 3
assert [b["name"] for b in output["integration_test_buckets"]] == [
"1/3",
"2/3",
"3/3",
]
for bucket in output["integration_test_buckets"]:
assert isinstance(bucket["tests"], list)
for path in bucket["tests"]:
assert isinstance(path, str)
bucket_files = [f for b in output["integration_test_buckets"] for f in b["tests"]]
assert bucket_files == fake_test_files
# Bucket sizes are balanced (max-min difference at most 1).
sizes = [len(b["tests"]) for b in output["integration_test_buckets"]]
assert max(sizes) - min(sizes) <= 1
assert sorted(bucket_files) == fake_test_files
assert output["clang_tidy"] is True
assert output["clang_tidy_mode"] in ["nosplit", "split"]
assert output["clang_format"] is True
@@ -529,14 +522,24 @@ def test_compute_integration_test_buckets_at_threshold_stays_single() -> None:
def test_compute_integration_test_buckets_just_over_threshold_splits() -> None:
"""One file over the threshold triggers the 3-bucket fan-out, balanced."""
"""One file over the threshold fans out fully when the weights demand it."""
n = determine_jobs.INTEGRATION_TESTS_SPLIT_THRESHOLD + 1
files = [f"tests/integration/test_{i:02d}.py" for i in range(n)]
run, buckets = determine_jobs._compute_integration_test_buckets(False, files)
with patch.object(
determine_jobs,
"load_integration_durations",
return_value=dict.fromkeys(files, 200.0),
):
run, buckets = determine_jobs._compute_integration_test_buckets(False, files)
assert run is True
assert [b["name"] for b in buckets] == ["1/3", "2/3", "3/3"]
union = [path for b in buckets for path in b["tests"]]
# threshold+1 files x 200s caps at the maximum bucket count.
n_buckets = determine_jobs.INTEGRATION_TESTS_SPLIT_BUCKETS
assert [b["name"] for b in buckets] == [
f"{i + 1}/{n_buckets}" for i in range(n_buckets)
]
union = sorted(path for b in buckets for path in b["tests"])
assert union == sorted(files)
# Equal weights => bucket sizes are balanced (difference at most 1).
sizes = [len(b["tests"]) for b in buckets]
assert max(sizes) - min(sizes) <= 1
@@ -546,7 +549,7 @@ def test_compute_integration_test_buckets_run_all_with_empty_glob_disables_run()
):
"""run_all=True but glob returns no files => run suppressed (otherwise
pytest would collect tests outside tests/integration/)."""
with patch.object(determine_jobs, "_all_integration_test_files", return_value=[]):
with patch.object(determine_jobs, "all_integration_test_files", return_value=[]):
run, buckets = determine_jobs._compute_integration_test_buckets(True, [])
assert run is False
assert buckets == []
@@ -572,6 +575,13 @@ def test_determine_integration_tests(
assert run_all is True
assert test_files == []
# Dependency pins and the session init fixture trigger run_all
for trigger in sorted(determine_jobs.INTEGRATION_TESTS_TRIGGER_FILES):
with patch.object(determine_jobs, "changed_files", return_value=[trigger]):
run_all, test_files = determine_jobs.determine_integration_tests()
assert run_all is True
assert test_files == []
# Python files directly in esphome/ do NOT trigger tests
with patch.object(
determine_jobs, "changed_files", return_value=["esphome/config.py"]
@@ -3238,3 +3248,86 @@ def test_esp8266_native_components_to_test_narrowing(
):
result = determine_jobs.esp8266_native_components_to_test()
assert result == expected
def test_compute_integration_test_buckets_no_durations_full_fanout() -> None:
"""Without recorded durations the fan-out stays at the maximum."""
files = [f"tests/integration/test_{i:03d}.py" for i in range(15)]
with patch.object(determine_jobs, "load_integration_durations", return_value={}):
run, buckets = determine_jobs._compute_integration_test_buckets(False, files)
assert run is True
assert len(buckets) == determine_jobs.INTEGRATION_TESTS_SPLIT_BUCKETS
assert sorted(f for b in buckets for f in b["tests"]) == files
def test_compute_integration_test_buckets_adaptive_count() -> None:
"""A small recorded total weight collapses to one bucket above the threshold."""
files = [f"tests/integration/test_{i:03d}.py" for i in range(15)]
with patch.object(
determine_jobs,
"load_integration_durations",
return_value=dict.fromkeys(files, 10.0),
):
run, buckets = determine_jobs._compute_integration_test_buckets(False, files)
assert run is True
# 15 files x 10s recorded = 150s, under the per-bucket weight target.
assert [b["name"] for b in buckets] == ["1/1"]
assert buckets[0]["tests"] == files
def test_compute_integration_test_buckets_duration_weighted() -> None:
"""Heavy files spread across buckets instead of clustering by sorted name."""
files = [f"tests/integration/test_{i:03d}.py" for i in range(12)]
durations = dict.fromkeys(files, 10.0)
durations[files[0]] = 600.0
durations[files[1]] = 600.0
with patch.object(
determine_jobs, "load_integration_durations", return_value=durations
):
run, buckets = determine_jobs._compute_integration_test_buckets(False, files)
assert run is True
assert len(buckets) >= 2
heavy_buckets = [b for b in buckets if set(files[:2]) & set(b["tests"])]
assert len(heavy_buckets) == 2, "heavy files should land in different buckets"
assert sorted(f for b in buckets for f in b["tests"]) == files
def test_load_integration_durations_missing_or_corrupt(tmp_path: Path) -> None:
"""Missing or unparsable durations data degrades to an empty mapping."""
with patch.object(helpers, "root_path", str(tmp_path)):
assert determine_jobs.load_integration_durations() == {}
durations_file = tmp_path / helpers.INTEGRATION_TEST_DURATIONS_FILE
durations_file.parent.mkdir(parents=True)
durations_file.write_text("not json")
assert determine_jobs.load_integration_durations() == {}
durations_file.write_text('{"tests/integration/test_a.py": 12.5}')
assert determine_jobs.load_integration_durations() == {
"tests/integration/test_a.py": 12.5
}
# Non-positive entries are dropped, valid ones survive
durations_file.write_text(
'{"tests/integration/test_a.py": 12.5, "tests/integration/test_b.py": -1}'
)
assert determine_jobs.load_integration_durations() == {
"tests/integration/test_a.py": 12.5
}
# One non-numeric entry cannot discard the whole recording
durations_file.write_text(
'{"tests/integration/test_a.py": 12.5, "tests/integration/test_b.py": null}'
)
assert determine_jobs.load_integration_durations() == {
"tests/integration/test_a.py": 12.5
}
# A non-dict top level degrades to empty
durations_file.write_text("[12.5]")
assert determine_jobs.load_integration_durations() == {}
def test_committed_integration_durations_are_sane() -> None:
"""The committed recording itself holds positive bounded floats."""
raw = json.loads(
(Path(helpers.root_path) / helpers.INTEGRATION_TEST_DURATIONS_FILE).read_text()
)
assert raw, "committed durations file missing or empty"
assert all(isinstance(v, (int, float)) and 0 < v < 86400 for v in raw.values())
assert all(k.startswith("tests/integration/test_") for k in raw)
+27
View File
@@ -2120,3 +2120,30 @@ def test_get_cpp_changed_components_independent_of_cwd(
assert helpers.get_cpp_changed_components(
["tests/components/time/__init__.py"]
) == ["time"]
def test_lpt_partition_balances_skewed_weights() -> None:
"""Heavy items spread across groups instead of clustering."""
items = [f"i{n}" for n in range(6)]
weights = {"i0": 100.0, "i1": 90.0, "i2": 10.0, "i3": 10.0, "i4": 5.0, "i5": 5.0}
groups = helpers.lpt_partition(items, weights, 2)
group_weights = sorted(sum(weights[i] for i in g) for g in groups)
# Contiguous split would give 200 vs 20; LPT lands at 110 vs 110
assert group_weights == [110.0, 110.0]
assert sorted(i for g in groups for i in g) == items
def test_lpt_partition_more_groups_than_items() -> None:
"""Surplus groups come back empty; every item still lands somewhere."""
items = ["a", "b"]
groups = helpers.lpt_partition(items, {"a": 1.0, "b": 1.0}, 4)
assert len(groups) == 4
assert sorted(i for g in groups for i in g) == items
assert sum(not g for g in groups) == 2
def test_lpt_partition_tie_determinism() -> None:
"""Equal weights assign in input order, so output is reproducible."""
items = [f"i{n}" for n in range(4)]
weights = dict.fromkeys(items, 1.0)
assert helpers.lpt_partition(items, weights, 2) == [["i0", "i2"], ["i1", "i3"]]
@@ -0,0 +1,130 @@
"""Unit tests for script/update_integration_test_durations.py."""
import json
from pathlib import Path
import sys
from unittest.mock import patch
import pytest
# Add the script directory to Python path so we can import the module
script_dir = str((Path(__file__).parent / ".." / ".." / "script").resolve())
sys.path.insert(0, script_dir)
import helpers # noqa: E402
import update_integration_test_durations as uitd # noqa: E402
JUNIT_TEMPLATE = """<?xml version="1.0" encoding="utf-8"?>
<testsuites><testsuite>{testcases}</testsuite></testsuites>
"""
KNOWN = {
"tests/integration/test_a.py",
"tests/integration/test_b.py",
}
def _write_junit(path: Path, testcases: str) -> None:
path.write_text(JUNIT_TEMPLATE.format(testcases=testcases), encoding="utf-8")
def test_collect_durations_sums_per_file(tmp_path: Path) -> None:
"""Testcases from the same module sum."""
_write_junit(
tmp_path / "a.xml",
'<testcase classname="tests.integration.test_a" name="t1" time="1.5"/>'
'<testcase classname="tests.integration.test_a" name="t2" time="2.0"/>'
'<testcase classname="tests.integration.test_b" name="t1" time="4.0"/>',
)
assert uitd.collect_durations(tmp_path, KNOWN) == {
"tests/integration/test_a.py": 3.5,
"tests/integration/test_b.py": 4.0,
}
def test_collect_durations_class_based_testcase(tmp_path: Path) -> None:
"""A class-based classname still maps to its module file."""
_write_junit(
tmp_path / "a.xml",
'<testcase classname="tests.integration.test_a.TestFoo" name="t" time="2.5"/>',
)
assert uitd.collect_durations(tmp_path, KNOWN) == {
"tests/integration/test_a.py": 2.5
}
def test_collect_durations_unknown_module_skipped(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""A classname that maps to no known file is skipped with a warning."""
_write_junit(
tmp_path / "a.xml",
'<testcase classname="tests.integration.test_gone" name="t" time="2.5"/>',
)
assert uitd.collect_durations(tmp_path, KNOWN) == {}
assert "test_gone" in capsys.readouterr().err
def test_collect_durations_skips_skipped_testcases(tmp_path: Path) -> None:
"""Skipped testcases do not record a bogus zero duration."""
_write_junit(
tmp_path / "a.xml",
'<testcase classname="tests.integration.test_a" name="t" time="0">'
"<skipped/></testcase>",
)
assert uitd.collect_durations(tmp_path, KNOWN) == {}
def test_collect_durations_unexpected_classname_aborts(tmp_path: Path) -> None:
"""A classname outside tests.integration means the junit layout changed."""
_write_junit(
tmp_path / "a.xml",
'<testcase classname="tests.unit_tests.test_x" name="t" time="9.0"/>',
)
with pytest.raises(SystemExit):
uitd.collect_durations(tmp_path, KNOWN)
def test_collect_durations_empty_dir_aborts(tmp_path: Path) -> None:
"""No junit XML at all is a hard error, not an empty recording."""
with pytest.raises(SystemExit):
uitd.collect_durations(tmp_path, KNOWN)
def test_main_merges_partial_run(tmp_path: Path) -> None:
"""A partial run merges over the previous data instead of truncating it."""
tests_dir = tmp_path / "tests" / "integration"
tests_dir.mkdir(parents=True)
for name in ("test_a", "test_b", "test_c"):
(tests_dir / f"{name}.py").write_text("", encoding="utf-8")
durations_file = tmp_path / helpers.INTEGRATION_TEST_DURATIONS_FILE
durations_file.write_text(
json.dumps(
{
"tests/integration/test_a.py": 5.0,
"tests/integration/test_b.py": 7.0,
"tests/integration/test_gone.py": 9.0,
}
),
encoding="utf-8",
)
junit_dir = tmp_path / "junit"
junit_dir.mkdir()
_write_junit(
junit_dir / "a.xml",
'<testcase classname="tests.integration.test_a" name="t" time="6.0"/>',
)
with (
patch.object(helpers, "root_path", str(tmp_path)),
patch.object(uitd, "DURATIONS_FILE", durations_file),
):
# 1 of 3 files covered: refused without --allow-partial
with patch.object(sys, "argv", ["uitd", str(junit_dir)]):
assert uitd.main() == uitd.EXIT_LOW_COVERAGE
with patch.object(sys, "argv", ["uitd", str(junit_dir), "--allow-partial"]):
assert uitd.main() == 0
# test_a updated, test_b kept, deleted test_gone dropped
assert json.loads(durations_file.read_text()) == {
"tests/integration/test_a.py": 6.0,
"tests/integration/test_b.py": 7.0,
}