comparison tests/basefs.py @ 974:72f17534d58e

tests: First pass on making unit tests work again. - Fix all imports - Add more helper functions to work with mock file-systems - Simplify some code by running chef directly on the mock FS - Fix a couple tests
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 17 Oct 2017 01:07:30 -0700
parents f987b29d6fab
children 45ad976712ec
comparison
equal deleted inserted replaced
973:8419daaa7a0e 974:72f17534d58e
1 import os.path 1 import os.path
2 import yaml 2 import yaml
3 from piecrust.app import PieCrust 3 from piecrust.app import PieCrust
4 from piecrust.main import _pre_parse_chef_args, _run_chef
5 from piecrust.sources.base import ContentItem
4 6
5 7
6 class TestFileSystemBase(object): 8 class TestFileSystemBase(object):
7 def __init__(self): 9 def __init__(self):
8 pass 10 pass
45 47
46 def withConfig(self, config=None): 48 def withConfig(self, config=None):
47 if config is None: 49 if config is None:
48 config = {} 50 config = {}
49 return self.withFile( 51 return self.withFile(
50 'kitchen/config.yml', 52 'kitchen/config.yml',
51 yaml.dump(config)) 53 yaml.dump(config))
52 54
53 def withThemeConfig(self, config): 55 def withThemeConfig(self, config):
54 return self.withFile( 56 return self.withFile(
55 'kitchen/theme_config.yml', 57 'kitchen/theme_config.yml',
56 yaml.dump(config)) 58 yaml.dump(config))
57 59
58 def withPage(self, url, config=None, contents=None): 60 def withPage(self, url, config=None, contents=None):
59 config = config or {} 61 config = config or {}
60 contents = contents or "A test page." 62 contents = contents or "A test page."
61 text = "---\n" 63 text = "---\n"
72 def withPageAsset(self, page_url, name, contents=None): 74 def withPageAsset(self, page_url, name, contents=None):
73 contents = contents or "A test asset." 75 contents = contents or "A test asset."
74 url_base, ext = os.path.splitext(page_url) 76 url_base, ext = os.path.splitext(page_url)
75 dirname = url_base + '-assets' 77 dirname = url_base + '-assets'
76 return self.withAsset( 78 return self.withAsset(
77 '%s/%s' % (dirname, name), contents) 79 '%s/%s' % (dirname, name), contents)
78 80
79 def withPages(self, num, url_factory, config_factory=None, 81 def withPages(self, num, url_factory, config_factory=None,
80 contents_factory=None): 82 contents_factory=None):
81 for i in range(num): 83 for i in range(num):
82 if isinstance(url_factory, str): 84 if isinstance(url_factory, str):
93 contents = contents_factory(i) 95 contents = contents_factory(i)
94 96
95 self.withPage(url, config, contents) 97 self.withPage(url, config, contents)
96 return self 98 return self
97 99
100 def runChef(self, *args):
101 root_dir = self.path('/kitchen')
102 chef_args = ['--root', root_dir] + args
103
104 pre_args = _pre_parse_chef_args(chef_args)
105 exit_code = _run_chef(pre_args, chef_args)
106 assert exit_code == 0
107
108 def getSimplePage(self, rel_path):
109 app = self.getApp()
110 source = app.getSource('pages')
111 content_item = ContentItem(
112 os.path.join(source.fs_endpoint_path, rel_path),
113 {'route_params': {
114 'slug': os.path.splitext(rel_path)[0]}})
115 return app.getPage(source, content_item)
116