diff --git a/edk2toollib/uefi/edk2/parsers/base_parser.py b/edk2toollib/uefi/edk2/parsers/base_parser.py index 771c8f37..4960d652 100644 --- a/edk2toollib/uefi/edk2/parsers/base_parser.py +++ b/edk2toollib/uefi/edk2/parsers/base_parser.py @@ -240,7 +240,8 @@ def ComputeResult(self, value, cond, value2): return (ivalue != ivalue2) and (value != value2) # check to make sure we only have digits from here on out - if value.upper() in ["TRUE", "FALSE"] or value2.upper() in ["TRUE", "FALSE"]: + if (isinstance(value, str) and value.upper() in ["TRUE", "FALSE"]) \ + or (isinstance(value, str) and value2.upper() in ["TRUE", "FALSE"]): self.Logger.error(f"Invalid comparison: {value} {cond} {value2}") self.Logger.debug(f"Invalid comparison: {value} {cond} {value2}") raise ValueError("Invalid comparison") diff --git a/tests.unit/parsers/test_base_parser.py b/tests.unit/parsers/test_base_parser.py index 771d3722..43c644f9 100644 --- a/tests.unit/parsers/test_base_parser.py +++ b/tests.unit/parsers/test_base_parser.py @@ -488,6 +488,27 @@ def test_emulator_conditional_not_it_all(self): parser.ResetParserState() + def test_conditional_with_variable(self): + ''' Makes sure conversions are correct when using variables ''' + parser = BaseParser("") + parser.LocalVars = {"MAX_SOCKET": '4', 'TARGET': 'DEBUG'} + + self.assertTrue(parser.ProcessConditional('!if $(MAX_SOCKET) <= 4')) + self.assertTrue(parser.InActiveCode()) + self.assertTrue(parser.ProcessConditional('!endif')) + + self.assertTrue(parser.ProcessConditional('!if $(TARGET) == "DEBUG" && $(MAX_SOCKET) <= 4')) + self.assertTrue(parser.InActiveCode()) + self.assertTrue(parser.ProcessConditional('!endif')) + + self.assertTrue(parser.ProcessConditional('!if $(MAX_SOCKET) <= 3')) + self.assertFalse(parser.InActiveCode()) + self.assertTrue(parser.ProcessConditional('!endif')) + + self.assertTrue(parser.ProcessConditional('!if ($(TARGET) == "RELEASE") && ($(MAX_SOCKET) <= 4)')) + self.assertFalse(parser.InActiveCode()) + self.assertTrue(parser.ProcessConditional('!endif')) + class TestBaseParserGuids(unittest.TestCase):