Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sort App instance correctly #110

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/check-pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
with:
extra_args: --files ${{ steps.changed-files.outputs.all_changed_files }}
18 changes: 14 additions & 4 deletions idf_build_apps/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -305,6 +306,7 @@ def files_matches_patterns(
return False


@functools.total_ordering
class BaseModel(_BaseModel):
"""
BaseModel that is hashable
Expand All @@ -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:
Expand Down
24 changes: 23 additions & 1 deletion tests/test_app.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
Loading