comparison tests/test_sources_base.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 1 import os
2 import pytest 2 import pytest
3 from piecrust.app import PieCrust 3 from piecrust.app import PieCrust
4 from piecrust.sources.pageref import PageRef, PageNotFoundError
5 from .mockutil import mock_fs, mock_fs_scope 4 from .mockutil import mock_fs, mock_fs_scope
6 from .pathutil import slashfix
7 5
8 6
9 @pytest.mark.parametrize('fs_fac, expected_paths, expected_slugs', [ 7 @pytest.mark.parametrize('fs_fac, expected_paths, expected_slugs', [
10 (lambda: mock_fs(), [], []), 8 (lambda: mock_fs(), [], []),
11 (lambda: mock_fs().withPage('test/foo.html'), 9 (lambda: mock_fs().withPage('test/foo.html'),
12 ['foo.html'], ['foo']), 10 ['foo.html'], ['foo']),
13 (lambda: mock_fs().withPage('test/foo.md'), 11 (lambda: mock_fs().withPage('test/foo.md'),
14 ['foo.md'], ['foo']), 12 ['foo.md'], ['foo']),
15 (lambda: mock_fs().withPage('test/foo.ext'), 13 (lambda: mock_fs().withPage('test/foo.ext'),
16 ['foo.ext'], ['foo.ext']), 14 ['foo.ext'], ['foo.ext']),
17 (lambda: mock_fs().withPage('test/foo/bar.html'), 15 (lambda: mock_fs().withPage('test/foo/bar.html'),
18 ['foo/bar.html'], ['foo/bar']), 16 ['foo/bar.html'], ['foo/bar']),
19 (lambda: mock_fs().withPage('test/foo/bar.md'), 17 (lambda: mock_fs().withPage('test/foo/bar.md'),
20 ['foo/bar.md'], ['foo/bar']), 18 ['foo/bar.md'], ['foo/bar']),
21 (lambda: mock_fs().withPage('test/foo/bar.ext'), 19 (lambda: mock_fs().withPage('test/foo/bar.ext'),
22 ['foo/bar.ext'], ['foo/bar.ext']), 20 ['foo/bar.ext'], ['foo/bar.ext']),
23 ]) 21 ])
24 def test_default_source_factories(fs_fac, expected_paths, expected_slugs): 22 def test_default_source_factories(fs_fac, expected_paths, expected_slugs):
25 fs = fs_fac() 23 fs = fs_fac()
26 fs.withConfig({ 24 fs.withConfig({
27 'site': { 25 'site': {
28 'sources': { 26 'sources': {
29 'test': {}}, 27 'test': {}},
30 'routes': [ 28 'routes': [
31 {'url': '/%path%', 'source': 'test'}] 29 {'url': '/%path%', 'source': 'test'}]
32 } 30 }
33 }) 31 })
34 fs.withDir('kitchen/test') 32 fs.withDir('kitchen/test')
35 with mock_fs_scope(fs): 33 with mock_fs_scope(fs):
36 app = PieCrust(fs.path('kitchen'), cache=False) 34 app = PieCrust(fs.path('kitchen'), cache=False)
37 s = app.getSource('test') 35 s = app.getSource('test')
38 facs = list(s.buildPageFactories()) 36 facs = list(s.buildPageFactories())
41 slugs = [f.metadata['slug'] for f in facs] 39 slugs = [f.metadata['slug'] for f in facs]
42 assert slugs == expected_slugs 40 assert slugs == expected_slugs
43 41
44 42
45 @pytest.mark.parametrize( 43 @pytest.mark.parametrize(
46 'ref_path, expected_path, expected_metadata', 44 'ref_path, expected_path, expected_metadata',
47 [ 45 [
48 ('foo.html', '/kitchen/test/foo.html', {'slug': 'foo'}), 46 ('foo.html', '/kitchen/test/foo.html', {'slug': 'foo'}),
49 ('foo/bar.html', '/kitchen/test/foo/bar.html', 47 ('foo/bar.html', '/kitchen/test/foo/bar.html',
50 {'slug': 'foo/bar'}), 48 {'slug': 'foo/bar'}),
51 ]) 49 ])
52 def test_default_source_resolve_ref(ref_path, expected_path, 50 def test_default_source_resolve_ref(ref_path, expected_path,
53 expected_metadata): 51 expected_metadata):
54 fs = mock_fs() 52 fs = mock_fs()
55 fs.withConfig({ 53 fs.withConfig({
56 'site': { 54 'site': {
57 'sources': { 55 'sources': {
58 'test': {}}, 56 'test': {}},
59 'routes': [ 57 'routes': [
60 {'url': '/%path%', 'source': 'test'}] 58 {'url': '/%path%', 'source': 'test'}]
61 } 59 }
62 }) 60 })
63 expected_path = fs.path(expected_path).replace('/', os.sep) 61 expected_path = fs.path(expected_path).replace('/', os.sep)
64 with mock_fs_scope(fs): 62 with mock_fs_scope(fs):
65 app = PieCrust(fs.path('kitchen'), cache=False) 63 app = PieCrust(fs.path('kitchen'), cache=False)
66 s = app.getSource('test') 64 s = app.getSource('test')
67 actual_path, actual_metadata = s.resolveRef(ref_path) 65 actual_path, actual_metadata = s.resolveRef(ref_path)
68 assert actual_path == expected_path 66 assert actual_path == expected_path
69 assert actual_metadata == expected_metadata 67 assert actual_metadata == expected_metadata
70
71
72 @pytest.mark.parametrize('page_ref, expected_source_name, expected_rel_path, '
73 'expected_possible_paths', [
74 ('foo:one.md', 'foo', 'one.md',
75 ['foo/one.md']),
76 ('foo:two.md', 'foo', 'two.md',
77 ['foo/two.md']),
78 ('foo:two.html', 'foo', 'two.html',
79 ['foo/two.html']),
80 ('foo:two.%ext%', 'foo', 'two.html',
81 ['foo/two.html', 'foo/two.md', 'foo/two.textile']),
82 ('foo:subdir/four.md', 'foo', 'subdir/four.md',
83 ['foo/subdir/four.md']),
84 ('foo:subdir/four.%ext%', 'foo', 'subdir/four.md',
85 ['foo/subdir/four.html', 'foo/subdir/four.md',
86 'foo/subdir/four.textile']),
87 ('foo:three.md;bar:three.md', 'foo', 'three.md',
88 ['foo/three.md', 'bar/three.md']),
89 ('foo:three.%ext%;bar:three.%ext%', 'foo', 'three.md',
90 ['foo/three.html', 'foo/three.md', 'foo/three.textile',
91 'bar/three.html', 'bar/three.md', 'bar/three.textile']),
92 ('foo:special.md;bar:special.md', 'bar', 'special.md',
93 ['foo/special.md', 'bar/special.md'])
94 ])
95 def test_page_ref(page_ref, expected_source_name, expected_rel_path,
96 expected_possible_paths):
97 fs = (mock_fs()
98 .withConfig({
99 'site': {
100 'sources': {
101 'foo': {},
102 'bar': {}
103 }
104 }
105 })
106 .withPage('foo/one.md')
107 .withPage('foo/two.md')
108 .withPage('foo/two.html')
109 .withPage('foo/three.md')
110 .withPage('foo/subdir/four.md')
111 .withPage('bar/three.md')
112 .withPage('bar/special.md'))
113 with mock_fs_scope(fs):
114 app = fs.getApp()
115 r = PageRef(app, page_ref)
116
117 assert r.possible_paths == slashfix(
118 [os.path.join(fs.path('/kitchen'), p)
119 for p in expected_possible_paths])
120
121 assert r.exists
122 assert r.source_name == expected_source_name
123 assert r.source == app.getSource(expected_source_name)
124 assert r.rel_path == expected_rel_path
125 assert r.path == slashfix(fs.path(os.path.join(
126 'kitchen', expected_source_name, expected_rel_path)))
127
128
129 def test_page_ref_with_missing_source():
130 fs = mock_fs().withConfig()
131 with mock_fs_scope(fs):
132 app = fs.getApp()
133 r = PageRef(app, 'whatever:doesnt_exist.md')
134 with pytest.raises(Exception):
135 r.possible_ref_specs
136
137
138 def test_page_ref_with_missing_file():
139 fs = mock_fs().withConfig()
140 with mock_fs_scope(fs):
141 app = fs.getApp()
142 r = PageRef(app, 'pages:doesnt_exist.%ext%')
143 assert r.possible_ref_specs == [
144 'pages:doesnt_exist.html', 'pages:doesnt_exist.md',
145 'pages:doesnt_exist.textile']
146 with pytest.raises(PageNotFoundError):
147 r.source_name
148 with pytest.raises(PageNotFoundError):
149 r.rel_path
150 with pytest.raises(PageNotFoundError):
151 r.path
152 assert not r.exists
153