Skip to content

Commit

Permalink
Merge pull request #138 from venthur/ruff
Browse files Browse the repository at this point in the history
switch from flake8 to ruff
  • Loading branch information
venthur authored Sep 20, 2023
2 parents 6431ffa + f2900fd commit 557077e
Show file tree
Hide file tree
Showing 17 changed files with 95 additions and 15 deletions.
2 changes: 0 additions & 2 deletions .flake8

This file was deleted.

5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [unreleased] -- XXXXXXX

* switched from flake8 to ruff
* added missing docstrings

## [2.1.0] -- 2023-08-27

* default theme: `img` have now `max-width: 100%` by default to avoid very
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ mypy: $(VENV)

.PHONY: lint
lint: $(VENV)
$(BIN)/flake8
$(BIN)/ruff check .

.PHONY: build
build: $(VENV)
Expand Down
8 changes: 3 additions & 5 deletions blag/blag.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env python3

"""blag's core methods.
"""
"""blag's core methods."""

# remove when we don't support py38 anymore
from __future__ import annotations
Expand Down Expand Up @@ -32,7 +30,7 @@


def main(arguments: list[str] | None = None) -> None:
"""Main entrypoint for the CLI.
"""Run the CLI.
This method parses the CLI arguments and executes the respective
commands.
Expand Down Expand Up @@ -328,7 +326,7 @@ def process_markdown(
for src, dst in convertibles:
logger.debug(f"Processing {src}")

with open(f"{input_dir}/{src}", "r") as fh:
with open(f"{input_dir}/{src}") as fh:
body = fh.read()

content, meta = convert_markdown(md, body)
Expand Down
5 changes: 4 additions & 1 deletion blag/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ def convert_markdown(


class MarkdownLinkTreeprocessor(Treeprocessor):
"""Converts relative links to .md files to .html"""
"""Converts relative links to .md files to .html."""

def run(self, root: Element) -> Element:
"""Process the ElementTree."""
for element in root.iter():
if element.tag == "a":
url = element.get("href")
Expand All @@ -109,6 +110,7 @@ def run(self, root: Element) -> Element:
return root

def convert(self, url: str) -> str:
"""Convert relative .md-links to .html-links."""
scheme, netloc, path, query, fragment = urlsplit(url)
logger.debug(
f"{url}: {scheme=} {netloc=} {path=} {query=} {fragment=}"
Expand All @@ -126,6 +128,7 @@ class MarkdownLinkExtension(Extension):
"""markdown.extension that converts relative .md- to .html-links."""

def extendMarkdown(self, md: Markdown) -> None:
"""Register the MarkdownLinkTreeprocessor."""
md.treeprocessors.register(
MarkdownLinkTreeprocessor(md),
"mdlink",
Expand Down
4 changes: 1 addition & 3 deletions blag/quickstart.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""Helper methods for blag's quickstart command.
"""
"""Helper methods for blag's quickstart command."""

# remove when we don't support py38 anymore
from __future__ import annotations
Expand Down
2 changes: 2 additions & 0 deletions blag/version.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
"""Version information for the blag package."""

__VERSION__ = "2.1.0"
17 changes: 16 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dev = [
"wheel",
"pytest",
"pytest-cov",
"flake8",
"ruff",
"mypy",
"types-markdown",
]
Expand All @@ -61,6 +61,21 @@ addopts = """
--cov-report=term-missing:skip-covered
"""

[tool.ruff]
select = [
"F", # pyflakes
"E", "W", # pycodestyle
"C90", # mccabe
"I", # isort
"D", # pydocstyle
"UP" # pyupgrade
]
line-length = 79
target-version = "py38"

[tool.ruff.pydocstyle]
convention = "numpy"

[tool.mypy]
files = "blag,tests"
strict = true
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ twine==4.0.2
wheel==0.41.2
pytest==7.4.2
pytest-cov==4.1.0
flake8==6.1.0
ruff==0.0.290
mypy==1.5.1
types-markdown==3.4.2.10
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Tests for blag."""
11 changes: 11 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""Pytest fixtures."""


# remove when we don't support py38 anymore
from __future__ import annotations

Expand All @@ -14,6 +17,7 @@

@pytest.fixture
def environment(cleandir: str) -> Iterator[Environment]:
"""Create a Jinja2 environment."""
site = {
"base_url": "site base_url",
"title": "site title",
Expand All @@ -26,31 +30,37 @@ def environment(cleandir: str) -> Iterator[Environment]:

@pytest.fixture
def page_template(environment: Environment) -> Iterator[Template]:
"""Create a Jinja2 page-template."""
yield environment.get_template("page.html")


@pytest.fixture
def article_template(environment: Environment) -> Iterator[Template]:
"""Create a Jinja2 article-template."""
yield environment.get_template("article.html")


@pytest.fixture
def index_template(environment: Environment) -> Iterator[Template]:
"""Create a Jinja2 index-template."""
yield environment.get_template("index.html")


@pytest.fixture
def archive_template(environment: Environment) -> Iterator[Template]:
"""Create a Jinja2 archive-template."""
yield environment.get_template("archive.html")


@pytest.fixture
def tags_template(environment: Environment) -> Iterator[Template]:
"""Create a Jinja2 tags-template."""
yield environment.get_template("tags.html")


@pytest.fixture
def tag_template(environment: Environment) -> Iterator[Template]:
"""Create a Jinja2 tag-template."""
yield environment.get_template("tag.html")


Expand Down Expand Up @@ -80,6 +90,7 @@ def cleandir() -> Iterator[str]:

@pytest.fixture
def args(cleandir: Callable[[], Iterator[str]]) -> Iterator[Namespace]:
"""Create a Namespace with default arguments."""
args = Namespace(
input_dir="content",
output_dir="build",
Expand Down
15 changes: 15 additions & 0 deletions tests/test_blag.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""Test blag."""


# remove when we don't support py38 anymore
from __future__ import annotations

Expand All @@ -15,12 +18,14 @@


def test_generate_feed(cleandir: str) -> None:
"""Test generate_feed."""
articles: list[tuple[str, dict[str, Any]]] = []
blag.generate_feed(articles, "build", " ", " ", " ", " ")
assert os.path.exists("build/atom.xml")


def test_feed(cleandir: str) -> None:
"""Test feed."""
articles: list[tuple[str, dict[str, Any]]] = [
(
"dest1.html",
Expand Down Expand Up @@ -73,6 +78,7 @@ def test_feed(cleandir: str) -> None:


def test_generate_feed_with_description(cleandir: str) -> None:
"""Test generate_feed with description."""
# if a description is provided, it will be used as the summary in
# the feed, otherwise we simply use the title of the article
articles: list[tuple[str, dict[str, Any]]] = [
Expand All @@ -98,6 +104,7 @@ def test_generate_feed_with_description(cleandir: str) -> None:


def test_parse_args_build() -> None:
"""Test parse_args with build."""
# test default args
args = blag.parse_args(["build"])
assert args.input_dir == "content"
Expand Down Expand Up @@ -131,6 +138,7 @@ def test_parse_args_build() -> None:


def test_get_config() -> None:
"""Test get_config."""
config = """
[main]
base_url = https://example.com/
Expand Down Expand Up @@ -180,6 +188,7 @@ def test_get_config() -> None:


def test_environment_factory(cleandir: str) -> None:
"""Test environment_factory."""
globals_: dict[str, object] = {"foo": "bar", "test": "me"}
env = blag.environment_factory("templates", globals_=globals_)
assert env.globals["foo"] == "bar"
Expand All @@ -191,6 +200,7 @@ def test_process_markdown(
page_template: Template,
article_template: Template,
) -> None:
"""Test process_markdown."""
page1 = """\
title: some page
Expand Down Expand Up @@ -240,6 +250,7 @@ def test_process_markdown(


def test_build(args: Namespace) -> None:
"""Test build."""
page1 = """\
title: some page
Expand Down Expand Up @@ -313,16 +324,19 @@ def test_build(args: Namespace) -> None:
],
)
def test_missing_template_raises(template: str, args: Namespace) -> None:
"""Test that missing templates raise SystemExit."""
os.remove(f"templates/{template}")
with pytest.raises(SystemExit):
blag.build(args)


def test_main(cleandir: str) -> None:
"""Test main."""
blag.main(["build"])


def test_cli_version(capsys: CaptureFixture[str]) -> None:
"""Test --version."""
with pytest.raises(SystemExit) as ex:
blag.main(["--version"])
# normal system exit
Expand All @@ -333,6 +347,7 @@ def test_cli_version(capsys: CaptureFixture[str]) -> None:


def test_cli_verbose(cleandir: str, caplog: LogCaptureFixture) -> None:
"""Test --verbose."""
blag.main(["build"])
assert "DEBUG" not in caplog.text

Expand Down
6 changes: 6 additions & 0 deletions tests/test_devserver.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""Tests for the devserver module."""


# remove when we don't support py38 anymore
from __future__ import annotations

Expand All @@ -11,6 +14,7 @@


def test_get_last_modified(cleandir: str) -> None:
"""Test get_last_modified."""
# take initial time
t1 = devserver.get_last_modified(["content"])

Expand All @@ -29,6 +33,7 @@ def test_get_last_modified(cleandir: str) -> None:


def test_autoreload_builds_immediately(args: Namespace) -> None:
"""Test autoreload builds immediately."""
# create a dummy file that can be build
with open("content/test.md", "w") as fh:
fh.write("boo")
Expand All @@ -54,6 +59,7 @@ def test_autoreload_builds_immediately(args: Namespace) -> None:
"ignore::pytest.PytestUnhandledThreadExceptionWarning"
)
def test_autoreload(args: Namespace) -> None:
"""Test autoreload."""
t = threading.Thread(
target=devserver.autoreload,
args=(args,),
Expand Down
9 changes: 9 additions & 0 deletions tests/test_markdown.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""Test markdown module."""


# remove when we don't support py38 anymore
from __future__ import annotations

Expand Down Expand Up @@ -34,6 +37,7 @@
],
)
def test_convert_markdown_links(input_: str, expected: str) -> None:
"""Test convert_markdown."""
md = markdown_factory()
html, _ = convert_markdown(md, input_)
assert expected in html
Expand All @@ -51,6 +55,7 @@ def test_convert_markdown_links(input_: str, expected: str) -> None:
],
)
def test_dont_convert_normal_links(input_: str, expected: str) -> None:
"""Test convert_markdown doesn't convert normal links."""
md = markdown_factory()
html, _ = convert_markdown(md, input_)
assert expected in html
Expand All @@ -70,17 +75,20 @@ def test_dont_convert_normal_links(input_: str, expected: str) -> None:
],
)
def test_convert_metadata(input_: str, expected: dict[str, Any]) -> None:
"""Test convert_markdown converts metadata correctly."""
md = markdown_factory()
_, meta = convert_markdown(md, input_)
assert expected == meta


def test_markdown_factory() -> None:
"""Test markdown_factory."""
md = markdown_factory()
assert isinstance(md, markdown.Markdown)


def test_smarty() -> None:
"""Test smarty."""
md = markdown_factory()

md1 = """
Expand All @@ -95,6 +103,7 @@ def test_smarty() -> None:


def test_smarty_code() -> None:
"""Test smarty doesn't touch code."""
md = markdown_factory()

md1 = """
Expand Down
Loading

0 comments on commit 557077e

Please sign in to comment.