diff --git a/htmd/site.py b/htmd/site.py index a981382..96bdea0 100644 --- a/htmd/site.py +++ b/htmd/site.py @@ -237,7 +237,7 @@ def post(year: str, month: str, day: str, path: str) -> ResponseReturnValue: @app.route('/draft//') def draft(post_uuid: str) -> ResponseReturnValue: for post in posts: - if str(post.meta.get('draft', '')) == post_uuid: + if str(post.meta.get('draft', '')).replace('build|', '') == post_uuid: return render_template('post.html', post=post) abort(404) # noqa: RET503 @@ -382,13 +382,16 @@ def day_view() -> Iterator[dict]: # noqa: F811 @freezer.register_generator # type: ignore[no-redef] def draft() -> Iterator[dict]: # noqa: F811 - draft_posts = [p for p in posts if p.meta.get('draft', False)] + draft_posts = [ + p for p in posts + if 'draft' in p.meta and 'build' in str(p.meta['draft']) + ] for post in draft_posts: - if not valid_uuid(str(post.meta['draft'])): - post.meta['draft'] = uuid.uuid4() + if not valid_uuid(post.meta['draft'].replace('build|', '')): + post.meta['draft'] = 'build|' + str(uuid.uuid4()) set_post_metadata(app, post, 'draft', post.meta['draft']) yield { - 'post_uuid': str(post.meta['draft']), + 'post_uuid': post.meta['draft'].replace('build|', ''), } diff --git a/tests/test_build.py b/tests/test_build.py index 56f9b27..b83fc24 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -6,7 +6,7 @@ from click.testing import CliRunner from htmd.cli import build -from utils import remove_fields_from_example_post, SUCCESS_REGEX +from utils import remove_fields_from_post, SUCCESS_REGEX def test_build(run_start: CliRunner) -> None: @@ -17,7 +17,7 @@ def test_build(run_start: CliRunner) -> None: def test_build_verify_fails(run_start: CliRunner) -> None: expected_output = 'Post "example" does not have field title.\n' - remove_fields_from_example_post(('title',)) + remove_fields_from_post('example', ('title',)) result = run_start.invoke(build) assert result.exit_code == 1 assert result.output == expected_output diff --git a/tests/test_drafts.py b/tests/test_drafts.py index 4b7f7f3..0301f59 100644 --- a/tests/test_drafts.py +++ b/tests/test_drafts.py @@ -6,11 +6,11 @@ from htmd.cli import build, start import pytest -from utils import remove_fields_from_example_post, SUCCESS_REGEX +from utils import remove_fields_from_post, SUCCESS_REGEX def set_example_as_draft() -> None: - remove_fields_from_example_post(('draft',)) + remove_fields_from_post('example', ('draft',)) post_path = Path('posts') / 'example.md' with post_path.open('r') as post_file: lines = post_file.readlines() @@ -21,13 +21,26 @@ def set_example_as_draft() -> None: post_file.write(line) -def get_example_draft_uuid() -> str: - draft_path = Path('posts') / 'example.md' - with draft_path.open('r') as draft_file: # pragma: no branch - for line in draft_file.readlines(): # pragma: no branch +def copy_example_as_draft_build() -> None: + post_path = Path('posts') / 'example.md' + copy_path = Path('posts') / 'copy.md' + with post_path.open('r') as post_file: + lines = post_file.readlines() + with copy_path.open('w') as copy_file: + for line in lines: if 'draft' in line: - return line.replace('draft:', '').strip() - return '' # pragma: no cover + copy_file.write('draft: build\n') + else: + copy_file.write(line) + + +def get_draft_uuid(path: str) -> str: + draft_path = Path('posts') / f'{path}.md' + with draft_path.open('r') as draft_file: + for line in draft_file.readlines(): + if 'draft: build' in line: + return line.replace('draft: build|', '').strip() + return '' @pytest.fixture(scope='module') @@ -36,17 +49,21 @@ def build_draft() -> Generator[CliRunner, None, None]: with runner.isolated_filesystem(): result = runner.invoke(start) set_example_as_draft() + copy_example_as_draft_build() result = runner.invoke(build) assert result.exit_code == 0 + assert re.search(SUCCESS_REGEX, result.output) # Tests code is run here yield runner -def test_draft_is_built(build_draft: CliRunner) -> None: +def test_draft_only_draft_build_is_in_build(build_draft: CliRunner) -> None: post_path = Path('build') / '2014' / '10' / '30' / 'example' / 'index.html' assert post_path.exists() is False - draft_uuid = get_example_draft_uuid() + example_uuid = get_draft_uuid('example') + assert example_uuid == '' + draft_uuid = get_draft_uuid('copy') draft_path = Path('build') / 'draft' / draft_uuid / 'index.html' assert draft_path.is_file() is True @@ -54,6 +71,7 @@ def test_draft_is_built(build_draft: CliRunner) -> None: result = build_draft.invoke(build) assert result.exit_code == 0 assert re.search(SUCCESS_REGEX, result.output) + assert draft_path.is_file() is True def test_no_drafts_home(build_draft: CliRunner) -> None: @@ -105,10 +123,13 @@ def test_no_drafts_for_day(build_draft: CliRunner) -> None: def test_draft_without_published(run_start: CliRunner) -> None: set_example_as_draft() - remove_fields_from_example_post(('published', 'updated')) + copy_example_as_draft_build() + example_path = Path('posts') / 'example.md' + example_path.unlink() + remove_fields_from_post('copy', ('published', 'updated')) result = run_start.invoke(build) assert result.exit_code == 0 assert re.search(SUCCESS_REGEX, result.output) - draft_uuid = get_example_draft_uuid() + draft_uuid = get_draft_uuid('copy') draft_path = Path('build') / 'draft' / draft_uuid / 'index.html' assert draft_path.is_file() is True diff --git a/tests/test_post_dates.py b/tests/test_post_dates.py index d59fb46..11ced69 100644 --- a/tests/test_post_dates.py +++ b/tests/test_post_dates.py @@ -4,7 +4,7 @@ from click.testing import CliRunner from htmd.cli import build -from utils import remove_fields_from_example_post +from utils import remove_fields_from_post def test_build_post_404_invalid_date_year(run_start: CliRunner) -> None: @@ -263,7 +263,7 @@ def test_build_published_time_is_added(run_start: CliRunner) -> None: # build will add it # verify that time is not there # ensure correct time is added - remove_fields_from_example_post(('updated',)) + remove_fields_from_post('example', ('updated',)) example_path = Path('posts') / 'example.md' with example_path.open('r') as post_file: b_lines = post_file.readlines() @@ -311,7 +311,7 @@ def test_build_updated_is_added(run_start: CliRunner) -> None: then build will add updated with time. """ # Remove updated from example post - remove_fields_from_example_post(('updated',)) + remove_fields_from_post('example', ('updated',)) # First build adds time to published result = run_start.invoke(build) assert result.exit_code == 0 @@ -369,7 +369,7 @@ def test_build_updated_is_added_once(run_start: CliRunner) -> None: def test_build_without_published(run_start: CliRunner) -> None: - remove_fields_from_example_post(('published', 'updated')) + remove_fields_from_post('example', ('published', 'updated')) # First build adds published time result = run_start.invoke(build) diff --git a/tests/test_verify.py b/tests/test_verify.py index 1426108..3822d4b 100644 --- a/tests/test_verify.py +++ b/tests/test_verify.py @@ -3,7 +3,7 @@ from click.testing import CliRunner from htmd.cli import verify -from utils import remove_fields_from_example_post +from utils import remove_fields_from_post def test_verify(run_start: CliRunner) -> None: @@ -15,7 +15,7 @@ def test_verify(run_start: CliRunner) -> None: def test_verify_author_missing(run_start: CliRunner) -> None: # Remove author from example post - remove_fields_from_example_post(('author',)) + remove_fields_from_post('example', ('author',)) result = run_start.invoke(verify) assert result.exit_code == 1 @@ -25,7 +25,7 @@ def test_verify_author_missing(run_start: CliRunner) -> None: def test_verify_title_missing(run_start: CliRunner) -> None: # Remove title from example post - remove_fields_from_example_post(('title',)) + remove_fields_from_post('example', ('title',)) result = run_start.invoke(verify) assert result.exit_code == 1 @@ -35,7 +35,7 @@ def test_verify_title_missing(run_start: CliRunner) -> None: def test_verify_published_missing(run_start: CliRunner) -> None: # Remove published from example post - remove_fields_from_example_post(('published',)) + remove_fields_from_post('example', ('published',)) result = run_start.invoke(verify) # verify doesn't check for published diff --git a/tests/utils.py b/tests/utils.py index 4508851..958278c 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -7,8 +7,8 @@ ) -def remove_fields_from_example_post(field_names: tuple[str, ...]) -> None: - example_post_path = Path('posts') / 'example.md' +def remove_fields_from_post(path: str, field_names: tuple[str, ...]) -> None: + example_post_path = Path('posts') / f'{path}.md' with example_post_path.open('r') as post: lines = post.readlines() with example_post_path.open('w') as post: