Change assetPath to a Path object

This commit is contained in:
Veronica Berglyd Olsen
2022-11-09 17:44:20 +01:00
parent 6792c11a71
commit 9d3291aba6
13 changed files with 142 additions and 151 deletions
+4 -4
View File
@@ -159,14 +159,14 @@ def testBaseConfig_Init(monkeypatch, tmpDir, fncDir, outDir, refDir, filesDir):
tstConf.initConfig(confPath=tmpDir, dataPath=tmpDir)
assert tstConf._confPath == tmpDir
assert tstConf._dataPath == tmpDir
appRoot = tstConf.appRoot
appRoot = tstConf._appRoot
mp.setattr("os.path.isfile", lambda *a: True)
tstConf.initConfig(confPath=tmpDir, dataPath=tmpDir)
assert tstConf._confPath == tmpDir
assert tstConf._dataPath == tmpDir
assert tstConf.appRoot == os.path.dirname(appRoot)
assert tstConf.appPath == os.path.dirname(appRoot)
assert tstConf._appRoot == os.path.dirname(appRoot)
assert tstConf._appPath == os.path.dirname(appRoot)
assert tstConf.loadConfig() is True
assert tstConf.saveConfig() is True
@@ -199,7 +199,7 @@ def testBaseConfig_Init(monkeypatch, tmpDir, fncDir, outDir, refDir, filesDir):
i18nDir = os.path.join(fncDir, "i18n")
os.mkdir(i18nDir)
os.mkdir(os.path.join(i18nDir, "stuff"))
tstConf.nwLangPath = i18nDir
tstConf._nwLangPath = i18nDir
copyfile(os.path.join(filesDir, "nw_en_GB.qm"), os.path.join(i18nDir, "nw_en_GB.qm"))
writeFile(os.path.join(i18nDir, "nw_en_GB.ts"), "")
+7 -5
View File
@@ -372,7 +372,7 @@ def testCoreTools_NewCustomB(monkeypatch, fncDir, outDir, refDir, mockGUI, mockR
@pytest.mark.core
def testCoreTools_NewSample(fncDir, tmpConf, mockGUI, tmpDir):
def testCoreTools_NewSample(monkeypatch, fncPath, tmpConf, tmpPath, mockGUI):
"""Check that we can create a new project can be created from the
provided sample project via a zip file.
"""
@@ -380,7 +380,7 @@ def testCoreTools_NewSample(fncDir, tmpConf, mockGUI, tmpDir):
"projName": "Test Sample",
"projTitle": "Test Novel",
"projAuthors": "Jane Doe\nJohn Doh\n",
"projPath": fncDir,
"projPath": fncPath,
"popSample": True,
"popMinimal": False,
"popCustom": False,
@@ -392,9 +392,11 @@ def testCoreTools_NewSample(fncDir, tmpConf, mockGUI, tmpDir):
assert projBuild.buildProject({"popSample": True}) is False
# Force the lookup path for assets to our temp folder
srcSample = os.path.abspath(os.path.join(tmpConf.appRoot, "sample"))
dstSample = os.path.join(tmpDir, "sample.zip")
tmpConf.assetPath = tmpDir
srcSample = tmpConf._appRoot / "sample"
dstSample = tmpPath / "sample.zip"
monkeypatch.setattr(
"novelwriter.config.Config.getAssetPath", lambda *a: tmpPath / "sample.zip"
)
# Cannot extract when the zip does not exist
assert projBuild.buildProject(projData) is False
+9 -8
View File
@@ -21,6 +21,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
import pytest
from pathlib import Path
from tools import getGuiItem
from PyQt5.QtWidgets import QAction, QMessageBox
@@ -29,7 +31,7 @@ from novelwriter.dialogs.about import GuiAbout
@pytest.mark.gui
def testDlgAbout_NWDialog(qtbot, nwGUI):
def testDlgAbout_NWDialog(qtbot, monkeypatch, nwGUI):
"""Test the novelWriter about dialogs.
"""
# NW About
@@ -45,13 +47,12 @@ def testDlgAbout_NWDialog(qtbot, nwGUI):
assert msgAbout.pageNotes.document().characterCount() > 100
assert msgAbout.pageLicense.document().characterCount() > 100
msgAbout.mainConf.assetPath = "whatever"
msgAbout._fillNotesPage()
assert msgAbout.pageNotes.toPlainText() == "Error loading release notes text ..."
msgAbout._fillLicensePage()
assert msgAbout.pageLicense.toPlainText() == "Error loading licence text ..."
with monkeypatch.context() as mp:
mp.setattr("novelwriter.config.Config.getAssetPath", lambda *a: Path("whatever"))
msgAbout._fillNotesPage()
assert msgAbout.pageNotes.toPlainText() == "Error loading release notes text ..."
msgAbout._fillLicensePage()
assert msgAbout.pageLicense.toPlainText() == "Error loading licence text ..."
msgAbout.showReleaseNotes()
assert msgAbout.tabBox.currentWidget() == msgAbout.pageNotes
+22 -35
View File
@@ -19,10 +19,10 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import os
import shutil
import pytest
from pathlib import Path
from configparser import ConfigParser
from mock import causeOSError
@@ -38,7 +38,7 @@ from novelwriter.gui.theme import GuiIcons, GuiTheme
@pytest.mark.gui
def testGuiTheme_Main(qtbot, nwGUI, fncDir):
def testGuiTheme_Main(qtbot, nwGUI, fncPath):
"""Test the theme class init.
"""
mainTheme: GuiTheme = nwGUI.mainTheme
@@ -75,15 +75,15 @@ def testGuiTheme_Main(qtbot, nwGUI, fncDir):
# Scan for Themes
# ===============
assert mainTheme._listConf({}, "not_a_path") is False
assert mainTheme._listConf({}, Path("not_a_path")) is False
themeOne = os.path.join(fncDir, "themes", "themeone.conf")
themeTwo = os.path.join(fncDir, "themes", "themetwo.conf")
themeOne = fncPath / "themes" / "themeone.conf"
themeTwo = fncPath / "themes" / "themetwo.conf"
writeFile(themeOne, "# Stuff")
writeFile(themeTwo, "# Stuff")
result = {}
assert mainTheme._listConf(result, os.path.join(fncDir, "themes")) is True
assert mainTheme._listConf(result, fncPath / "themes") is True
assert result["themeone"] == themeOne
assert result["themetwo"] == themeTwo
@@ -123,7 +123,7 @@ def testGuiTheme_Main(qtbot, nwGUI, fncDir):
@pytest.mark.gui
def testGuiTheme_Theme(qtbot, monkeypatch, nwGUI, fncDir):
def testGuiTheme_Theme(qtbot, monkeypatch, nwGUI, fncPath):
"""Test the theme part of the class.
"""
mainTheme: GuiTheme = nwGUI.mainTheme
@@ -132,15 +132,8 @@ def testGuiTheme_Theme(qtbot, monkeypatch, nwGUI, fncDir):
# List Themes
# ===========
shutil.copy(
os.path.join(mainConf.assetPath, "themes", "default_dark.conf"),
os.path.join(fncDir, "themes")
)
shutil.copy(
os.path.join(mainConf.assetPath, "themes", "default.conf"),
os.path.join(fncDir, "themes")
)
writeFile(os.path.join(fncDir, "themes", "default.qss"), "/* Stuff */")
shutil.copy(mainConf.getAssetPath("themes") / "default_dark.conf", fncPath / "themes")
shutil.copy(mainConf.getAssetPath("themes") / "default.conf", fncPath / "themes")
# Block the reading of the files
with monkeypatch.context() as mp:
@@ -197,7 +190,7 @@ def testGuiTheme_Theme(qtbot, monkeypatch, nwGUI, fncDir):
@pytest.mark.gui
def testGuiTheme_Syntax(qtbot, monkeypatch, nwGUI, fncDir):
def testGuiTheme_Syntax(qtbot, monkeypatch, nwGUI, fncPath):
"""Test the syntax part of the class.
"""
mainTheme: GuiTheme = nwGUI.mainTheme
@@ -206,14 +199,8 @@ def testGuiTheme_Syntax(qtbot, monkeypatch, nwGUI, fncDir):
# List Themes
# ===========
shutil.copy(
os.path.join(mainConf.assetPath, "syntax", "default_dark.conf"),
os.path.join(fncDir, "syntax")
)
shutil.copy(
os.path.join(mainConf.assetPath, "syntax", "default_light.conf"),
os.path.join(fncDir, "syntax")
)
shutil.copy(mainConf.getAssetPath("syntax") / "default_dark.conf", fncPath / "syntax")
shutil.copy(mainConf.getAssetPath("syntax") / "default_light.conf", fncPath / "syntax")
# Block the reading of the files
with monkeypatch.context() as mp:
@@ -270,11 +257,10 @@ def testGuiTheme_Syntax(qtbot, monkeypatch, nwGUI, fncDir):
@pytest.mark.gui
def testGuiTheme_Icons(qtbot, caplog, monkeypatch, nwGUI, fncDir):
def testGuiTheme_Icons(qtbot, caplog, monkeypatch, nwGUI, fncPath):
"""Test the icon cache class.
"""
iconCache: GuiIcons = nwGUI.mainTheme.iconCache
mainConf: Config = nwGUI.mainConf
# Load Theme
# ==========
@@ -288,10 +274,11 @@ def testGuiTheme_Icons(qtbot, caplog, monkeypatch, nwGUI, fncDir):
assert iconCache.loadTheme("typicons_dark") is False
# Load a broken theme file
iconsDir = os.path.join(fncDir, "icons")
os.mkdir(iconsDir)
os.mkdir(os.path.join(iconsDir, "testicons"))
writeFile(os.path.join(iconsDir, "testicons", "icons.conf"), (
iconsDir = fncPath / "icons"
testIcons = iconsDir / "testicons"
iconsDir.mkdir()
testIcons.mkdir()
writeFile(testIcons / "icons.conf", (
"[Main]\n"
"name = Test Icons\n"
"\n"
@@ -300,15 +287,15 @@ def testGuiTheme_Icons(qtbot, caplog, monkeypatch, nwGUI, fncDir):
"stuff = stuff.svg\n"
))
assetPath = mainConf.assetPath
mainConf.assetPath = fncDir
iconPath = iconCache._iconPath
iconCache._iconPath = fncPath / "icons"
caplog.clear()
assert iconCache.loadTheme("testicons") is True
assert "Unknown icon name 'stuff' in config file" in caplog.text
assert "Icon file 'add.svg' not in theme folder" in caplog.text
mainConf.assetPath = assetPath
iconCache._iconPath = iconPath
# Load working theme file
assert iconCache.loadTheme("typicons_dark") is True
@@ -327,7 +314,7 @@ def testGuiTheme_Icons(qtbot, caplog, monkeypatch, nwGUI, fncDir):
# Fail finding the file
with monkeypatch.context() as mp:
mp.setattr("os.path.isfile", lambda *a: False)
mp.setattr("pathlib.Path.is_file", lambda *a: False)
qPix = iconCache.loadDecoration("wiz-back")
assert qPix.isNull() is True