changeset 391:3e4bb57d8506

tests: Add support for testing the Chef server. * Make the server be functional without calling `getWsgiApp`. * Add new type of test files that use a Werkzeug test client.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 17 May 2015 10:48:41 -0700
parents 3a184fbc900b
children 923a9b96411d
files piecrust/serving/server.py tests/conftest.py tests/servings/test_debug_info.serve
diffstat 3 files changed, 79 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/serving/server.py	Sun May 17 08:30:19 2015 -0700
+++ b/piecrust/serving/server.py	Sun May 17 10:48:41 2015 -0700
@@ -12,6 +12,7 @@
 from werkzeug.wrappers import Request, Response
 from werkzeug.wsgi import ClosingIterator, wrap_file
 from jinja2 import FileSystemLoader, Environment
+from piecrust import CACHE_DIR
 from piecrust.app import PieCrust
 from piecrust.rendering import QualifiedPage, PageRenderingContext, render_page
 from piecrust.sources.base import MODE_PARSING
@@ -62,8 +63,8 @@
         self.enable_debug_info = enable_debug_info
         self.run_sse_check = run_sse_check
         self.static_preview = static_preview
-        self._out_dir = None
-        self._page_record = None
+        self._page_record = ServeRecord()
+        self._out_dir = os.path.join(root_dir, CACHE_DIR, 'server')
         self._proc_loop = None
         self._mimetype_map = load_mimetype_map()
 
@@ -71,9 +72,9 @@
         # Bake all the assets so we know what we have, and so we can serve
         # them to the client. We need a temp app for this.
         app = PieCrust(root_dir=self.root_dir, debug=self.debug)
-        app._useSubCacheDir(self.sub_cache_dir)
+        if self.sub_cache_dir:
+            app._useSubCacheDir(self.sub_cache_dir)
         self._out_dir = os.path.join(app.sub_cache_dir, 'server')
-        self._page_record = ServeRecord()
 
         if not self.run_sse_check or self.run_sse_check():
             # When using a server with code reloading, some implementations
@@ -120,7 +121,8 @@
 
         # Create the app for this request.
         app = PieCrust(root_dir=self.root_dir, debug=self.debug)
-        app._useSubCacheDir(self.sub_cache_dir)
+        if self.sub_cache_dir:
+            app._useSubCacheDir(self.sub_cache_dir)
         app.config.set('site/root', '/')
         app.config.set('server/is_serving', True)
         if (app.config.get('site/enable_debug_info') and
--- a/tests/conftest.py	Sun May 17 08:30:19 2015 -0700
+++ b/tests/conftest.py	Sun May 17 10:48:41 2015 -0700
@@ -29,10 +29,13 @@
 
 
 def pytest_collect_file(parent, path):
-    if path.ext == ".bake" and path.basename.startswith("test"):
-        return BakeTestFile(path, parent)
-    elif path.ext == ".chef" and path.basename.startswith("test"):
-        return ChefTestFile(path, parent)
+    if path.basename.startswith("test"):
+        if path.ext == ".bake":
+            return BakeTestFile(path, parent)
+        elif path.ext == ".chef":
+            return ChefTestFile(path, parent)
+        elif path.ext == ".serve":
+            return ServeTestFile(path, parent)
 
 
 class YamlTestFileBase(pytest.File):
@@ -189,6 +192,55 @@
     __item_class__ = BakeTestItem
 
 
+class ServeTestItem(YamlTestItemBase):
+    class _TestApp(object):
+        def __init__(self, server):
+            self.server = server
+
+        def __call__(self, environ, start_response):
+            return self.server._try_run_request(environ, start_response)
+
+    def runtest(self):
+        fs = self._prepareMockFs()
+
+        url = self.spec.get('url')
+        if url is None:
+            raise Exception("Missing URL in test spec.")
+
+        expected_status = self.spec.get('status', 200)
+        expected_headers = self.spec.get('headers')
+        expected_output = self.spec.get('out')
+        expected_contains = self.spec.get('out_contains')
+
+        from werkzeug.test import Client
+        from werkzeug.wrappers import BaseResponse
+        from piecrust.serving.server import Server
+        with mock_fs_scope(fs):
+            server = Server(fs.path('/kitchen'))
+            test_app = self._TestApp(server)
+            client = Client(test_app, BaseResponse)
+            resp = client.get(url)
+            assert expected_status == resp.status_code
+
+            if expected_headers:
+                for k, v in expected_headers.items():
+                    assert v == resp.headers.get(k)
+
+            actual = resp.data.decode('utf8').rstrip()
+            if expected_output:
+                assert expected_output.rstrip() == actual
+
+            if expected_contains:
+                assert expected_contains.rstrip() in actual
+
+    def reportinfo(self):
+        return self.fspath, 0, "serve: %s" % self.name
+
+
+class ServeTestFile(YamlTestFileBase):
+    __item_class__ = ServeTestItem
+
+
 def _add_mock_files(fs, parent_path, spec):
     for name, subspec in spec.items():
         path = os.path.join(parent_path, name)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/servings/test_debug_info.serve	Sun May 17 10:48:41 2015 -0700
@@ -0,0 +1,16 @@
+---
+url: /foo
+in: 
+    pages/foo.md: |
+        BLAH
+        {{piecrust.debug_info}}
+out: BLAH
+---
+url: /foo?!debug
+in:
+    pages/foo.md: |
+        BLAH
+        {{piecrust.debug_info}}
+out_contains: |
+    BLAH
+    <div id="piecrust-debug-info"