From 2998f2c3631fc7138a7297ae5302224e762674a3 Mon Sep 17 00:00:00 2001
From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com>
Date: Thu, 23 Nov 2023 16:45:14 +0100
Subject: [PATCH] Update tests
---
tests/test_base/test_base_common.py | 41 ++++++++++++++++++++---
tests/test_tools/test_tools_manusbuild.py | 15 +++++++++
2 files changed, 52 insertions(+), 4 deletions(-)
diff --git a/tests/test_base/test_base_common.py b/tests/test_base/test_base_common.py
index 8ac8f357..08e8b3e9 100644
--- a/tests/test_base/test_base_common.py
+++ b/tests/test_base/test_base_common.py
@@ -20,6 +20,8 @@ along with this program. If not, see .
"""
import time
+from PyQt5.QtCore import QUrl
+from PyQt5.QtGui import QDesktopServices
import pytest
from pathlib import Path
@@ -32,10 +34,10 @@ from novelwriter.guimain import GuiMain
from novelwriter.common import (
checkBool, checkFloat, checkHandle, checkInt, checkIntTuple, checkPath,
checkString, checkStringNone, checkUuid, formatInt, formatTime,
- formatTimeStamp, fuzzyTime, getGuiItem, hexToInt, isHandle, isItemClass,
- isItemLayout, isItemType, isTitleTag, jsonEncode, makeFileNameSafe, minmax,
- numberToRoman, NWConfigParser, readTextFile, simplified, transferCase,
- xmlIndent, yesNo
+ formatTimeStamp, fuzzyTime, getFileSize, getGuiItem, hexToInt, isHandle,
+ isItemClass, isItemLayout, isItemType, isTitleTag, jsonEncode,
+ makeFileNameSafe, minmax, numberToRoman, NWConfigParser, openExternalPath,
+ readTextFile, simplified, transferCase, xmlIndent, yesNo
)
@@ -621,6 +623,37 @@ def testBaseCommon_makeFileNameSafe():
# END Test testBaseCommon_makeFileNameSafe
+@pytest.mark.base
+def testBaseCommon_getFileSize(fncPath):
+ """Test the getFileSize function."""
+ (fncPath / "one.txt").write_bytes(b"foobar")
+ (fncPath / "two.txt").touch()
+
+ assert getFileSize(fncPath / "nope.txt") == -1
+ assert getFileSize(fncPath / "one.txt") == 6
+ assert getFileSize(fncPath / "two.txt") == 0
+
+# END Test testBaseCommon_getFileSize
+
+
+@pytest.mark.base
+def testBaseCommon_openExternalPath(monkeypatch, tstPaths):
+ """Test the openExternalPath function."""
+ lastUrl = ""
+
+ def mockOpenUrl(url: QUrl) -> None:
+ nonlocal lastUrl
+ lastUrl = url.toString()
+ return
+
+ monkeypatch.setattr(QDesktopServices, "openUrl", mockOpenUrl)
+ assert openExternalPath(Path("/foo/bar")) is False
+ assert openExternalPath(tstPaths.tmpDir) is True
+ assert lastUrl.startswith("file://")
+
+# END Test testBaseCommon_openExternalPath
+
+
@pytest.mark.base
def testBaseCommon_getGuiItem(nwGUI):
"""Check the GUI item function."""
diff --git a/tests/test_tools/test_tools_manusbuild.py b/tests/test_tools/test_tools_manusbuild.py
index 271f9b0f..a23f383e 100644
--- a/tests/test_tools/test_tools_manusbuild.py
+++ b/tests/test_tools/test_tools_manusbuild.py
@@ -19,6 +19,8 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see .
"""
+from PyQt5.QtCore import QUrl
+from PyQt5.QtGui import QDesktopServices
import pytest
from pathlib import Path
@@ -132,6 +134,19 @@ def testManuscriptBuild_Main(
mp.setattr(QMessageBox, "result", lambda *a: QMessageBox.No)
assert manus._runBuild() is False
+ # Test that the open button works
+ lastUrl = ""
+
+ def mockOpenUrl(url: QUrl) -> None:
+ nonlocal lastUrl
+ lastUrl = url.toString()
+ return
+
+ with monkeypatch.context() as mp:
+ mp.setattr(QDesktopServices, "openUrl", mockOpenUrl)
+ manus.btnOpen.click()
+ assert lastUrl.startswith("file://")
+
# Finish
manus._dialogButtonClicked(manus.dlgButtons.button(QDialogButtonBox.Close))
# qtbot.stop()