Skip to content

Commit

Permalink
Fix crash in dictionaries install tool (#1876)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkbo authored May 19, 2024
2 parents 09018d1 + 5b0a6f0 commit fddd5fd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
9 changes: 5 additions & 4 deletions novelwriter/tools/dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
)

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.extensions.modified import NIconToolButton
from novelwriter.types import QtDialogClose
Expand Down Expand Up @@ -143,11 +143,12 @@ def initDialog(self) -> bool:
try:
import enchant
path = Path(enchant.get_user_config_dir())
self._installPath = Path(path).resolve()
self._installPath.mkdir(exist_ok=True, parents=True)
except Exception:
logger.error("Could not get enchant path")
return False

self._installPath = Path(path).resolve()
if path.is_dir():
self.inPath.setText(str(path))
hunspell = path / "hunspell"
Expand Down Expand Up @@ -199,9 +200,9 @@ def _doImportHunspell(self) -> None:
if self._installPath:
temp = self.huInput.text()
if temp and (path := Path(temp)).is_file():
hunspell = self._installPath / "hunspell"
hunspell.mkdir(exist_ok=True)
try:
hunspell = self._installPath / "hunspell"
hunspell.mkdir(exist_ok=True)
nAff, nDic = self._extractDicts(path, hunspell)
if nAff == 0 or nDic == 0:
self._appendLog(procErr, err=True)
Expand Down
26 changes: 14 additions & 12 deletions tests/test_tools/test_tools_dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,26 @@
"""
from __future__ import annotations

import pytest
import enchant

from zipfile import ZipFile

from mocked import causeException
import enchant
import pytest

from PyQt5.QtGui import QDesktopServices
from PyQt5.QtWidgets import QFileDialog

from novelwriter import SHARED
from novelwriter.tools.dictionaries import GuiDictionaries

from tests.mocked import causeException


@pytest.mark.gui
def testToolDictionaries_Main(qtbot, monkeypatch, nwGUI, fncPath):
"""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
with monkeypatch.context() as mp:
Expand All @@ -52,7 +54,7 @@ def testToolDictionaries_Main(qtbot, monkeypatch, nwGUI, fncPath):
nwDicts = SHARED.findTopLevelWidget(GuiDictionaries)
assert isinstance(nwDicts, GuiDictionaries)
assert nwDicts.isVisible()
assert nwDicts.inPath.text() == str(fncPath)
assert nwDicts.inPath.text() == str(enchPath)

# Allow Open Dir
SHARED._lastAlert = ""
Expand Down Expand Up @@ -98,9 +100,9 @@ def testToolDictionaries_Main(qtbot, monkeypatch, nwGUI, fncPath):
nwDicts._doBrowseHunspell()
assert nwDicts.huInput.text() == str(foDict)
nwDicts._doImportHunspell()
assert (fncPath / "hunspell").is_dir()
assert (fncPath / "hunspell" / "en_GB.aff").is_file()
assert (fncPath / "hunspell" / "en_GB.dic").is_file()
assert (enchPath / "hunspell").is_dir()
assert (enchPath / "hunspell" / "en_GB.aff").is_file()
assert (enchPath / "hunspell" / "en_GB.dic").is_file()
assert nwDicts.infoBox.blockCount() == 3

# Import Libre Office Dictionary
Expand All @@ -109,9 +111,9 @@ def testToolDictionaries_Main(qtbot, monkeypatch, nwGUI, fncPath):
nwDicts._doBrowseHunspell()
assert nwDicts.huInput.text() == str(loDict)
nwDicts._doImportHunspell()
assert (fncPath / "hunspell").is_dir()
assert (fncPath / "hunspell" / "en_US.aff").is_file()
assert (fncPath / "hunspell" / "en_US.dic").is_file()
assert (enchPath / "hunspell").is_dir()
assert (enchPath / "hunspell" / "en_US.aff").is_file()
assert (enchPath / "hunspell" / "en_US.dic").is_file()
assert nwDicts.infoBox.blockCount() == 5

# Handle Unreadable File
Expand Down

0 comments on commit fddd5fd

Please sign in to comment.