From 63c3182a0e8acc95aaf49316f2664f014abcb2f9 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" Date: Thu, 16 May 2019 20:27:56 +0200 Subject: [PATCH] More tests for class Config --- nw/config.py | 23 +++++++++++++++-------- tests/test_config.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/nw/config.py b/nw/config.py index 254c06df..5805396b 100644 --- a/nw/config.py +++ b/nw/config.py @@ -117,7 +117,11 @@ class Config: logger.debug("Loading config file") confParser = configparser.ConfigParser() - confParser.read_file(open(path.join(self.confPath,self.confFile))) + try: + confParser.read_file(open(path.join(self.confPath,self.confFile))) + except Exception as e: + logger.error("Could not load config file") + return False # Get options @@ -182,7 +186,7 @@ class Config: if confParser.has_option(cnfSec,"recent%d" % i): self.recentList[i] = confParser.get(cnfSec,"recent%d" % i) - return + return True def saveConfig(self): @@ -266,13 +270,14 @@ class Config: return def setConfPath(self, newPath): - if newPath is None: return + if newPath is None: + return True if not path.isfile(newPath): logger.error("Config: File not found. Using default config path instead.") - return + return False self.confPath = path.dirname(newPath) self.confFile = path.basename(newPath) - return + return True def setWinSize(self, newWidth, newHeight): if abs(self.winGeometry[self.WIN_WIDTH] - newWidth) >= 10: @@ -281,14 +286,16 @@ class Config: if abs(self.winGeometry[self.WIN_HEIGHT] - newHeight) >= 10: self.winGeometry[self.WIN_HEIGHT] = newHeight self.confChanged = True - return + return True def setTreeColWidths(self, colWidths): self.treeColWidth = colWidths - return + self.confChanged = True + return True def setMainPanePos(self, panePos): self.mainPanePos = panePos - return + self.confChanged = True + return True # End Class Config diff --git a/tests/test_config.py b/tests/test_config.py index 90c68d8b..555ccf97 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -23,7 +23,47 @@ if path.isfile(tmpConf): def testConfigInit(): assert theConf.initConfig(testTemp) assert cmpFiles(tmpConf, refConf, [2]) + assert not theConf.confChanged def testConfigSave(): assert theConf.saveConfig() assert cmpFiles(tmpConf, refConf, [2]) + assert not theConf.confChanged + +def testConfigSetConfPath(): + assert theConf.setConfPath(None) + assert not theConf.setConfPath(path.join("somewhere","over","the","rainbow")) + assert theConf.setConfPath(path.join(testTemp,"novelwriter.conf")) + assert theConf.confPath == testTemp + assert theConf.confFile == "novelwriter.conf" + assert not theConf.confChanged + +def testConfigLoad(): + assert theConf.loadConfig() + assert not theConf.confChanged + +def testConfigSetWinSize(): + assert theConf.setWinSize(1105, 655) + assert not theConf.confChanged + assert theConf.setWinSize(70,70) + assert theConf.confChanged + assert theConf.setWinSize(1100, 650) + assert theConf.saveConfig() + assert cmpFiles(tmpConf, refConf, [2]) + assert not theConf.confChanged + +def testConfigSetTreeColWidths(): + assert theConf.setTreeColWidths([0, 0, 0]) + assert theConf.confChanged + assert theConf.setTreeColWidths([120, 30, 50]) + assert theConf.saveConfig() + assert cmpFiles(tmpConf, refConf, [2]) + assert not theConf.confChanged + +def testConfigSetMainPanePos(): + assert theConf.setMainPanePos([0, 0]) + assert theConf.confChanged + assert theConf.setMainPanePos([300, 800]) + assert theConf.saveConfig() + assert cmpFiles(tmpConf, refConf, [2]) + assert not theConf.confChanged