diff --git a/nw/error.py b/nw/error.py index 988b49e6..6db88647 100644 --- a/nw/error.py +++ b/nw/error.py @@ -135,7 +135,7 @@ class NWErrorMessage(QDialog): # END Class NWErrorMessage -def exceptionHandler(exType, exValue, exTrace, testMode=False): +def exceptionHandler(exType, exValue, exTrace): """Function to catch unhandled global exceptions. """ import nw @@ -173,10 +173,7 @@ def exceptionHandler(exType, exValue, exTrace, testMode=False): logger.critical("Could not close the project before exiting") logger.critical(str(e)) - if testMode: - return errMsg.msgBody.toPlainText() - else: - qApp.exit(1) + qApp.exit(1) except Exception as e: logger.critical(str(e)) diff --git a/tests/test_base_error.py b/tests/test_base_error.py index e3bd689d..540b51f0 100644 --- a/tests/test_base_error.py +++ b/tests/test_base_error.py @@ -13,6 +13,8 @@ from nw.error import NWErrorMessage, exceptionHandler @pytest.mark.base def testBaseError_Dialog(qtbot, monkeypatch, fncDir, tmpDir): + """Test the error dialog. + """ qApp.closeAllWindows() nwGUI = nw.main(["--testmode", "--config=%s" % fncDir, "--data=%s" % tmpDir]) qtbot.addWidget(nwGUI) @@ -28,60 +30,72 @@ def testBaseError_Dialog(qtbot, monkeypatch, fncDir, tmpDir): assert nwErr.msgBody.toPlainText() == "Failed to generate error report ..." # Valid Error Message - nwErr.setMessage(Exception, "First Error", None) + monkeypatch.setattr("PyQt5.QtCore.QSysInfo.kernelVersion", lambda: "1.2.3") + nwErr.setMessage(Exception, "Fine Error", None) theMessage = nwErr.msgBody.toPlainText() assert theMessage - assert "First Error" in theMessage - assert "Exception" in theMessage - nwErr._doClose() - nwErr.close() - - # Valid Error - monkeypatch.setattr("PyQt5.QtCore.QSysInfo.kernelVersion", lambda: "1.2.3") - theMessage = exceptionHandler(Exception, "Second Error", None, testMode=True) - assert theMessage - assert "Second Error" in theMessage + assert "Fine Error" in theMessage assert "Exception" in theMessage assert "(1.2.3)" in theMessage monkeypatch.undo() # No kernel version retrieved monkeypatch.setattr("PyQt5.QtCore.QSysInfo.kernelVersion", causeException) - theMessage = exceptionHandler(Exception, "Third Error", None, testMode=True) + nwErr.setMessage(Exception, "Almost Fine Error", None) + theMessage = nwErr.msgBody.toPlainText() assert theMessage - assert "Third Error" in theMessage - assert "Exception" in theMessage assert "(Unknown)" in theMessage monkeypatch.undo() - # Normal shutdown, but not testmode + nwErr._doClose() + nwErr.close() + nwGUI.closeMain() + +# END Test testBaseError_Dialog + +@pytest.mark.base +def testBaseError_Handler(qtbot, monkeypatch, fncDir, tmpDir): + """Test the error handler. This test doesn'thave any asserts, but it + checks that the error handler handles potential exceptions. The test + will fail if excpetions are not handled. + """ + qApp.closeAllWindows() + nwGUI = nw.main(["--testmode", "--config=%s" % fncDir, "--data=%s" % tmpDir]) + qtbot.addWidget(nwGUI) + nwGUI.show() + qtbot.waitForWindowShown(nwGUI) + + # Normal shutdown monkeypatch.setattr("PyQt5.QtWidgets.qApp.exit", lambda x: None) nwGUI.mainConf.showGUI = True - exceptionHandler(Exception, "Third Error", None, testMode=False) + exceptionHandler(Exception, "Error Message", None) nwGUI.mainConf.showGUI = False monkeypatch.undo() # Disable blocking of GUI monkeypatch.setattr("PyQt5.QtWidgets.QDialog.exec_", lambda: None) - exceptionHandler(Exception, "Third Error", None, testMode=True) + monkeypatch.setattr("PyQt5.QtWidgets.qApp.exit", lambda x: None) + exceptionHandler(Exception, "Error Message", None) monkeypatch.undo() # Should handle qApp failing monkeypatch.setattr("PyQt5.QtWidgets.qApp.topLevelWidgets", causeException) - exceptionHandler(Exception, "Third Error", None, testMode=True) + monkeypatch.setattr("PyQt5.QtWidgets.qApp.exit", lambda x: None) + exceptionHandler(Exception, "Error Message", None) monkeypatch.undo() # Should handle failing to close main GUI monkeypatch.setattr(nwGUI, "closeMain", causeException) - exceptionHandler(Exception, "Third Error", None, testMode=True) + monkeypatch.setattr("PyQt5.QtWidgets.qApp.exit", lambda x: None) + exceptionHandler(Exception, "Error Message", None) monkeypatch.undo() # Should not crash when no GUI is found - nwGUI.setObjectName("Stuff") - assert exceptionHandler(Exception, "Third Error", None, testMode=True) is None + monkeypatch.setattr("PyQt5.QtWidgets.qApp.topLevelWidgets", lambda: []) + monkeypatch.setattr("PyQt5.QtWidgets.qApp.exit", lambda x: None) + exceptionHandler(Exception, "Error Message", None) + monkeypatch.undo() nwGUI.closeMain() - # qtbot.stopForInteraction() - -# END Test testBaseError_Dialog +# END Test testBaseError_Handler