changeset 674:f987b29d6fab

tests: Add ability to run tests with a theme site. * Remove automatic creating of `config.yml`. * Add option to specify `theme_config` in YAML tests.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 06 Mar 2016 23:48:01 -0800
parents d6403c21bdea
children 3df808b133f8
files tests/basefs.py tests/conftest.py tests/memfs.py tests/test_baking_baker.py tests/test_data_assetor.py tests/test_data_linker.py tests/test_data_provider.py tests/test_processing_base.py tests/test_serving.py tests/test_sources_base.py tests/tmpfs.py
diffstat 11 files changed, 60 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/tests/basefs.py	Sun Mar 06 23:41:35 2016 -0800
+++ b/tests/basefs.py	Sun Mar 06 23:48:01 2016 -0800
@@ -7,12 +7,6 @@
     def __init__(self):
         pass
 
-    def _initDefaultSpec(self):
-        self.withDir('counter')
-        self.withFile(
-                'kitchen/config.yml',
-                "site:\n  title: Mock Website\n")
-
     def path(self, p):
         raise NotImplementedError()
 
@@ -28,9 +22,10 @@
     def _createFile(self, path, contents):
         raise NotImplementedError()
 
-    def getApp(self, cache=True):
+    def getApp(self, *, cache=True, theme_site=False):
         root_dir = self.path('/kitchen')
-        return PieCrust(root_dir, cache=cache, debug=True)
+        return PieCrust(root_dir, cache=cache, debug=True,
+                        theme_site=theme_site)
 
     def withDir(self, path):
         path = self.path(path)
@@ -48,14 +43,16 @@
     def withAssetDir(self, path):
         return self.withDir('kitchen/' + path)
 
-    def withConfig(self, config):
+    def withConfig(self, config=None):
+        if config is None:
+            config = {}
         return self.withFile(
                 'kitchen/config.yml',
                 yaml.dump(config))
 
     def withThemeConfig(self, config):
         return self.withFile(
-                'kitchen/theme/theme_config.yml',
+                'kitchen/theme_config.yml',
                 yaml.dump(config))
 
     def withPage(self, url, config=None, contents=None):
--- a/tests/conftest.py	Sun Mar 06 23:41:35 2016 -0800
+++ b/tests/conftest.py	Sun Mar 06 23:48:01 2016 -0800
@@ -76,23 +76,35 @@
         self.spec = spec
 
     @property
+    def is_theme_site(self):
+        return self.spec.get('theme_config') is not None
+
+    @property
     def mock_debug(self):
         return bool(self.config.getoption('--mock-debug'))
 
     def _prepareMockFs(self):
         fs = mock_fs()
 
-        # Website config.
+        # Suppress any formatting or layout so we can compare
+        # much simpler strings.
         config = {
                 'site': {
                     'default_format': 'none',
                     'default_page_layout': 'none',
                     'default_post_layout': 'none'}
                 }
-        test_config = self.spec.get('config')
-        if test_config is not None:
-            merge_dicts(config, test_config)
-        fs.withConfig(config)
+
+        # Website or theme config.
+        test_theme_config = self.spec.get('theme_config')
+        if test_theme_config is not None:
+            merge_dicts(config, test_theme_config)
+            fs.withThemeConfig(config)
+        else:
+            test_config = self.spec.get('config')
+            if test_config is not None:
+                merge_dicts(config, test_config)
+            fs.withConfig(config)
 
         # Input file-system.
         input_files = self.spec.get('in')
@@ -181,6 +193,9 @@
         argv = self.spec['args']
         if isinstance(argv, str):
             argv = argv.split(' ')
+        if self.is_theme_site:
+            argv.insert(0, '--theme')
+        argv = ['--root', fs.path('/kitchen')] + argv
 
         expected_code = self.spec.get('code', 0)
         expected_out = self.spec.get('out', None)
@@ -191,8 +206,7 @@
             logging.getLogger().addHandler(hdl)
             try:
                 from piecrust.main import _pre_parse_chef_args, _run_chef
-                pre_args = _pre_parse_chef_args([
-                        '--root', fs.path('/kitchen')])
+                pre_args = _pre_parse_chef_args(argv)
                 exit_code = _run_chef(pre_args, argv)
             finally:
                 logging.getLogger().removeHandler(hdl)
@@ -232,7 +246,7 @@
         from piecrust.baking.baker import Baker
         with mock_fs_scope(fs, keep=self.mock_debug):
             out_dir = fs.path('kitchen/_counter')
-            app = fs.getApp()
+            app = fs.getApp(theme_site=self.is_theme_site)
 
             variant = self.spec.get('config_variant')
             values = self.spec.get('config_values')
@@ -290,7 +304,7 @@
         from piecrust.processing.pipeline import ProcessorPipeline
         with mock_fs_scope(fs, keep=self.mock_debug):
             out_dir = fs.path('kitchen/_counter')
-            app = fs.getApp()
+            app = fs.getApp(theme_site=self.is_theme_site)
             pipeline = ProcessorPipeline(app, out_dir)
 
             proc_names = self.spec.get('processors')
@@ -363,7 +377,9 @@
         from piecrust.app import PieCrustFactory
         from piecrust.serving.server import Server
         with mock_fs_scope(fs, keep=self.mock_debug):
-            appfactory = PieCrustFactory(fs.path('/kitchen'))
+            appfactory = PieCrustFactory(
+                    fs.path('/kitchen'),
+                    theme_site=self.is_theme_site)
             server = Server(appfactory)
             test_app = self._TestApp(server)
             client = Client(test_app, BaseResponse)
--- a/tests/memfs.py	Sun Mar 06 23:41:35 2016 -0800
+++ b/tests/memfs.py	Sun Mar 06 23:48:01 2016 -0800
@@ -46,11 +46,9 @@
 
 
 class MemoryFileSystem(TestFileSystemBase):
-    def __init__(self, default_spec=True):
+    def __init__(self):
         self._root = 'root_%d' % random.randrange(1000)
         self._fs = {self._root: {}}
-        if default_spec:
-            self._initDefaultSpec()
 
     def path(self, p):
         p = p.replace('\\', '/')
--- a/tests/test_baking_baker.py	Sun Mar 06 23:41:35 2016 -0800
+++ b/tests/test_baking_baker.py	Sun Mar 06 23:48:01 2016 -0800
@@ -53,6 +53,7 @@
 
 def test_removed():
     fs = (mock_fs()
+            .withConfig()
             .withPage('pages/foo.md', {'layout': 'none', 'format': 'none'}, 'a foo page')
             .withPage('pages/_index.md', {'layout': 'none', 'format': 'none'}, "something"))
     with mock_fs_scope(fs):
@@ -77,6 +78,7 @@
 
 def test_record_version_change():
     fs = (mock_fs()
+            .withConfig()
             .withPage('pages/foo.md', {'layout': 'none', 'format': 'none'}, 'a foo page'))
     with mock_fs_scope(fs):
         out_dir = fs.path('kitchen/_counter')
--- a/tests/test_data_assetor.py	Sun Mar 06 23:41:35 2016 -0800
+++ b/tests/test_data_assetor.py	Sun Mar 06 23:48:01 2016 -0800
@@ -50,7 +50,9 @@
 
 def test_missing_asset():
     with pytest.raises(KeyError):
-        fs = mock_fs().withPage('pages/foo/bar')
+        fs = (mock_fs()
+                .withConfig()
+                .withPage('pages/foo/bar'))
         with mock_fs_scope(fs):
             page = MagicMock()
             page.app = fs.getApp(cache=False)
@@ -62,6 +64,7 @@
 def test_multiple_assets_with_same_name():
     with pytest.raises(UnsupportedAssetsError):
         fs = (mock_fs()
+                .withConfig()
                 .withPage('pages/foo/bar')
                 .withPageAsset('pages/foo/bar', 'one.txt', 'one text')
                 .withPageAsset('pages/foo/bar', 'one.jpg', 'one picture'))
--- a/tests/test_data_linker.py	Sun Mar 06 23:41:35 2016 -0800
+++ b/tests/test_data_linker.py	Sun Mar 06 23:48:01 2016 -0800
@@ -37,6 +37,7 @@
     ])
 def test_linker_iteration(fs_fac, page_path, expected):
     fs = fs_fac()
+    fs.withConfig()
     with mock_fs_scope(fs):
         app = fs.getApp()
         app.config.set('site/pretty_urls', True)
@@ -83,6 +84,7 @@
         ])
 def test_recursive_linker_iteration(fs_fac, page_path, expected):
     fs = fs_fac()
+    fs.withConfig()
     with mock_fs_scope(fs):
         app = fs.getApp()
         app.config.set('site/pretty_urls', True)
--- a/tests/test_data_provider.py	Sun Mar 06 23:41:35 2016 -0800
+++ b/tests/test_data_provider.py	Sun Mar 06 23:48:01 2016 -0800
@@ -4,6 +4,7 @@
 
 def test_blog_provider():
     fs = (mock_fs()
+          .withConfig()
           .withPage('posts/2015-03-01_one.md',
                     {'title': 'One', 'category': 'Foo'})
           .withPage('posts/2015-03-02_two.md',
--- a/tests/test_processing_base.py	Sun Mar 06 23:41:35 2016 -0800
+++ b/tests/test_processing_base.py	Sun Mar 06 23:48:01 2016 -0800
@@ -42,7 +42,9 @@
 
 
 def test_empty():
-    fs = mock_fs()
+    fs = (mock_fs()
+            .withDir('counter')
+            .withConfig())
     with mock_fs_scope(fs):
         pp = _get_pipeline(fs)
         pp.enabled_processors = ['copy']
@@ -55,6 +57,8 @@
 
 def test_one_file():
     fs = (mock_fs()
+            .withDir('counter')
+            .withConfig()
             .withFile('kitchen/assets/something.html', 'A test file.'))
     with mock_fs_scope(fs):
         pp = _get_pipeline(fs)
@@ -68,6 +72,7 @@
 
 def test_one_level_dirtyness():
     fs = (mock_fs()
+            .withConfig()
             .withFile('kitchen/assets/blah.foo', 'A test file.'))
     with mock_fs_scope(fs):
         pp = _get_pipeline(fs)
@@ -93,6 +98,7 @@
 
 def test_two_levels_dirtyness():
     fs = (mock_fs()
+            .withConfig()
             .withFile('kitchen/assets/blah.foo', 'A test file.'))
     with mock_fs_scope(fs):
         pp = _get_pipeline(fs)
@@ -120,6 +126,7 @@
 
 def test_removed():
     fs = (mock_fs()
+            .withConfig()
             .withFile('kitchen/assets/blah1.foo', 'A test file.')
             .withFile('kitchen/assets/blah2.foo', 'Ooops'))
     with mock_fs_scope(fs):
@@ -143,6 +150,7 @@
 
 def test_record_version_change():
     fs = (mock_fs()
+            .withConfig()
             .withFile('kitchen/assets/blah.foo', 'A test file.'))
     with mock_fs_scope(fs):
         pp = _get_pipeline(fs)
@@ -177,6 +185,8 @@
         ])
 def test_ignore_pattern(patterns, expected):
     fs = (mock_fs()
+            .withDir('counter')
+            .withConfig()
             .withFile('kitchen/assets/something.html', 'A test file.')
             .withFile('kitchen/assets/_hidden.html', 'Shhh')
             .withFile('kitchen/assets/foo/_important.html', 'Important!'))
@@ -201,7 +211,7 @@
         ('less sass', ['less', 'sass'])
     ])
 def test_filter_processor(names, expected):
-    fs = mock_fs()
+    fs = mock_fs().withConfig()
     with mock_fs_scope(fs):
         app = fs.getApp()
         processors = app.plugin_loader.getProcessors()
--- a/tests/test_serving.py	Sun Mar 06 23:41:35 2016 -0800
+++ b/tests/test_serving.py	Sun Mar 06 23:48:01 2016 -0800
@@ -65,6 +65,7 @@
         return c
 
     fs = (mock_fs()
+          .withConfig()
           .withPages(8, 'posts/2015-03-{idx1:02}_post{idx1:02}.md',
                      config_factory)
           .withPage('pages/_tag.md', {'layout': 'none', 'format': 'none'},
@@ -108,6 +109,7 @@
         return c
 
     fs = (mock_fs()
+          .withConfig()
           .withPages(6, 'posts/2015-03-{idx1:02}_post{idx1:02}.md',
                      config_factory)
           .withPage('pages/_category.md', {'layout': 'none', 'format': 'none'},
--- a/tests/test_sources_base.py	Sun Mar 06 23:41:35 2016 -0800
+++ b/tests/test_sources_base.py	Sun Mar 06 23:48:01 2016 -0800
@@ -127,7 +127,7 @@
 
 
 def test_page_ref_with_missing_source():
-    fs = mock_fs()
+    fs = mock_fs().withConfig()
     with mock_fs_scope(fs):
         app = fs.getApp()
         r = PageRef(app, 'whatever:doesnt_exist.md')
@@ -136,7 +136,7 @@
 
 
 def test_page_ref_with_missing_file():
-    fs = mock_fs()
+    fs = mock_fs().withConfig()
     with mock_fs_scope(fs):
         app = fs.getApp()
         r = PageRef(app, 'pages:doesnt_exist.%ext%')
--- a/tests/tmpfs.py	Sun Mar 06 23:41:35 2016 -0800
+++ b/tests/tmpfs.py	Sun Mar 06 23:48:01 2016 -0800
@@ -7,14 +7,12 @@
 
 
 class TempDirFileSystem(TestFileSystemBase):
-    def __init__(self, default_spec=True):
+    def __init__(self):
         self._root = os.path.join(
                 os.path.dirname(__file__),
                 '__tmpfs__',
                 '%d' % random.randrange(1000))
         self._done = False
-        if default_spec:
-            self._initDefaultSpec()
 
     def path(self, p):
         p = p.lstrip('/\\')