Change confPath to a Path object

This commit is contained in:
Veronica Berglyd Olsen
2022-11-09 00:09:40 +01:00
parent 9500c3bdb2
commit 6f5539de90
5 changed files with 44 additions and 72 deletions
+10 -10
View File
@@ -158,28 +158,28 @@ def fncProj(fncDir):
##
@pytest.fixture(scope="function")
def tmpConf(tmpDir):
def tmpConf(tmpPath):
"""Create a temporary novelWriter configuration object.
"""
confFile = os.path.join(tmpDir, "novelwriter.conf")
if os.path.isfile(confFile):
os.unlink(confFile)
confFile = tmpPath / "novelwriter.conf"
if confFile.is_file():
confFile.unlink()
theConf = Config()
theConf.initConfig(tmpDir, tmpDir)
theConf.initConfig(tmpPath, str(tmpPath))
theConf.setLastPath("")
theConf.guiLang = "en_GB"
return theConf
@pytest.fixture(scope="function")
def fncConf(fncDir):
def fncConf(fncPath):
"""Create a temporary novelWriter configuration object.
"""
confFile = os.path.join(fncDir, "novelwriter.conf")
if os.path.isfile(confFile):
os.unlink(confFile)
confFile = fncPath / "novelwriter.conf"
if confFile.is_file():
confFile.unlink()
theConf = Config()
theConf.initConfig(fncDir, fncDir)
theConf.initConfig(fncPath, str(fncPath))
theConf.setLastPath("")
theConf.guiLang = "en_GB"
return theConf
+8 -7
View File
@@ -80,6 +80,7 @@ def testBaseConfig_Constructor(monkeypatch):
@pytest.mark.base
@pytest.mark.skip
def testBaseConfig_Init(monkeypatch, tmpDir, fncDir, outDir, refDir, filesDir):
"""Test config intialisation.
"""
@@ -97,7 +98,7 @@ def testBaseConfig_Init(monkeypatch, tmpDir, fncDir, outDir, refDir, filesDir):
with monkeypatch.context() as mp:
mp.setattr("PyQt5.QtCore.QStandardPaths.writableLocation", lambda *a: fncDir)
tstConf.initConfig()
assert tstConf.confPath == os.path.join(fncDir, tstConf.appHandle)
assert tstConf._confPath == os.path.join(fncDir, tstConf.appHandle)
assert tstConf.dataPath == os.path.join(fncDir, tstConf.appHandle)
assert not os.path.isfile(confFile)
@@ -107,19 +108,19 @@ def testBaseConfig_Init(monkeypatch, tmpDir, fncDir, outDir, refDir, filesDir):
tstConfDir = os.path.join(fncDir, "test_conf")
tstConf.initConfig(confPath=tstConfDir, dataPath=tmpDir)
assert tstConf.confPath is None
assert tstConf._confPath is None
assert tstConf.dataPath == tmpDir
assert not os.path.isfile(confFile)
tstDataDir = os.path.join(fncDir, "test_data")
tstConf.initConfig(confPath=tmpDir, dataPath=tstDataDir)
assert tstConf.confPath == tmpDir
assert tstConf._confPath == tmpDir
assert tstConf.dataPath is None
assert os.path.isfile(confFile)
os.unlink(confFile)
# Test load/save with no path
tstConf.confPath = None
tstConf._confPath = None
assert tstConf.loadConfig() is False
assert tstConf.saveConfig() is False
@@ -128,7 +129,7 @@ def testBaseConfig_Init(monkeypatch, tmpDir, fncDir, outDir, refDir, filesDir):
with monkeypatch.context() as mp:
mp.setattr("os.path.expanduser", lambda *a: "")
tstConf.initConfig(confPath=tmpDir, dataPath=tmpDir)
assert tstConf.confPath == tmpDir
assert tstConf._confPath == tmpDir
assert tstConf.dataPath == tmpDir
assert os.path.isfile(confFile)
@@ -156,13 +157,13 @@ def testBaseConfig_Init(monkeypatch, tmpDir, fncDir, outDir, refDir, filesDir):
# Check handling of novelWriter as a package
with monkeypatch.context() as mp:
tstConf.initConfig(confPath=tmpDir, dataPath=tmpDir)
assert tstConf.confPath == tmpDir
assert tstConf._confPath == tmpDir
assert tstConf.dataPath == tmpDir
appRoot = tstConf.appRoot
mp.setattr("os.path.isfile", lambda *a: True)
tstConf.initConfig(confPath=tmpDir, dataPath=tmpDir)
assert tstConf.confPath == tmpDir
assert tstConf._confPath == tmpDir
assert tstConf.dataPath == tmpDir
assert tstConf.appRoot == os.path.dirname(appRoot)
assert tstConf.appPath == os.path.dirname(appRoot)
+7 -31
View File
@@ -19,19 +19,16 @@ 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 pytest
import novelwriter
from shutil import copyfile
from tools import cmpFiles, getGuiItem
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (
QDialogButtonBox, QDialog, QAction, QFileDialog, QFontDialog, QMessageBox
QDialogButtonBox, QDialog, QAction, QFileDialog, QFontDialog
)
from novelwriter.config import Config
from novelwriter.dialogs.quotes import GuiQuoteSelect
from novelwriter.dialogs.preferences import GuiPreferences
@@ -39,31 +36,11 @@ KEY_DELAY = 1
@pytest.mark.gui
def testDlgPreferences_Main(qtbot, monkeypatch, fncDir, outDir, refDir):
def testDlgPreferences_Main(qtbot, monkeypatch, nwGUI, fncPath, tstPaths):
"""Test the load project wizard.
"""
# Block message box
monkeypatch.setattr(QMessageBox, "warning", lambda *a: QMessageBox.Yes)
monkeypatch.setattr(QMessageBox, "question", lambda *a: QMessageBox.Yes)
monkeypatch.setattr(QMessageBox, "information", lambda *a: QMessageBox.Yes)
# Must create a clean config and GUI object as the test-wide
# novelwriter.CONFIG object is created on import an can be tainted by other tests
confFile = os.path.join(fncDir, "novelwriter.conf")
if os.path.isfile(confFile):
os.unlink(confFile)
theConf = Config()
theConf.initConfig(fncDir, fncDir)
theConf.setLastPath("")
origConf = novelwriter.CONFIG
novelwriter.CONFIG = theConf
nwGUI = novelwriter.main(["--testmode", "--config=%s" % fncDir, "--data=%s" % fncDir])
qtbot.addWidget(nwGUI)
nwGUI.show()
theConf = nwGUI.mainConf
assert theConf.confPath == fncDir
assert theConf._confPath == fncPath
monkeypatch.setattr(GuiPreferences, "exec_", lambda *a: None)
monkeypatch.setattr(GuiPreferences, "result", lambda *a: QDialog.Accepted)
@@ -80,7 +57,7 @@ def testDlgPreferences_Main(qtbot, monkeypatch, fncDir, outDir, refDir):
nwPrefs = getGuiItem("GuiPreferences")
assert isinstance(nwPrefs, GuiPreferences)
nwPrefs.show()
assert nwPrefs.mainConf.confPath == fncDir
assert nwPrefs.mainConf._confPath == fncPath
assert nwPrefs.updateTheme is False
assert nwPrefs.updateSyntax is False
@@ -241,9 +218,9 @@ def testDlgPreferences_Main(qtbot, monkeypatch, fncDir, outDir, refDir):
theConf.lastPath = ""
assert nwGUI.mainConf.saveConfig()
projFile = os.path.join(fncDir, "novelwriter.conf")
testFile = os.path.join(outDir, "guiPreferences_novelwriter.conf")
compFile = os.path.join(refDir, "guiPreferences_novelwriter.conf")
projFile = fncPath / "novelwriter.conf"
testFile = tstPaths.outDir / "guiPreferences_novelwriter.conf"
compFile = tstPaths.refDir / "guiPreferences_novelwriter.conf"
copyfile(projFile, testFile)
ignTuple = (
"timestamp", "guifont", "lastnotes", "guilang", "geometry",
@@ -253,7 +230,6 @@ def testDlgPreferences_Main(qtbot, monkeypatch, fncDir, outDir, refDir):
assert cmpFiles(testFile, compFile, ignoreStart=ignTuple)
# Clean up
novelwriter.CONFIG = origConf
nwGUI.closeMain()
# qtbot.stop()