Mercurial > piecrust2
changeset 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 | 5b12c1d96523 |
children | dce37d1d4f05 |
files | piecrust/sources/base.py tests/test_sources_base.py |
diffstat | 2 files changed, 84 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/piecrust/sources/base.py Sat Nov 29 18:34:53 2014 -0800 +++ b/piecrust/sources/base.py Sat Nov 29 20:51:30 2014 -0800 @@ -177,10 +177,12 @@ if self._first_valid_path_index >= 0: return if self._first_valid_path_index == -1: - raise PageNotFoundError("No valid paths were found for page reference:" % + raise PageNotFoundError( + "No valid paths were found for page reference: %s" % self._page_ref) self._load() + self._first_valid_path_index = -1 for i, path_info in enumerate(self._paths): if os.path.isfile(path_info[2]): self._first_valid_path_index = i
--- a/tests/test_sources_base.py Sat Nov 29 18:34:53 2014 -0800 +++ b/tests/test_sources_base.py Sat Nov 29 20:51:30 2014 -0800 @@ -1,6 +1,7 @@ import os import pytest from piecrust.app import PieCrust +from piecrust.sources.base import PageRef, PageNotFoundError from .mockutil import mock_fs, mock_fs_scope @@ -61,3 +62,83 @@ actual = s.resolveRef(ref_path) assert actual == expected + +@pytest.mark.parametrize('page_ref, expected_source_name, expected_rel_path, ' + 'expected_possible_paths', [ + ('foo:one.md', 'foo', 'one.md', + ['foo/one.md']), + ('foo:two.md', 'foo', 'two.md', + ['foo/two.md']), + ('foo:two.html', 'foo', 'two.html', + ['foo/two.html']), + ('foo:two.%ext%', 'foo', 'two.html', + ['foo/two.html', 'foo/two.md', 'foo/two.textile']), + ('foo:subdir/four.md', 'foo', 'subdir/four.md', + ['foo/subdir/four.md']), + ('foo:subdir/four.%ext%', 'foo', 'subdir/four.md', + ['foo/subdir/four.html', 'foo/subdir/four.md', + 'foo/subdir/four.textile']), + ('foo:three.md;bar:three.md', 'foo', 'three.md', + ['foo/three.md', 'bar/three.md']), + ('foo:three.%ext%;bar:three.%ext%', 'foo', 'three.md', + ['foo/three.html', 'foo/three.md', 'foo/three.textile', + 'bar/three.html', 'bar/three.md', 'bar/three.textile']), + ('foo:special.md;bar:special.md', 'bar', 'special.md', + ['foo/special.md', 'bar/special.md']) + ]) +def test_page_ref(page_ref, expected_source_name, expected_rel_path, + expected_possible_paths): + fs = (mock_fs() + .withConfig({ + 'site': { + 'sources': { + 'foo': {}, + 'bar': {} + } + } + }) + .withPage('foo/one.md') + .withPage('foo/two.md') + .withPage('foo/two.html') + .withPage('foo/three.md') + .withPage('foo/subdir/four.md') + .withPage('bar/three.md') + .withPage('bar/special.md')) + with mock_fs_scope(fs): + app = fs.getApp() + r = PageRef(app, page_ref) + + assert r.possible_paths == [os.path.join(fs.path('/kitchen'), p) + for p in expected_possible_paths] + + assert r.exists + assert r.source_name == expected_source_name + assert r.source == app.getSource(expected_source_name) + assert r.rel_path == expected_rel_path + assert r.path == fs.path(os.path.join( + 'kitchen', expected_source_name, expected_rel_path)) + + +def test_page_ref_with_missing_source(): + fs = mock_fs() + with mock_fs_scope(fs): + app = fs.getApp() + r = PageRef(app, 'whatever:doesnt_exist.md') + with pytest.raises(Exception): + r.possible_rel_paths + + +def test_page_ref_with_missing_file(): + fs = mock_fs() + with mock_fs_scope(fs): + app = fs.getApp() + r = PageRef(app, 'pages:doesnt_exist.%ext%') + assert r.possible_rel_paths == [ + 'doesnt_exist.html', 'doesnt_exist.md', 'doesnt_exist.textile'] + assert r.source_name == 'pages' + with pytest.raises(PageNotFoundError): + r.rel_path + with pytest.raises(PageNotFoundError): + r.path + assert not r.exists +