File: test_xml-bad.py

package info (click to toggle)
daps 3.3.2%2Bcleaned1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,540 kB
  • sloc: xml: 9,773; sh: 3,059; python: 1,322; lisp: 380; makefile: 239
file content (118 lines) | stat: -rw-r--r-- 3,026 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from pathlib import Path
import logging
import re
import sys

import pytest

# "dxwf" is the abbreviated name for daps-xmlwellformed
# We can't import a name with a "-" so we use a link here:
import dxwf


THISDIR = Path(__file__).parent
DATADIR = THISDIR / "data"
BADDIR = DATADIR / "bad"


def test_syntax_error():
    # Given
    xmlfile = str(BADDIR / "test-syntax-err-tag-name-mismatch.xml")

    # When
    result = dxwf.check_wellformedness(xmlfile)

    # Then
    assert result != 0


def test_syntax_error_stderr(caplog):
    # Given
    xmlfile = "test-syntax-err-tag-name-mismatch.xml"
    messages = (
        ("Opening and ending tag mismatch: title line 7 and article",),
        # These error messages are different between different lxml versions:
        ("EndTag: '</' not found",  # lxml >=4.5.2
         "Premature end of data in tag article line 5", # lxml >=4.4.3
         ),
    )
    file = str(BADDIR / xmlfile)

    # When
    with caplog.at_level(logging.ERROR):
        dxwf.check_wellformedness(file)

    # Then
    assert len(caplog.records) == len(messages)
    for record, msg in zip(caplog.records, messages):
        assert record.msg in msg


def test_unknown_entity_stderr(caplog):
    # Given
    xmlfile = "test-syntax-err-undeclared-entity.xml"
    messages = (
        "Entity 'unknown' not defined",
    )
    file = str(BADDIR / xmlfile)

    # When
    with caplog.at_level(logging.ERROR):
        result = dxwf.check_wellformedness(file)

    # Then
    assert result == dxwf.ExitCode.syntax
    assert len(caplog.records) == len(messages)
    for record, msg in zip(caplog.records, messages):
        assert record.msg == msg


@pytest.mark.parametrize("xmlfile", [
    "test-syntax-err-hyphen-in-comment.xml",
    "test-syntax-err-name-required.xml",
    "test-syntax-err-ltslash-required.xml",
])
def test_syntax_errors(xmlfile):
    # Given
    xmlfile = str(BADDIR / xmlfile)

    # When
    result = dxwf.check_wellformedness(xmlfile)

    # Then
    assert result == dxwf.ExitCode.syntax


def test_file_not_found():
    # Given
    xmlfile = str(BADDIR / "unknown.xml")

    # When
    result = dxwf.check_wellformedness(xmlfile)

    # Then
    assert result == dxwf.ExitCode.file_not_found


@pytest.mark.parametrize("xmlfile,messages", [
    ("test-xinclude-file-not-found.xml",
     (r"could not load (.*)tests/data/bad/file-not-found.xml, and no fallback was found",
      )
     ),
    ("test-xinclude-undeclared-entity.xml",
     ("Entity 'unknown' not defined",
      r"could not load (.*)tests/data/bad/test-syntax-err-undeclared-entity.xml, and no fallback was found",
      )
     ),
])
def test_xinclude_errors(xmlfile, messages, caplog):
    # Given
    xmlfile = str(BADDIR / xmlfile)

    # When
    with caplog.at_level(logging.ERROR):
        result = dxwf.check_wellformedness(xmlfile, xinclude=True)

    assert len(caplog.records) == len(messages)
    for record, pattern in zip(caplog.records, messages):
        assert re.search(pattern, record.msg)