Config class improvements (#826)
* Subclass ConfigParser * Remove redundant variables and update tests * Update file description * Move NWConfigParser class to common.py * Improve coverage of config class * Use NWConfigParser also in themes.py * Update config class file header * Fix comment * Update the usage of ConfigParser to recommended practice and extend tests * Improve logging for info level a bit
This commit is contained in:
committed by
GitHub
parent
e92506dc07
commit
e5c715695e
@@ -19,10 +19,9 @@ 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 pytest
|
||||
import sys
|
||||
import os
|
||||
import configparser
|
||||
import sys
|
||||
import pytest
|
||||
|
||||
from shutil import copyfile
|
||||
|
||||
@@ -161,6 +160,20 @@ def testBaseConfig_Init(monkeypatch, tmpDir, fncDir, outDir, refDir, filesDir):
|
||||
assert tstConf.hasError is False
|
||||
assert tstConf.errData == []
|
||||
|
||||
# Check handling of novelWriter as a package
|
||||
with monkeypatch.context() as mp:
|
||||
tstConf.initConfig(confPath=tmpDir, dataPath=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.dataPath == tmpDir
|
||||
assert tstConf.appRoot == os.path.dirname(appRoot)
|
||||
assert tstConf.appPath == os.path.dirname(appRoot)
|
||||
|
||||
assert tstConf.loadConfig()
|
||||
assert tstConf.saveConfig()
|
||||
|
||||
@@ -187,19 +200,34 @@ def testBaseConfig_Init(monkeypatch, tmpDir, fncDir, outDir, refDir, filesDir):
|
||||
assert tstConf.saveConfig()
|
||||
|
||||
# Localisation
|
||||
# ============
|
||||
|
||||
i18nDir = os.path.join(fncDir, "i18n")
|
||||
os.mkdir(i18nDir)
|
||||
os.mkdir(os.path.join(i18nDir, "stuff"))
|
||||
tstConf.nwLangPath = i18nDir
|
||||
|
||||
copyfile(os.path.join(filesDir, "nw_en_GB.qm"), os.path.join(fncDir, "nw_en_GB.qm"))
|
||||
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"), "")
|
||||
writeFile(os.path.join(i18nDir, "nw_abcd.qm"), "")
|
||||
|
||||
tstApp = MockApp()
|
||||
tstConf.initLocalisation(tstApp)
|
||||
|
||||
# Check Lists
|
||||
theList = tstConf.listLanguages(tstConf.LANG_NW)
|
||||
assert theList == [("en_GB", "British English")]
|
||||
theList = tstConf.listLanguages(tstConf.LANG_PROJ)
|
||||
assert theList == [("en", "English")]
|
||||
theList = tstConf.listLanguages(None)
|
||||
assert theList == []
|
||||
|
||||
# Add Language
|
||||
copyfile(os.path.join(filesDir, "nw_en_GB.qm"), os.path.join(i18nDir, "nw_fr.qm"))
|
||||
writeFile(os.path.join(i18nDir, "nw_fr.ts"), "")
|
||||
|
||||
theList = tstConf.listLanguages(tstConf.LANG_NW)
|
||||
assert theList == [("en_GB", "British English"), ("fr", "Français")]
|
||||
|
||||
copyfile(confFile, testFile)
|
||||
assert cmpFiles(testFile, compFile, [2, 9, 10])
|
||||
@@ -311,6 +339,7 @@ def testBaseConfig_SettersGetters(tmpConf, tmpDir, outDir, refDir):
|
||||
|
||||
# GUI Scaling
|
||||
# ===========
|
||||
|
||||
tmpConf.guiScale = 1.0
|
||||
assert tmpConf.pxInt(10) == 10
|
||||
assert tmpConf.pxInt(13) == 13
|
||||
@@ -343,6 +372,19 @@ def testBaseConfig_SettersGetters(tmpConf, tmpDir, outDir, refDir):
|
||||
|
||||
assert tmpConf.setWinSize(1200, 650)
|
||||
|
||||
# Preferences Size
|
||||
tmpConf.guiScale = 2.0
|
||||
assert tmpConf.setPreferencesSize(70, 70)
|
||||
assert tmpConf.getPreferencesSize() == [70, 70]
|
||||
assert tmpConf.prefGeometry == [35, 35]
|
||||
|
||||
tmpConf.guiScale = 1.0
|
||||
assert tmpConf.setPreferencesSize(70, 70)
|
||||
assert tmpConf.getPreferencesSize() == [70, 70]
|
||||
assert tmpConf.prefGeometry == [70, 70]
|
||||
|
||||
assert tmpConf.setPreferencesSize(700, 615)
|
||||
|
||||
# Project Tree Columns
|
||||
tmpConf.guiScale = 2.0
|
||||
assert tmpConf.setTreeColWidths([10, 20, 25])
|
||||
@@ -436,6 +478,7 @@ def testBaseConfig_SettersGetters(tmpConf, tmpDir, outDir, refDir):
|
||||
|
||||
# Getters Only
|
||||
# ============
|
||||
|
||||
tmpConf.guiScale = 1.0
|
||||
assert tmpConf.getTextWidth() == 600
|
||||
assert tmpConf.getTextMargin() == 40
|
||||
@@ -450,6 +493,7 @@ def testBaseConfig_SettersGetters(tmpConf, tmpDir, outDir, refDir):
|
||||
|
||||
# Flag Setters
|
||||
# ============
|
||||
|
||||
assert not tmpConf.setShowRefPanel(False)
|
||||
assert not tmpConf.showRefPanel
|
||||
assert tmpConf.setShowRefPanel(True)
|
||||
@@ -482,74 +526,16 @@ def testBaseConfig_Internal(monkeypatch, tmpConf):
|
||||
# Function _packList
|
||||
assert tmpConf._packList(["A", 1, 2.0, None, False]) == "A, 1, 2.0, None, False"
|
||||
|
||||
# Function _unpackList
|
||||
assert tmpConf._unpackList("1, 2, 3", [0, 0, 0], tmpConf.CNF_I_LST) == [1, 2, 3]
|
||||
assert tmpConf._unpackList("1, 2 ", [0, 0, 0], tmpConf.CNF_I_LST) == [1, 2, 0]
|
||||
assert tmpConf._unpackList("A, B, C", [0, 0, 0], tmpConf.CNF_I_LST) == [0, 0, 0]
|
||||
assert tmpConf._unpackList("1, 2, 3", ["X", "Y", "Z"], tmpConf.CNF_S_LST) == ["1", "2", "3"]
|
||||
assert tmpConf._unpackList("A, B ", ["X", "Y", "Z"], tmpConf.CNF_S_LST) == ["A", "B", "Z"]
|
||||
assert tmpConf._unpackList("A, B, C", ["X", "Y", "Z"], tmpConf.CNF_S_LST) == ["A", "B", "C"]
|
||||
assert tmpConf._unpackList("A, B, C", ["X", "Y", "Z"], tmpConf.CNF_STR) == ["X", "Y", "Z"]
|
||||
|
||||
# Function _parseLine
|
||||
cnfParse = configparser.ConfigParser()
|
||||
cnfParse.read_string(
|
||||
"[Main]\n"
|
||||
"val_string = stuff\n"
|
||||
"val_int = 123\n"
|
||||
"val_bool = True\n"
|
||||
"val_list_string = A, B, C\n"
|
||||
"val_list_int = 1, 2, 3\n"
|
||||
)
|
||||
|
||||
assert tmpConf._parseLine(
|
||||
cnfParse, "Main", "val_string", tmpConf.CNF_STR, "default"
|
||||
) == "stuff"
|
||||
assert tmpConf._parseLine(
|
||||
cnfParse, "Main", "nope", tmpConf.CNF_STR, "default"
|
||||
) == "default"
|
||||
|
||||
assert tmpConf._parseLine(
|
||||
cnfParse, "Main", "val_int", tmpConf.CNF_INT, "0"
|
||||
) == 123
|
||||
assert tmpConf._parseLine(
|
||||
cnfParse, "Main", "nope", tmpConf.CNF_INT, 0
|
||||
) == 0
|
||||
assert tmpConf._parseLine(
|
||||
cnfParse, "Main", "val_string", tmpConf.CNF_INT, 0
|
||||
) == 0
|
||||
|
||||
assert tmpConf._parseLine(
|
||||
cnfParse, "Main", "val_bool", tmpConf.CNF_BOOL, False
|
||||
) is True
|
||||
assert tmpConf._parseLine(
|
||||
cnfParse, "Main", "nope", tmpConf.CNF_BOOL, False
|
||||
) is False
|
||||
assert tmpConf._parseLine(
|
||||
cnfParse, "Main", "val_string", tmpConf.CNF_BOOL, False
|
||||
) is False
|
||||
|
||||
assert tmpConf._parseLine(
|
||||
cnfParse, "Main", "val_list_string", tmpConf.CNF_S_LST, ["W", "X", "Y", "Z"]
|
||||
) == ["A", "B", "C", "Z"]
|
||||
assert tmpConf._parseLine(
|
||||
cnfParse, "Main", "nope", tmpConf.CNF_S_LST, ["W", "X", "Y", "Z"]
|
||||
) == ["W", "X", "Y", "Z"]
|
||||
|
||||
assert tmpConf._parseLine(
|
||||
cnfParse, "Main", "val_list_int", tmpConf.CNF_I_LST, [6, 7, 8, 9]
|
||||
) == [1, 2, 3, 9]
|
||||
assert tmpConf._parseLine(
|
||||
cnfParse, "Main", "nope", tmpConf.CNF_S_LST, [6, 7, 8, 9]
|
||||
) == [6, 7, 8, 9]
|
||||
|
||||
# Function _checkNone
|
||||
assert tmpConf._checkNone(None) is None
|
||||
assert tmpConf._checkNone("None") is None
|
||||
assert tmpConf._checkNone("stuff") == "stuff"
|
||||
assert tmpConf._checkNone("none") is None
|
||||
assert tmpConf._checkNone("NONE") is None
|
||||
assert tmpConf._checkNone("NoNe") is None
|
||||
assert tmpConf._checkNone(123456) == 123456
|
||||
|
||||
# Function _checkOptionalPackages
|
||||
# (Assumes enchant package exists ans is importable)
|
||||
# (Assumes enchant package exists and is importable)
|
||||
tmpConf._checkOptionalPackages()
|
||||
assert tmpConf.hasEnchant is True
|
||||
|
||||
@@ -559,12 +545,12 @@ def testBaseConfig_Internal(monkeypatch, tmpConf):
|
||||
assert tmpConf.hasEnchant is False
|
||||
|
||||
with monkeypatch.context() as mp:
|
||||
mp.setattr("shutil.which", lambda *args: "stuff")
|
||||
mp.setattr("shutil.which", lambda *a: "stuff")
|
||||
tmpConf._checkOptionalPackages()
|
||||
assert tmpConf.hasAssistant is True
|
||||
|
||||
with monkeypatch.context() as mp:
|
||||
mp.setattr("shutil.which", lambda *args: None)
|
||||
mp.setattr("shutil.which", lambda *a: None)
|
||||
tmpConf._checkOptionalPackages()
|
||||
assert tmpConf.hasAssistant is False
|
||||
|
||||
|
||||
Reference in New Issue
Block a user