sphinxext-ip/tests/test_ip.py

58 lines
2 KiB
Python
Raw Normal View History

2016-05-01 18:12:02 +02:00
# -*- coding: utf-8 -*-
import shutil
2016-05-01 18:12:02 +02:00
import unittest
2021-01-02 06:20:53 +01:00
from io import StringIO
2016-05-01 18:12:02 +02:00
2021-01-02 06:20:53 +01:00
from .util import SphinxTestApplication, test_root
2016-05-01 18:12:02 +02:00
2021-01-02 06:20:53 +01:00
IP4_ADDRESSES = ["127.0.0.1", "192.168.0.1"]
IP6_ADDRESSES = ["::1", "2001:dead:beef::1"]
IP4_RANGES = ["172.16.0.0/24", "192.168.0.0/24"]
IP6_RANGES = ["2001:dead:beef::/64", "2001:dada:b001::/64"]
2016-05-01 18:12:02 +02:00
class TestIPExtension(unittest.TestCase):
def setUp(self):
2021-01-02 06:20:53 +01:00
if not (test_root / "_static").exists():
(test_root / "_static").mkdir()
2016-05-01 18:12:02 +02:00
self.feed_warnfile = StringIO()
self.app = SphinxTestApplication(
2021-01-02 06:20:53 +01:00
buildername="html", warning=self.feed_warnfile, cleanenv=True
)
self.app.build(force_all=True, filenames=[])
2016-05-01 18:12:02 +02:00
def tearDown(self):
self.app.cleanup()
shutil.rmtree((test_root / "_build"))
2016-05-01 18:12:02 +02:00
def test_ip_domaindata(self):
2021-01-02 06:20:53 +01:00
self.assertIn("ip", self.app.env.domaindata)
ipdomdata = self.app.env.domaindata["ip"]
self.assertIn("ip_refs", ipdomdata)
self.assertIn("range_refs", ipdomdata)
self.assertIn("range_nodes", ipdomdata)
self.assertIn("ranges", ipdomdata)
2021-09-04 19:54:24 +02:00
self.assertIn("ip_dict", ipdomdata)
def find_in_index(self, entry):
2021-01-02 06:20:53 +01:00
indexentries = self.app.env.get_domain("index").entries
for index in indexentries:
for value in indexentries[index]:
if value[1] == entry:
return
self.fail("%s not found in index" % entry)
def test_ip4_addresses(self):
2021-09-04 19:54:24 +02:00
ips = self.app.env.domaindata["ip"]["ip_refs"]
for ip in IP4_ADDRESSES:
self.assertIn(ip, ips)
self.find_in_index("IPv4 address; %s" % ip)
self.find_in_index("%s; Test page 2" % ip)
def test_ip6_addresses(self):
2021-09-04 19:54:24 +02:00
ips = self.app.env.domaindata["ip"]["ip_refs"]
for ip in IP6_ADDRESSES:
self.assertIn(ip, ips)
self.find_in_index("IPv6 address; %s" % ip)
self.find_in_index("%s; Test page 2" % ip)