Updated and extended the testing of the main function

This commit is contained in:
Veronica K. B. Olsen
2020-12-07 20:00:58 +01:00
parent ee7a5e1681
commit a4ee1e112c
4 changed files with 132 additions and 47 deletions
+15 -14
View File
@@ -246,19 +246,20 @@ def main(sysArgs=None):
errorCode |= 32 errorCode |= 32
if errorData: if errorData:
if not testMode: errApp = QApplication([])
errApp = QApplication([]) errMsg = QErrorMessage()
errMsg = QErrorMessage() errMsg.resize(500, 300)
errMsg.resize(500, 300) errMsg.showMessage((
errMsg.showMessage(( "<h3>A critical error has been encountered</h3>"
"<h3>A critical error has been encountered</h3>" "<p>novelWriter cannot start due to the following issues:<p>"
"<p>novelWriter cannot start due to the following issues:<p>" "<p>&nbsp;-&nbsp;%s</p>"
"<p>&nbsp;-&nbsp;%s</p>" "<p>Shutting down ...</p>"
"<p>Shutting down ...</p>" ) % (
) % ( "<br>&nbsp;-&nbsp;".join(errorData)
"<br>&nbsp;-&nbsp;".join(errorData) ))
)) for errMsg in errorData:
errApp.exec_() logger.critical(errMsg)
errApp.exec_()
sys.exit(errorCode) sys.exit(errorCode)
# Finish initialising config # Finish initialising config
@@ -293,4 +294,4 @@ def main(sysArgs=None):
nwGUI = GuiMain() nwGUI = GuiMain()
sys.exit(nwApp.exec_()) sys.exit(nwApp.exec_())
return # END Function main
+2
View File
@@ -61,7 +61,9 @@ The commands for the respective test categories are listed below.
| Type | Test Target | Source File(s) | Marker | Filter | | Type | Test Target | Source File(s) | Marker | Filter |
| :--- | :----------------- | :-------------------- | :-------- | :-------------------- | | :--- | :----------------- | :-------------------- | :-------- | :-------------------- |
| Unit | Main function | nw/\_\_init\_\_.py | `-m base` | `-k testBaseInit` |
| Unit | Common functions | nw/common.py | `-m base` | `-k testBaseCommon` | | Unit | Common functions | nw/common.py | `-m base` | `-k testBaseCommon` |
| Unit | Error handlers | nw/error.py | `-m base` | `-k testBaseError` |
| Unit | Core functions | nw/core/tools.py | `-m core` | `-k testCoreTools` | | Unit | Core functions | nw/core/tools.py | `-m core` | `-k testCoreTools` |
| Unit | NWDoc class | nw/core/document.py | `-m core` | `-k testCoreDocument` | | Unit | NWDoc class | nw/core/document.py | `-m core` | `-k testCoreDocument` |
| Unit | NWIndex class | nw/core/index.py | `-m core` | `-k testCoreIndex` | | Unit | NWIndex class | nw/core/index.py | `-m core` | `-k testCoreIndex` |
+10 -4
View File
@@ -13,7 +13,7 @@ class DummyMain():
self.hasProject = True self.hasProject = True
self.theIndex = None self.theIndex = None
self.theProject = None self.theProject = None
self.statusBar = StatusBar() self.statusBar = DummyStatusBar()
# Test Variables # Test Variables
self.askResponse = True self.askResponse = True
@@ -42,6 +42,12 @@ class DummyMain():
def rebuildIndex(self): def rebuildIndex(self):
return return
def closeMain(self):
return "closeMain"
def close(self):
return "close"
# Test Functions # Test Functions
def undo(self): def undo(self):
@@ -52,9 +58,9 @@ class DummyMain():
self.lastAlert = "" self.lastAlert = ""
return return
# END Class GuiMain # END Class DummyMain
class StatusBar(): class DummyStatusBar():
def __init__(self): def __init__(self):
return return
@@ -62,7 +68,7 @@ class StatusBar():
def setStatus(self, theText): def setStatus(self, theText):
return return
# END Class StatusBar # END Class DummyStatusBar
# =========================================================================== # # =========================================================================== #
# Error Functions # Error Functions
+105 -29
View File
@@ -7,81 +7,157 @@ import pytest
import logging import logging
import sys import sys
from dummy import DummyMain
@pytest.mark.base @pytest.mark.base
def testBaseInit_Launch(qtbot, monkeypatch, fncDir, tmpDir): def testBaseInit_Launch(caplog, monkeypatch, tmpDir):
"""Test the main __init__.py file. """Check launching the main GUI.
""" """
monkeypatch.setattr("nw.guimain.GuiMain", DummyMain)
# Testmode launch
nwGUI = nw.main(
["--testmode", "--config=%s" % tmpDir, "--data=%s" % tmpDir]
)
assert isinstance(nwGUI, DummyMain)
# Darwin launch
monkeypatch.setitem(sys.modules, "Foundation", None)
osDarwin = nw.CONFIG.osDarwin
nw.CONFIG.osDarwin = True
nwGUI = nw.main(
["--testmode", "--config=%s" % tmpDir, "--data=%s" % tmpDir]
)
assert isinstance(nwGUI, DummyMain)
assert "Foundation" in caplog.messages[1]
nw.CONFIG.osDarwin = osDarwin
# Normal launch
monkeypatch.setattr("PyQt5.QtWidgets.QApplication.__init__", lambda *args: None)
monkeypatch.setattr("PyQt5.QtWidgets.QApplication.setApplicationName", lambda *args: None)
monkeypatch.setattr("PyQt5.QtWidgets.QApplication.setApplicationVersion", lambda *args: None)
monkeypatch.setattr("PyQt5.QtWidgets.QApplication.setWindowIcon", lambda *args: None)
monkeypatch.setattr("PyQt5.QtWidgets.QApplication.setOrganizationDomain", lambda *args: None)
monkeypatch.setattr("PyQt5.QtWidgets.QApplication.exec_", lambda *args: 0)
with pytest.raises(SystemExit) as ex:
nw.main(["--config=%s" % tmpDir, "--data=%s" % tmpDir])
assert ex.value.code == 0
monkeypatch.undo()
# END Test testBaseInit_Launch
@pytest.mark.base
def testBaseInit_Options(monkeypatch, tmpDir):
"""Test command line options for logging level.
"""
monkeypatch.setattr("nw.guimain.GuiMain", DummyMain)
monkeypatch.setattr(sys, "argv", [
"novelWriter.py", "--testmode", "--config=%s" % tmpDir, "--data=%s" % tmpDir
])
# Defaults w/None Args
nwGUI = nw.main()
assert nw.logger.getEffectiveLevel() == logging.WARNING
assert nw.CONFIG.debugInfo is False
assert nw.CONFIG.showGUI is False
assert nwGUI.closeMain() == "closeMain"
# Defaults # Defaults
nwGUI = nw.main( nwGUI = nw.main(
["--testmode", "--config=%s" % fncDir, "--data=%s" % tmpDir, "--style=Fusion"] ["--testmode", "--config=%s" % tmpDir, "--data=%s" % tmpDir, "--style=Fusion"]
) )
assert nw.logger.getEffectiveLevel() == logging.WARNING assert nw.logger.getEffectiveLevel() == logging.WARNING
nwGUI.closeMain() assert nw.CONFIG.debugInfo is False
nwGUI.close() assert nwGUI.closeMain() == "closeMain"
# Log Levels # Log Levels
nwGUI = nw.main( nwGUI = nw.main(
["--testmode", "--info", "--config=%s" % fncDir, "--data=%s" % tmpDir] ["--testmode", "--info", "--config=%s" % tmpDir, "--data=%s" % tmpDir]
) )
assert nw.logger.getEffectiveLevel() == logging.INFO assert nw.logger.getEffectiveLevel() == logging.INFO
nwGUI.closeMain() assert nw.CONFIG.debugInfo is False
nwGUI.close() assert nwGUI.closeMain() == "closeMain"
nwGUI = nw.main( nwGUI = nw.main(
["--testmode", "--debug", "--config=%s" % fncDir, "--data=%s" % tmpDir] ["--testmode", "--debug", "--config=%s" % tmpDir, "--data=%s" % tmpDir]
) )
assert nw.logger.getEffectiveLevel() == logging.DEBUG assert nw.logger.getEffectiveLevel() == logging.DEBUG
nwGUI.closeMain() assert nw.CONFIG.debugInfo is True
nwGUI.close() assert nwGUI.closeMain() == "closeMain"
nwGUI = nw.main( nwGUI = nw.main(
["--testmode", "--verbose", "--config=%s" % fncDir, "--data=%s" % tmpDir] ["--testmode", "--verbose", "--config=%s" % tmpDir, "--data=%s" % tmpDir]
) )
assert nw.logger.getEffectiveLevel() == 5 assert nw.logger.getEffectiveLevel() == 5
nwGUI.closeMain() assert nw.CONFIG.debugInfo is True
nwGUI.close() assert nwGUI.closeMain() == "closeMain"
# Help and Version # Help and Version
with pytest.raises(SystemExit) as ex: with pytest.raises(SystemExit) as ex:
nwGUI = nw.main( nwGUI = nw.main(
["--testmode", "--help", "--config=%s" % fncDir, "--data=%s" % tmpDir] ["--testmode", "--help", "--config=%s" % tmpDir, "--data=%s" % tmpDir]
) )
nwGUI.closeMain() assert nwGUI.closeMain() == "closeMain"
nwGUI.close()
assert ex.value.code == 0 assert ex.value.code == 0
with pytest.raises(SystemExit) as ex: with pytest.raises(SystemExit) as ex:
nwGUI = nw.main( nwGUI = nw.main(
["--testmode", "--version", "--config=%s" % fncDir, "--data=%s" % tmpDir] ["--testmode", "--version", "--config=%s" % tmpDir, "--data=%s" % tmpDir]
) )
nwGUI.closeMain() assert nwGUI.closeMain() == "closeMain"
nwGUI.close()
assert ex.value.code == 0 assert ex.value.code == 0
# Invalid options # Invalid options
with pytest.raises(SystemExit) as ex: with pytest.raises(SystemExit) as ex:
nwGUI = nw.main( nwGUI = nw.main(
["--testmode", "--invalid", "--config=%s" % fncDir, "--data=%s" % tmpDir] ["--testmode", "--invalid", "--config=%s" % tmpDir, "--data=%s" % tmpDir]
) )
nwGUI.closeMain() assert nwGUI.closeMain() == "closeMain"
nwGUI.close()
assert ex.value.code == 2 assert ex.value.code == 2
# Simulate import error # Project Path
nwGUI = nw.main(
["--testmode", "--config=%s" % tmpDir, "--data=%s" % tmpDir, "sample/"]
)
assert nw.CONFIG.cmdOpen == "sample/"
assert nwGUI.closeMain() == "closeMain"
monkeypatch.undo()
# END Test testBaseInit_Options
@pytest.mark.base
def testBaseInit_Imports(caplog, monkeypatch, tmpDir):
"""Check import error handling.
"""
monkeypatch.setattr("nw.guimain.GuiMain", DummyMain)
monkeypatch.setattr("PyQt5.QtWidgets.QApplication.__init__", lambda *args: None)
monkeypatch.setattr("PyQt5.QtWidgets.QApplication.exec_", lambda *args: 0)
monkeypatch.setattr("PyQt5.QtWidgets.QErrorMessage.__init__", lambda *args: None)
monkeypatch.setattr("PyQt5.QtWidgets.QErrorMessage.resize", lambda *args: None)
monkeypatch.setattr("PyQt5.QtWidgets.QErrorMessage.showMessage", lambda *args: None)
monkeypatch.setitem(sys.modules, "lxml", None) monkeypatch.setitem(sys.modules, "lxml", None)
monkeypatch.setattr("sys.hexversion", 0x0) monkeypatch.setattr("sys.hexversion", 0x0)
monkeypatch.setattr("nw.CONFIG.verQtValue", 50000) monkeypatch.setattr("nw.CONFIG.verQtValue", 50000)
monkeypatch.setattr("nw.CONFIG.verPyQtValue", 50000) monkeypatch.setattr("nw.CONFIG.verPyQtValue", 50000)
with pytest.raises(SystemExit) as ex: with pytest.raises(SystemExit) as ex:
nwGUI = nw.main( _ = nw.main(
["--testmode", "--config=%s" % fncDir, "--data=%s" % tmpDir] ["--testmode", "--config=%s" % tmpDir, "--data=%s" % tmpDir]
) )
nwGUI.closeMain()
nwGUI.close()
assert ex.value.code & 4 == 4 # Python version not satisfied assert ex.value.code & 4 == 4 # Python version not satisfied
assert ex.value.code & 8 == 8 # Qt version not satisfied assert ex.value.code & 8 == 8 # Qt version not satisfied
assert ex.value.code & 16 == 16 # PyQt version not satisfied assert ex.value.code & 16 == 16 # PyQt version not satisfied
assert ex.value.code & 32 == 32 # lxml package missing assert ex.value.code & 32 == 32 # lxml package missing
assert "At least Python" in caplog.messages[0]
assert "At least Qt5" in caplog.messages[1]
assert "At least PyQt5" in caplog.messages[2]
assert "lxml" in caplog.messages[3]
monkeypatch.undo() monkeypatch.undo()
# END Test testBaseInit_Launch # END Test testBaseInit_Imports