Skip to content

Commit

Permalink
Initial investigation of missing dependencies scenario.
Browse files Browse the repository at this point in the history
  • Loading branch information
lextm committed Nov 2, 2024
1 parent 7805e64 commit e470a8f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
3 changes: 2 additions & 1 deletion pysmi/codegen/pysnmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pysmi import debug, error
from pysmi.codegen import jfilters
from pysmi.codegen.intermediate import IntermediateCodeGen
from pysmi.mibinfo import MibInfo


class PySnmpCodeGen(IntermediateCodeGen):
Expand Down Expand Up @@ -77,7 +78,7 @@ class PySnmpCodeGen(IntermediateCodeGen):
"INET-ADDRESS-MIB",
) + IntermediateCodeGen.baseMibs

def gen_code(self, ast, symbolTable, **kwargs):
def gen_code(self, ast, symbolTable, **kwargs) -> "tuple[MibInfo, str]":
mibInfo, context = IntermediateCodeGen.gen_code(
self, ast, symbolTable, **kwargs
)
Expand Down
4 changes: 3 additions & 1 deletion pysmi/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class MibCompiler:
_borrowers: list[AbstractBorrower]
_parsedMibs: dict[str, tuple]

failedMibs: dict[str, error.PySmiError]

def __init__(self, parser, codegen: AbstractCodeGen, writer: AbstractWriter):
"""Creates an instance of *MibCompiler* class.
Expand Down Expand Up @@ -274,7 +276,7 @@ class instances (values)
break
except UnicodeDecodeError:
debug.logger & debug.FLAG_COMPILER and debug.logger(
f"http exeception {mibname} found at {source}"
f"http exception {mibname} found at {source}"
)
continue

Expand Down
2 changes: 1 addition & 1 deletion pysmi/mibinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class MibInfo:
compliance = ()

#: imported MIB names
imported = ()
imported: tuple[str, ...] = ()

def __init__(self, **kwargs):
for k in kwargs:
Expand Down
52 changes: 46 additions & 6 deletions tests/test_imports_smiv2_pysnmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@
# License: https://www.pysnmp.com/pysmi/license.html
#
import sys
import unittest

try:
import unittest2 as unittest

except ImportError:
import unittest

from pysmi import error
from pysmi.parser.smi import parserFactory
from pysmi.codegen.pysnmp import PySnmpCodeGen
from pysmi.codegen.symtable import SymtableCodeGen
Expand Down Expand Up @@ -878,6 +874,50 @@ def testCompilerCycleDetection(self):
self.assertEqual(results["Other-MIB"], "compiled", "Other-MIB was not compiled")


class ImportMissingTestCase(unittest.TestCase):
"""
Test-MIB DEFINITIONS ::= BEGIN
IMPORTS
someObject
FROM OTHER-MIB;
END
"""

def setUp(self):
self.mibCompiler = MibCompiler(
SmiStarParser(), PySnmpCodeGen(), CallbackWriter(lambda m, d, c: None)
)

self.testMibLoaded = False

def getMibData(mibname, context):
if mibname in PySnmpCodeGen.baseMibs:
return f"{mibname} DEFINITIONS ::= BEGIN\nEND"

if mibname == "OTHER-MIB":
raise error.PySmiReaderFileNotFoundError(
f"source MIB {mibname} not found", reader=self
)
else:
self.assertEqual(mibname, "TEST-MIB", f"unexpected MIB name {mibname}")
self.assertFalse(
self.testMibLoaded, "TEST-MIB was loaded more than once"
)
self.testMibLoaded = True
return self.__class__.__doc__

self.mibCompiler.add_sources(CallbackReader(getMibData))
self.mibCompiler.add_searchers(StubSearcher(*PySnmpCodeGen.baseMibs))

def testMissingImports(self):
results = self.mibCompiler.compile("TEST-MIB", noDeps=False)
# TODO: this test case is invalid right now, as the accurate error reported should point out that OTHER-MIB is missing.
self.assertEqual(
results["Test-MIB"], "unprocessed", "Test-MIB was not marked as missing"
)


suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])

if __name__ == "__main__":
Expand Down

0 comments on commit e470a8f

Please sign in to comment.