| 1 |
from pathlib import Path |
| 2 |
import logging |
| 3 |
import re |
| 4 |
import sys |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
# "dxwf" is the abbreviated name for daps-xmlwellformed |
| 9 |
# We can't import a name with a "-" so we use a link here: |
| 10 |
import dxwf |
| 11 |
|
| 12 |
|
| 13 |
THISDIR = Path(__file__).parent |
| 14 |
DATADIR = THISDIR / "data" |
| 15 |
BADDIR = DATADIR / "bad" |
| 16 |
|
| 17 |
|
| 18 |
def test_syntax_error(): |
| 19 |
# Given |
| 20 |
xmlfile = str(BADDIR / "test-syntax-err-tag-name-mismatch.xml") |
| 21 |
|
| 22 |
# When |
| 23 |
result = dxwf.check_wellformedness(xmlfile) |
| 24 |
|
| 25 |
# Then |
| 26 |
assert result != 0 |
| 27 |
|
| 28 |
|
| 29 |
def test_syntax_error_stderr(caplog): |
| 30 |
# Given |
| 31 |
xmlfile = "test-syntax-err-tag-name-mismatch.xml" |
| 32 |
messages = ( |
| 33 |
("Opening and ending tag mismatch: title line 7 and article",), |
| 34 |
# These error messages are different between different lxml versions: |
| 35 |
("EndTag: '</' not found", # lxml >=4.5.2 |
| 36 |
"Premature end of data in tag article line 5", # lxml >=4.4.3 |
| 37 |
), |
| 38 |
) |
| 39 |
file = str(BADDIR / xmlfile) |
| 40 |
|
| 41 |
# When |
| 42 |
with caplog.at_level(logging.ERROR): |
| 43 |
dxwf.check_wellformedness(file) |
| 44 |
|
| 45 |
# Then |
| 46 |
assert len(caplog.records) == len(messages) |
| 47 |
for record, msg in zip(caplog.records, messages): |
| 48 |
assert record.msg in msg |
| 49 |
|
| 50 |
|
| 51 |
def test_unknown_entity_stderr(caplog): |
| 52 |
# Given |
| 53 |
xmlfile = "test-syntax-err-undeclared-entity.xml" |
| 54 |
messages = ( |
| 55 |
"Entity 'unknown' not defined", |
| 56 |
) |
| 57 |
file = str(BADDIR / xmlfile) |
| 58 |
|
| 59 |
# When |
| 60 |
with caplog.at_level(logging.ERROR): |
| 61 |
result = dxwf.check_wellformedness(file) |
| 62 |
|
| 63 |
# Then |
| 64 |
assert result == dxwf.ExitCode.syntax |
| 65 |
assert len(caplog.records) == len(messages) |
| 66 |
for record, msg in zip(caplog.records, messages): |
| 67 |
assert record.msg == msg |
| 68 |
|
| 69 |
|
| 70 |
@pytest.mark.parametrize("xmlfile", [ |
| 71 |
"test-syntax-err-hyphen-in-comment.xml", |
| 72 |
"test-syntax-err-name-required.xml", |
| 73 |
"test-syntax-err-ltslash-required.xml", |
| 74 |
]) |
| 75 |
def test_syntax_errors(xmlfile): |
| 76 |
# Given |
| 77 |
xmlfile = str(BADDIR / xmlfile) |
| 78 |
|
| 79 |
# When |
| 80 |
result = dxwf.check_wellformedness(xmlfile) |
| 81 |
|
| 82 |
# Then |
| 83 |
assert result == dxwf.ExitCode.syntax |
| 84 |
|
| 85 |
|
| 86 |
def test_file_not_found(): |
| 87 |
# Given |
| 88 |
xmlfile = str(BADDIR / "unknown.xml") |
| 89 |
|
| 90 |
# When |
| 91 |
result = dxwf.check_wellformedness(xmlfile) |
| 92 |
|
| 93 |
# Then |
| 94 |
assert result == dxwf.ExitCode.file_not_found |
| 95 |
|
| 96 |
|
| 97 |
@pytest.mark.parametrize("xmlfile,messages", [ |
| 98 |
("test-xinclude-file-not-found.xml", |
| 99 |
(r"could not load (.*)tests/data/bad/file-not-found.xml, and no fallback was found", |
| 100 |
) |
| 101 |
), |
| 102 |
("test-xinclude-undeclared-entity.xml", |
| 103 |
("Entity 'unknown' not defined", |
| 104 |
r"could not load (.*)tests/data/bad/test-syntax-err-undeclared-entity.xml, and no fallback was found", |
| 105 |
) |
| 106 |
), |
| 107 |
]) |
| 108 |
def test_xinclude_errors(xmlfile, messages, caplog): |
| 109 |
# Given |
| 110 |
xmlfile = str(BADDIR / xmlfile) |
| 111 |
|
| 112 |
# When |
| 113 |
with caplog.at_level(logging.ERROR): |
| 114 |
result = dxwf.check_wellformedness(xmlfile, xinclude=True) |
| 115 |
|
| 116 |
assert len(caplog.records) == len(messages) |
| 117 |
for record, pattern in zip(caplog.records, messages): |
| 118 |
assert re.search(pattern, record.msg) |