Skip to content

Commit 7d570db

Browse files
committed
[fix] Update code, add tests, fix tests
1 parent 8ecfe15 commit 7d570db

File tree

7 files changed

+71
-13
lines changed

7 files changed

+71
-13
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
55

66
[project]
77
name = "python-code-validator"
8-
version = "0.1.0"
8+
version = "0.1.1"
99
authors = [{ name = "Qu1nel", email = "covach.qn@gmail.com" }]
1010
description = "A flexible framework for static validation of Python code based on JSON rules."
1111
readme = "README.md"
@@ -34,6 +34,7 @@ validate-code = "code_validator.cli:run_from_cli"
3434
[project.optional-dependencies]
3535
dev = [
3636
"ruff>=0.4.0",
37+
"flake8>=7.0.0",
3738
"build",
3839
"twine",
3940
"coverage>=7.5.0",

src/code_validator/output.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from .config import LogLevel
1313

14-
LOG_FORMAT = "%(asctime)s - [%(levelname)s] - %(message)s"
14+
LOG_FORMAT = "%(asctime)s | %(filename)-15s | %(funcName)-15s (%(lineno)-3s) | [%(levelname)s] - %(message)s"
1515
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
1616

1717

@@ -65,10 +65,10 @@ def __init__(self, logger: logging.Logger, *, is_silent: bool = False):
6565
self._stdout = sys.stdout
6666

6767
def print(
68-
self,
69-
message: str,
70-
*,
71-
level: LogLevel | Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = LogLevel.INFO,
68+
self,
69+
message: str,
70+
*,
71+
level: LogLevel | Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = LogLevel.INFO,
7272
) -> None:
7373
"""Prints a message to stdout and logs it simultaneously.
7474

tests/fixtures/debug_pep8.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"description": "Только проверка PEP8 для отладки",
3+
"validation_rules": [
4+
{
5+
"rule_id": 999,
6+
"type": "check_linter_pep8",
7+
"message": "Найдены ошибки PEP8"
8+
}
9+
]
10+
}

tests/fixtures/p01_simple_program.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
"""Simple program for basic checks."""
2-
# Этот файл должен проходить базовую валидацию
32
import sys
43

54
GLOBAL_CONST = "ALLOWED"
65

76

87
def solve():
98
"""This function is required."""
10-
x = 12 # local var
9+
x = 12
1110
print(x)
12-
print(sys.version)
11+
# Используем sys, чтобы flake8 был доволен
12+
print(f"Python version: {sys.version_info.major}.{sys.version_info.minor}")
1313

1414

1515
def main():

tests/fixtures/r_full_py_general.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,9 @@
169169
"type": "value_must_be_in",
170170
"allowed_values": [
171171
"__main__",
172-
"OK",
173-
"ALLOWED"
172+
"ALLOWED",
173+
"Python version: ",
174+
"."
174175
]
175176
}
176177
}

tests/test_validator.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ def test_run_with_malformed_rule_raises_error(self):
9393

9494
def test_linter_rule_passes_for_valid_code(self):
9595
"""Tests that CheckLinterRule passes for clean code."""
96-
rules = {"validation_rules": [{"rule_id": 1, "type": "check_linter_pep8", "message": "PEP8 fail"}]}
96+
rules = {"validation_rules": [{"rule_id": 1, "type": "check_linter_pep8", "message": "PEP8 fail", "params": {
97+
"ignore": ["F401", "E302", "E305", "E261"]
98+
}}]}
9799
rules_path = FIXTURES_DIR / "temp_linter_rules.json"
98100
with open(rules_path, "w", encoding="utf-8") as f:
99101
# noinspection PyTypeChecker
@@ -213,6 +215,7 @@ def test_full_rule_sets(self):
213215

214216
test_cases = [
215217
# Название теста, файл с кодом, файл с правилами, ожидаемый результат (True=pass)
218+
("debug_pep8_valid", "p01_simple_program.py", "debug_pep8.json", True),
216219
("py_general_valid", "p01_simple_program.py", "r_full_py_general.json", True),
217220
("api_valid", "p06_api_client.py", "r_full_api_rules.json", True),
218221
("flask_valid", "p08_flask_app.py", "r_full_flask_rules.json", True),

uv.lock

Lines changed: 44 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)