コード例 #1
0
def testBaseConfig_Init(monkeypatch, tmpDir, fncDir, outDir, refDir, filesDir):
    """Test config intialisation.
    """
    tstConf = Config()

    confFile = os.path.join(tmpDir, "novelwriter.conf")
    testFile = os.path.join(outDir, "baseConfig_novelwriter.conf")
    compFile = os.path.join(refDir, "baseConfig_novelwriter.conf")

    # Make sure we don't have any old conf file
    if os.path.isfile(confFile):
        os.unlink(confFile)

    # Let the config class figure out the path
    with monkeypatch.context() as mp:
        mp.setattr("PyQt5.QtCore.QStandardPaths.writableLocation", lambda *args: fncDir)
        tstConf.verQtValue = 50600
        tstConf.initConfig()
        assert tstConf.confPath == os.path.join(fncDir, tstConf.appHandle)
        assert tstConf.dataPath == os.path.join(fncDir, tstConf.appHandle)
        assert not os.path.isfile(confFile)
        tstConf.verQtValue = 50000
        tstConf.initConfig()
        assert tstConf.confPath == os.path.join(fncDir, tstConf.appHandle)
        assert tstConf.dataPath == os.path.join(fncDir, tstConf.appHandle)
        assert not os.path.isfile(confFile)

    # Fail to make folders
    with monkeypatch.context() as mp:
        mp.setattr("os.mkdir", causeOSError)

        tstConfDir = os.path.join(fncDir, "test_conf")
        tstConf.initConfig(confPath=tstConfDir, dataPath=tmpDir)
        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.dataPath is None
        assert os.path.isfile(confFile)
        os.unlink(confFile)

    # Test load/save with no path
    tstConf.confPath = None
    assert not tstConf.loadConfig()
    assert not tstConf.saveConfig()

    # Run again and set the paths directly and correctly
    # This should create a config file as well
    with monkeypatch.context() as mp:
        mp.setattr("os.path.expanduser", lambda *args: "")
        tstConf.spellTool = nwConst.SP_INTERNAL
        tstConf.initConfig(confPath=tmpDir, dataPath=tmpDir)
        assert tstConf.confPath == tmpDir
        assert tstConf.dataPath == tmpDir
        assert os.path.isfile(confFile)

        copyfile(confFile, testFile)
        assert cmpFiles(testFile, compFile, [2, 9, 10])

    # Load and save with OSError
    with monkeypatch.context() as mp:
        mp.setattr("builtins.open", causeOSError)

        assert not tstConf.loadConfig()
        assert tstConf.hasError is True
        assert tstConf.errData != []
        assert tstConf.getErrData().startswith("Could not")
        assert tstConf.hasError is False
        assert tstConf.errData == []

        assert not tstConf.saveConfig()
        assert tstConf.hasError is True
        assert tstConf.errData != []
        assert tstConf.getErrData().startswith("Could not")
        assert tstConf.hasError is False
        assert tstConf.errData == []

    assert tstConf.loadConfig()
    assert tstConf.saveConfig()

    # Test Correcting Quote Settings
    origDbl = tstConf.fmtDoubleQuotes
    origSng = tstConf.fmtSingleQuotes
    orDoDbl = tstConf.doReplaceDQuote
    orDoSng = tstConf.doReplaceSQuote

    tstConf.fmtDoubleQuotes = ["\"", "\""]
    tstConf.fmtSingleQuotes = ["'", "'"]
    tstConf.doReplaceDQuote = True
    tstConf.doReplaceSQuote = True
    assert tstConf.saveConfig()

    assert tstConf.loadConfig()
    assert not tstConf.doReplaceDQuote
    assert not tstConf.doReplaceSQuote

    tstConf.fmtDoubleQuotes = origDbl
    tstConf.fmtSingleQuotes = origSng
    tstConf.doReplaceDQuote = orDoDbl
    tstConf.doReplaceSQuote = orDoSng
    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"))
    writeFile(os.path.join(i18nDir, "nw_en_GB.ts"), "")
    writeFile(os.path.join(i18nDir, "nw_abcd.qm"), "")

    tstApp = DummyApp()
    tstConf.initLocalisation(tstApp)
    theList = tstConf.listLanguages(tstConf.LANG_NW)
    assert theList == [("en_GB", "British English")]

    copyfile(confFile, testFile)
    assert cmpFiles(testFile, compFile, [2, 9, 10])
コード例 #2
0
def testBaseConfig_Init(monkeypatch, tmpDir, fncDir, outDir, refDir):
    """Test config intialisation.
    """
    tstConf = Config()

    confFile = os.path.join(tmpDir, "novelwriter.conf")
    testFile = os.path.join(outDir, "baseConfig_novelwriter.conf")
    compFile = os.path.join(refDir, "baseConfig_novelwriter.conf")

    # Make sure we don't have any old conf file
    if os.path.isfile(confFile):
        os.unlink(confFile)

    # Let the config class figure out the path
    monkeypatch.setattr("PyQt5.QtCore.QStandardPaths.writableLocation", lambda *args: fncDir)
    tstConf.verQtValue = 50600
    tstConf.initConfig()
    assert tstConf.confPath == os.path.join(fncDir, tstConf.appHandle)
    assert tstConf.dataPath == os.path.join(fncDir, tstConf.appHandle)
    assert not os.path.isfile(confFile)
    tstConf.verQtValue = 50000
    tstConf.initConfig()
    assert tstConf.confPath == os.path.join(fncDir, tstConf.appHandle)
    assert tstConf.dataPath == os.path.join(fncDir, tstConf.appHandle)
    assert not os.path.isfile(confFile)
    monkeypatch.undo()

    # Fail to make folders
    monkeypatch.setattr("os.mkdir", causeOSError)

    tstConfDir = os.path.join(fncDir, "test_conf")
    tstConf.initConfig(confPath=tstConfDir, dataPath=tmpDir)
    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.dataPath is None
    assert os.path.isfile(confFile)
    os.unlink(confFile)

    monkeypatch.undo()

    # Test load/save with no path
    tstConf.confPath = None
    assert not tstConf.loadConfig()
    assert not tstConf.saveConfig()

    # Run again and set the paths directly and correctly
    # This should create a config file as well
    monkeypatch.setattr("os.path.expanduser", lambda *args: "")
    tstConf.spellTool = nwConst.SP_INTERNAL
    tstConf.initConfig(confPath=tmpDir, dataPath=tmpDir)
    assert tstConf.confPath == tmpDir
    assert tstConf.dataPath == tmpDir
    assert os.path.isfile(confFile)

    copyfile(confFile, testFile)
    assert cmpFiles(testFile, compFile, [2, 9])
    monkeypatch.undo()

    # Load and save with OSError
    monkeypatch.setattr("builtins.open", causeOSError)

    assert not tstConf.loadConfig()
    assert tstConf.hasError is True
    assert tstConf.errData != []
    assert tstConf.getErrData().startswith("Could not")
    assert tstConf.hasError is False
    assert tstConf.errData == []

    assert not tstConf.saveConfig()
    assert tstConf.hasError is True
    assert tstConf.errData != []
    assert tstConf.getErrData().startswith("Could not")
    assert tstConf.hasError is False
    assert tstConf.errData == []

    monkeypatch.undo()

    assert tstConf.loadConfig()
    assert tstConf.saveConfig()

    copyfile(confFile, testFile)
    assert cmpFiles(testFile, compFile, [2, 9])