Mercurial > piecrust2
comparison tests/test_sources_autoconfig.py @ 411:e7b865f8f335
bake: Enable multiprocess baking.
Baking is now done by running a worker per CPU, and sending jobs to them.
This changes several things across the codebase:
* Ability to not cache things related to pages other than the 'main' page
(i.e. the page at the bottom of the execution stack).
* Decouple the baking process from the bake records, so only the main process
keeps track (and modifies) the bake record.
* Remove the need for 'batch page getters' and loading a page directly from
the page factories.
There are various smaller changes too included here, including support for
scope performance timers that are saved with the bake record and can be
printed out to the console. Yes I got carried away.
For testing, the in-memory 'mock' file-system doesn't work anymore, since
we're spawning processes, so this is replaced by a 'tmpfs' file-system which
is saved in temporary files on disk and deleted after tests have run.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Fri, 12 Jun 2015 17:09:19 -0700 |
parents | dd25bd3ce1f9 |
children | 72f17534d58e |
comparison
equal
deleted
inserted
replaced
410:d1a472464e57 | 411:e7b865f8f335 |
---|---|
3 from .mockutil import mock_fs, mock_fs_scope | 3 from .mockutil import mock_fs, mock_fs_scope |
4 from .pathutil import slashfix | 4 from .pathutil import slashfix |
5 | 5 |
6 | 6 |
7 @pytest.mark.parametrize( | 7 @pytest.mark.parametrize( |
8 'fs, src_config, expected_paths, expected_metadata', | 8 'fs_fac, src_config, expected_paths, expected_metadata', |
9 [ | 9 [ |
10 (mock_fs(), {}, [], []), | 10 (lambda: mock_fs(), {}, [], []), |
11 (mock_fs().withPage('test/_index.md'), | 11 (lambda: mock_fs().withPage('test/_index.md'), |
12 {}, | 12 {}, |
13 ['_index.md'], | 13 ['_index.md'], |
14 [{'slug': '', 'config': {'foo': []}}]), | 14 [{'slug': '', 'config': {'foo': []}}]), |
15 (mock_fs().withPage('test/something.md'), | 15 (lambda: mock_fs().withPage('test/something.md'), |
16 {}, | 16 {}, |
17 ['something.md'], | 17 ['something.md'], |
18 [{'slug': 'something', 'config': {'foo': []}}]), | 18 [{'slug': 'something', 'config': {'foo': []}}]), |
19 (mock_fs().withPage('test/bar/something.md'), | 19 (lambda: mock_fs().withPage('test/bar/something.md'), |
20 {}, | 20 {}, |
21 ['bar/something.md'], | 21 ['bar/something.md'], |
22 [{'slug': 'something', 'config': {'foo': ['bar']}}]), | 22 [{'slug': 'something', 'config': {'foo': ['bar']}}]), |
23 (mock_fs().withPage('test/bar1/bar2/something.md'), | 23 (lambda: mock_fs().withPage('test/bar1/bar2/something.md'), |
24 {}, | 24 {}, |
25 ['bar1/bar2/something.md'], | 25 ['bar1/bar2/something.md'], |
26 [{'slug': 'something', 'config': {'foo': ['bar1', 'bar2']}}]), | 26 [{'slug': 'something', 'config': {'foo': ['bar1', 'bar2']}}]), |
27 | 27 |
28 (mock_fs().withPage('test/something.md'), | 28 (lambda: mock_fs().withPage('test/something.md'), |
29 {'collapse_single_values': True}, | 29 {'collapse_single_values': True}, |
30 ['something.md'], | 30 ['something.md'], |
31 [{'slug': 'something', 'config': {'foo': None}}]), | 31 [{'slug': 'something', 'config': {'foo': None}}]), |
32 (mock_fs().withPage('test/bar/something.md'), | 32 (lambda: mock_fs().withPage('test/bar/something.md'), |
33 {'collapse_single_values': True}, | 33 {'collapse_single_values': True}, |
34 ['bar/something.md'], | 34 ['bar/something.md'], |
35 [{'slug': 'something', 'config': {'foo': 'bar'}}]), | 35 [{'slug': 'something', 'config': {'foo': 'bar'}}]), |
36 (mock_fs().withPage('test/bar1/bar2/something.md'), | 36 (lambda: mock_fs().withPage('test/bar1/bar2/something.md'), |
37 {'collapse_single_values': True}, | 37 {'collapse_single_values': True}, |
38 ['bar1/bar2/something.md'], | 38 ['bar1/bar2/something.md'], |
39 [{'slug': 'something', 'config': {'foo': ['bar1', 'bar2']}}]), | 39 [{'slug': 'something', 'config': {'foo': ['bar1', 'bar2']}}]), |
40 | 40 |
41 (mock_fs().withPage('test/something.md'), | 41 (lambda: mock_fs().withPage('test/something.md'), |
42 {'only_single_values': True}, | 42 {'only_single_values': True}, |
43 ['something.md'], | 43 ['something.md'], |
44 [{'slug': 'something', 'config': {'foo': None}}]), | 44 [{'slug': 'something', 'config': {'foo': None}}]), |
45 (mock_fs().withPage('test/bar/something.md'), | 45 (lambda: mock_fs().withPage('test/bar/something.md'), |
46 {'only_single_values': True}, | 46 {'only_single_values': True}, |
47 ['bar/something.md'], | 47 ['bar/something.md'], |
48 [{'slug': 'something', 'config': {'foo': 'bar'}}]), | 48 [{'slug': 'something', 'config': {'foo': 'bar'}}]), |
49 ]) | 49 ]) |
50 def test_autoconfig_source_factories(fs, src_config, expected_paths, | 50 def test_autoconfig_source_factories(fs_fac, src_config, expected_paths, |
51 expected_metadata): | 51 expected_metadata): |
52 site_config = { | 52 site_config = { |
53 'sources': { | 53 'sources': { |
54 'test': {'type': 'autoconfig', | 54 'test': {'type': 'autoconfig', |
55 'setting_name': 'foo'} | 55 'setting_name': 'foo'} |
56 }, | 56 }, |
57 'routes': [ | 57 'routes': [ |
58 {'url': '/%slug%', 'source': 'test'}] | 58 {'url': '/%slug%', 'source': 'test'}] |
59 } | 59 } |
60 site_config['sources']['test'].update(src_config) | 60 site_config['sources']['test'].update(src_config) |
61 fs = fs_fac() | |
61 fs.withConfig({'site': site_config}) | 62 fs.withConfig({'site': site_config}) |
62 fs.withDir('kitchen/test') | 63 fs.withDir('kitchen/test') |
63 with mock_fs_scope(fs): | 64 with mock_fs_scope(fs): |
64 app = fs.getApp() | 65 app = fs.getApp() |
65 s = app.getSource('test') | 66 s = app.getSource('test') |
86 with pytest.raises(Exception): | 87 with pytest.raises(Exception): |
87 list(s.buildPageFactories()) | 88 list(s.buildPageFactories()) |
88 | 89 |
89 | 90 |
90 @pytest.mark.parametrize( | 91 @pytest.mark.parametrize( |
91 'fs, expected_paths, expected_metadata', | 92 'fs_fac, expected_paths, expected_metadata', |
92 [ | 93 [ |
93 (mock_fs(), [], []), | 94 (lambda: mock_fs(), [], []), |
94 (mock_fs().withPage('test/_index.md'), | 95 (lambda: mock_fs().withPage('test/_index.md'), |
95 ['_index.md'], | 96 ['_index.md'], |
96 [{'slug': '', | 97 [{'slug': '', |
97 'config': {'foo': 0, 'foo_trail': [0]}}]), | 98 'config': {'foo': 0, 'foo_trail': [0]}}]), |
98 (mock_fs().withPage('test/something.md'), | 99 (lambda: mock_fs().withPage('test/something.md'), |
99 ['something.md'], | 100 ['something.md'], |
100 [{'slug': 'something', | 101 [{'slug': 'something', |
101 'config': {'foo': 0, 'foo_trail': [0]}}]), | 102 'config': {'foo': 0, 'foo_trail': [0]}}]), |
102 (mock_fs().withPage('test/08_something.md'), | 103 (lambda: mock_fs().withPage('test/08_something.md'), |
103 ['08_something.md'], | 104 ['08_something.md'], |
104 [{'slug': 'something', | 105 [{'slug': 'something', |
105 'config': {'foo': 8, 'foo_trail': [8]}}]), | 106 'config': {'foo': 8, 'foo_trail': [8]}}]), |
106 (mock_fs().withPage('test/02_there/08_something.md'), | 107 (lambda: mock_fs().withPage('test/02_there/08_something.md'), |
107 ['02_there/08_something.md'], | 108 ['02_there/08_something.md'], |
108 [{'slug': 'there/something', | 109 [{'slug': 'there/something', |
109 'config': {'foo': 8, 'foo_trail': [2, 8]}}]), | 110 'config': {'foo': 8, 'foo_trail': [2, 8]}}]), |
110 ]) | 111 ]) |
111 def test_ordered_source_factories(fs, expected_paths, expected_metadata): | 112 def test_ordered_source_factories(fs_fac, expected_paths, expected_metadata): |
112 site_config = { | 113 site_config = { |
113 'sources': { | 114 'sources': { |
114 'test': {'type': 'ordered', | 115 'test': {'type': 'ordered', |
115 'setting_name': 'foo'} | 116 'setting_name': 'foo'} |
116 }, | 117 }, |
117 'routes': [ | 118 'routes': [ |
118 {'url': '/%slug%', 'source': 'test'}] | 119 {'url': '/%slug%', 'source': 'test'}] |
119 } | 120 } |
121 fs = fs_fac() | |
120 fs.withConfig({'site': site_config}) | 122 fs.withConfig({'site': site_config}) |
121 fs.withDir('kitchen/test') | 123 fs.withDir('kitchen/test') |
122 with mock_fs_scope(fs): | 124 with mock_fs_scope(fs): |
123 app = fs.getApp() | 125 app = fs.getApp() |
124 s = app.getSource('test') | 126 s = app.getSource('test') |
128 metadata = [f.metadata for f in facs] | 130 metadata = [f.metadata for f in facs] |
129 assert metadata == expected_metadata | 131 assert metadata == expected_metadata |
130 | 132 |
131 | 133 |
132 @pytest.mark.parametrize( | 134 @pytest.mark.parametrize( |
133 'fs, route_path, expected_path, expected_metadata', | 135 'fs_fac, route_path, expected_path, expected_metadata', |
134 [ | 136 [ |
135 (mock_fs(), 'missing', None, None), | 137 (lambda: mock_fs(), 'missing', None, None), |
136 (mock_fs().withPage('test/something.md'), | 138 (lambda: mock_fs().withPage('test/something.md'), |
137 'something', 'something.md', | 139 'something', 'something.md', |
138 {'slug': 'something', | 140 {'slug': 'something', |
139 'config': {'foo': 0, 'foo_trail': [0]}}), | 141 'config': {'foo': 0, 'foo_trail': [0]}}), |
140 (mock_fs().withPage('test/bar/something.md'), | 142 (lambda: mock_fs().withPage('test/bar/something.md'), |
141 'bar/something', 'bar/something.md', | 143 'bar/something', 'bar/something.md', |
142 {'slug': 'bar/something', | 144 {'slug': 'bar/something', |
143 'config': {'foo': 0, 'foo_trail': [0]}}), | 145 'config': {'foo': 0, 'foo_trail': [0]}}), |
144 (mock_fs().withPage('test/42_something.md'), | 146 (lambda: mock_fs().withPage('test/42_something.md'), |
145 'something', '42_something.md', | 147 'something', '42_something.md', |
146 {'slug': 'something', | 148 {'slug': 'something', |
147 'config': {'foo': 42, 'foo_trail': [42]}}), | 149 'config': {'foo': 42, 'foo_trail': [42]}}), |
148 (mock_fs().withPage('test/bar/42_something.md'), | 150 (lambda: mock_fs().withPage('test/bar/42_something.md'), |
149 'bar/something', 'bar/42_something.md', | 151 'bar/something', 'bar/42_something.md', |
150 {'slug': 'bar/something', | 152 {'slug': 'bar/something', |
151 'config': {'foo': 42, 'foo_trail': [42]}}), | 153 'config': {'foo': 42, 'foo_trail': [42]}}), |
152 | 154 |
153 ((mock_fs() | 155 ((lambda: mock_fs() |
154 .withPage('test/42_something.md') | 156 .withPage('test/42_something.md') |
155 .withPage('test/43_other_something.md')), | 157 .withPage('test/43_other_something.md')), |
156 'something', '42_something.md', | 158 'something', '42_something.md', |
157 {'slug': 'something', | 159 {'slug': 'something', |
158 'config': {'foo': 42, 'foo_trail': [42]}}), | 160 'config': {'foo': 42, 'foo_trail': [42]}}), |
159 ]) | 161 ]) |
160 def test_ordered_source_find(fs, route_path, expected_path, | 162 def test_ordered_source_find(fs_fac, route_path, expected_path, |
161 expected_metadata): | 163 expected_metadata): |
162 site_config = { | 164 site_config = { |
163 'sources': { | 165 'sources': { |
164 'test': {'type': 'ordered', | 166 'test': {'type': 'ordered', |
165 'setting_name': 'foo'} | 167 'setting_name': 'foo'} |
166 }, | 168 }, |
167 'routes': [ | 169 'routes': [ |
168 {'url': '/%slug%', 'source': 'test'}] | 170 {'url': '/%slug%', 'source': 'test'}] |
169 } | 171 } |
172 fs = fs_fac() | |
170 fs.withConfig({'site': site_config}) | 173 fs.withConfig({'site': site_config}) |
171 fs.withDir('kitchen/test') | 174 fs.withDir('kitchen/test') |
172 with mock_fs_scope(fs): | 175 with mock_fs_scope(fs): |
173 app = fs.getApp() | 176 app = fs.getApp() |
174 s = app.getSource('test') | 177 s = app.getSource('test') |