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
+5 -4
View File
@@ -28,6 +28,7 @@ from zipfile import ZipFile
from mock import causeOSError
from tools import C, buildTestProject, cmpFiles, XML_IGNORE
from novelwriter import CONFIG
from novelwriter.constants import nwItemClass
from novelwriter.core.project import NWProject
from novelwriter.core.coretools import DocMerger, DocSplitter, ProjectBuilder
@@ -371,7 +372,7 @@ def testCoreTools_NewCustomB(monkeypatch, fncPath, tstPaths, mockGUI, mockRnd):
@pytest.mark.core
def testCoreTools_NewSample(monkeypatch, fncPath, tmpConf, tmpPath, mockGUI):
def testCoreTools_NewSample(monkeypatch, fncPath, tstPaths, mockGUI):
"""Check that we can create a new project can be created from the
provided sample project via a zip file.
"""
@@ -391,10 +392,10 @@ def testCoreTools_NewSample(monkeypatch, fncPath, tmpConf, tmpPath, mockGUI):
assert projBuild.buildProject({"popSample": True}) is False
# Force the lookup path for assets to our temp folder
srcSample = tmpConf._appRoot / "sample"
dstSample = tmpPath / "sample.zip"
srcSample = CONFIG._appRoot / "sample"
dstSample = tstPaths.tmpDir / "sample.zip"
monkeypatch.setattr(
"novelwriter.config.Config.assetPath", lambda *a: tmpPath / "sample.zip"
"novelwriter.config.Config.assetPath", lambda *a: tstPaths.tmpDir / "sample.zip"
)
# Cannot extract when the zip does not exist
+7 -12
View File
@@ -704,7 +704,7 @@ def testCoreProject_OrphanedFiles(mockGUI, prjLipsum):
@pytest.mark.core
def testCoreProject_Backup(monkeypatch, mockGUI, fncPath, tmpPath):
def testCoreProject_Backup(monkeypatch, mockGUI, fncPath, tstPaths):
"""Test the automated backup feature of the project class. The test
creates a backup of the Minimal test project, and then unzips the
backupd file and checks that the project XML file is identical to
@@ -720,23 +720,18 @@ def testCoreProject_Backup(monkeypatch, mockGUI, fncPath, tmpPath):
# Invalid Settings
# ================
# No project
mockGUI.hasProject = False
assert theProject.backupProject(doNotify=False) is False
mockGUI.hasProject = True
# Invalid path
theProject.mainConf._backupPath = None
assert theProject.backupProject(doNotify=False) is False
# Missing project name
theProject.mainConf._backupPath = tmpPath
theProject.mainConf._backupPath = tstPaths.tmpDir
theProject.data.setName("")
assert theProject.backupProject(doNotify=False) is False
# Valid Settings
# ==============
theProject.mainConf._backupPath = tmpPath
theProject.mainConf._backupPath = tstPaths.tmpDir
theProject.data.setName("Test Minimal")
# Can't make folder
@@ -752,7 +747,7 @@ def testCoreProject_Backup(monkeypatch, mockGUI, fncPath, tmpPath):
# Test correct settings
assert theProject.backupProject(doNotify=True) is True
theFiles = list((tmpPath / "Test Minimal").iterdir())
theFiles = list((tstPaths.tmpDir / "Test Minimal").iterdir())
assert len(theFiles) == 1
theZip = theFiles[0].name
@@ -760,13 +755,13 @@ def testCoreProject_Backup(monkeypatch, mockGUI, fncPath, tmpPath):
assert theZip[-4:] == ".zip"
# Extract the archive
with ZipFile(tmpPath / "Test Minimal" / theZip, mode="r") as inZip:
inZip.extractall(tmpPath / "extract")
with ZipFile(tstPaths.tmpDir / "Test Minimal" / theZip, mode="r") as inZip:
inZip.extractall(tstPaths.tmpDir / "extract")
# Check that the main project file was restored
assert cmpFiles(
fncPath / "nwProject.nwx",
tmpPath / "extract" / "nwProject.nwx"
tstPaths.tmpDir / "extract" / "nwProject.nwx"
)
# END Test testCoreProject_Backup
+2 -2
View File
@@ -299,10 +299,10 @@ def testCoreStorage_PrepareStorage(monkeypatch, fncPath):
@pytest.mark.core
def testCoreStorage_ZipIt(monkeypatch, mockGUI, fncPath, tmpPath, mockRnd):
def testCoreStorage_ZipIt(monkeypatch, mockGUI, fncPath, tstPaths, mockRnd):
"""Test making a zip archive of a project.
"""
zipFile = tmpPath / "project.zip"
zipFile = tstPaths.tmpDir / "project.zip"
theProject = NWProject(mockGUI)
storage = theProject.storage
+5 -5
View File
@@ -437,7 +437,7 @@ def testCoreTree_Reorder(caplog, mockGUI, mockItems):
@pytest.mark.core
def testCoreTree_ToCFile(monkeypatch, mockGUI, mockItems, tmpPath):
def testCoreTree_ToCFile(monkeypatch, tstPaths, mockGUI, mockItems):
"""Test writing the ToC.txt file.
"""
theProject = NWProject(mockGUI)
@@ -463,20 +463,20 @@ def testCoreTree_ToCFile(monkeypatch, mockGUI, mockItems, tmpPath):
theProject._storage._runtimePath = None
assert theTree.writeToCFile() is False
theProject._storage._runtimePath = tmpPath
theProject._storage._runtimePath = tstPaths.tmpDir
with monkeypatch.context() as mp:
mp.setattr("builtins.open", causeOSError)
assert theTree.writeToCFile() is False
theProject._storage._runtimePath = tmpPath
(tmpPath / "content").mkdir()
theProject._storage._runtimePath = tstPaths.tmpDir
(tstPaths.tmpDir / "content").mkdir()
assert theTree.writeToCFile() is True
pathA = str(Path("content") / "c000000000001.nwd")
pathB = str(Path("content") / "c000000000002.nwd")
pathC = str(Path("content") / "b000000000002.nwd")
assert readFile(tmpPath / nwFiles.TOC_TXT) == (
assert readFile(tstPaths.tmpDir / nwFiles.TOC_TXT) == (
"\n"
"Table of Contents\n"
"=================\n"