From b88375c4051c01640ba6201d47d4944d6f8959b7 Mon Sep 17 00:00:00 2001 From: Fu Hanxi Date: Thu, 4 Jan 2024 14:47:47 +0100 Subject: [PATCH 1/2] fix: sort App instance correctly --- idf_build_apps/utils.py | 18 ++++++++++++++---- tests/test_app.py | 24 +++++++++++++++++++++++- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/idf_build_apps/utils.py b/idf_build_apps/utils.py index e5fd5e8..48ec1cd 100644 --- a/idf_build_apps/utils.py +++ b/idf_build_apps/utils.py @@ -1,7 +1,8 @@ -# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Apache-2.0 import fnmatch +import functools import glob import logging import os @@ -305,6 +306,7 @@ def files_matches_patterns( return False +@functools.total_ordering class BaseModel(_BaseModel): """ BaseModel that is hashable @@ -315,11 +317,19 @@ class BaseModel(_BaseModel): def __lt__(self, other: t.Any) -> bool: if isinstance(other, self.__class__): for k in self.model_dump(): - if getattr(self, k) != getattr(other, k): - return getattr(self, k) < getattr(other, k) - else: + if k in self.__EQ_IGNORE_FIELDS__: continue + self_attr = getattr(self, k, '') or '' + other_attr = getattr(other, k, '') or '' + + if self_attr != other_attr: + return self_attr < other_attr + + continue + + return False + return NotImplemented def __eq__(self, other: t.Any) -> bool: diff --git a/tests/test_app.py b/tests/test_app.py index 0d6ba71..77e81d3 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -1,6 +1,8 @@ -# SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Apache-2.0 +import pytest + from idf_build_apps import ( AppDeserializer, CMakeApp, @@ -32,3 +34,23 @@ def test_deserialization(tmp_path): assert a == a_s assert b == b_s + + +def test_app_sorting(): + a = CMakeApp('foo', 'esp32') + b = MakeApp('foo', 'esp32') + + c = CMakeApp('foo', 'esp32', size_json_filename='size.json') + d = CMakeApp('foo', 'esp32s2') + e = CMakeApp('foo', 'esp32s2', build_comment='build_comment') + + with pytest.raises(TypeError, match="'<' not supported between instances of 'CMakeApp' and 'MakeApp'"): + assert a < b + + assert a < c < d + assert d > c > a + + # __EQ_IGNORE_FIELDS__ + assert d == e + assert not d < e + assert not d > e From a8d6abfacbb26b5366c444ff734ca9f2c2f320e0 Mon Sep 17 00:00:00 2001 From: Fu Hanxi Date: Thu, 4 Jan 2024 15:09:37 +0100 Subject: [PATCH 2/2] ci: run pre-commit only with changed files --- .github/workflows/check-pre-commit.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/check-pre-commit.yml b/.github/workflows/check-pre-commit.yml index 96c98cb..817ff10 100644 --- a/.github/workflows/check-pre-commit.yml +++ b/.github/workflows/check-pre-commit.yml @@ -11,4 +11,8 @@ jobs: with: fetch-depth: 0 - uses: actions/setup-python@v5 + - id: changed-files + uses: tj-actions/changed-files@v41 - uses: pre-commit/action@v3.0.0 + with: + extra_args: --files ${{ steps.changed-files.outputs.all_changed_files }}