Skip to content

Commit

Permalink
oelint.var.inherit: check on correct usage
Browse files Browse the repository at this point in the history
of inherit and inherit_defer statements

Closes #477

Signed-off-by: Konrad Weihmann <[email protected]>
  • Loading branch information
priv-kweihmann committed Jan 17, 2024
1 parent 34ec3f9 commit d84deb1
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ Rules marked with **[S]** can have multiple sub-IDs
* oelint.var.bbclassextend - Use BBCLASSEXTEND when possible
* oelint.var.filesoverride - FILES:*(FILES_*) variables should not be overridden
* oelint.var.improperinherit - Warn about improperly named inherits
* oelint.var.inherit - Check the correct usage of inherit and inherit_defer ('scarthgap' release and newer)
* oelint.var.licenseremotefile - License shall be a file in remote source not a local file
* oelint.var.licensesdpx - Check for correct SPDX syntax in licenses
* oelint.var.mandatoryvar - Check for mandatory variables **[S]**
Expand Down
36 changes: 36 additions & 0 deletions oelint_adv/rule_base/rule_vars_inherit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from typing import List, Tuple

from oelint_parser.cls_item import Inherit
from oelint_parser.cls_stash import Stash

from oelint_adv.cls_rule import Rule


class VarInherit(Rule):
def __init__(self) -> None:
self.__supports_defer = False
super().__init__(id='oelint.var.inherit',
severity='warning',
message='<FOO>',
appendix=['inherit', 'inherit_defer'])

def check(self, _file: str, stash: Stash) -> List[Tuple[str, int, str]]:
res = []
if not self.__supports_defer:
return res
items: List[Inherit] = stash.GetItemsFor(filename=_file, classifier=Inherit.CLASSIFIER)
for i in items:
if '${' not in i.RealRaw and i.Statement == 'inherit_defer':
res += self.finding(i.Origin, i.InFileLine,
'inherit_defer should only be used if there is a variable involved',
appendix='inherit')
if '${' in i.RealRaw and i.Statement == 'inherit':
res += self.finding(i.Origin, i.InFileLine,
'Variable detected in inherit statement. Use inherit_defer instead',
appendix='inherit_defer')
return res

def check_release_range(self, release_range: List[str]) -> bool:
if 'scarthgap' in release_range:
self.__supports_defer = True
return super().check_release_range(release_range)
86 changes: 86 additions & 0 deletions tests/test_class_oelint_var_inherit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import pytest # noqa: I900

from .base import TestBaseClass


class TestClassOelintVarInherit(TestBaseClass):

@pytest.mark.parametrize('id_', ['oelint.var.inherit.inherit_defer'])
@pytest.mark.parametrize('occurrence', [1])
@pytest.mark.parametrize('input_',
[
{
'oelint_adv_test.bb':
'inherit ${A}',
},
{
'oelint_adv_test.bb':
'''
inherit def ${C}
''',
},
{
'oelint_adv_test.bb':
'''
inherit def ${@oe.utils.ifelse(1 == 1, 'a', C}
''',
},
],
)
def test_bad_inherit(self, input_, id_, occurrence):
self.check_for_id(self._create_args(input_), id_, occurrence)

@pytest.mark.parametrize('id_', ['oelint.var.inherit.inherit'])
@pytest.mark.parametrize('occurrence', [1])
@pytest.mark.parametrize('input_',
[
{
'oelint_adv_test.bb':
'inherit_defer A',
},
],
)
def test_bad_inherit_defer(self, input_, id_, occurrence):
self.check_for_id(self._create_args(input_), id_, occurrence)

@pytest.mark.parametrize('id_', ['oelint.var.inherit.inherit'])
@pytest.mark.parametrize('occurrence', [0])
@pytest.mark.parametrize('input_',
[
{
'oelint_adv_test.bb':
'''
inherit abc def
''',
},
],
)
def test_good_inherit(self, input_, id_, occurrence):
self.check_for_id(self._create_args(input_), id_, occurrence)

@pytest.mark.parametrize('id_', ['oelint.var.inherit.inherit_defer'])
@pytest.mark.parametrize('occurrence', [0])
@pytest.mark.parametrize('input_',
[
{
'oelint_adv_test.bb':
'''
inherit_defer ${A}
''',
},
{
'oelint_adv_test.bb':
'''
inherit_defer abc ${A}
''',
},
{
'oelint_adv_test.bb':
'''
inherit_defer ${@oe.utils.ifelse(1 == 1, 'a', C}
''',
},
],
)
def test_good_inherit_defer(self, input_, id_, occurrence):
self.check_for_id(self._create_args(input_), id_, occurrence)

0 comments on commit d84deb1

Please sign in to comment.