Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement integration test #12

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,10 @@ lint:
poetry run flake8 ondivi tests
poetry run mypy ondivi tests --strict

test:
poetry run pytest --cov=ondivi --cov-report=term-missing:skip-covered -vv
test: unit it

unit:
poetry run pytest tests/unit --cov=ondivi --cov-report=term-missing:skip-covered -vv

it: # integration tests
poetry run pytest tests/it -s -vv
25 changes: 24 additions & 1 deletion ondivi/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
only for changed lines in a Git repo.
"""

import argparse
import sys
from contextlib import suppress

Expand Down Expand Up @@ -99,10 +100,32 @@ def controller(diff: Diff, violations: list[str]) -> list[str]:

def main() -> None:
"""Entrypoint."""
parser = argparse.ArgumentParser(
description='\n'.join([
'Ondivi (Only diff violations).\n',
'Python script filtering coding violations, identified by static analysis,',
'only for changed lines in a Git repo.\n',
'Usage example:\n',
'flake8 script.py | ondivi',
]),
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument(
'--baseline',
dest='baseline',
type=str,
default='master',
help=' '.join([
'Commit or branch which will contain legacy code.',
'Program filter out violations on baseline',
'(default: "master")',
]),
)
args = parser.parse_args()
violations = sys.stdin.read().strip().splitlines()
sys.stdout.write('\n'.join(
controller(
Repo('.').git.diff('--unified=0', 'origin/master..HEAD'),
Repo('.').git.diff('--unified=0', args.baseline),
violations,
),
))
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ ignore = [
"ARG002", # Unused method argument
"D203", # no-blank-line-before-class
"D213", # multi-line-summary-first-line
"D401", # First line of docstring should be in imperative mood
"D418", # Function decorated with `@overload` shouldn't contain a docstring
"FBT001", # Boolean-typed positional argument in function definition
"FBT002", # Boolean-typed positional argument in function definition
Expand Down
Binary file added tests/fixtures/ondivi-test-repo.zip
Binary file not shown.
81 changes: 81 additions & 0 deletions tests/it/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# The MIT License (MIT).
#
# Copyright (c) 2024 Almaz Ilaletdinov <[email protected]>
#
# 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.

"""Integration test with installing and check on real git repo."""

import os
import subprocess
import zipfile
from pathlib import Path

import pytest
from _pytest.legacypath import TempdirFactory


@pytest.fixture(scope='module')
def current_dir() -> Path:
"""Current directory for installing actual ondivi."""
return Path().absolute()


# flake8: noqa: S603, S607. Not a production code
@pytest.fixture(scope='module')
def _test_repo(tmpdir_factory: TempdirFactory, current_dir: str) -> None:
"""Real git repository."""
tmp_path = tmpdir_factory.mktemp('test')
with zipfile.ZipFile('tests/fixtures/ondivi-test-repo.zip', 'r') as zip_ref:
zip_ref.extractall(tmp_path)
os.chdir(tmp_path / 'ondivi-test-repo')
subprocess.run(['python', '-m', 'venv', 'venv'], check=True)
subprocess.run(['venv/bin/pip', 'install', 'flake8', str(current_dir)], check=True)


@pytest.mark.usefixtures('_test_repo')
def test() -> None:
"""Test script with real git repo."""
got = subprocess.run(
['ondivi', '--baseline', '56faa56'],
stdin=subprocess.Popen(
['venv/bin/flake8', 'file.py'],
stdout=subprocess.PIPE,
).stdout,
stdout=subprocess.PIPE,
check=False,
).stdout.decode('utf-8').strip()

assert got == 'file.py:4:80: E501 line too long (119 > 79 characters)'


@pytest.mark.usefixtures('_test_repo')
def test_baseline_default() -> None:
"""Test baseline default."""
got = subprocess.run(
['ondivi'],
stdin=subprocess.Popen(
['venv/bin/flake8', 'file.py'],
stdout=subprocess.PIPE,
).stdout,
stdout=subprocess.PIPE,
check=False,
).stdout.decode('utf-8').strip()

assert got == 'file.py:4:80: E501 line too long (119 > 79 characters)'
File renamed without changes.
Loading