Fix crash in dictionaries install tool (#1876)

This commit is contained in:
Veronica Berglyd Olsen
2024-05-19 21:52:54 +02:00
committed by GitHub
2 changed files with 19 additions and 16 deletions
+5 -4
View File
@@ -36,7 +36,7 @@ from PyQt5.QtWidgets import (
) )
from novelwriter import CONFIG, SHARED from novelwriter import CONFIG, SHARED
from novelwriter.common import formatFileFilter, openExternalPath, formatInt, getFileSize from novelwriter.common import formatFileFilter, formatInt, getFileSize, openExternalPath
from novelwriter.error import formatException from novelwriter.error import formatException
from novelwriter.extensions.modified import NIconToolButton from novelwriter.extensions.modified import NIconToolButton
from novelwriter.types import QtDialogClose from novelwriter.types import QtDialogClose
@@ -143,11 +143,12 @@ class GuiDictionaries(QDialog):
try: try:
import enchant import enchant
path = Path(enchant.get_user_config_dir()) path = Path(enchant.get_user_config_dir())
self._installPath = Path(path).resolve()
self._installPath.mkdir(exist_ok=True, parents=True)
except Exception: except Exception:
logger.error("Could not get enchant path") logger.error("Could not get enchant path")
return False return False
self._installPath = Path(path).resolve()
if path.is_dir(): if path.is_dir():
self.inPath.setText(str(path)) self.inPath.setText(str(path))
hunspell = path / "hunspell" hunspell = path / "hunspell"
@@ -199,9 +200,9 @@ class GuiDictionaries(QDialog):
if self._installPath: if self._installPath:
temp = self.huInput.text() temp = self.huInput.text()
if temp and (path := Path(temp)).is_file(): if temp and (path := Path(temp)).is_file():
hunspell = self._installPath / "hunspell"
hunspell.mkdir(exist_ok=True)
try: try:
hunspell = self._installPath / "hunspell"
hunspell.mkdir(exist_ok=True)
nAff, nDic = self._extractDicts(path, hunspell) nAff, nDic = self._extractDicts(path, hunspell)
if nAff == 0 or nDic == 0: if nAff == 0 or nDic == 0:
self._appendLog(procErr, err=True) self._appendLog(procErr, err=True)
+14 -12
View File
@@ -20,12 +20,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
""" """
from __future__ import annotations from __future__ import annotations
import pytest
import enchant
from zipfile import ZipFile from zipfile import ZipFile
from mocked import causeException import enchant
import pytest
from PyQt5.QtGui import QDesktopServices from PyQt5.QtGui import QDesktopServices
from PyQt5.QtWidgets import QFileDialog from PyQt5.QtWidgets import QFileDialog
@@ -33,11 +31,15 @@ from PyQt5.QtWidgets import QFileDialog
from novelwriter import SHARED from novelwriter import SHARED
from novelwriter.tools.dictionaries import GuiDictionaries from novelwriter.tools.dictionaries import GuiDictionaries
from tests.mocked import causeException
@pytest.mark.gui @pytest.mark.gui
def testToolDictionaries_Main(qtbot, monkeypatch, nwGUI, fncPath): def testToolDictionaries_Main(qtbot, monkeypatch, nwGUI, fncPath):
"""Test the Dictionaries downloader tool.""" """Test the Dictionaries downloader tool."""
monkeypatch.setattr(enchant, "get_user_config_dir", lambda *a: str(fncPath)) # Must also create the enchant folder, see issue #1874
enchPath = fncPath / "enchant"
monkeypatch.setattr(enchant, "get_user_config_dir", lambda *a: str(enchPath))
# Fail to open # Fail to open
with monkeypatch.context() as mp: with monkeypatch.context() as mp:
@@ -52,7 +54,7 @@ def testToolDictionaries_Main(qtbot, monkeypatch, nwGUI, fncPath):
nwDicts = SHARED.findTopLevelWidget(GuiDictionaries) nwDicts = SHARED.findTopLevelWidget(GuiDictionaries)
assert isinstance(nwDicts, GuiDictionaries) assert isinstance(nwDicts, GuiDictionaries)
assert nwDicts.isVisible() assert nwDicts.isVisible()
assert nwDicts.inPath.text() == str(fncPath) assert nwDicts.inPath.text() == str(enchPath)
# Allow Open Dir # Allow Open Dir
SHARED._lastAlert = "" SHARED._lastAlert = ""
@@ -98,9 +100,9 @@ def testToolDictionaries_Main(qtbot, monkeypatch, nwGUI, fncPath):
nwDicts._doBrowseHunspell() nwDicts._doBrowseHunspell()
assert nwDicts.huInput.text() == str(foDict) assert nwDicts.huInput.text() == str(foDict)
nwDicts._doImportHunspell() nwDicts._doImportHunspell()
assert (fncPath / "hunspell").is_dir() assert (enchPath / "hunspell").is_dir()
assert (fncPath / "hunspell" / "en_GB.aff").is_file() assert (enchPath / "hunspell" / "en_GB.aff").is_file()
assert (fncPath / "hunspell" / "en_GB.dic").is_file() assert (enchPath / "hunspell" / "en_GB.dic").is_file()
assert nwDicts.infoBox.blockCount() == 3 assert nwDicts.infoBox.blockCount() == 3
# Import Libre Office Dictionary # Import Libre Office Dictionary
@@ -109,9 +111,9 @@ def testToolDictionaries_Main(qtbot, monkeypatch, nwGUI, fncPath):
nwDicts._doBrowseHunspell() nwDicts._doBrowseHunspell()
assert nwDicts.huInput.text() == str(loDict) assert nwDicts.huInput.text() == str(loDict)
nwDicts._doImportHunspell() nwDicts._doImportHunspell()
assert (fncPath / "hunspell").is_dir() assert (enchPath / "hunspell").is_dir()
assert (fncPath / "hunspell" / "en_US.aff").is_file() assert (enchPath / "hunspell" / "en_US.aff").is_file()
assert (fncPath / "hunspell" / "en_US.dic").is_file() assert (enchPath / "hunspell" / "en_US.dic").is_file()
assert nwDicts.infoBox.blockCount() == 5 assert nwDicts.infoBox.blockCount() == 5
# Handle Unreadable File # Handle Unreadable File