From f014641405385f8477f221242274c188aab0e172 Mon Sep 17 00:00:00 2001 From: Jacob Mason Date: Fri, 30 Jul 2010 11:20:43 -0500 Subject: [PATCH] Added test for build and made web support buildable without specifying search adapter --- sphinx/websupport/__init__.py | 21 ++++++------ sphinx/websupport/search/__init__.py | 5 +++ sphinx/websupport/search/nullsearch.py | 22 ++++++++++++ tests/test_websupport.py | 47 ++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 sphinx/websupport/search/nullsearch.py create mode 100644 tests/test_websupport.py diff --git a/sphinx/websupport/__init__.py b/sphinx/websupport/__init__.py index 9d5f0cbac..4d73c0e10 100644 --- a/sphinx/websupport/__init__.py +++ b/sphinx/websupport/__init__.py @@ -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() diff --git a/sphinx/websupport/search/__init__.py b/sphinx/websupport/search/__init__.py index e1d7ea471..d41b560c3 100644 --- a/sphinx/websupport/search/__init__.py +++ b/sphinx/websupport/search/__init__.py @@ -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') } + diff --git a/sphinx/websupport/search/nullsearch.py b/sphinx/websupport/search/nullsearch.py new file mode 100644 index 000000000..ad3d7daef --- /dev/null +++ b/sphinx/websupport/search/nullsearch.py @@ -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.') diff --git a/tests/test_websupport.py b/tests/test_websupport.py new file mode 100644 index 000000000..d9251eb61 --- /dev/null +++ b/tests/test_websupport.py @@ -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() +