Skip to content

Commit

Permalink
Add a basic test-suite
Browse files Browse the repository at this point in the history
  • Loading branch information
praiskup committed Oct 2, 2024
1 parent 51bd21d commit 24b6058
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__pycache__
.coverage
*.bundle
test-repo
1 change: 1 addition & 0 deletions .tito/tito.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ builder = tito.builder.Builder
tagger = tito.tagger.VersionTagger
changelog_do_not_remove_cherrypick = 0
changelog_format = %s
fetch_sources = True
10 changes: 10 additions & 0 deletions run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#! /bin/bash -e
cov=--cov
args=()
for arg; do
case $arg in
--no-cov) cov= ;;
*) args+=( "$arg" )
esac
done
exec python3 -m pytest -vv $cov "${args[@]}"
28 changes: 28 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
vcs-diff-lint tests
"""

import os
import shutil
from subprocess import Popen, PIPE, check_call

SRCDIR = os.path.normpath(os.path.join(os.path.dirname(__file__), ".."))
GITROOTDIR = os.path.join(SRCDIR, 'test-repo')

bundle = None
with Popen(['spectool', '-S', 'vcs-diff-lint.spec'],
stdout=PIPE, cwd=SRCDIR) as spectool:
with Popen(['sed', '-n', 's/^Source1: \\(.*\\)/\\1/p'],
stdin=spectool.stdout, stdout=PIPE,
encoding="utf-8") as sed:
bundle, _ = sed.communicate()
bundle = os.path.basename(bundle.strip())

try:
shutil.rmtree(GITROOTDIR)
except FileNotFoundError:
pass
check_call(['git', 'clone', bundle, GITROOTDIR], cwd=SRCDIR)

os.environ["PATH"] = SRCDIR+":"+os.environ["PATH"]
check_call(["bash", "-c", "env"])
31 changes: 31 additions & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
Run vcs-diff-lint tests against the bundled git repo.
"""

import subprocess
from tests import GITROOTDIR


def run_vcs_diff_lint(old, new, expected_output):
""" Run vcs-diff-lint in the datadir """
subprocess.check_call(["git", "checkout", new], cwd=GITROOTDIR)
out = subprocess.run(["vcs-diff-lint", "--compare-against",
old], cwd=GITROOTDIR, stdout=subprocess.PIPE,
check=False, encoding="utf-8")
assert out.stdout == expected_output


def test_basic():
"""
Test added issues.
"""
old = '1059de39'
new = '51806b39'
data="""\
Error: PYLINT_WARNING:
python/hello-world-python:1: C0114[missing-module-docstring]: Missing module docstring
Error: PYLINT_WARNING:
python/hello-world-python:4: C0116[missing-function-docstring]: api_method: Missing function or method docstring
"""
run_vcs_diff_lint(old, new, data)
4 changes: 2 additions & 2 deletions vcs-diff-lint-csdiff-pylint
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The script accepts the same parameters as pylint itself.

import sys
import json
import subprocess
from subprocess import run, PIPE

PYLINT = ["pylint", "-rn", "--score=no", "--output-format=json"]

Expand All @@ -19,7 +19,7 @@ def _main():
pylint_command = PYLINT + sys.argv[1:]

# pylint: disable=subprocess-run-check
pylint_result = subprocess.run(pylint_command, capture_output=True)
pylint_result = run(pylint_command, stdout=PIPE, stderr=PIPE)
data = json.loads(pylint_result.stdout)
for defect in data:
message = defect["obj"] + ": " if defect["obj"] else ""
Expand Down
26 changes: 26 additions & 0 deletions vcs-diff-lint.spec
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,38 @@ Release: 1%{?dist}
Summary: VCS Differential Code Analysis Tool
BuildArch: noarch

%if 0%{?rhel} == 10
# missing csdiff / pylint
%bcond_with check
%else
%bcond_without check
%endif

License: GPL-2.0-or-later
URL: https://github.com/fedora-copr/vcs-diff-lint
# Source is created by:
# git clone %%url && cd vcs-diff-lint
# tito build --tgz --tag %%name-%%version-%%release
Source0: %name-%version.tar.gz

Source1: https://github.com/praiskup/vcs-diff-lint-testdata/releases/download/v1.0.0/vcs-diff-lint-testdata-1.0.0.bundle

Requires: csdiff
Requires: git
Recommends: pylint
Recommends: python3-mypy
Recommends: python3-types-requests
Recommends: ruff

%if %{with check}
BuildRequires: csdiff
BuildRequires: git
BuildRequires: pylint
BuildRequires: rpmdevtools
BuildRequires: python3-pytest
%endif


%description
Analyze code, and print only reports related to a particular change.

Expand All @@ -31,6 +49,9 @@ added (or even fixed, as opt-in) analyzers' warnings.

%prep
%autosetup
%if %{with check}
cp %{SOURCE1} ./
%endif


%build
Expand All @@ -44,6 +65,11 @@ install -p vcs-diff-lint-csdiff-pylint %buildroot%_bindir
install -p vcs-diff-lint-csdiff-mypy %buildroot%_bindir
install -p vcs-diff-lint-csdiff-ruff %buildroot%_bindir

%if %{with check}
%check
./run-tests.sh --no-cov
%endif


%files
%license LICENSE
Expand Down

0 comments on commit 24b6058

Please sign in to comment.