Add test suite

This commit adds a test suite and fixes replacement of ip address nodes
for IP addresses that are not part of a IP range directive.
This commit is contained in:
Jan Dittberner 2016-05-02 20:55:09 +02:00
parent a5c82a6985
commit 869039df5d
8 changed files with 113 additions and 15 deletions

1
.gitignore vendored
View file

@ -6,3 +6,4 @@
__pycache__/
dist/
.coverage
htmlcov/

View file

@ -189,18 +189,17 @@ class IPDomain(Domain):
if role is None:
return None
resnode = role.result_nodes(env.get_doctree(fromdocname),
env, node, True)[0][0]
env, node, True)[0][2]
if isinstance(resnode, addnodes.pending_xref):
text = node[0][0]
reporter = env.get_doctree(fromdocname).reporter
reporter.warning('Cannot resolve reference to %r' % text,
line=node.line)
return None
return node.children
return resnode
else:
title = typ.upper() + ' ' + target
anchor = ip_object_anchor(typ, target)
return make_refnode(builder, fromdocname, info[0], anchor,
return make_refnode(builder, fromdocname, info[0], key,
contnode, title)
@property
@ -227,7 +226,8 @@ def process_ips(app, doctree):
'ip': ip,
'typ': node.parent['typ'],
})
node.replace_self(nodes.literal('', ip))
replacement = nodes.literal(ip, ip)
node.replace_self(replacement)
def sort_ip_info(item):
@ -260,8 +260,6 @@ def process_ip_nodes(app, doctree, fromdocname):
para += nodes.Text(" in ")
para += newnode
content.append(para)
#print(ip_info, 'in', node['rangespec'])
node.replace_self(content)

View file

@ -13,13 +13,13 @@
# All configuration values have a default; values that are commented out
# serve to show the default.
import sys
import os
#import sys
#import os
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
sys.path.insert(0, os.path.abspath('..'))
#sys.path.insert(0, os.path.abspath('..'))
# -- General configuration ------------------------------------------------

View file

@ -11,6 +11,8 @@ Contents:
.. toctree::
:maxdepth: 2
testpage1
testpage2
Indices and tables

6
tests/root/testpage1.rst Normal file
View file

@ -0,0 +1,6 @@
Test page 1
===========
.. ip:v4range:: 192.168.0.1/24
.. ip:v6range:: 2001:dead:beef::/64

8
tests/root/testpage2.rst Normal file
View file

@ -0,0 +1,8 @@
Test page 2
===========
This page contains IP addresses :ip:v4:`127.0.0.1`, :ip:v4:`192.168.0.1` and
:ip:v6:`2001:dead:beef::1` as well as :ip:v6:`::1`.
There is also :ip:v6range:`2001:dada:b001::/64` and
:ip:v4range:`172.16.0.0/24`.

43
tests/run.py Executable file
View file

@ -0,0 +1,43 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
jandd.sphinxext.ip unit test driver
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This script runs the jandd.sphinxext.ip unit test suite.
:copyright: Copyright 2016 Jan Dittberner
:license: GPLv3+, see COPYING for details.
"""
import sys
from os import path
import unittest
def run(extra_args=[]):
sys.path.insert(0, path.join(path.dirname(__file__), path.pardir))
sys.path.insert(1, path.abspath(
path.join(path.dirname(__file__), path.pardir,
'jandd', 'sphinxext', 'ip'
))
)
try:
import sphinx
except ImportError:
print("The sphinx package is needed to run the jandd.sphinxext.ip "
"test suite.")
import test_ip
print("Running jandd.sphinxext.ip test suite ...")
suite = unittest.TestLoader().loadTestsFromTestCase(
test_ip.TestIPExtension
)
unittest.TextTestRunner(verbosity=2).run(suite)
if __name__ == '__main__':
run()

View file

@ -1,20 +1,60 @@
# -*- coding: utf-8 -*-
from io import StringIO
from .util import TestApp, test_root
from util import TestApp, test_root
import unittest
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']
class TestIPExtension(unittest.TestCase):
def setUp(self):
if not (test_root / '_static').exists():
(test_root / '_static').mkdir()
self.feed_warnfile = StringIO()
self.app = TestApp(
buildername='html', warning=self.feed_warnfile, cleanenv=True)
self.app.build(force_all=True, filenames=[])
def tearDown(self):
self.app.cleanup()
(test_root / '_build').rmtree(True)
def test_ip4_role(self):
feed_warnfile = self.feed_warnfile
app = TestApp(buildername='html', warning=feed_warnfile, cleanenv=True)
app.build(force_all=True, filenames=[])
def test_ip_domaindata(self):
self.assertIn('ip', self.app.env.domaindata)
ipdomdata = self.app.env.domaindata['ip']
self.assertIn('v4', ipdomdata)
self.assertIn('v6', ipdomdata)
self.assertIn('v4range', ipdomdata)
self.assertIn('v6range', ipdomdata)
self.assertIn('ips', ipdomdata)
def find_in_index(self, entry):
indexentries = self.app.env.indexentries
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):
ipv4 = self.app.env.domaindata['ip']['v4']
ips = self.app.env.domaindata['ip']['ips']
for ip in IP4_ADDRESSES:
self.assertIn(ip, ipv4)
self.assertIn(ip, [item['ip'] for item in ips])
self.find_in_index("IPv4 address; %s" % ip)
self.find_in_index("%s; Test page 2" % ip)
def test_ip6_addresses(self):
ipv6 = self.app.env.domaindata['ip']['v6']
ips = self.app.env.domaindata['ip']['ips']
for ip in IP6_ADDRESSES:
self.assertIn(ip, ipv6)
self.assertIn(ip, [item['ip'] for item in ips])
self.find_in_index("IPv6 address; %s" % ip)
self.find_in_index("%s; Test page 2" % ip)