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