Remove code for mocking CONFIG object and instead reset it for each test

This commit is contained in:
Veronica Berglyd Olsen
2023-05-16 00:34:11 +02:00
parent 76daa3f463
commit e8c6e50e92
12 changed files with 268 additions and 284 deletions
+16 -16
View File
@@ -28,13 +28,13 @@ from mock import MockGuiMain
@pytest.mark.base
def testBaseInit_Launch(caplog, monkeypatch, tmpPath):
def testBaseInit_Launch(caplog, monkeypatch, fncPath):
"""Check launching the main GUI.
"""
monkeypatch.setattr("novelwriter.guimain.GuiMain", MockGuiMain)
# TestMode Launch
nwGUI = novelwriter.main(["--testmode", f"--config={tmpPath}", f"--data={tmpPath}"])
nwGUI = novelwriter.main(["--testmode", f"--config={fncPath}", f"--data={fncPath}"])
assert isinstance(nwGUI, MockGuiMain)
# Darwin Launch
@@ -43,7 +43,7 @@ def testBaseInit_Launch(caplog, monkeypatch, tmpPath):
novelwriter.CONFIG.osDarwin = True
with monkeypatch.context() as mp:
mp.setitem(sys.modules, "Foundation", None)
nwGUI = novelwriter.main(["--testmode", f"--config={tmpPath}", f"--data={tmpPath}"])
nwGUI = novelwriter.main(["--testmode", f"--config={fncPath}", f"--data={fncPath}"])
assert isinstance(nwGUI, MockGuiMain)
assert "Failed" in caplog.text
@@ -55,7 +55,7 @@ def testBaseInit_Launch(caplog, monkeypatch, tmpPath):
novelwriter.CONFIG.osWindows = True
with monkeypatch.context() as mp:
mp.setitem(sys.modules, "ctypes", None)
nwGUI = novelwriter.main(["--testmode", f"--config={tmpPath}", f"--data={tmpPath}"])
nwGUI = novelwriter.main(["--testmode", f"--config={fncPath}", f"--data={fncPath}"])
assert isinstance(nwGUI, MockGuiMain)
if not sys.platform.startswith("darwin"):
# For some reason, the test doesn't work on macOS
@@ -71,19 +71,19 @@ def testBaseInit_Launch(caplog, monkeypatch, tmpPath):
monkeypatch.setattr("PyQt5.QtWidgets.QApplication.setOrganizationDomain", lambda *a: None)
monkeypatch.setattr("PyQt5.QtWidgets.QApplication.exec_", lambda *a: 0)
with pytest.raises(SystemExit) as ex:
novelwriter.main([f"--config={tmpPath}", f"--data={tmpPath}"])
novelwriter.main([f"--config={fncPath}", f"--data={fncPath}"])
assert ex.value.code == 0
# END Test testBaseInit_Launch
@pytest.mark.base
def testBaseInit_Options(monkeypatch, tmpPath):
def testBaseInit_Options(monkeypatch, fncPath):
"""Test command line options for logging level.
"""
monkeypatch.setattr("novelwriter.guimain.GuiMain", MockGuiMain)
monkeypatch.setattr(sys, "argv", [
"novelWriter.py", "--testmode", f"--config={tmpPath}", f"--data={tmpPath}"
"novelWriter.py", "--testmode", f"--config={fncPath}", f"--data={fncPath}"
])
# Defaults w/None Args
@@ -93,20 +93,20 @@ def testBaseInit_Options(monkeypatch, tmpPath):
# Defaults
nwGUI = novelwriter.main(
["--testmode", f"--config={tmpPath}", f"--data={tmpPath}", "--style=Fusion"]
["--testmode", f"--config={fncPath}", f"--data={fncPath}", "--style=Fusion"]
)
assert novelwriter.logger.getEffectiveLevel() == logging.WARNING
assert nwGUI.closeMain() == "closeMain"
# Log Levels
nwGUI = novelwriter.main(
["--testmode", "--info", f"--config={tmpPath}", f"--data={tmpPath}"]
["--testmode", "--info", f"--config={fncPath}", f"--data={fncPath}"]
)
assert novelwriter.logger.getEffectiveLevel() == logging.INFO
assert nwGUI.closeMain() == "closeMain"
nwGUI = novelwriter.main(
["--testmode", "--debug", f"--config={tmpPath}", f"--data={tmpPath}"]
["--testmode", "--debug", f"--config={fncPath}", f"--data={fncPath}"]
)
assert novelwriter.logger.getEffectiveLevel() == logging.DEBUG
assert nwGUI.closeMain() == "closeMain"
@@ -114,14 +114,14 @@ def testBaseInit_Options(monkeypatch, tmpPath):
# Help and Version
with pytest.raises(SystemExit) as ex:
nwGUI = novelwriter.main(
["--testmode", "--help", f"--config={tmpPath}", f"--data={tmpPath}"]
["--testmode", "--help", f"--config={fncPath}", f"--data={fncPath}"]
)
assert nwGUI.closeMain() == "closeMain"
assert ex.value.code == 0
with pytest.raises(SystemExit) as ex:
nwGUI = novelwriter.main(
["--testmode", "--version", f"--config={tmpPath}", f"--data={tmpPath}"]
["--testmode", "--version", f"--config={fncPath}", f"--data={fncPath}"]
)
assert nwGUI.closeMain() == "closeMain"
assert ex.value.code == 0
@@ -129,14 +129,14 @@ def testBaseInit_Options(monkeypatch, tmpPath):
# Invalid options
with pytest.raises(SystemExit) as ex:
nwGUI = novelwriter.main(
["--testmode", "--invalid", f"--config={tmpPath}", f"--data={tmpPath}"]
["--testmode", "--invalid", f"--config={fncPath}", f"--data={fncPath}"]
)
assert nwGUI.closeMain() == "closeMain"
assert ex.value.code == 2
# Project Path
nwGUI = novelwriter.main(
["--testmode", f"--config={tmpPath}", f"--data={tmpPath}", "sample/"]
["--testmode", f"--config={fncPath}", f"--data={fncPath}", "sample/"]
)
assert nwGUI.closeMain() == "closeMain"
@@ -144,7 +144,7 @@ def testBaseInit_Options(monkeypatch, tmpPath):
@pytest.mark.base
def testBaseInit_Imports(caplog, monkeypatch, tmpPath):
def testBaseInit_Imports(caplog, monkeypatch, fncPath):
"""Check import error handling.
"""
monkeypatch.setattr("novelwriter.guimain.GuiMain", MockGuiMain)
@@ -160,7 +160,7 @@ def testBaseInit_Imports(caplog, monkeypatch, tmpPath):
with pytest.raises(SystemExit) as ex:
_ = novelwriter.main(
["--testmode", f"--config={tmpPath}", f"--data={tmpPath}"]
["--testmode", f"--config={fncPath}", f"--data={fncPath}"]
)
assert ex.value.code & 4 == 4 # Python version not satisfied