Skip to content

Commit

Permalink
Test read violations from file
Browse files Browse the repository at this point in the history
  • Loading branch information
blablatdinov committed Nov 5, 2024
1 parent f8f9a5d commit 1f640a6
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 8 deletions.
35 changes: 35 additions & 0 deletions ondivi/_internal/linter_output_from_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# The MIT License (MIT).
#
# Copyright (c) 2024 Almaz Ilaletdinov <a.ilaletdinov@yandex.ru>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.

"""Read violations from file."""

import sys
import os

from pathlib import Path


def linter_output_from_file(file_path: Path):
if not Path(file_path).exists():
sys.stdout.write('File with violations "{0}" not found\n'.format(file_path))
raise FileNotFoundError
return Path(file_path).read_text(encoding='utf-8').strip().splitlines()
13 changes: 5 additions & 8 deletions ondivi/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

from ondivi._internal.define_changed_lines import define_changed_lines
from ondivi._internal.filter_out_violations import filter_out_violations
from ondivi._internal.linter_output_from_file import linter_output_from_file
from ondivi._internal.types import (
ActualViolationsListStr,
BaselineStr,
Expand Down Expand Up @@ -73,13 +74,6 @@ def controller(
)


def _linter_output_from_file(file_path: Path):
if not Path(file_path).exists():
sys.stdout.write('File with violations "{0}" not found\n'.format(file_path))
sys.exit(1)
return Path(file_path).read_text(encoding='utf-8').strip().splitlines()


def cli(
baseline: BaselineStr,
fromfile: FromFilePathStr | None,
Expand All @@ -94,7 +88,10 @@ def cli(
:param only_violations: bool
"""
if fromfile:
linter_output = _linter_output_from_file(fromfile)
try:
linter_output = linter_output_from_file(fromfile)
except FileNotFoundError:
sys.exit(1)
else:
linter_output = sys.stdin.read().strip().splitlines()
try:
Expand Down
60 changes: 60 additions & 0 deletions tests/unit/_internal/test_linter_output_from_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# The MIT License (MIT).
#
# Copyright (c) 2024 Almaz Ilaletdinov <a.ilaletdinov@yandex.ru>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.

"""Tests read violations from file."""

import uuid
from pathlib import Path

import pytest

# _internal allow into ondivi app
from ondivi._internal.linter_output_from_file import linter_output_from_file # noqa: WPS436


@pytest.fixture
def violations() -> list[str]:
return [
'django/db/models/sql/query.py:1404:9: ANN201 Missing return type annotation for public function `try_transform`',
'django/db/models/sql/query.py:1404:29: ANN001 Missing type annotation for function argument `lhs`',
'django/db/models/sql/query.py:1404:34: ANN001 Missing type annotation for function argument `name`',
]


@pytest.fixture
def file(tmp_path: Path, violations: list[str]) -> Path:
path = tmp_path / 'file.txt'
path.write_text('\n'.join(violations))
return path


def test_linter_out_from_file(file: Path, violations: list[str]) -> None:
"""Test read violations from file."""
got = linter_output_from_file(file)

assert got == violations


def test_file_not_found() -> None:
"""Test file not found."""
with pytest.raises(FileNotFoundError):
linter_output_from_file('/{0}'.format(uuid.uuid4()))

0 comments on commit 1f640a6

Please sign in to comment.