comparison tests/test_sources_base.py @ 144:8d16ca75075f

Fix a bug with page references in cases of failure. Add unit tests.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 29 Nov 2014 20:51:30 -0800
parents b536022078a2
children 85a6c7ba5e3b
comparison
equal deleted inserted replaced
143:5b12c1d96523 144:8d16ca75075f
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.base import PageRef, PageNotFoundError
4 from .mockutil import mock_fs, mock_fs_scope 5 from .mockutil import mock_fs, mock_fs_scope
5 6
6 7
7 @pytest.mark.parametrize('fs, expected_paths, expected_slugs', [ 8 @pytest.mark.parametrize('fs, expected_paths, expected_slugs', [
8 (mock_fs(), [], []), 9 (mock_fs(), [], []),
59 app = PieCrust(fs.path('kitchen'), cache=False) 60 app = PieCrust(fs.path('kitchen'), cache=False)
60 s = app.getSource('test') 61 s = app.getSource('test')
61 actual = s.resolveRef(ref_path) 62 actual = s.resolveRef(ref_path)
62 assert actual == expected 63 assert actual == expected
63 64
65
66 @pytest.mark.parametrize('page_ref, expected_source_name, expected_rel_path, '
67 'expected_possible_paths', [
68 ('foo:one.md', 'foo', 'one.md',
69 ['foo/one.md']),
70 ('foo:two.md', 'foo', 'two.md',
71 ['foo/two.md']),
72 ('foo:two.html', 'foo', 'two.html',
73 ['foo/two.html']),
74 ('foo:two.%ext%', 'foo', 'two.html',
75 ['foo/two.html', 'foo/two.md', 'foo/two.textile']),
76 ('foo:subdir/four.md', 'foo', 'subdir/four.md',
77 ['foo/subdir/four.md']),
78 ('foo:subdir/four.%ext%', 'foo', 'subdir/four.md',
79 ['foo/subdir/four.html', 'foo/subdir/four.md',
80 'foo/subdir/four.textile']),
81 ('foo:three.md;bar:three.md', 'foo', 'three.md',
82 ['foo/three.md', 'bar/three.md']),
83 ('foo:three.%ext%;bar:three.%ext%', 'foo', 'three.md',
84 ['foo/three.html', 'foo/three.md', 'foo/three.textile',
85 'bar/three.html', 'bar/three.md', 'bar/three.textile']),
86 ('foo:special.md;bar:special.md', 'bar', 'special.md',
87 ['foo/special.md', 'bar/special.md'])
88 ])
89 def test_page_ref(page_ref, expected_source_name, expected_rel_path,
90 expected_possible_paths):
91 fs = (mock_fs()
92 .withConfig({
93 'site': {
94 'sources': {
95 'foo': {},
96 'bar': {}
97 }
98 }
99 })
100 .withPage('foo/one.md')
101 .withPage('foo/two.md')
102 .withPage('foo/two.html')
103 .withPage('foo/three.md')
104 .withPage('foo/subdir/four.md')
105 .withPage('bar/three.md')
106 .withPage('bar/special.md'))
107 with mock_fs_scope(fs):
108 app = fs.getApp()
109 r = PageRef(app, page_ref)
110
111 assert r.possible_paths == [os.path.join(fs.path('/kitchen'), p)
112 for p in expected_possible_paths]
113
114 assert r.exists
115 assert r.source_name == expected_source_name
116 assert r.source == app.getSource(expected_source_name)
117 assert r.rel_path == expected_rel_path
118 assert r.path == fs.path(os.path.join(
119 'kitchen', expected_source_name, expected_rel_path))
120
121
122 def test_page_ref_with_missing_source():
123 fs = mock_fs()
124 with mock_fs_scope(fs):
125 app = fs.getApp()
126 r = PageRef(app, 'whatever:doesnt_exist.md')
127 with pytest.raises(Exception):
128 r.possible_rel_paths
129
130
131 def test_page_ref_with_missing_file():
132 fs = mock_fs()
133 with mock_fs_scope(fs):
134 app = fs.getApp()
135 r = PageRef(app, 'pages:doesnt_exist.%ext%')
136 assert r.possible_rel_paths == [
137 'doesnt_exist.html', 'doesnt_exist.md', 'doesnt_exist.textile']
138 assert r.source_name == 'pages'
139 with pytest.raises(PageNotFoundError):
140 r.rel_path
141 with pytest.raises(PageNotFoundError):
142 r.path
143 assert not r.exists
144