From 9c7663996c288dd297613e2339c8005b499aeaca Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Fri, 1 Sep 2023 17:34:34 +0200 Subject: [PATCH] Fix issues discovered when testing --- novelwriter/core/project.py | 2 +- novelwriter/core/tokenizer.py | 8 ++++---- novelwriter/tools/manuscript.py | 9 +++++---- tests/test_core/test_core_buildsettings.py | 16 +++++++++++++--- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/novelwriter/core/project.py b/novelwriter/core/project.py index fc59f77f..4cba0c82 100644 --- a/novelwriter/core/project.py +++ b/novelwriter/core/project.py @@ -600,7 +600,7 @@ class NWProject: self._langData = json.load(inFile) logger.debug("Loaded project language file: %s", langFile.name) except Exception: - logger.error("Failed to project language file") + logger.error("Failed to load project language file") logException() return False diff --git a/novelwriter/core/tokenizer.py b/novelwriter/core/tokenizer.py index 839f9f69..6fc281b3 100644 --- a/novelwriter/core/tokenizer.py +++ b/novelwriter/core/tokenizer.py @@ -36,7 +36,6 @@ from functools import partial from PyQt5.QtCore import QCoreApplication, QRegularExpression -from novelwriter import SHARED from novelwriter.enum import nwItemLayout from novelwriter.common import formatTimeStamp, numberToRoman, checkInt from novelwriter.constants import nwConst, nwHeadFmt, nwRegEx, nwUnicode @@ -148,7 +147,7 @@ class Tokenizer(ABC): self._linkHeaders = False # Add an anchor before headers # Instance Variables - self._hFormatter = HeadingFormatter() + self._hFormatter = HeadingFormatter(self._project) self._firstScene = False # Flag to indicate that the first scene of the chapter # This File @@ -772,7 +771,8 @@ class Tokenizer(ABC): class HeadingFormatter: - def __init__(self) -> None: + def __init__(self, project: NWProject) -> None: + self._project = project self._chCount = 0 self._scChCount = 0 self._scAbsCount = 0 @@ -801,7 +801,7 @@ class HeadingFormatter: hFormat = hFormat.replace(nwHeadFmt.SC_NUM, str(self._scChCount)) hFormat = hFormat.replace(nwHeadFmt.SC_ABS, str(self._scAbsCount)) if nwHeadFmt.CH_WORD in hFormat: - chWord = SHARED.project.localLookup(self._chCount) + chWord = self._project.localLookup(self._chCount) hFormat = hFormat.replace(nwHeadFmt.CH_WORD, chWord) if nwHeadFmt.CH_ROML in hFormat: chRom = numberToRoman(self._chCount, toLower=True) diff --git a/novelwriter/tools/manuscript.py b/novelwriter/tools/manuscript.py index 193312f1..b4f7cb0f 100644 --- a/novelwriter/tools/manuscript.py +++ b/novelwriter/tools/manuscript.py @@ -290,9 +290,10 @@ class GuiManuscript(QDialog): @pyqtSlot("QListWidgetItem*", "QListWidgetItem*") def _updateBuildDetails(self, current: QListWidgetItem, previous: QListWidgetItem) -> None: """Process change of build selection to update the details.""" - build = self._builds.getBuild(current.data(self.D_KEY)) - if build is not None: - self.buildDetails.updateInfo(build) + if isinstance(current, QListWidgetItem): + build = self._builds.getBuild(current.data(self.D_KEY)) + if build is not None: + self.buildDetails.updateInfo(build) return @pyqtSlot() @@ -586,7 +587,7 @@ class _DetailsWidget(QWidget): item.addChild(sub) # Headings - hFmt = HeadingFormatter() + hFmt = HeadingFormatter(SHARED.project) hFmt.incChapter() hFmt.incScene() hFmt.resetScene() diff --git a/tests/test_core/test_core_buildsettings.py b/tests/test_core/test_core_buildsettings.py index 505e9bef..ca75cc40 100644 --- a/tests/test_core/test_core_buildsettings.py +++ b/tests/test_core/test_core_buildsettings.py @@ -377,6 +377,8 @@ def testCoreBuildSettings_Collection(monkeypatch, mockGUI, fncPath: Path, mockRn builds = BuildCollection(project) assert len(builds) == 0 assert not buildsFile.exists() + assert builds.lastBuild == "" + assert builds.defaultBuild == "" # Create a default build buildOne = BuildSettings() @@ -412,7 +414,9 @@ def testCoreBuildSettings_Collection(monkeypatch, mockGUI, fncPath: Path, mockRn # Check the file content data = json.loads(buildsFile.read_text(encoding="utf-8")) - assert list(data["novelWriter.builds"].keys()) == [buildIDOne, buildIDTwo] + assert list(data["novelWriter.builds"].keys()) == [ + "lastBuild", "defaultBuild", buildIDOne, buildIDTwo + ] # Remove a build builds.removeBuild(buildIDOne) @@ -423,12 +427,16 @@ def testCoreBuildSettings_Collection(monkeypatch, mockGUI, fncPath: Path, mockRn # Check the file content data = json.loads(buildsFile.read_text(encoding="utf-8")) - assert list(data["novelWriter.builds"].keys()) == [buildIDTwo] + assert list(data["novelWriter.builds"].keys()) == [ + "lastBuild", "defaultBuild", buildIDTwo + ] builds.setBuild(buildOne) assert list(builds.builds()) == [ (buildIDTwo, "Build Two"), (buildIDOne, "Build One"), ] + builds.setLastBuild(buildIDOne) + builds.setDefaultBuild(buildIDTwo) # Check errors: No valid path with monkeypatch.context() as mp: @@ -447,7 +455,7 @@ def testCoreBuildSettings_Collection(monkeypatch, mockGUI, fncPath: Path, mockRn buildsFile.write_text("foobar") assert builds._loadCollection() is False - # Check errors: Valid jason file, but list instead of object + # Check errors: Valid json file, but list instead of object buildsFile.write_text("[]") assert builds._loadCollection() is False buildsFile.unlink() @@ -459,5 +467,7 @@ def testCoreBuildSettings_Collection(monkeypatch, mockGUI, fncPath: Path, mockRn (buildIDTwo, "Build Two"), (buildIDOne, "Build One"), ] + assert another.lastBuild == buildIDOne + assert another.defaultBuild == buildIDTwo # END Test testCoreBuildSettings_Collection