Skip to content

Commit

Permalink
finish fixing all new ruff checks
Browse files Browse the repository at this point in the history
  • Loading branch information
TabulateJarl8 committed Nov 17, 2024
1 parent ba53398 commit ff1ce58
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
12 changes: 11 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,17 @@ ignore = [


[tool.ruff.lint.per-file-ignores]
"tests/*" = ["S101", "ANN001", "ANN002", "PLC2701", "ARG002", "PLR2004", "DOC"]
"tests/*" = [
"S101",
"ANN001",
"ANN002",
"PLC2701",
"ARG002",
"PLR2004",
"DOC",
"INP001",
"S",
]
"randfacts/randfacts.py" = ["T201"]
"randfacts/__main__.py" = ["D100"]

Expand Down
10 changes: 6 additions & 4 deletions tests/fix_encoding.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Fixes common encoding errors that can get into the fact lists after web scraping."""

from pathlib import Path

parent = Path(__file__).resolve().parent.parent
Expand All @@ -6,15 +8,15 @@
unsafe_path = parent / "randfacts" / "unsafe.txt"

bad_characters = [
("‘", "'"),
("’", "'"),
("‘", "'"), # noqa: RUF001
("’", "'"), # noqa: RUF001
("“", '"'),
("”", '"'),
("…", "..."),
("—", "-"),
]

with open(safe_path, "r+", encoding="utf-8") as f:
with safe_path.open("r+", encoding="utf-8") as f:
safe = f.read()

for char in bad_characters:
Expand All @@ -23,7 +25,7 @@
f.seek(0)
f.write(safe)

with open(unsafe_path, "r+", encoding="utf-8") as f:
with unsafe_path.open("r+", encoding="utf-8") as f:
unsafe = f.read()

for char in bad_characters:
Expand Down
27 changes: 25 additions & 2 deletions tests/test_general.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""General functionality unit tests."""

import pathlib
import subprocess
import sys
Expand All @@ -12,33 +14,47 @@


def test_get_fact() -> None:
"""Make sure get_fact works without extra arguments."""
assert isinstance(randfacts.get_fact(), str), "get_fact() must return a string"


def test_getFact_deprecated() -> None:
def test_getFact_deprecated() -> None: # noqa: N802
"""Make sure getFact throws a deprecation warning."""
with pytest.deprecated_call():
_ = getFact()


def test_all_facts_list() -> None:
"""Test that all_facts list is present in the module."""
assert isinstance(randfacts.all_facts, list), "all_facts must be a list"


def test_safe_facts_list() -> None:
"""Test that safe_facts list is present in the module."""
assert isinstance(randfacts.safe_facts, list), "safe_facts must be a list"


def test_unsafe_facts_list() -> None:
"""Test that unsafe_facts list is present in the module."""
assert isinstance(randfacts.unsafe_facts, list), "unsafe_facts must be a list"


def test_cli_no_args() -> None:
"""Test that a basic randfacts CLI call will work."""
child = subprocess.Popen(["python3", "-m", "randfacts"], stdout=subprocess.DEVNULL)
child.communicate()
assert child.returncode == 0, "`python3 -m randfacts` must return with exit code 0"


def test_cli_script_installed() -> None:
"""Test that the `randfacts` script is installed to the PATH."""
child = subprocess.Popen(["randfacts"], stdout=subprocess.DEVNULL)
child.communicate()
assert child.returncode == 0, "`randfacts` must return with exit code 0"


def test_cli_unsafe_args() -> None:
"""Test that CLI with --unsafe works."""
child = subprocess.Popen(
["python3", "-m", "randfacts", "--unsafe"],
stdout=subprocess.DEVNULL,
Expand All @@ -50,6 +66,7 @@ def test_cli_unsafe_args() -> None:


def test_cli_mixed_args() -> None:
"""Test that CLI with --mixed works."""
child = subprocess.Popen(
["python3", "-m", "randfacts", "--mixed"],
stdout=subprocess.DEVNULL,
Expand All @@ -61,6 +78,7 @@ def test_cli_mixed_args() -> None:


def test_cli_version() -> None:
"""Test that CLI with --version returns the correct version."""
child = subprocess.Popen(
["python3", "-m", "randfacts", "--version"],
stdout=subprocess.PIPE,
Expand All @@ -73,6 +91,7 @@ def test_cli_version() -> None:


def test_main_entrypoint() -> None:
"""Test the main entrypoint in randfacts.py."""
# Path to the module or script you want to test
script_path = (
pathlib.Path(__file__).resolve().parents[1] / "randfacts" / "randfacts.py"
Expand All @@ -90,8 +109,12 @@ def test_main_entrypoint() -> None:
assert result.returncode == 0, f"Script failed with stderr: {result.stderr}"


@pytest.mark.parametrize("bad_char", ["‘", "’", "“", "”", "…", "—"])
@pytest.mark.parametrize("bad_char", ["‘", "’", "“", "”", "…", "—"]) # noqa: RUF001
def test_invalid_characters(bad_char: str) -> None:
"""Make sure no invalid characters are present in the fact lists.
If this test fails, try running `fix_encoding.py`
"""
for index, fact in enumerate(randfacts.all_facts):
assert (
bad_char not in fact
Expand Down

0 comments on commit ff1ce58

Please sign in to comment.