Skip to content

Commit

Permalink
Enable ruff check in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Siecje committed Jan 18, 2024
1 parent fa6a3a8 commit cd400f4
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 65 deletions.
22 changes: 8 additions & 14 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python package
name: htmd

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
on: [ push, pull_request ]

jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: chartboost/ruff-action@v1
build:

runs-on: ubuntu-latest
strategy:
fail-fast: false
Expand All @@ -27,14 +27,8 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest
python -m pip install pytest
python -m pip install .
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
17 changes: 9 additions & 8 deletions htmd/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,15 @@ def verify():
msg = f'Post "{post.path}" does not have field {field}.'
click.echo(click.style(msg, fg='red'))
if 'published' in post.meta:
try:
post.meta.get('published').year
except AttributeError:
published = post.meta.get('published')
if not hasattr(published, 'year'):
correct = False
published = post.meta.get('published')
msg = (
f'Published date {published} for {post.path}'
' is not in the format YYYY-MM-DD.'
)
click.echo(click.style(msg, fg='red'))

if correct:
msg = 'All posts are correctly formatted.'
click.echo(click.style(msg, fg='green'))
Expand Down Expand Up @@ -104,9 +103,11 @@ def set_post_time(app, post, field, date_time):
with open(file_path, 'w') as file:
for line in lines:
if not found and field in line:
line = f'{field}: {date_time.isoformat()}\n'
# Update datetime value
line = f'{field}: {date_time.isoformat()}\n' # noqa: PLW2901
found = True
elif not found and '...' in line:
# Write field and value before '...'
file.write(f'{field}: {date_time.isoformat()}\n')
found = True
file.write(line)
Expand All @@ -126,12 +127,12 @@ def set_posts_datetime(app, posts):
field = 'updated'

post_datetime = post.meta.get(field)
now = datetime.datetime.now()
now = datetime.datetime.now(tz=datetime.UTC)
current_time = now.time()
if isinstance(post_datetime, datetime.date):
post_datetime = datetime.datetime.combine(
post_datetime, current_time,
)
).replace(tzinfo=datetime.UTC)
else:
post_datetime = now
post.meta[field] = post_datetime
Expand Down Expand Up @@ -204,7 +205,7 @@ def build(ctx, css_minify, js_minify):
default=True,
help='If JavaScript should be minified',
)
def preview(ctx, host, port, css_minify, js_minify):
def preview(_ctx, host, port, css_minify, js_minify):
from . import site
# reload for tests to refresh app.static_folder
# otherwise app.static_folder will be from another test
Expand Down
23 changes: 10 additions & 13 deletions htmd/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def get_project_dir():
try:
with open(os.path.join(project_dir, 'config.toml'), 'rb') as config_file:
htmd_config = tomllib.load(config_file)
except IOError:
print('Can not find config.toml')
sys.exit(1)
except FileNotFoundError:
msg = 'Can not find config.toml'
sys.exit(msg)

# Flask configs are flat, config.toml is not
# Define the configuration keys and their default values
Expand Down Expand Up @@ -215,7 +215,7 @@ def all_posts():
# If month and day are ints then Flask removes leading zeros
@app.route('/<year>/<month>/<day>/<path:path>/')
def post(year, month, day, path):
if len(year) != 4 or len(month) != 2 or len(day) != 2:
if len(year) != 4 or len(month) != 2 or len(day) != 2: # noqa: PLR2004
abort(404)
post = posts.get_or_404(path)
date_str = f'{year}-{month}-{day}'
Expand All @@ -225,10 +225,7 @@ def post(year, month, day, path):


def tag_in_list(list_of_tags, tag):
for i in list_of_tags:
if i['tag'] == tag:
return True
return False
return any(i['tag'] == tag for i in list_of_tags)


def increment_tag_count(list_of_tags, tag):
Expand Down Expand Up @@ -285,7 +282,7 @@ def not_found():
@app.route('/<int:year>/')
def year_view(year):
year = str(year)
if len(year) != 4:
if len(year) != len('YYYY'):
abort(404)
year_posts = [
p for p in posts if year == p.meta.get('published', []).strftime('%Y')
Expand Down Expand Up @@ -342,21 +339,21 @@ def day_view(year, month, day):


@app.errorhandler(404)
def page_not_found(e):
def page_not_found(_e):
return render_template('404.html'), 404


# Telling Frozen-Flask about routes that are not linked to in templates
@freezer.register_generator
def year_view():
def year_view(): # noqa: F811
for post in posts:
yield {
'year': post.meta.get('published').year,
}


@freezer.register_generator
def month_view():
def month_view(): # noqa: F811
for post in posts:
yield {
'month': post.meta.get('published').strftime('%m'),
Expand All @@ -365,7 +362,7 @@ def month_view():


@freezer.register_generator
def day_view():
def day_view(): # noqa: F811
for post in posts:
yield {
'day': post.meta.get('published').strftime('%d'),
Expand Down
22 changes: 9 additions & 13 deletions htmd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ def create_directory(name):

def combine_and_minify_css(static_folder):
# Combine and minify all .css files in the static folder
css_files = sorted(
[
f for f in os.listdir(static_folder)
if f.endswith('.css') and f != 'combined.min.css'
]
)
css_files = sorted([
f for f in os.listdir(static_folder)
if f.endswith('.css') and f != 'combined.min.css'
])
if not css_files:
# There are no .css files in the static folder
return
Expand All @@ -43,13 +41,11 @@ def combine_and_minify_css(static_folder):

def combine_and_minify_js(static_folder):
# Combine and minify all .js files in the static folder
js_files = sorted(
[
f for f in os.listdir(static_folder)
if f.endswith('.js')
and f != 'combined.min.js'
]
)
js_files = sorted([
f for f in os.listdir(static_folder)
if f.endswith('.js')
and f != 'combined.min.js'
])
if not js_files:
# There are no .js files in the static folder
return
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ ignore = [
"RET504",
"S101",
"UP015",
"ANN", "PTH", "I",
]

[tool.ruff.lint.flake8-quotes]
Expand Down
29 changes: 14 additions & 15 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def test_build_page_404():
with open(os.path.join('pages', 'about.html'), 'r') as about_file:
lines = about_file.readlines()

new_line = '''<p><a href="{{ url_for('pages.page', path='dne') }}">DNE link</a></p>\n'''
new_line = '''<p><a href="{{ url_for('pages.page', path='dne') }}">DNE link</a></p>\n''' # noqa: E501
with open(os.path.join('pages', 'about.html'), 'w') as about_file:
for line in lines:
if '<p>This is the about page.</p>' in line:
Expand All @@ -178,7 +178,7 @@ def test_build_post_404_invalid_date_year():
with open(os.path.join('pages', 'about.html'), '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'''
new_line = '''<p><a href="{{ url_for('post', year=14, month='10', day='30', path='example') }}">DNE link</a></p>\n''' # noqa: E501
with open(os.path.join('pages', 'about.html'), 'w') as about_file:
for line in lines:
if '<p>This is the about page.</p>' in line:
Expand Down Expand Up @@ -217,7 +217,7 @@ def test_build_post_404_invalid_date_month():
with open(os.path.join('pages', 'about.html'), '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'''
new_line = '''<p><a href="{{ url_for('post', year=2014, month='1', day='30', path='example') }}">DNE link</a></p>\n''' # noqa: E501
with open(os.path.join('pages', 'about.html'), 'w') as about_file:
for line in lines:
if '<p>This is the about page.</p>' in line:
Expand Down Expand Up @@ -256,7 +256,7 @@ def test_build_post_404_invalid_date_day():
with open(os.path.join('pages', 'about.html'), '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'''
new_line = '''<p><a href="{{ url_for('post', year=2014, month='10', day='3', path='example') }}">DNE link</a></p>\n''' # noqa: E501
with open(os.path.join('pages', 'about.html'), 'w') as about_file:
for line in lines:
if '<p>This is the about page.</p>' in line:
Expand Down Expand Up @@ -284,7 +284,7 @@ def test_build_post_404_different_date():
with open(os.path.join('pages', 'about.html'), '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'''
new_line = '''<p><a href="{{ url_for('post', year=2014, month='10', day='29', path='example') }}">DNE link</a></p>\n''' # noqa: E501
with open(os.path.join('pages', 'about.html'), 'w') as about_file:
for line in lines:
if '<p>This is the about page.</p>' in line:
Expand Down Expand Up @@ -325,7 +325,7 @@ def test_build_year_404_incorrect():
with open(os.path.join('pages', 'about.html'), 'r') as about_file:
lines = about_file.readlines()

new_line = '''<p><a href="{{ url_for('year_view', year=14) }}">DNE link</a></p>\n'''
new_line = '''<p><a href="{{ url_for('year_view', year=14) }}">DNE link</a></p>\n''' # noqa: E501
with open(os.path.join('pages', 'about.html'), 'w') as about_file:
for line in lines:
if '<p>This is the about page.</p>' in line:
Expand Down Expand Up @@ -353,7 +353,7 @@ def test_build_year_404_no_posts():
with open(os.path.join('pages', 'about.html'), 'r') as about_file:
lines = about_file.readlines()

new_line = '''<p><a href="{{ url_for('year_view', year=2013) }}">DNE link</a></p>\n'''
new_line = '''<p><a href="{{ url_for('year_view', year=2013) }}">DNE link</a></p>\n''' # noqa: E501
with open(os.path.join('pages', 'about.html'), 'w') as about_file:
for line in lines:
if '<p>This is the about page.</p>' in line:
Expand Down Expand Up @@ -381,7 +381,7 @@ def test_build_month_404_no_posts():
with open(os.path.join('pages', 'about.html'), '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'''
new_line = '''<p><a href="{{ url_for('month_view', year=2014, month='01') }}">DNE link</a></p>\n''' # noqa: E501
with open(os.path.join('pages', 'about.html'), 'w') as about_file:
for line in lines:
if '<p>This is the about page.</p>' in line:
Expand Down Expand Up @@ -409,7 +409,7 @@ def test_build_day_404_no_posts():
with open(os.path.join('pages', 'about.html'), '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'''
new_line = '''<p><a href="{{ url_for('day_view', year=2014, month='10', day='29') }}">DNE link</a></p>\n''' # noqa: E501
with open(os.path.join('pages', 'about.html'), 'w') as about_file:
for line in lines:
if '<p>This is the about page.</p>' in line:
Expand Down Expand Up @@ -452,10 +452,9 @@ def test_build_updated_time_is_added():
runner.invoke(start)
with open(os.path.join('posts', 'example.md'), 'r') as post_file:
b_lines = post_file.readlines()
runner.invoke(build)
with open(os.path.join('posts', 'example.md'), 'r') as post_file:
a_lines = post_file.readlines()
for b_line, a_line in zip(b_lines, a_lines):
for b_line, a_line in zip(b_lines, a_lines, strict=True):
if 'updated' in b_line:
b_updated = b_line
a_updated = a_line
Expand All @@ -479,7 +478,7 @@ def test_build_updated_time_is_added():
assert b_datetime.minute == 0
assert b_datetime.second == 0

date_with_current_time = datetime.datetime.now().replace(
date_with_current_time = datetime.datetime.now(tz=datetime.UTC).replace(
year=a_datetime.year,
month=a_datetime.month,
day=a_datetime.day,
Expand All @@ -505,7 +504,7 @@ def test_build_published_time_is_added():
runner.invoke(build)
with open(os.path.join('posts', 'example.md'), 'r') as post_file:
a_lines = post_file.readlines()
for b_line, a_line in zip(b_lines, a_lines):
for b_line, a_line in zip(b_lines, a_lines, strict=True):
if 'published' in b_line:
b_published = b_line
a_published = a_line
Expand All @@ -522,7 +521,7 @@ def test_build_published_time_is_added():
assert b_datetime.minute == 0
assert b_datetime.second == 0

date_with_current_time = datetime.datetime.now().replace(
date_with_current_time = datetime.datetime.now(datetime.UTC).replace(
year=a_datetime.year,
month=a_datetime.month,
day=a_datetime.day,
Expand Down Expand Up @@ -559,7 +558,7 @@ def test_build_updated_is_added():
a_datetime_str = a_updated.replace('updated:', '').strip()
a_datetime = datetime.datetime.fromisoformat(a_datetime_str)

date_with_current_time = datetime.datetime.now().replace(
date_with_current_time = datetime.datetime.now(datetime.UTC).replace(
year=a_datetime.year,
month=a_datetime.month,
day=a_datetime.day,
Expand Down
4 changes: 3 additions & 1 deletion tests/test_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ def test_preview():
with runner.isolated_filesystem():
result = runner.invoke(start)
result = runner.invoke(preview)
assert result.exit_code == 5
# Why is this 5?
expected_exit_code = 5
assert result.exit_code == expected_exit_code
2 changes: 1 addition & 1 deletion tests/test_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def test_verify_site_name_empty():
assert result.output == expected_output


def test_verify_SITE_NAME_missing():
def test_verify_site_name_missing():
expected_output = (
'All posts are correctly formatted.\n'
'[site] name is not set in config.toml.\n'
Expand Down

0 comments on commit cd400f4

Please sign in to comment.