| 1 |
import argparse |
| 2 |
import re |
| 3 |
from unittest import mock |
| 4 |
import pytest |
| 5 |
import py |
| 6 |
import sys |
| 7 |
|
| 8 |
# "gen" is the abbreviated name for "getentityname.py" |
| 9 |
import gen |
| 10 |
|
| 11 |
|
| 12 |
@pytest.fixture |
| 13 |
def cliargs(): |
| 14 |
"""Simulates parsed CLI arguments""" |
| 15 |
return argparse.Namespace(absolute=False, |
| 16 |
catalog=gen.MAINCATALOG, |
| 17 |
separator=' ', |
| 18 |
skip_public=True, |
| 19 |
verbose=1, |
| 20 |
xmlfiles=["fake.xml"], |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
def test_parse_ent_file_empty(cliargs, tmpdir): |
| 25 |
# given |
| 26 |
# cliargs: parsed CLI arguments |
| 27 |
# tmpdir: temporary directory |
| 28 |
entityfile = (tmpdir / "entity-file.ent") |
| 29 |
entityfile.write_text("", encoding="UTF-8") |
| 30 |
|
| 31 |
# when |
| 32 |
entity = gen.parse_ent_file(str(entityfile), cliargs) |
| 33 |
|
| 34 |
# then |
| 35 |
assert list(entity) == [] |
| 36 |
|
| 37 |
|
| 38 |
def test_parse_ent_file_content(cliargs, tmpdir): |
| 39 |
# given |
| 40 |
# cliargs: parsed CLI arguments |
| 41 |
# tmpdir: temporary directory |
| 42 |
entityfile = (tmpdir / "entity-file.ent") |
| 43 |
entityfile.write_text("""<!ENTITY x "X">""", |
| 44 |
encoding="UTF-8") |
| 45 |
|
| 46 |
# when |
| 47 |
entity = gen.parse_ent_file(str(entityfile), cliargs) |
| 48 |
|
| 49 |
# then |
| 50 |
assert list(entity) == [] |
| 51 |
|
| 52 |
|
| 53 |
def test_parse_ent_file_with_one_entity(cliargs, tmpdir): |
| 54 |
# given |
| 55 |
# cliargs: parsed CLI arguments |
| 56 |
# tmpdir: temporary directory |
| 57 |
entityfile = (tmpdir / "entity-file.ent") |
| 58 |
entityfile.write_text("""<!ENTITY % foo SYSTEM "foo.ent"> |
| 59 |
%foo;""", |
| 60 |
encoding="UTF-8") |
| 61 |
secondfile = (tmpdir / "foo.ent") |
| 62 |
secondfile.write_text("", encoding="UTF-8") |
| 63 |
|
| 64 |
# when |
| 65 |
entity = gen.parse_ent_file(str(entityfile), cliargs) |
| 66 |
|
| 67 |
# then |
| 68 |
assert list(entity) == [str(tmpdir / 'foo.ent')] |
| 69 |
|
| 70 |
|
| 71 |
def test_parse_ent_file_with_two_entities(cliargs, tmpdir): |
| 72 |
# given |
| 73 |
# cliargs: parsed CLI arguments |
| 74 |
# tmpdir: temporary directory |
| 75 |
entityfile = (tmpdir / "entity-file.ent") |
| 76 |
entityfile.write_text("""<!ENTITY % foo SYSTEM "foo.ent"> |
| 77 |
%foo; |
| 78 |
<!ENTITY % bar SYSTEM "bar.ent"> |
| 79 |
%bar; |
| 80 |
""", |
| 81 |
encoding="UTF-8") |
| 82 |
secondfile = (tmpdir / "foo.ent") |
| 83 |
secondfile.write_text("", encoding="UTF-8") |
| 84 |
thirdfile = (tmpdir / "bar.ent") |
| 85 |
thirdfile.write_text("", encoding="UTF-8") |
| 86 |
|
| 87 |
|
| 88 |
# when |
| 89 |
entity = gen.parse_ent_file(str(entityfile), cliargs) |
| 90 |
|
| 91 |
# then |
| 92 |
assert set(entity) == set([str(tmpdir / 'foo.ent'), |
| 93 |
str(tmpdir / 'bar.ent'), |
| 94 |
]) |
| 95 |
|
| 96 |
|
| 97 |
def test_parse_ent_file_with_public_entity(cliargs, tmpdir): |
| 98 |
# given |
| 99 |
# cliargs: parsed CLI arguments |
| 100 |
# tmpdir: temporary directory |
| 101 |
resultentity = "my-fake.ent" |
| 102 |
publicid = "-//TOMS//ENTITIES Example V1//EN" |
| 103 |
entityfile = (tmpdir / "entity-file.ent") |
| 104 |
entityfile.write_text(f""" |
| 105 |
<!ENTITY % foo PUBLIC "{publicid}" "http://www.example.org/foo.ent"> |
| 106 |
%foo; |
| 107 |
""", |
| 108 |
encoding="UTF-8") |
| 109 |
|
| 110 |
catalogfile = (tmpdir / "catalog.xml") |
| 111 |
catalogfile.write_text(f"""<catalog prefer="public" xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog"> |
| 112 |
<public publicId="{publicid}" uri="{resultentity}"/> |
| 113 |
</catalog>""", |
| 114 |
encoding="UTF-8", |
| 115 |
) |
| 116 |
cliargs.skip_public = False |
| 117 |
cliargs.catalog = str(catalogfile) |
| 118 |
cliargs.verbose = 4 |
| 119 |
|
| 120 |
# when |
| 121 |
result = list(gen.parse_ent_file(str(entityfile), cliargs)) |
| 122 |
|
| 123 |
# then |
| 124 |
assert result == [str(tmpdir / resultentity)] |
| 125 |
|
| 126 |
|
| 127 |
def test_parse_ent_file_with_system_entity(cliargs, tmpdir): |
| 128 |
# given |
| 129 |
# cliargs: parsed CLI arguments |
| 130 |
# tmpdir: temporary directory |
| 131 |
resultentity = "my-fake.ent" |
| 132 |
entityfile = (tmpdir / "entity-file.ent") |
| 133 |
entityfile.write_text("""<!ENTITY % foo SYSTEM "http://www.example.org/foo.ent"> |
| 134 |
%foo; |
| 135 |
""", |
| 136 |
encoding="UTF-8") |
| 137 |
catalogfile = (tmpdir / "catalog.xml") |
| 138 |
catalogfile.write_text("""<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog"> |
| 139 |
<system systemId="http://www.example.org/foo.ent" uri="%s"/> |
| 140 |
</catalog>""" % resultentity, |
| 141 |
encoding="UTF-8", |
| 142 |
) |
| 143 |
# cliargs.skip_public = False |
| 144 |
cliargs.catalog = str(catalogfile) |
| 145 |
|
| 146 |
# when |
| 147 |
result = list(gen.parse_ent_file(str(entityfile), cliargs)) |
| 148 |
|
| 149 |
# then |
| 150 |
assert result == [str(tmpdir / resultentity)] |
| 151 |
|
| 152 |
|
| 153 |
def test_skip_public_parameter_entity_in_investigate_identifiers(cliargs, caplog): |
| 154 |
import logging |
| 155 |
|
| 156 |
# given |
| 157 |
cliargs.skip_public = True |
| 158 |
cliargs.verbose = 4 |
| 159 |
content = """ |
| 160 |
<!ENTITY % dbcent PUBLIC |
| 161 |
"-//OASIS//ENTITIES DocBook Character Entities V4.5//EN" |
| 162 |
"http://www.oasis-open.org/docbook/xml/4.5/dbcentx.mod"> |
| 163 |
%dbcent; |
| 164 |
""" |
| 165 |
result = None |
| 166 |
# We are interested in a specific logger only |
| 167 |
caplog.set_level(logging.DEBUG, logger=gen.LOGGERNAME) |
| 168 |
|
| 169 |
# when |
| 170 |
# we are not interested in the result, but the logging output |
| 171 |
list(gen.investigate_identifiers(content, cliargs)) |
| 172 |
|
| 173 |
# then |
| 174 |
for record in caplog.records: |
| 175 |
assert record.levelname == "DEBUG" |
| 176 |
if record.msg.startswith("Skipping public parameter entity"): |
| 177 |
result = True |
| 178 |
|
| 179 |
assert result, "Could not find 'Skipping public...' inside log records" |