Added test for build and made web support buildable without specifying search adapter

This commit is contained in:
Jacob Mason 2010-07-30 11:20:43 -05:00
parent 0039b09a36
commit f014641405
4 changed files with 85 additions and 10 deletions

View File

@ -10,7 +10,7 @@
"""
import cPickle as pickle
import re
import re, sys
from os import path
from cgi import escape
from difflib import Differ
@ -35,7 +35,7 @@ class WebSupport(object):
"""
def __init__(self, srcdir='', outdir='', datadir='', search=None,
storage=None):
storage=None, status=sys.stdout, warning=sys.stderr):
self.srcdir = srcdir
self.outdir = outdir or path.join(self.srcdir, '_build',
'websupport')
@ -43,9 +43,10 @@ class WebSupport(object):
self.outdir = outdir or datadir
if search is not None:
self._init_search(search)
self.status = status
self.warning = warning
self._init_search(search)
self._init_storage(storage)
def _init_storage(self, storage):
@ -73,11 +74,11 @@ class WebSupport(object):
if isinstance(search, BaseSearch):
self.search = search
else:
mod, cls = search_adapters[search]
search_class = getattr(__import__('sphinx.websupport.search.' + mod,
None, None, [cls]), cls)
mod, cls = search_adapters[search or 'null']
mod = 'sphinx.websupport.search.' + mod
SearchClass = getattr(__import__(mod, None, None, [cls]), cls)
search_path = path.join(self.outdir, 'search')
self.search = search_class(search_path)
self.search = SearchClass(search_path)
self.results_template = \
self.template_env.get_template('searchresults.html')
@ -95,8 +96,8 @@ class WebSupport(object):
doctreedir = path.join(self.outdir, 'doctrees')
app = WebSupportApp(self.srcdir, self.srcdir,
self.outdir, doctreedir, 'websupport',
search=self.search,
storage=self.storage)
search=self.search, status=self.status,
warning=self.warning, storage=self.storage)
self.storage.pre_build()
app.build()

View File

@ -12,6 +12,9 @@
import re
class BaseSearch(object):
def __init__(self, path):
pass
def init_indexing(self, changed=[]):
"""Called by the builder to initialize the search indexer. `changed`
is a list of pagenames that will be reindexed. You may want to remove
@ -117,4 +120,6 @@ class BaseSearch(object):
search_adapters = {
'xapian': ('xapiansearch', 'XapianSearch'),
'whoosh': ('whooshsearch', 'WhooshSearch'),
'null': ('nullsearch', 'NullSearch')
}

View File

@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
"""
sphinx.websupport.search.nullsearch
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The default search adapter, does nothing.
:copyright: Copyright 2007-2010 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from sphinx.websupport.search import BaseSearch
class NullSearchException(Exception):
pass
class NullSearch(BaseSearch):
def feed(self, pagename, title, doctree):
pass
def query(self, q):
raise NullSearchException('No search adapter specified.')

47
tests/test_websupport.py Normal file
View File

@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
"""
test_websupport
~~~~~~~~~~~~~~~
Test the Web Support Package
:copyright: Copyright 2007-2010 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
import os
from StringIO import StringIO
from sphinx.websupport import WebSupport
try:
from functools import wraps
except ImportError:
# functools is new in 2.4
wraps = lambda f: (lambda w: w)
from util import *
def teardown_module():
(test_root / 'websupport').rmtree(True)
def with_support(*args, **kwargs):
"""Make a WebSupport object and pass it the test."""
settings = {'srcdir': test_root,
'outdir': os.path.join(test_root, 'websupport'),
'status': StringIO(),
'warning': StringIO()}
settings.update(kwargs)
def generator(func):
@wraps(func)
def new_func(*args2, **kwargs2):
support = WebSupport(**settings)
func(support, *args2, **kwargs2)
return new_func
return generator
@with_support()
def test_build(support):
support.build()