annotate tests/test_pipelines_page.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
children 45ad976712ec
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
974
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import time
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import os.path
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import urllib.parse
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 import pytest
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 from piecrust.pipelines.records import MultiRecord
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 from piecrust.pipelines._pagebaker import PageBaker
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 from .mockutil import get_mock_app, mock_fs, mock_fs_scope
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 @pytest.mark.parametrize('uri, pretty, expected', [
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 # Pretty URLs
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 ('', True, 'index.html'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 ('2', True, '2/index.html'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 ('foo', True, 'foo/index.html'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 ('foo/2', True, 'foo/2/index.html'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 ('foo/bar', True, 'foo/bar/index.html'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 ('foo/bar/2', True, 'foo/bar/2/index.html'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 ('foo.ext', True, 'foo.ext/index.html'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 ('foo.ext/2', True, 'foo.ext/2/index.html'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 ('foo/bar.ext', True, 'foo/bar.ext/index.html'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 ('foo/bar.ext/2', True, 'foo/bar.ext/2/index.html'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 ('foo.bar.ext', True, 'foo.bar.ext/index.html'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 ('foo.bar.ext/2', True, 'foo.bar.ext/2/index.html'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 # Ugly URLs
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 ('', False, 'index.html'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 ('2.html', False, '2.html'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 ('foo.html', False, 'foo.html'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 ('foo/2.html', False, 'foo/2.html'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 ('foo/bar.html', False, 'foo/bar.html'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 ('foo/bar/2.html', False, 'foo/bar/2.html'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 ('foo.ext', False, 'foo.ext'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 ('foo/2.ext', False, 'foo/2.ext'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 ('foo/bar.ext', False, 'foo/bar.ext'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 ('foo/bar/2.ext', False, 'foo/bar/2.ext'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 ('foo.bar.ext', False, 'foo.bar.ext'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 ('foo.bar/2.ext', False, 'foo.bar/2.ext')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 ])
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 def test_get_output_path(uri, pretty, expected):
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 app = get_mock_app()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 if pretty:
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 app.config.set('site/pretty_urls', True)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 assert app.config.get('site/pretty_urls') == pretty
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 for site_root in ['/', '/whatever/', '/~johndoe/']:
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 app.config.set('site/root', urllib.parse.quote(site_root))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 baker = PageBaker(app, '/destination')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 try:
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 path = baker.getOutputPath(urllib.parse.quote(site_root) + uri,
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49 pretty)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50 expected = os.path.normpath(
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51 os.path.join('/destination', expected))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 assert expected == path
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53 finally:
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 baker.shutdown()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 def test_removed():
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 fs = (mock_fs()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 .withConfig()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 .withPage('pages/foo.md', {'layout': 'none', 'format': 'none'},
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 "a foo page")
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 .withPage('pages/_index.md', {'layout': 'none', 'format': 'none'},
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 "something"))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64 with mock_fs_scope(fs):
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65 fs.runChef('bake')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 structure = fs.getStructure('kitchen/_counter')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 assert structure == {
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 'foo.html': 'a foo page',
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69 'index.html': 'something'}
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71 os.remove(fs.path('kitchen/pages/foo.md'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 fs.runChef('bake')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 structure = fs.getStructure('kitchen/_counter')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74 assert structure == {
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 'index.html': 'something'}
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 def test_record_version_change():
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 fs = (mock_fs()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80 .withConfig()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81 .withPage('pages/foo.md', {'layout': 'none', 'format': 'none'},
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82 'a foo page'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 with mock_fs_scope(fs):
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 fs.runChef('bake')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 mtime = os.path.getmtime(fs.path('kitchen/_counter/foo.html'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 time.sleep(1)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88 fs.runChef('bake')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 assert mtime == os.path.getmtime(fs.path('kitchen/_counter/foo.html'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 MultiRecord.RECORD_VERSION += 1
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 try:
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 fs.runChef('bake')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94 assert mtime < os.path.getmtime(fs.path(
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 'kitchen/_counter/foo.html'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 finally:
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 MultiRecord.RECORD_VERSION -= 1
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98