Add test coverage of the doc builder class
This commit is contained in:
@@ -21,6 +21,11 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import pytest
|
||||
|
||||
from shutil import copyfile
|
||||
|
||||
from mock import causeException, causeOSError
|
||||
from tools import cmpFiles
|
||||
|
||||
from novelwriter.core.project import NWProject
|
||||
from novelwriter.mbuild.docbuild import NWBuildDocument
|
||||
|
||||
@@ -43,11 +48,12 @@ BUILD_CONF = {
|
||||
"filter.includeComments": True,
|
||||
"filter.includeKeywords": True,
|
||||
"filter.includeBody": True,
|
||||
"process.replaceTabs": True,
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.core
|
||||
def testMBuildDocBuild_OpenDocument(mockGUI, prjLipsum, fncPath, tstPaths):
|
||||
def testMBuildDocBuild_OpenDocument(monkeypatch, mockGUI, prjLipsum, fncPath, tstPaths):
|
||||
"""Test builing an open document manuscript.
|
||||
"""
|
||||
theProject = NWProject(mockGUI)
|
||||
@@ -59,9 +65,18 @@ def testMBuildDocBuild_OpenDocument(mockGUI, prjLipsum, fncPath, tstPaths):
|
||||
for tItem in theProject.tree:
|
||||
docBuild.addDocument(tItem.itemHandle)
|
||||
|
||||
assert docBuild.buildLength == 21
|
||||
|
||||
# Check FODT Build
|
||||
# ================
|
||||
|
||||
docFile = fncPath / "Lorem Ipsum.fodt"
|
||||
tstFile = tstPaths.outDir / "mBuildDocBuild_OpenDocument_Lorem_Ipsum.fodt"
|
||||
cmpFile = tstPaths.refDir / "mBuildDocBuild_OpenDocument_Lorem_Ipsum.fodt"
|
||||
|
||||
count = 0
|
||||
error = []
|
||||
for _, success in docBuild.buildOpenDocument(fncPath / "Lorem Ipsum.fodt", True):
|
||||
for _, success in docBuild.buildOpenDocument(docFile, True):
|
||||
count += 1 if success else 0
|
||||
if docBuild.error:
|
||||
error.append(docBuild.error)
|
||||
@@ -69,4 +84,199 @@ def testMBuildDocBuild_OpenDocument(mockGUI, prjLipsum, fncPath, tstPaths):
|
||||
assert count == 21
|
||||
assert error == []
|
||||
|
||||
copyfile(docFile, tstFile)
|
||||
assert cmpFiles(tstFile, cmpFile, ignoreStart="<meta:")
|
||||
|
||||
# Check ODT Build
|
||||
# ===============
|
||||
|
||||
docFile = fncPath / "Lorem Ipsum.odt"
|
||||
|
||||
count = 0
|
||||
error = []
|
||||
for _, success in docBuild.buildOpenDocument(docFile, False):
|
||||
count += 1 if success else 0
|
||||
if docBuild.error:
|
||||
error.append(docBuild.error)
|
||||
|
||||
assert count == 21
|
||||
assert error == []
|
||||
|
||||
assert docFile.is_file()
|
||||
|
||||
# Check Error Handling
|
||||
# ====================
|
||||
|
||||
with monkeypatch.context() as mp:
|
||||
mp.setattr("builtins.open", causeOSError)
|
||||
|
||||
docFile = fncPath / "Lorem Ipsum Err.fodt"
|
||||
for _ in docBuild.buildOpenDocument(docFile, True):
|
||||
pass
|
||||
|
||||
assert docBuild.error == "OSError: Mock OSError"
|
||||
assert not docFile.is_file()
|
||||
|
||||
# Check Build Issues
|
||||
# ==================
|
||||
|
||||
with monkeypatch.context() as mp:
|
||||
mp.setattr("novelwriter.core.toodt.ToOdt.doConvert", causeException)
|
||||
|
||||
docBuild.addDocument("0000000000000")
|
||||
assert docBuild.buildLength == 22
|
||||
|
||||
count = 0
|
||||
error = []
|
||||
docFile = fncPath / "Lorem Ipsum Err.fodt"
|
||||
for _, success in docBuild.buildOpenDocument(docFile, True):
|
||||
count += 1 if success else 0
|
||||
if docBuild.error:
|
||||
error.append(docBuild.error)
|
||||
|
||||
assert count == 3
|
||||
assert error == [
|
||||
"Build: Failed to build '7a992350f3eb6'",
|
||||
"Build: Failed to build '8c58a65414c23'",
|
||||
"Build: Failed to build '88d59a277361b'",
|
||||
"Build: Failed to build 'db7e733775d4d'",
|
||||
"Build: Failed to build 'fb609cd8319dc'",
|
||||
"Build: Failed to build '88243afbe5ed8'",
|
||||
"Build: Failed to build 'f96ec11c6a3da'",
|
||||
"Build: Failed to build '846352075de7d'",
|
||||
"Build: Failed to build '441420a886d82'",
|
||||
"Build: Failed to build 'eb103bc70c90c'",
|
||||
"Build: Failed to build 'f8c0562e50f1b'",
|
||||
"Build: Failed to build '47666c91c7ccf'",
|
||||
"Build: Failed to build '67a8707f2f249'",
|
||||
"Build: Failed to build '4c4f28287af27'",
|
||||
"Build: Failed to build '6c6afb1247750'",
|
||||
"Build: Failed to build '2426c6f0ca922'",
|
||||
"Build: Failed to build '60bdf227455cc'",
|
||||
"Build: Failed to build '04468803b92e1'",
|
||||
"Build: Unknown item '0000000000000'",
|
||||
]
|
||||
|
||||
# END Test testMBuildDocBuild_OpenDocument
|
||||
|
||||
|
||||
@pytest.mark.core
|
||||
def testMBuildDocBuild_HTML(monkeypatch, mockGUI, prjLipsum, fncPath, tstPaths):
|
||||
"""Test builing an HTML manuscript.
|
||||
"""
|
||||
theProject = NWProject(mockGUI)
|
||||
theProject.openProject(prjLipsum)
|
||||
|
||||
docBuild = NWBuildDocument(theProject)
|
||||
docBuild.setBuildConfig(BUILD_CONF)
|
||||
|
||||
for tItem in theProject.tree:
|
||||
docBuild.addDocument(tItem.itemHandle)
|
||||
|
||||
assert docBuild.buildLength == 21
|
||||
|
||||
# Check HTML5 Build
|
||||
# =================
|
||||
|
||||
docFile = fncPath / "Lorem Ipsum.htm"
|
||||
tstFile = tstPaths.outDir / "mBuildDocBuild_HTML5_Lorem_Ipsum.htm"
|
||||
cmpFile = tstPaths.refDir / "mBuildDocBuild_HTML5_Lorem_Ipsum.htm"
|
||||
|
||||
count = 0
|
||||
error = []
|
||||
for _, success in docBuild.buildHTML(docFile):
|
||||
count += 1 if success else 0
|
||||
if docBuild.error:
|
||||
error.append(docBuild.error)
|
||||
|
||||
assert count == 21
|
||||
assert error == []
|
||||
|
||||
copyfile(docFile, tstFile)
|
||||
assert cmpFiles(tstFile, cmpFile)
|
||||
|
||||
# Check Error Handling
|
||||
# ====================
|
||||
|
||||
with monkeypatch.context() as mp:
|
||||
mp.setattr("builtins.open", causeOSError)
|
||||
|
||||
docFile = fncPath / "Lorem Ipsum Err.htm"
|
||||
for _ in docBuild.buildHTML(docFile):
|
||||
pass
|
||||
|
||||
assert docBuild.error == "OSError: Mock OSError"
|
||||
assert not docFile.is_file()
|
||||
|
||||
# END Test testMBuildDocBuild_HTML
|
||||
|
||||
|
||||
@pytest.mark.core
|
||||
def testMBuildDocBuild_Markdown(monkeypatch, mockGUI, prjLipsum, fncPath, tstPaths):
|
||||
"""Test builing an Markdown manuscript.
|
||||
"""
|
||||
theProject = NWProject(mockGUI)
|
||||
theProject.openProject(prjLipsum)
|
||||
|
||||
docBuild = NWBuildDocument(theProject)
|
||||
docBuild.setBuildConfig(BUILD_CONF)
|
||||
|
||||
for tItem in theProject.tree:
|
||||
docBuild.addDocument(tItem.itemHandle)
|
||||
|
||||
assert docBuild.buildLength == 21
|
||||
|
||||
# Check Standard Markdown Build
|
||||
# =============================
|
||||
|
||||
docFile = fncPath / "Lorem Ipsum Standard.md"
|
||||
tstFile = tstPaths.outDir / "mBuildDocBuild_Standard_Markdown_Lorem_Ipsum.md"
|
||||
cmpFile = tstPaths.refDir / "mBuildDocBuild_Standard_Markdown_Lorem_Ipsum.md"
|
||||
|
||||
count = 0
|
||||
error = []
|
||||
for _, success in docBuild.buildMarkdown(docFile, False):
|
||||
count += 1 if success else 0
|
||||
if docBuild.error:
|
||||
error.append(docBuild.error)
|
||||
|
||||
assert count == 21
|
||||
assert error == []
|
||||
|
||||
copyfile(docFile, tstFile)
|
||||
assert cmpFiles(tstFile, cmpFile)
|
||||
|
||||
# Check Extended Markdown Build
|
||||
# =============================
|
||||
|
||||
docFile = fncPath / "Lorem Ipsum Standard.md"
|
||||
tstFile = tstPaths.outDir / "mBuildDocBuild_Extended_Markdown_Lorem_Ipsum.md"
|
||||
cmpFile = tstPaths.refDir / "mBuildDocBuild_Extended_Markdown_Lorem_Ipsum.md"
|
||||
|
||||
count = 0
|
||||
error = []
|
||||
for _, success in docBuild.buildMarkdown(docFile, True):
|
||||
count += 1 if success else 0
|
||||
if docBuild.error:
|
||||
error.append(docBuild.error)
|
||||
|
||||
assert count == 21
|
||||
assert error == []
|
||||
|
||||
copyfile(docFile, tstFile)
|
||||
assert cmpFiles(tstFile, cmpFile)
|
||||
|
||||
# Check Error Handling
|
||||
# ====================
|
||||
|
||||
with monkeypatch.context() as mp:
|
||||
mp.setattr("builtins.open", causeOSError)
|
||||
|
||||
docFile = fncPath / "Lorem Ipsum Err.md"
|
||||
for _ in docBuild.buildMarkdown(docFile, False):
|
||||
pass
|
||||
|
||||
assert docBuild.error == "OSError: Mock OSError"
|
||||
assert not docFile.is_file()
|
||||
|
||||
# END Test testMBuildDocBuild_Markdown
|
||||
|
||||
Reference in New Issue
Block a user