diff --git a/oelint_parser/cls_stash.py b/oelint_parser/cls_stash.py index 73a9f53..ba127ad 100644 --- a/oelint_parser/cls_stash.py +++ b/oelint_parser/cls_stash.py @@ -464,7 +464,7 @@ def ExpandVar(self, return {} _exp = { "PN": self.GuessRecipeName(filename), - "PV": self.GuessRecipeVersion(filename), + "PV": self.GuessRecipeVersion(filename) or "1.0", "BPN": self.GuessBaseRecipeName(filename), } _exp = {**_exp, **CONSTANTS.SetsBase} @@ -692,7 +692,7 @@ def GuessBaseRecipeName(self, _file: str) -> str: return tmp_ @functools.cache # noqa: B019 - def GuessRecipeVersion(self, _file: str) -> str: + def GuessRecipeVersion(self, _file: str) -> str | None: """Get recipe version from filename Arguments: @@ -702,7 +702,8 @@ def GuessRecipeVersion(self, _file: str) -> str: str -- recipe version """ _name, _ = os.path.splitext(os.path.basename(_file)) - return _name.split("_")[-1] + _pn, *_pv = _name.split("_") + return [*_pv, None][0] def _ReverseInlineBlock(self, varref: Variable) -> str: res = varref.VarValueStripped @@ -763,7 +764,7 @@ def _expand(res, _file, m, quote: str = ''): elif m.group(1) in ["BPN"]: res = res.replace(m.group(0), self.GuessBaseRecipeName(_file)) elif m.group(1) in ["PV"]: - res = res.replace(m.group(0), self.GuessRecipeVersion(_file)) + res = res.replace(m.group(0), self.GuessRecipeVersion(_file) or "1.0") elif m.group(1) in ["FILE"]: res = res.replace(m.group(0), f'{quote}{_file}{quote}') elif m.group(1) in ["THISDIR"]: diff --git a/tests/test_expand.py b/tests/test_expand.py index 1f6353c..a14e771 100644 --- a/tests/test_expand.py +++ b/tests/test_expand.py @@ -6,12 +6,11 @@ class OelintLinking(unittest.TestCase): - def _create_tempfile(self, _input): + def _create_tempfile(self, _input, _file = 'testfile.bb'): self.__created_files = getattr(self, '__created_files', {}) self._collected_tmpdirs = getattr(self, '_collect_tmpdirs', []) self._tmpdir = getattr(self, '_tmpdir', tempfile.mkdtemp()) self._collected_tmpdirs.append(self._tmpdir) - _file = 'testfile.bb' _path = os.path.join(self._tmpdir, _file) os.makedirs(os.path.dirname(_path), exist_ok=True) @@ -302,3 +301,22 @@ def test_expandvar_file_ref(self): _file, attribute=Variable.ATTR_VAR, attributeValue='A') self.assertEqual(' '.join(res.get('A', '')), f'${{@some.function(d, "{_file}")}}/abc') + + def test_expand_pv(self): + from oelint_parser.cls_stash import Stash + from oelint_parser.cls_item import Variable + + for file_name, file_content, expected in [ + ('myrecipe.bb', 'A = "b"', '1.0'), + ('myrecipe.bb', 'PV = "2.1"', '2.1'), + ('myrecipe_1.0.bb', 'A = "b"', '1.0'), + ('myrecipe_3.3.bb', 'A = "b"', '3.3'), + ('myrecipe_3.1.bb', 'PV = "4.2"', '4.2'), + ]: + self.__stash = Stash() + _file = self._create_tempfile(file_content, file_name) + self.__stash.AddFile(_file) + self.__stash.Finalize() + + res = self.__stash.ExpandTerm(_file, '${PV}') + self.assertEqual(res, expected)