Mercurial > piecrust2
annotate tests/test_baking_baker.py @ 380:f33712c4cfab
routing: Fix bugs with matching URLs with correct route but missing metadata.
When matching a route like `/foo/%slug%` against an URL like `/foo`, the route
will (correctly) return a match, but it will be completely missing the `slug`
metadata, resulting in problems elsewhere. This change makes it so that any
missing route metadata will be filled in with an empty string.
And because this means generated URLs may differ from the incoming URL when
using trailing slashes (`/foo/` _vs._ `/foo`), we make the assert in the
chef server handle those discrepancies.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 10 May 2015 00:34:21 -0700 |
parents | 76c838453dbe |
children | e7b865f8f335 |
rev | line source |
---|---|
286
a2d283d1033d
tests: Fixes for running on Windows.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
1 import time |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
2 import os.path |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 import pytest |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
4 from piecrust.baking.baker import PageBaker, Baker |
120
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
5 from piecrust.baking.records import BakeRecord |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
6 from .mockutil import get_mock_app, mock_fs, mock_fs_scope |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 |
262
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
9 @pytest.mark.parametrize('uri, pretty, expected', [ |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 # Pretty URLs |
262
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
11 ('', True, 'index.html'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
12 ('2', True, '2/index.html'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
13 ('foo', True, 'foo/index.html'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
14 ('foo/2', True, 'foo/2/index.html'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
15 ('foo/bar', True, 'foo/bar/index.html'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
16 ('foo/bar/2', True, 'foo/bar/2/index.html'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
17 ('foo.ext', True, 'foo.ext/index.html'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
18 ('foo.ext/2', True, 'foo.ext/2/index.html'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
19 ('foo/bar.ext', True, 'foo/bar.ext/index.html'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
20 ('foo/bar.ext/2', True, 'foo/bar.ext/2/index.html'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
21 ('foo.bar.ext', True, 'foo.bar.ext/index.html'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
22 ('foo.bar.ext/2', True, 'foo.bar.ext/2/index.html'), |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 # Ugly URLs |
262
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
24 ('', False, 'index.html'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
25 ('2.html', False, '2.html'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
26 ('foo.html', False, 'foo.html'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
27 ('foo/2.html', False, 'foo/2.html'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
28 ('foo/bar.html', False, 'foo/bar.html'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
29 ('foo/bar/2.html', False, 'foo/bar/2.html'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
30 ('foo.ext', False, 'foo.ext'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
31 ('foo/2.ext', False, 'foo/2.ext'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
32 ('foo/bar.ext', False, 'foo/bar.ext'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
33 ('foo/bar/2.ext', False, 'foo/bar/2.ext'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
34 ('foo.bar.ext', False, 'foo.bar.ext'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
35 ('foo.bar/2.ext', False, 'foo.bar/2.ext') |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 ]) |
262
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
145
diff
changeset
|
37 def test_get_output_path(uri, pretty, expected): |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
38 app = get_mock_app() |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 if pretty: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 app.config.set('site/pretty_urls', True) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 assert app.config.get('site/pretty_urls') == pretty |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
42 |
329
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
324
diff
changeset
|
43 for site_root in ['/', '/whatever/']: |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
324
diff
changeset
|
44 app.config.set('site/root', site_root) |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
324
diff
changeset
|
45 baker = PageBaker(app, '/destination') |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
324
diff
changeset
|
46 path = baker.getOutputPath(site_root + uri) |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
324
diff
changeset
|
47 expected = os.path.normpath( |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
324
diff
changeset
|
48 os.path.join('/destination', expected)) |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
324
diff
changeset
|
49 assert expected == path |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
51 |
120
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
52 def test_removed(): |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
53 fs = (mock_fs() |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
54 .withPage('pages/foo.md', {'layout': 'none', 'format': 'none'}, 'a foo page') |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
55 .withPage('pages/_index.md', {'layout': 'none', 'format': 'none'}, "something")) |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
56 with mock_fs_scope(fs): |
145 | 57 out_dir = fs.path('kitchen/_counter') |
120
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
58 app = fs.getApp() |
145 | 59 baker = Baker(app, out_dir) |
120
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
60 baker.bake() |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
61 structure = fs.getStructure('kitchen/_counter') |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
62 assert structure == { |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
63 'foo.html': 'a foo page', |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
64 'index.html': 'something'} |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
65 |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
66 os.remove(fs.path('kitchen/pages/foo.md')) |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
67 app = fs.getApp() |
145 | 68 baker = Baker(app, out_dir) |
120
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
69 baker.bake() |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
70 structure = fs.getStructure('kitchen/_counter') |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
71 assert structure == { |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
72 'index.html': 'something'} |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
73 |
324
65e6d72f3877
bake/serve: Fix how taxonomy index pages are setup and rendered.
Ludovic Chabant <ludovic@chabant.com>
parents:
286
diff
changeset
|
74 |
120
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
75 def test_record_version_change(): |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
76 fs = (mock_fs() |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
77 .withPage('pages/foo.md', {'layout': 'none', 'format': 'none'}, 'a foo page')) |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
78 with mock_fs_scope(fs): |
145 | 79 out_dir = fs.path('kitchen/_counter') |
120
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
80 app = fs.getApp() |
145 | 81 baker = Baker(app, out_dir) |
120
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
82 baker.bake() |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
83 mtime = os.path.getmtime(fs.path('kitchen/_counter/foo.html')) |
286
a2d283d1033d
tests: Fixes for running on Windows.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
84 time.sleep(1) |
120
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
85 |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
86 app = fs.getApp() |
145 | 87 baker = Baker(app, out_dir) |
120
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
88 baker.bake() |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
89 assert mtime == os.path.getmtime(fs.path('kitchen/_counter/foo.html')) |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
90 |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
91 BakeRecord.RECORD_VERSION += 1 |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
92 try: |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
93 app = fs.getApp() |
145 | 94 baker = Baker(app, out_dir) |
120
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
95 baker.bake() |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
96 assert mtime < os.path.getmtime(fs.path('kitchen/_counter/foo.html')) |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
97 finally: |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
98 BakeRecord.RECORD_VERSION -= 1 |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
99 |