2009-09-09 14:56:53 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
test_intersphinx
|
|
|
|
~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Test the intersphinx extension.
|
|
|
|
|
2011-01-04 03:00:51 -06:00
|
|
|
:copyright: Copyright 2007-2011 by the Sphinx team, see AUTHORS.
|
2009-09-09 14:56:53 -05:00
|
|
|
:license: BSD, see LICENSE for details.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import zlib
|
|
|
|
import posixpath
|
2010-06-06 18:34:01 -05:00
|
|
|
try:
|
|
|
|
from io import BytesIO
|
|
|
|
except ImportError:
|
|
|
|
from cStringIO import StringIO as BytesIO
|
2009-09-09 14:56:53 -05:00
|
|
|
|
|
|
|
from docutils import nodes
|
|
|
|
|
|
|
|
from sphinx import addnodes
|
|
|
|
from sphinx.ext.intersphinx import read_inventory_v1, read_inventory_v2, \
|
2010-01-17 05:02:15 -06:00
|
|
|
load_mappings, missing_reference
|
2009-09-09 14:56:53 -05:00
|
|
|
|
|
|
|
from util import *
|
|
|
|
|
|
|
|
|
|
|
|
inventory_v1 = '''\
|
|
|
|
# Sphinx inventory version 1
|
|
|
|
# Project: foo
|
|
|
|
# Version: 1.0
|
|
|
|
module mod foo.html
|
|
|
|
module.cls class foo.html
|
2010-06-06 18:34:01 -05:00
|
|
|
'''.encode('utf-8')
|
2009-09-09 14:56:53 -05:00
|
|
|
|
|
|
|
inventory_v2 = '''\
|
|
|
|
# Sphinx inventory version 2
|
|
|
|
# Project: foo
|
|
|
|
# Version: 2.0
|
|
|
|
# The remainder of this file is compressed with zlib.
|
2010-06-06 18:34:01 -05:00
|
|
|
'''.encode('utf-8') + zlib.compress('''\
|
2010-05-23 06:06:01 -05:00
|
|
|
module1 py:module 0 foo.html#module-module1 Long Module desc
|
|
|
|
module2 py:module 0 foo.html#module-$ -
|
|
|
|
module1.func py:function 1 sub/foo.html#$ -
|
|
|
|
CFunc c:function 2 cfunc.html#CFunc -
|
2010-06-06 18:34:01 -05:00
|
|
|
'''.encode('utf-8'))
|
2009-09-09 14:56:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_read_inventory_v1():
|
2010-06-06 18:34:01 -05:00
|
|
|
f = BytesIO(inventory_v1)
|
2009-09-09 14:56:53 -05:00
|
|
|
f.readline()
|
|
|
|
invdata = read_inventory_v1(f, '/util', posixpath.join)
|
|
|
|
assert invdata['py:module']['module'] == \
|
2010-05-23 06:06:01 -05:00
|
|
|
('foo', '1.0', '/util/foo.html#module-module', '-')
|
2009-09-09 14:56:53 -05:00
|
|
|
assert invdata['py:class']['module.cls'] == \
|
2010-05-23 06:06:01 -05:00
|
|
|
('foo', '1.0', '/util/foo.html#module.cls', '-')
|
2009-09-09 14:56:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_read_inventory_v2():
|
2010-06-06 18:34:01 -05:00
|
|
|
f = BytesIO(inventory_v2)
|
2009-09-09 14:56:53 -05:00
|
|
|
f.readline()
|
|
|
|
invdata1 = read_inventory_v2(f, '/util', posixpath.join)
|
|
|
|
|
|
|
|
# try again with a small buffer size to test the chunking algorithm
|
2010-06-06 18:34:01 -05:00
|
|
|
f = BytesIO(inventory_v2)
|
2009-09-09 14:56:53 -05:00
|
|
|
f.readline()
|
|
|
|
invdata2 = read_inventory_v2(f, '/util', posixpath.join, bufsize=5)
|
|
|
|
|
|
|
|
assert invdata1 == invdata2
|
|
|
|
|
|
|
|
assert len(invdata1['py:module']) == 2
|
|
|
|
assert invdata1['py:module']['module1'] == \
|
2010-05-23 06:06:01 -05:00
|
|
|
('foo', '2.0', '/util/foo.html#module-module1', 'Long Module desc')
|
2009-09-09 14:56:53 -05:00
|
|
|
assert invdata1['py:module']['module2'] == \
|
2010-05-23 06:06:01 -05:00
|
|
|
('foo', '2.0', '/util/foo.html#module-module2', '-')
|
2009-09-09 14:56:53 -05:00
|
|
|
assert invdata1['py:function']['module1.func'][2] == \
|
|
|
|
'/util/sub/foo.html#module1.func'
|
|
|
|
assert invdata1['c:function']['CFunc'][2] == '/util/cfunc.html#CFunc'
|
|
|
|
|
|
|
|
|
|
|
|
@with_app(confoverrides={'extensions': 'sphinx.ext.intersphinx'})
|
|
|
|
@with_tempdir
|
|
|
|
def test_missing_reference(tempdir, app):
|
|
|
|
inv_file = tempdir / 'inventory'
|
|
|
|
write_file(inv_file, inventory_v2)
|
2010-07-27 06:20:58 -05:00
|
|
|
app.config.intersphinx_mapping = {
|
|
|
|
'http://docs.python.org/': inv_file,
|
|
|
|
'py3k': ('http://docs.python.org/py3k/', inv_file),
|
|
|
|
}
|
2009-09-09 14:56:53 -05:00
|
|
|
app.config.intersphinx_cache_limit = 0
|
|
|
|
|
|
|
|
# load the inventory and check if it's done correctly
|
|
|
|
load_mappings(app)
|
|
|
|
inv = app.env.intersphinx_inventory
|
|
|
|
|
|
|
|
assert inv['py:module']['module2'] == \
|
2010-05-23 06:06:01 -05:00
|
|
|
('foo', '2.0', 'http://docs.python.org/foo.html#module-module2', '-')
|
2009-09-09 14:56:53 -05:00
|
|
|
|
|
|
|
# create fake nodes and check referencing
|
|
|
|
|
2010-08-05 04:58:43 -05:00
|
|
|
def fake_node(domain, type, target, content, **attrs):
|
|
|
|
contnode = nodes.emphasis(content, content)
|
|
|
|
node = addnodes.pending_xref('')
|
|
|
|
node['reftarget'] = target
|
|
|
|
node['reftype'] = type
|
|
|
|
node['refdomain'] = domain
|
|
|
|
node.attributes.update(attrs)
|
|
|
|
node += contnode
|
|
|
|
return node, contnode
|
|
|
|
|
|
|
|
def reference_check(*args, **kwds):
|
|
|
|
node, contnode = fake_node(*args, **kwds)
|
|
|
|
return missing_reference(app, app.env, node, contnode)
|
|
|
|
|
|
|
|
# check resolution when a target is found
|
|
|
|
rn = reference_check('py', 'func', 'module1.func', 'foo')
|
2009-09-09 14:56:53 -05:00
|
|
|
assert isinstance(rn, nodes.reference)
|
|
|
|
assert rn['refuri'] == 'http://docs.python.org/sub/foo.html#module1.func'
|
|
|
|
assert rn['reftitle'] == '(in foo v2.0)'
|
2010-08-05 04:58:43 -05:00
|
|
|
assert rn[0].astext() == 'foo'
|
2009-09-09 14:56:53 -05:00
|
|
|
|
|
|
|
# create unresolvable nodes and check None return value
|
2010-08-05 04:58:43 -05:00
|
|
|
assert reference_check('py', 'foo', 'module1.func', 'foo') is None
|
|
|
|
assert reference_check('py', 'func', 'foo', 'foo') is None
|
|
|
|
assert reference_check('py', 'func', 'foo', 'foo') is None
|
2010-07-27 06:20:58 -05:00
|
|
|
|
|
|
|
# check handling of prefixes
|
|
|
|
|
|
|
|
# prefix given, target found: prefix is stripped
|
2010-08-05 04:58:43 -05:00
|
|
|
rn = reference_check('py', 'mod', 'py3k:module2', 'py3k:module2')
|
|
|
|
assert rn[0].astext() == 'module2'
|
|
|
|
|
|
|
|
# prefix given, but not in title: nothing stripped
|
|
|
|
rn = reference_check('py', 'mod', 'py3k:module2', 'module2')
|
2010-07-27 06:20:58 -05:00
|
|
|
assert rn[0].astext() == 'module2'
|
|
|
|
|
2010-08-05 04:58:43 -05:00
|
|
|
# prefix given, but explicit: nothing stripped
|
|
|
|
rn = reference_check('py', 'mod', 'py3k:module2', 'py3k:module2',
|
|
|
|
refexplicit=True)
|
|
|
|
assert rn[0].astext() == 'py3k:module2'
|
|
|
|
|
2010-07-27 06:20:58 -05:00
|
|
|
# prefix given, target not found and nonexplicit title: prefix is stripped
|
2010-08-05 04:58:43 -05:00
|
|
|
node, contnode = fake_node('py', 'mod', 'py3k:unknown', 'py3k:unknown',
|
|
|
|
refexplicit=False)
|
|
|
|
rn = missing_reference(app, app.env, node, contnode)
|
2010-07-27 06:20:58 -05:00
|
|
|
assert rn is None
|
|
|
|
assert contnode[0].astext() == 'unknown'
|
|
|
|
|
|
|
|
# prefix given, target not found and explicit title: nothing is changed
|
2010-08-05 04:58:43 -05:00
|
|
|
node, contnode = fake_node('py', 'mod', 'py3k:unknown', 'py3k:unknown',
|
|
|
|
refexplicit=True)
|
|
|
|
rn = missing_reference(app, app.env, node, contnode)
|
2010-07-27 06:20:58 -05:00
|
|
|
assert rn is None
|
|
|
|
assert contnode[0].astext() == 'py3k:unknown'
|