Skip to content

Commit

Permalink
Refactor tests to declare paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Siecje committed Jan 28, 2024
1 parent 598146e commit de32b13
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 51 deletions.
15 changes: 9 additions & 6 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ def test_build_css_minify_no_css_files(run_start: CliRunner) -> None:


def test_build_html_pretty_true(run_start: CliRunner) -> None:
with Path('config.toml').open('r') as config_file:
config_path = Path('config.toml')
with config_path.open('r') as config_file:
lines = config_file.readlines()

with Path('config.toml').open('w') as config_file:
with config_path.open('w') as config_file:
for line in lines:
if 'pretty =' in line:
config_file.write('pretty = true\n')
Expand All @@ -90,10 +91,11 @@ def test_build_html_pretty_true(run_start: CliRunner) -> None:


def test_build_html_minify_true(run_start: CliRunner) -> None:
with Path('config.toml').open('r') as config_file:
config_path = Path('config.toml')
with config_path.open('r') as config_file:
lines = config_file.readlines()

with Path('config.toml').open('w') as config_file:
with config_path.open('w') as config_file:
for line in lines:
if 'minify =' in line:
config_file.write('minify = true\n')
Expand All @@ -113,11 +115,12 @@ def test_build_page_404(run_start: CliRunner) -> None:
'All posts are correctly formatted.\n'
"Unexpected status '404 NOT FOUND' on URL /dne/\n"
)
with (Path('pages') / 'about.html').open('r') as about_file:
about_path = Path('pages') / 'about.html'
with about_path.open('r') as about_file:
lines = about_file.readlines()

new_line = '''<p><a href="{{ url_for('pages.page', path='dne') }}">DNE link</a></p>\n''' # noqa: E501
with (Path('pages') / 'about.html').open('w') as about_file:
with about_path.open('w') as about_file:
for line in lines:
if '<p>This is the about page.</p>' in line:
about_file.write(new_line)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_drafts.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def build_draft() -> Generator[None, None, None]: # noqa: PT004


def test_draft_is_built(build_draft: None) -> None:
post_path = (Path('build') / '2014' / '10' / '30' / 'example' / 'index.html')
post_path = Path('build') / '2014' / '10' / '30' / 'example' / 'index.html'
with post_path.open('r') as post_page:
assert 'Example Post' in post_page.read()

Expand Down
68 changes: 38 additions & 30 deletions tests/test_post_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ def test_build_post_404_invalid_date_year(run_start: CliRunner) -> None:
"Unexpected status '404 NOT FOUND' on URL /14/10/30/example/\n"
)

with (Path('pages') / 'about.html').open('r') as about_file:
about_path = Path('pages') / 'about.html'
with about_path.open('r') as about_file:
lines = about_file.readlines()

new_line = '''<p><a href="{{ url_for('post', year=14, month='10', day='30', path='example') }}">DNE link</a></p>\n''' # noqa: E501
with (Path('pages') / 'about.html').open('w') as about_file:
with about_path.open('w') as about_file:
for line in lines:
if '<p>This is the about page.</p>' in line:
about_file.write(new_line)
Expand All @@ -39,14 +40,12 @@ def test_build_post_404_invalid_date_month(run_start: CliRunner) -> None:
"Unexpected status '404 NOT FOUND' on URL /2014/1/30/example/\n"
)

with (Path('posts') / 'example.md').open('r') as post_file:
lines = post_file.readlines()

with (Path('pages') / 'about.html').open('r') as about_file:
about_path = Path('pages') / 'about.html'
with about_path.open('r') as about_file:
lines = about_file.readlines()

new_line = '''<p><a href="{{ url_for('post', year=2014, month='1', day='30', path='example') }}">DNE link</a></p>\n''' # noqa: E501
with (Path('pages') / 'about.html').open('w') as about_file:
with about_path.open('w') as about_file:
for line in lines:
if '<p>This is the about page.</p>' in line:
about_file.write(new_line)
Expand All @@ -65,14 +64,13 @@ def test_build_post_404_invalid_date_day(run_start: CliRunner) -> None:
'All posts are correctly formatted.\n'
"Unexpected status '404 NOT FOUND' on URL /2014/10/3/example/\n"
)
with (Path('posts') / 'example.md').open('r') as post_file:
lines = post_file.readlines()

with (Path('pages') / 'about.html').open('r') as about_file:
about_path = Path('pages') / 'about.html'
with about_path.open('r') as about_file:
lines = about_file.readlines()

new_line = '''<p><a href="{{ url_for('post', year=2014, month='10', day='3', path='example') }}">DNE link</a></p>\n''' # noqa: E501
with (Path('pages') / 'about.html').open('w') as about_file:
with about_path.open('w') as about_file:
for line in lines:
if '<p>This is the about page.</p>' in line:
about_file.write(new_line)
Expand All @@ -92,11 +90,12 @@ def test_build_post_404_different_date(run_start: CliRunner) -> None:
'All posts are correctly formatted.\n'
"Unexpected status '404 NOT FOUND' on URL /2014/10/29/example/\n"
)
with (Path('pages') / 'about.html').open('r') as about_file:
about_path = Path('pages') / 'about.html'
with about_path.open('r') as about_file:
lines = about_file.readlines()

new_line = '''<p><a href="{{ url_for('post', year=2014, month='10', day='29', path='example') }}">DNE link</a></p>\n''' # noqa: E501
with (Path('pages') / 'about.html').open('w') as about_file:
with about_path.open('w') as about_file:
for line in lines:
if '<p>This is the about page.</p>' in line:
about_file.write(new_line)
Expand All @@ -117,11 +116,12 @@ def test_build_year_404_incorrect(run_start: CliRunner) -> None:
"Unexpected status '404 NOT FOUND' on URL /14/\n"
)

with (Path('pages') / 'about.html').open('r') as about_file:
about_path = Path('pages') / 'about.html'
with about_path.open('r') as about_file:
lines = about_file.readlines()

new_line = '''<p><a href="{{ url_for('year_view', year=14) }}">DNE link</a></p>\n'''
with (Path('pages') / 'about.html').open('w') as about_file:
with about_path.open('w') as about_file:
for line in lines:
if '<p>This is the about page.</p>' in line:
about_file.write(new_line)
Expand All @@ -141,11 +141,12 @@ def test_build_year_404_no_posts(run_start: CliRunner) -> None:
'All posts are correctly formatted.\n'
"Unexpected status '404 NOT FOUND' on URL /2013/\n"
)
with (Path('pages') / 'about.html').open('r') as about_file:
about_path = Path('pages') / 'about.html'
with about_path.open('r') as about_file:
lines = about_file.readlines()

new_line = '''<p><a href="{{ url_for('year_view', year=2013) }}">DNE link</a></p>\n''' # noqa: E501
with (Path('pages') / 'about.html').open('w') as about_file:
with about_path.open('w') as about_file:
for line in lines:
if '<p>This is the about page.</p>' in line:
about_file.write(new_line)
Expand All @@ -166,11 +167,12 @@ def test_build_month_404_no_posts(run_start: CliRunner) -> None:
"Unexpected status '404 NOT FOUND' on URL /2014/01/\n"
)

with (Path('pages') / 'about.html').open('r') as about_file:
about_path = Path('pages') / 'about.html'
with about_path.open('r') as about_file:
lines = about_file.readlines()

new_line = '''<p><a href="{{ url_for('month_view', year=2014, month='01') }}">DNE link</a></p>\n''' # noqa: E501
with (Path('pages') / 'about.html').open('w') as about_file:
with about_path.open('w') as about_file:
for line in lines:
if '<p>This is the about page.</p>' in line:
about_file.write(new_line)
Expand All @@ -191,11 +193,12 @@ def test_build_day_404_no_posts(run_start: CliRunner) -> None:
"Unexpected status '404 NOT FOUND' on URL /2014/10/29/\n"
)

with (Path('pages') / 'about.html').open('r') as about_file:
about_path = Path('pages') / 'about.html'
with about_path.open('r') as about_file:
lines = about_file.readlines()

new_line = '''<p><a href="{{ url_for('day_view', year=2014, month='10', day='29') }}">DNE link</a></p>\n''' # noqa: E501
with (Path('pages') / 'about.html').open('w') as about_file:
with about_path.open('w') as about_file:
for line in lines:
if '<p>This is the about page.</p>' in line:
about_file.write(new_line)
Expand All @@ -212,11 +215,12 @@ def test_build_updated_time_is_added(run_start: CliRunner) -> None:
# build will add it
# verify that time is not there
# ensure correct time is added
with (Path('posts') / 'example.md').open('r') as post_file:
example_path = Path('posts') / 'example.md'
with example_path.open('r') as post_file:
b_lines = post_file.readlines()
result = run_start.invoke(build)
assert result.exit_code == 0
with (Path('posts') / 'example.md').open('r') as post_file:
with example_path.open('r') as post_file:
a_lines = post_file.readlines()
for b_line, a_line in zip(b_lines, a_lines, strict=True):
if 'updated' in b_line:
Expand Down Expand Up @@ -260,11 +264,12 @@ def test_build_published_time_is_added(run_start: CliRunner) -> None:
# verify that time is not there
# ensure correct time is added
remove_fields_from_example_post(('updated',))
with (Path('posts') / 'example.md').open('r') as post_file:
example_path = Path('posts') / 'example.md'
with example_path.open('r') as post_file:
b_lines = post_file.readlines()
result = run_start.invoke(build)
assert result.exit_code == 0
with (Path('posts') / 'example.md').open('r') as post_file:
with example_path.open('r') as post_file:
a_lines = post_file.readlines()
for b_line, a_line in zip(b_lines, a_lines, strict=True):
if 'published' in b_line:
Expand Down Expand Up @@ -313,7 +318,8 @@ def test_build_updated_is_added(run_start: CliRunner) -> None:
# Second build adds updated with time
result2 = run_start.invoke(build)
assert result2.exit_code == 0
with (Path('posts') / 'example.md').open('r') as post_file:
example_path = Path('posts') / 'example.md'
with example_path.open('r') as post_file:
a_lines = post_file.readlines()
for a_line in a_lines:
if 'updated' in a_line:
Expand All @@ -336,9 +342,10 @@ def test_build_updated_is_added(run_start: CliRunner) -> None:

def test_build_updated_is_added_once(run_start: CliRunner) -> None:
# Remove updated from example post
with (Path('posts') / 'example.md').open('r') as post_file:
example_path = Path('posts') / 'example.md'
with example_path.open('r') as post_file:
b_lines = post_file.readlines()
with (Path('posts') / 'example.md').open('w') as post_file:
with example_path.open('w') as post_file:
for line in b_lines:
if 'updated' not in line:
post_file.write(line)
Expand All @@ -351,7 +358,7 @@ def test_build_updated_is_added_once(run_start: CliRunner) -> None:
# Second build adds updated
result2 = run_start.invoke(build)
assert result2.exit_code == 0
with (Path('posts') / 'example.md').open('r') as post_file:
with example_path.open('r') as post_file:
a_lines = post_file.readlines()
count = 0
for a_line in a_lines:
Expand All @@ -367,7 +374,8 @@ def test_build_without_published(run_start: CliRunner) -> None:
# First build adds published time
result = run_start.invoke(build)
assert result.exit_code == 0
with (Path('posts') / 'example.md').open('r') as post_file:
example_path = Path('posts') / 'example.md'
with example_path.open('r') as post_file:
a_lines = post_file.readlines()
count = 0
for a_line in a_lines:
Expand Down
5 changes: 3 additions & 2 deletions tests/test_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ def test_start_with_existing_template() -> None:
runner = CliRunner()
with runner.isolated_filesystem():
Path('templates').mkdir()
with (Path('templates') / '_layout.html').open('w') as layout:
layout_path = Path('templates') / '_layout.html'
with layout_path.open('w') as layout:
pass
result = runner.invoke(start)
with (Path('templates') / '_layout.html').open('r') as layout:
with layout_path.open('r') as layout:
# _layout.html was not replaced
assert layout.read() == ''
assert result.exit_code == 0
Expand Down
25 changes: 15 additions & 10 deletions tests/test_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ def test_verify_published_missing(run_start: CliRunner) -> None:


def test_verify_published_invalid_year(run_start: CliRunner) -> None:
with (Path('posts') / 'example.md').open('r') as post:
example_post_path = Path('posts') / 'example.md'
with example_post_path.open('r') as post:
lines = post.readlines()
with (Path('posts') / 'example.md').open('w') as post:
with example_post_path.open('w') as post:
for line in lines:
if 'published' in line:
post.write('published: 14-10-30\n')
Expand All @@ -65,9 +66,10 @@ def test_verify_published_invalid_year(run_start: CliRunner) -> None:


def test_verify_published_invalid_month(run_start: CliRunner) -> None:
with (Path('posts') / 'example.md').open('r') as post:
example_post_path = Path('posts') / 'example.md'
with example_post_path.open('r') as post:
lines = post.readlines()
with (Path('posts') / 'example.md').open('w') as post:
with example_post_path.open('w') as post:
for line in lines:
if 'published' in line:
post.write('published: 2014-1-30\n')
Expand All @@ -84,9 +86,10 @@ def test_verify_published_invalid_month(run_start: CliRunner) -> None:


def test_verify_published_invalid_day(run_start: CliRunner) -> None:
with (Path('posts') / 'example.md').open('r') as post:
example_post_path = Path('posts') / 'example.md'
with example_post_path.open('r') as post:
lines = post.readlines()
with (Path('posts') / 'example.md').open('w') as post:
with example_post_path.open('w') as post:
for line in lines:
if 'published' in line:
post.write('published: 2014-01-3\n')
Expand All @@ -108,9 +111,10 @@ def test_verify_site_name_empty(run_start: CliRunner) -> None:
'[site] name is not set in config.toml.\n'
)

with Path('config.toml').open('r') as post:
config_path = Path('config.toml')
with config_path.open('r') as post:
lines = post.readlines()
with Path('config.toml').open('w') as post:
with config_path.open('w') as post:
seen = False
for line in lines:
if 'name' in line and not seen:
Expand All @@ -131,9 +135,10 @@ def test_verify_site_name_missing(run_start: CliRunner) -> None:
'All posts are correctly formatted.\n'
'[site] name is not set in config.toml.\n'
)
with Path('config.toml').open('r') as post:
config_path = Path('config.toml')
with config_path.open('r') as post:
lines = post.readlines()
with Path('config.toml').open('w') as post:
with config_path.open('w') as post:
for line in lines:
if 'name' not in line:
post.write(line)
Expand Down
5 changes: 3 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@


def remove_fields_from_example_post(field_names: tuple[str, ...]) -> None:
with (Path('posts') / 'example.md').open('r') as post:
example_post_path = Path('posts') / 'example.md'
with example_post_path.open('r') as post:
lines = post.readlines()
with (Path('posts') / 'example.md').open('w') as post:
with example_post_path.open('w') as post:
for line in lines:
for field_name in field_names:
if field_name in line:
Expand Down

0 comments on commit de32b13

Please sign in to comment.