33_mlab_do_not_reload =
True
35gCurrentSpecialMessage =
""
36gSpecialMessageMarker =
"<MLAB TC=1/>"
37gEnablePrettyLogging =
False
38gLogInfoMessages =
True
41 global gCurrentSpecialMessage
42 msg = gCurrentSpecialMessage
43 gCurrentSpecialMessage =
""
46def emitSpecialMessage(loggingMethod, file, line, type, message, escapeHtml=True, formattedStack=''):
47 global gCurrentSpecialMessage
49 escapedMsg = mevis.MLAB.escapeString(message)
50 escapedMsg = escapedMsg.replace(
"\n",
"<br>")
54 if sys.version_info.major >= 3:
55 if isinstance(formattedStack, str):
56 formattedStack = formattedStack.encode()
58 encodedStackTrace = str(base64.b64encode(formattedStack))
60 specialMessage =
"##M[%s|%s|%s|%s]: %s" % (file, line, type, encodedStackTrace, escapedMsg)
61 if gEnablePrettyLogging:
62 gCurrentSpecialMessage = specialMessage
63 escapedMsg =
"<font style='font-weight:normal'><font color=black>" + mevis.MLAB.createHyperLinkWithLine(file, line) +
"(" + str(line) +
"):</font></font> " + escapedMsg
64 escapedMsg += gSpecialMessageMarker
65 loggingMethod(escapedMsg)
67 loggingMethod(specialMessage)
70 global gCurrentSpecialMessage
72 escapedMsg = mevis.MLAB.escapeString(message)
77 specialMessage =
"##C[%s|%s|%s:%s] %s" % (file, line, type,
",".join(fileList), str(escapedMsg))
78 if gEnablePrettyLogging:
79 gCurrentSpecialMessage = specialMessage
80 escapedMsg =
"<font style='font-weight:normal'><font color=black>" + mevis.MLAB.createHyperLinkWithLine(file, line) +
"(" + str(line) +
"):</font></font> " + escapedMsg
81 for fileInList
in fileList:
83 fileInList = fileInList[fileInList.index(
"|")+1:]
84 escapedMsg +=
" " + mevis.MLAB.createHyperLink(fileInList)
85 escapedMsg += gSpecialMessageMarker
86 mevis.MLAB.logHTML(escapedMsg)
88 mevis.MLAB.logHTML(specialMessage)
92 """ This decorator is used to specify the stack frame that is used to detect the location while logging.
93 It uses the stack frame that calls the given function. Functions with this decorator can call each other
94 recursively. The first stack frame of such a recursion is then used. """
95 def setLoggingCallerStackFrameWrapper(*args, **kwargs):
97 return func(*args, **kwargs)
98 return setLoggingCallerStackFrameWrapper
101IncreaseLoggingStackDepthDecorator = UseStackFrameFromCallerForLogging
121 return sys._getframe(stackDepth+1)
137 def __init__(self, errorRegExp, logErrorFunc, preventReplacingMessageSeverity=False):
140 handling = MessageHandling.EXPECT
if logErrorFunc
is not None else MessageHandling.IGNORE
146 def __enableErrorHandling( self, enable ):
147 logHandler = TestHelper.getInstance().getLogHandler()
158 mevis.MLAB.priv().flushPendingMessages()
163 return hasCaughtError
167 """Helper object to supress warnings.
168 Make sure that you call handleResult() in a finally: clause.
169 If no logErrorFunc it given, matching warnings will just be ignored, not expected,
170 so no error will be logged if no match was found.
172 def __init__(self, warningRegExp, logErrorFunc=None, expectWarning=True):
174 handling = MessageHandling.EXPECT
if logErrorFunc
is not None else MessageHandling.IGNORE
180 def __enableWarningHandling( self, enable ):
181 TestHelper.getInstance().getLogHandler().pushOrPopMessageFilter(enable, self.
__messageFilter)
184 mevis.MLAB.priv().flushPendingMessages()
199 def __init__(self, infoRegExp, allowUnexpectedMessages, logErrorFunc):
206 def __enableInfoHandling( self, enable ):
207 TestHelper.getInstance().getLogHandler().pushOrPopMessageFilter(enable, self.
__messageFilter)
210 mevis.MLAB.priv().flushPendingMessages()
214 if hadUnexpectedInfo:
217 if not hadExpectedInfo:
226 ChangeSet.__init__(self, context)
231 def wrapper (*args, **kwds):
233 loggingMethod, message = func(*args, **kwds)
234 if gLogInfoMessages
or (loggingMethod != mevis.MLAB.logHTML):
235 stackFrame = TestHelper.getInstance().getCallerStackFrame()
236 stackLine = traceback.extract_stack(stackFrame, 1)[0]
237 emitSpecialMessage(loggingMethod, stackLine[0], stackLine[1],
"ChangeSet", message, escapeHtml=
True)
244 return mevis.MLAB.logHTML, message
250 return mevis.MLAB.logErrorHTML, message
270 _mTestCaseContext =
None
272 _mHelperModule =
None
274 _mResultDirectory =
None
279 _mTestCaseName =
None
284 _mTestCaseDataDirectory =
None
286 _mTestCaseResultDirectory =
None
289 _mChangeSetStack =
None
298 _mEnvironmentStack =
None
301 _mExtraTestCaseResults =
None
304 _mMacrosLogOnSuccess =
True
307 __lockObj = _thread.allocate_lock()
330 '''Static method to have a reference to **THE UNIQUE** instance'''
331 if len(kargs)
not in (0, 2):
332 raise Exception(
"Singleton's getInstance must be called with context and result directory arguments or without any!")
334 initialize = (len(kargs) == 2)
340 if not "context" in kargs
or not "resultDir" in kargs:
341 raise Exception(
"Singleton must be initialized with context and result directory!")
346 '''Initialize object **here**, as you would do in __init__()...'''
348 self.
_mCtx = kargs[
"context"]
353 self.
_mHelperModule = self.
_mCtx.addModule(mevis.MLAB.moduleLiteral(
"TestCenterHelperModule"))
456 if functionName !=
None:
493 changeSet.enableAutoRevert(
False)
563 os.environ = dict(os.environ)
Class to handle field changes and make them revertable.
__enableInfoHandling(self, enable)
__init__(self, infoRegExp, allowUnexpectedMessages, logErrorFunc)
__allowUnexpectedMessages
__init__(self, stackDepth)
getCallingStackFrame(self, stackDepth)
__preventReplacingMessageSeverity
__previousKeepMessageSeverity
__enableErrorHandling(self, enable)
__init__(self, errorRegExp, logErrorFunc, preventReplacingMessageSeverity=False)
__enableWarningHandling(self, enable)
__init__(self, warningRegExp, logErrorFunc=None, expectWarning=True)
__init__(self, context)
The default constructor.
pushChangeSet(self)
Push a new ChangeSet to the stack.
popEnvironment(self)
Pops an environment dictionary from the stack and makes it the current environment.
getLogHandler(self)
Get the current log handler.
getCallerStackFrame(self)
_mStackFrame
The stack frame of the calling function that is used when logging events.
takeExtraTestCaseResults(self)
_mHelperModule
The context of the helper module.
_mTestCaseName
The name of the current test case.
setLogHandler(self, logHandler)
Set the current log handler.
setTestCaseName(self, testCaseName)
_mChangeSetStack
A stack of ChangeSets used to revert changes to field values.
_mExtraTestCaseResults
A list of extra test case results.
setMacrosLogOnSuccess(self, macrosLogOnSuccess)
bool _mMacrosLogOnSuccess
This is the default value for the 'logOnSuccess' parameter (if it is None) of the ASSERT_*/EXPECT_* m...
__new__(self, *args, **kargs)
The new method.
clearChangeSet(self)
Clear the change set stakc without restoring the field values.
popChangeSet(self)
Pop a ChangeSet from the stack and delete it.
__init__(self, *args, **kargs)
The default constructor.
_mEnvironmentStack
A stack of environment dictionaries used to revert changes to the environment.
setModuleName(self, moduleName)
hasCallerStackFrame(self)
_mModuleName
The name of the currently tested module.
setTestCaseContext(self, ctx)
Set the context of the test.
getChangeSet(self)
Return the ChangeSet on top of the stack.
_mTestCaseContext
The current test's context.
getChangeSetStackLength(self)
Get the length of the ChangeSet stack.
getTestCaseResultDirectory(self)
_mTestCaseDataDirectory
The path to the test case data.
setPackageList(self, packageList)
Set the list of available packages.
getMacrosLogOnSuccess(self)
addExtraTestCaseResult(self, extraTestCaseResult)
setCallerStackFrame(self, stackFrame)
getTestCaseDataDirectory(self)
resetCallerStackFrame(self)
pushEnvironment(self)
Pushes the current environment to the stack.
setTestCaseResultDirectory(self, testCaseName, functionName)
hasInstance(self)
Check if an instance has been initialized.
getPackageList(self)
Return the list of available packages.
unsetLogHandler(self)
Unset the current log handler.
setTestCaseDataDirectory(self, testCaseDataPath)
_mPackageList
The list of packages that should be assumed to be available.
getTestCaseContext(self)
Get the context of the test.
getInstance(self, *args, **kargs)
The default constructor.
_mTestCaseResultDirectory
The path to the results.
_mLogHandler
The log handler that can be used to handle the reports.
_mResultDirectory
The directory used to save results.
emitSpecialMessage(loggingMethod, file, line, type, message, escapeHtml=True, formattedStack='')
emitSpecialCommand(file, line, type, fileList, message, escapeHtml=True)
UseStackFrameFromCallerForLogging(func)
takeCurrentSpecialMessage()