Make project xml class and tests use pathlib instead of os
This commit is contained in:
@@ -24,7 +24,6 @@ You should have received a copy of the GNU General Public License
|
|||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
|
||||||
import logging
|
import logging
|
||||||
import novelwriter
|
import novelwriter
|
||||||
|
|
||||||
@@ -163,7 +162,7 @@ class ProjectXMLReader:
|
|||||||
self._state = XMLReadState.CANNOT_PARSE
|
self._state = XMLReadState.CANNOT_PARSE
|
||||||
|
|
||||||
backFile = self._path.with_suffix(".bak")
|
backFile = self._path.with_suffix(".bak")
|
||||||
if os.path.isfile(backFile):
|
if backFile.is_file():
|
||||||
try:
|
try:
|
||||||
xml = etree.parse(str(backFile))
|
xml = etree.parse(str(backFile))
|
||||||
self._state = XMLReadState.PARSED_BACKUP
|
self._state = XMLReadState.PARSED_BACKUP
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ import sys
|
|||||||
import pytest
|
import pytest
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from mock import MockGuiMain
|
from mock import MockGuiMain
|
||||||
from tools import cleanProject
|
from tools import cleanProject
|
||||||
|
|
||||||
@@ -62,6 +64,35 @@ def tmpDir():
|
|||||||
return theDir
|
return theDir
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="session")
|
||||||
|
def tstPaths(tmpDir):
|
||||||
|
"""Returns an object that can provide the various paths needed for
|
||||||
|
running tests.
|
||||||
|
"""
|
||||||
|
class _Store:
|
||||||
|
testDir = Path(__file__).parent
|
||||||
|
filesDir = testDir / "files"
|
||||||
|
refDir = testDir / "reference"
|
||||||
|
outDir = testDir / tmpDir / "results"
|
||||||
|
|
||||||
|
store = _Store()
|
||||||
|
store.outDir.mkdir(exist_ok=True)
|
||||||
|
|
||||||
|
return store
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="function")
|
||||||
|
def fncPath(tmpDir):
|
||||||
|
"""A temporary folder for a single test function.
|
||||||
|
"""
|
||||||
|
fncPath = Path(tmpDir) / "f_temp"
|
||||||
|
if fncPath.is_dir():
|
||||||
|
shutil.rmtree(fncPath)
|
||||||
|
if not fncPath.is_dir():
|
||||||
|
fncPath.mkdir()
|
||||||
|
return fncPath
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session")
|
@pytest.fixture(scope="session")
|
||||||
def refDir():
|
def refDir():
|
||||||
"""The folder where all the reference files are stored for verifying
|
"""The folder where all the reference files are stored for verifying
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ You should have received a copy of the GNU General Public License
|
|||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
|
||||||
import json
|
import json
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@@ -40,14 +39,14 @@ class MockProject:
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.core
|
@pytest.mark.core
|
||||||
def testCoreProjectXML_ReadCurrent(monkeypatch, filesDir, fncDir, outDir, refDir):
|
def testCoreProjectXML_ReadCurrent(monkeypatch, tstPaths, fncPath):
|
||||||
"""Test reading the current XML file format.
|
"""Test reading the current XML file format.
|
||||||
"""
|
"""
|
||||||
refFile = os.path.join(filesDir, "nwProject-1.4.nwx")
|
refFile = tstPaths.filesDir / "nwProject-1.4.nwx"
|
||||||
xmlFile = os.path.join(fncDir, "nwProject-1.4.nwx")
|
tstFile = tstPaths.outDir / "ProjectXML_ReadCurrent.nwx"
|
||||||
bakFile = os.path.join(fncDir, "nwProject-1.4.bak")
|
xmlFile = fncPath / "nwProject-1.4.nwx"
|
||||||
outFile = os.path.join(fncDir, "nwProject.nwx")
|
bakFile = fncPath / "nwProject-1.4.bak"
|
||||||
tstFile = os.path.join(outDir, "ProjectXML_ReadCurrent.nwx")
|
outFile = fncPath / "nwProject.nwx"
|
||||||
|
|
||||||
xmlReader = ProjectXMLReader(xmlFile)
|
xmlReader = ProjectXMLReader(xmlFile)
|
||||||
assert xmlReader.state == XMLReadState.NO_ACTION
|
assert xmlReader.state == XMLReadState.NO_ACTION
|
||||||
@@ -200,8 +199,8 @@ def testCoreProjectXML_ReadCurrent(monkeypatch, filesDir, fncDir, outDir, refDir
|
|||||||
assert data.itemImport.count("i56be10") == 1
|
assert data.itemImport.count("i56be10") == 1
|
||||||
|
|
||||||
# Compare content
|
# Compare content
|
||||||
dumpFile = os.path.join(outDir, "projectXML_ReadCurrent.json")
|
dumpFile = tstPaths.outDir / "projectXML_ReadCurrent.json"
|
||||||
compFile = os.path.join(refDir, "projectXML_ReadCurrent.json")
|
compFile = tstPaths.refDir / "projectXML_ReadCurrent.json"
|
||||||
with open(dumpFile, mode="w", encoding="utf-8") as dump:
|
with open(dumpFile, mode="w", encoding="utf-8") as dump:
|
||||||
json.dump(content, dump, indent=2)
|
json.dump(content, dump, indent=2)
|
||||||
assert cmpFiles(dumpFile, compFile)
|
assert cmpFiles(dumpFile, compFile)
|
||||||
@@ -216,7 +215,7 @@ def testCoreProjectXML_ReadCurrent(monkeypatch, filesDir, fncDir, outDir, refDir
|
|||||||
|
|
||||||
# Save the project again, which should produce an identical project xml
|
# Save the project again, which should produce an identical project xml
|
||||||
timeStamp = int(datetime.fromisoformat(xmlReader.timeStamp).timestamp())
|
timeStamp = int(datetime.fromisoformat(xmlReader.timeStamp).timestamp())
|
||||||
xmlWriter = ProjectXMLWriter(fncDir)
|
xmlWriter = ProjectXMLWriter(fncPath)
|
||||||
|
|
||||||
# Fail saving
|
# Fail saving
|
||||||
with monkeypatch.context() as mp:
|
with monkeypatch.context() as mp:
|
||||||
@@ -239,12 +238,12 @@ def testCoreProjectXML_ReadCurrent(monkeypatch, filesDir, fncDir, outDir, refDir
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.core
|
@pytest.mark.core
|
||||||
def testCoreProjectXML_ReadLegacy10(filesDir, fncDir, outDir, refDir, mockRnd):
|
def testCoreProjectXML_ReadLegacy10(tstPaths, fncPath, mockRnd):
|
||||||
"""Test reading the version 1.0 XML file format.
|
"""Test reading the version 1.0 XML file format.
|
||||||
"""
|
"""
|
||||||
refFile = os.path.join(filesDir, "nwProject-1.0.nwx")
|
refFile = tstPaths.filesDir / "nwProject-1.0.nwx"
|
||||||
xmlFile = os.path.join(fncDir, "nwProject-1.0.nwx")
|
xmlFile = fncPath / "nwProject-1.0.nwx"
|
||||||
outFile = os.path.join(fncDir, "nwProject.nwx")
|
outFile = fncPath / "nwProject.nwx"
|
||||||
copyfile(refFile, xmlFile)
|
copyfile(refFile, xmlFile)
|
||||||
|
|
||||||
xmlReader = ProjectXMLReader(xmlFile)
|
xmlReader = ProjectXMLReader(xmlFile)
|
||||||
@@ -326,8 +325,8 @@ def testCoreProjectXML_ReadLegacy10(filesDir, fncDir, outDir, refDir, mockRnd):
|
|||||||
assert data.itemImport.count("i00000a") == 0
|
assert data.itemImport.count("i00000a") == 0
|
||||||
|
|
||||||
# Compare content
|
# Compare content
|
||||||
dumpFile = os.path.join(outDir, "projectXML_ReadLegacy10.json")
|
dumpFile = tstPaths.outDir / "projectXML_ReadLegacy10.json"
|
||||||
compFile = os.path.join(refDir, "projectXML_ReadLegacy10.json")
|
compFile = tstPaths.refDir / "projectXML_ReadLegacy10.json"
|
||||||
with open(dumpFile, mode="w", encoding="utf-8") as dump:
|
with open(dumpFile, mode="w", encoding="utf-8") as dump:
|
||||||
json.dump(content, dump, indent=2)
|
json.dump(content, dump, indent=2)
|
||||||
assert cmpFiles(dumpFile, compFile)
|
assert cmpFiles(dumpFile, compFile)
|
||||||
@@ -369,10 +368,10 @@ def testCoreProjectXML_ReadLegacy10(filesDir, fncDir, outDir, refDir, mockRnd):
|
|||||||
|
|
||||||
# Save the project again, which should produce an identical project xml
|
# Save the project again, which should produce an identical project xml
|
||||||
timeStamp = int(datetime.fromisoformat(xmlReader.timeStamp).timestamp())
|
timeStamp = int(datetime.fromisoformat(xmlReader.timeStamp).timestamp())
|
||||||
xmlWriter = ProjectXMLWriter(fncDir)
|
xmlWriter = ProjectXMLWriter(fncPath)
|
||||||
assert xmlWriter.write(data, packedContent, timeStamp, 1000) is True
|
assert xmlWriter.write(data, packedContent, timeStamp, 1000) is True
|
||||||
testFile = os.path.join(outDir, "projectXML_ReadLegacy10.nwx")
|
testFile = tstPaths.outDir / "projectXML_ReadLegacy10.nwx"
|
||||||
compFile = os.path.join(refDir, "projectXML_ReadLegacy10.nwx")
|
compFile = tstPaths.refDir / "projectXML_ReadLegacy10.nwx"
|
||||||
copyfile(outFile, testFile)
|
copyfile(outFile, testFile)
|
||||||
assert cmpFiles(testFile, compFile)
|
assert cmpFiles(testFile, compFile)
|
||||||
|
|
||||||
@@ -380,12 +379,12 @@ def testCoreProjectXML_ReadLegacy10(filesDir, fncDir, outDir, refDir, mockRnd):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.core
|
@pytest.mark.core
|
||||||
def testCoreProjectXML_ReadLegacy11(filesDir, fncDir, outDir, refDir, mockRnd):
|
def testCoreProjectXML_ReadLegacy11(tstPaths, fncPath, mockRnd):
|
||||||
"""Test reading the version 1.1 XML file format.
|
"""Test reading the version 1.1 XML file format.
|
||||||
"""
|
"""
|
||||||
refFile = os.path.join(filesDir, "nwProject-1.1.nwx")
|
refFile = tstPaths.filesDir / "nwProject-1.1.nwx"
|
||||||
xmlFile = os.path.join(fncDir, "nwProject-1.1.nwx")
|
xmlFile = fncPath / "nwProject-1.1.nwx"
|
||||||
outFile = os.path.join(fncDir, "nwProject.nwx")
|
outFile = fncPath / "nwProject.nwx"
|
||||||
copyfile(refFile, xmlFile)
|
copyfile(refFile, xmlFile)
|
||||||
|
|
||||||
xmlReader = ProjectXMLReader(xmlFile)
|
xmlReader = ProjectXMLReader(xmlFile)
|
||||||
@@ -467,8 +466,8 @@ def testCoreProjectXML_ReadLegacy11(filesDir, fncDir, outDir, refDir, mockRnd):
|
|||||||
assert data.itemImport.count("i00000a") == 0
|
assert data.itemImport.count("i00000a") == 0
|
||||||
|
|
||||||
# Compare content
|
# Compare content
|
||||||
dumpFile = os.path.join(outDir, "projectXML_ReadLegacy11.json")
|
dumpFile = tstPaths.outDir / "projectXML_ReadLegacy11.json"
|
||||||
compFile = os.path.join(refDir, "projectXML_ReadLegacy11.json")
|
compFile = tstPaths.refDir / "projectXML_ReadLegacy11.json"
|
||||||
with open(dumpFile, mode="w", encoding="utf-8") as dump:
|
with open(dumpFile, mode="w", encoding="utf-8") as dump:
|
||||||
json.dump(content, dump, indent=2)
|
json.dump(content, dump, indent=2)
|
||||||
assert cmpFiles(dumpFile, compFile)
|
assert cmpFiles(dumpFile, compFile)
|
||||||
@@ -510,10 +509,10 @@ def testCoreProjectXML_ReadLegacy11(filesDir, fncDir, outDir, refDir, mockRnd):
|
|||||||
|
|
||||||
# Save the project again, which should produce an identical project xml
|
# Save the project again, which should produce an identical project xml
|
||||||
timeStamp = int(datetime.fromisoformat(xmlReader.timeStamp).timestamp())
|
timeStamp = int(datetime.fromisoformat(xmlReader.timeStamp).timestamp())
|
||||||
xmlWriter = ProjectXMLWriter(fncDir)
|
xmlWriter = ProjectXMLWriter(fncPath)
|
||||||
assert xmlWriter.write(data, packedContent, timeStamp, 1000) is True
|
assert xmlWriter.write(data, packedContent, timeStamp, 1000) is True
|
||||||
testFile = os.path.join(outDir, "projectXML_ReadLegacy11.nwx")
|
testFile = tstPaths.outDir / "projectXML_ReadLegacy11.nwx"
|
||||||
compFile = os.path.join(refDir, "projectXML_ReadLegacy11.nwx")
|
compFile = tstPaths.refDir / "projectXML_ReadLegacy11.nwx"
|
||||||
copyfile(outFile, testFile)
|
copyfile(outFile, testFile)
|
||||||
assert cmpFiles(testFile, compFile)
|
assert cmpFiles(testFile, compFile)
|
||||||
|
|
||||||
@@ -521,12 +520,12 @@ def testCoreProjectXML_ReadLegacy11(filesDir, fncDir, outDir, refDir, mockRnd):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.core
|
@pytest.mark.core
|
||||||
def testCoreProjectXML_ReadLegacy12(filesDir, fncDir, outDir, refDir, mockRnd):
|
def testCoreProjectXML_ReadLegacy12(tstPaths, fncPath, mockRnd):
|
||||||
"""Test reading the version 1.2 XML file format.
|
"""Test reading the version 1.2 XML file format.
|
||||||
"""
|
"""
|
||||||
refFile = os.path.join(filesDir, "nwProject-1.2.nwx")
|
refFile = tstPaths.filesDir / "nwProject-1.2.nwx"
|
||||||
xmlFile = os.path.join(fncDir, "nwProject-1.2.nwx")
|
xmlFile = fncPath / "nwProject-1.2.nwx"
|
||||||
outFile = os.path.join(fncDir, "nwProject.nwx")
|
outFile = fncPath / "nwProject.nwx"
|
||||||
copyfile(refFile, xmlFile)
|
copyfile(refFile, xmlFile)
|
||||||
|
|
||||||
xmlReader = ProjectXMLReader(xmlFile)
|
xmlReader = ProjectXMLReader(xmlFile)
|
||||||
@@ -608,8 +607,8 @@ def testCoreProjectXML_ReadLegacy12(filesDir, fncDir, outDir, refDir, mockRnd):
|
|||||||
assert data.itemImport.count("i00000a") == 0
|
assert data.itemImport.count("i00000a") == 0
|
||||||
|
|
||||||
# Compare content
|
# Compare content
|
||||||
dumpFile = os.path.join(outDir, "projectXML_ReadLegacy12.json")
|
dumpFile = tstPaths.outDir / "projectXML_ReadLegacy12.json"
|
||||||
compFile = os.path.join(refDir, "projectXML_ReadLegacy12.json")
|
compFile = tstPaths.refDir / "projectXML_ReadLegacy12.json"
|
||||||
with open(dumpFile, mode="w", encoding="utf-8") as dump:
|
with open(dumpFile, mode="w", encoding="utf-8") as dump:
|
||||||
json.dump(content, dump, indent=2)
|
json.dump(content, dump, indent=2)
|
||||||
assert cmpFiles(dumpFile, compFile)
|
assert cmpFiles(dumpFile, compFile)
|
||||||
@@ -654,10 +653,10 @@ def testCoreProjectXML_ReadLegacy12(filesDir, fncDir, outDir, refDir, mockRnd):
|
|||||||
|
|
||||||
# Save the project again, which should produce an identical project xml
|
# Save the project again, which should produce an identical project xml
|
||||||
timeStamp = int(datetime.fromisoformat(xmlReader.timeStamp).timestamp())
|
timeStamp = int(datetime.fromisoformat(xmlReader.timeStamp).timestamp())
|
||||||
xmlWriter = ProjectXMLWriter(fncDir)
|
xmlWriter = ProjectXMLWriter(fncPath)
|
||||||
assert xmlWriter.write(data, packedContent, timeStamp, 1000) is True
|
assert xmlWriter.write(data, packedContent, timeStamp, 1000) is True
|
||||||
testFile = os.path.join(outDir, "projectXML_ReadLegacy12.nwx")
|
testFile = tstPaths.outDir / "projectXML_ReadLegacy12.nwx"
|
||||||
compFile = os.path.join(refDir, "projectXML_ReadLegacy12.nwx")
|
compFile = tstPaths.refDir / "projectXML_ReadLegacy12.nwx"
|
||||||
copyfile(outFile, testFile)
|
copyfile(outFile, testFile)
|
||||||
assert cmpFiles(testFile, compFile)
|
assert cmpFiles(testFile, compFile)
|
||||||
|
|
||||||
@@ -665,12 +664,12 @@ def testCoreProjectXML_ReadLegacy12(filesDir, fncDir, outDir, refDir, mockRnd):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.core
|
@pytest.mark.core
|
||||||
def testCoreProjectXML_ReadLegacy13(filesDir, fncDir, outDir, refDir, mockRnd):
|
def testCoreProjectXML_ReadLegacy13(tstPaths, fncPath, mockRnd):
|
||||||
"""Test reading the version 1.3 XML file format.
|
"""Test reading the version 1.3 XML file format.
|
||||||
"""
|
"""
|
||||||
refFile = os.path.join(filesDir, "nwProject-1.3.nwx")
|
refFile = tstPaths.filesDir / "nwProject-1.3.nwx"
|
||||||
xmlFile = os.path.join(fncDir, "nwProject-1.3.nwx")
|
xmlFile = fncPath / "nwProject-1.3.nwx"
|
||||||
outFile = os.path.join(fncDir, "nwProject.nwx")
|
outFile = fncPath / "nwProject.nwx"
|
||||||
copyfile(refFile, xmlFile)
|
copyfile(refFile, xmlFile)
|
||||||
|
|
||||||
xmlReader = ProjectXMLReader(xmlFile)
|
xmlReader = ProjectXMLReader(xmlFile)
|
||||||
@@ -752,8 +751,8 @@ def testCoreProjectXML_ReadLegacy13(filesDir, fncDir, outDir, refDir, mockRnd):
|
|||||||
assert data.itemImport.count("i00000a") == 0
|
assert data.itemImport.count("i00000a") == 0
|
||||||
|
|
||||||
# Compare content
|
# Compare content
|
||||||
dumpFile = os.path.join(outDir, "projectXML_ReadLegacy13.json")
|
dumpFile = tstPaths.outDir / "projectXML_ReadLegacy13.json"
|
||||||
compFile = os.path.join(refDir, "projectXML_ReadLegacy13.json")
|
compFile = tstPaths.refDir / "projectXML_ReadLegacy13.json"
|
||||||
with open(dumpFile, mode="w", encoding="utf-8") as dump:
|
with open(dumpFile, mode="w", encoding="utf-8") as dump:
|
||||||
json.dump(content, dump, indent=2)
|
json.dump(content, dump, indent=2)
|
||||||
assert cmpFiles(dumpFile, compFile)
|
assert cmpFiles(dumpFile, compFile)
|
||||||
@@ -798,10 +797,10 @@ def testCoreProjectXML_ReadLegacy13(filesDir, fncDir, outDir, refDir, mockRnd):
|
|||||||
|
|
||||||
# Save the project again, which should produce an identical project xml
|
# Save the project again, which should produce an identical project xml
|
||||||
timeStamp = int(datetime.fromisoformat(xmlReader.timeStamp).timestamp())
|
timeStamp = int(datetime.fromisoformat(xmlReader.timeStamp).timestamp())
|
||||||
xmlWriter = ProjectXMLWriter(fncDir)
|
xmlWriter = ProjectXMLWriter(fncPath)
|
||||||
assert xmlWriter.write(data, packedContent, timeStamp, 1000) is True
|
assert xmlWriter.write(data, packedContent, timeStamp, 1000) is True
|
||||||
testFile = os.path.join(outDir, "projectXML_ReadLegacy13.nwx")
|
testFile = tstPaths.outDir / "projectXML_ReadLegacy13.nwx"
|
||||||
compFile = os.path.join(refDir, "projectXML_ReadLegacy13.nwx")
|
compFile = tstPaths.refDir / "projectXML_ReadLegacy13.nwx"
|
||||||
copyfile(outFile, testFile)
|
copyfile(outFile, testFile)
|
||||||
assert cmpFiles(testFile, compFile)
|
assert cmpFiles(testFile, compFile)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user