File: test_entities.py

package info (click to toggle)
daps 3.3.2%2Bcleaned1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,536 kB
  • sloc: xml: 9,773; sh: 3,059; python: 1,322; lisp: 380; makefile: 239
file content (179 lines) | stat: -rw-r--r-- 5,484 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import argparse
import re
from unittest import mock
import pytest
import py
import sys

# "gen" is the abbreviated name for "getentityname.py"
import gen


@pytest.fixture
def cliargs():
    """Simulates parsed CLI arguments"""
    return argparse.Namespace(absolute=False,
                              catalog=gen.MAINCATALOG,
                              separator=' ',
                              skip_public=True,
                              verbose=1,
                              xmlfiles=["fake.xml"],
                              )


def test_parse_ent_file_empty(cliargs, tmpdir):
    # given
    # cliargs: parsed CLI arguments
    # tmpdir: temporary directory
    entityfile = (tmpdir / "entity-file.ent")
    entityfile.write_text("", encoding="UTF-8")
    
    # when
    entity = gen.parse_ent_file(str(entityfile), cliargs)
    
    # then
    assert list(entity) == []


def test_parse_ent_file_content(cliargs, tmpdir):
    # given
    # cliargs: parsed CLI arguments
    # tmpdir: temporary directory
    entityfile = (tmpdir / "entity-file.ent")
    entityfile.write_text("""<!ENTITY x "X">""",
                          encoding="UTF-8")
    
    # when
    entity = gen.parse_ent_file(str(entityfile), cliargs)
    
    # then
    assert list(entity) == []


def test_parse_ent_file_with_one_entity(cliargs, tmpdir):
    # given
    # cliargs: parsed CLI arguments
    # tmpdir: temporary directory
    entityfile = (tmpdir / "entity-file.ent")
    entityfile.write_text("""<!ENTITY % foo SYSTEM "foo.ent">
                          %foo;""",
                          encoding="UTF-8")
    secondfile = (tmpdir / "foo.ent")
    secondfile.write_text("", encoding="UTF-8")
    
    # when
    entity = gen.parse_ent_file(str(entityfile), cliargs)
    
    # then
    assert list(entity) == [str(tmpdir / 'foo.ent')]


def test_parse_ent_file_with_two_entities(cliargs, tmpdir):
    # given
    # cliargs: parsed CLI arguments
    # tmpdir: temporary directory
    entityfile = (tmpdir / "entity-file.ent")
    entityfile.write_text("""<!ENTITY % foo SYSTEM "foo.ent">
                          %foo;
                          <!ENTITY % bar SYSTEM "bar.ent">
                          %bar;
                          """,
                          encoding="UTF-8")
    secondfile = (tmpdir / "foo.ent")
    secondfile.write_text("", encoding="UTF-8")
    thirdfile = (tmpdir / "bar.ent")
    thirdfile.write_text("", encoding="UTF-8")
    
    
    # when
    entity = gen.parse_ent_file(str(entityfile), cliargs)
    
    # then
    assert set(entity) == set([str(tmpdir / 'foo.ent'),
                               str(tmpdir / 'bar.ent'),
                              ])


def test_parse_ent_file_with_public_entity(cliargs, tmpdir):
    # given
    # cliargs: parsed CLI arguments
    # tmpdir: temporary directory
    resultentity = "my-fake.ent"
    publicid = "-//TOMS//ENTITIES Example V1//EN"
    entityfile = (tmpdir / "entity-file.ent")
    entityfile.write_text(f"""
<!ENTITY % foo PUBLIC "{publicid}" "http://www.example.org/foo.ent">
%foo;
""",
                          encoding="UTF-8")

    catalogfile = (tmpdir / "catalog.xml")
    catalogfile.write_text(f"""<catalog prefer="public" xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
    <public publicId="{publicid}" uri="{resultentity}"/>
</catalog>""",
                           encoding="UTF-8",
                           )
    cliargs.skip_public = False
    cliargs.catalog = str(catalogfile)
    cliargs.verbose = 4

    # when
    result = list(gen.parse_ent_file(str(entityfile), cliargs))

    # then
    assert result == [str(tmpdir / resultentity)]


def test_parse_ent_file_with_system_entity(cliargs, tmpdir):
    # given
    # cliargs: parsed CLI arguments
    # tmpdir: temporary directory
    resultentity = "my-fake.ent"
    entityfile = (tmpdir / "entity-file.ent")
    entityfile.write_text("""<!ENTITY % foo SYSTEM "http://www.example.org/foo.ent">
                          %foo;
                          """,
                          encoding="UTF-8")
    catalogfile = (tmpdir / "catalog.xml")
    catalogfile.write_text("""<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog"> 
    <system systemId="http://www.example.org/foo.ent" uri="%s"/>
</catalog>""" % resultentity,
                           encoding="UTF-8",
                           )
    # cliargs.skip_public = False
    cliargs.catalog = str(catalogfile)
    
    # when
    result = list(gen.parse_ent_file(str(entityfile), cliargs))
    
    # then
    assert result == [str(tmpdir / resultentity)]


def test_skip_public_parameter_entity_in_investigate_identifiers(cliargs, caplog):
    import logging

    # given
    cliargs.skip_public = True
    cliargs.verbose = 4
    content = """
<!ENTITY % dbcent PUBLIC
    "-//OASIS//ENTITIES DocBook Character Entities V4.5//EN"
    "http://www.oasis-open.org/docbook/xml/4.5/dbcentx.mod">
%dbcent;
    """
    result = None
    # We are interested in a specific logger only
    caplog.set_level(logging.DEBUG, logger=gen.LOGGERNAME)

    # when
    # we are not interested in the result, but the logging output
    list(gen.investigate_identifiers(content, cliargs))

    # then
    for record in caplog.records:
        assert record.levelname == "DEBUG"
        if record.msg.startswith("Skipping public parameter entity"):
            result = True

    assert result, "Could not find 'Skipping public...' inside log records"