-- -- --- --- --- --- --- --- ------- ------- ------- |
"""
-- -- --- --- --- --- --- --- ------- ------- ------- |
Parameterised tests for each operator (and some combinations).
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
Build them and test out some of the metrics/commands work correctly.
-- -- --- --- --- --- --- --- ------- ------- ------- |
"""
-- -- --- --- --- --- --- --- ------- ------- ------- |
import pathlib
-- -- --- --- --- --- --- --- ------- ------- ------- |
import sys
-- -- --- --- --- --- --- --- ------- ------- ------- |
from textwrap import dedent
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
import pytest
-- -- --- --- --- --- --- --- ------- ------- ------- |
from click.testing import CliRunner
-- -- --- --- --- --- --- --- ------- ------- ------- |
from git.repo.base import Repo
-- -- --- --- --- --- --- --- ------- ------- ------- |
from git.util import Actor
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
import wily.__main__ as main
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
_path = "src\\test.py" if sys.platform == "win32" else "src/test.py"
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
operators = (
-- -- --- --- --- --- --- --- ------- ------- ------- |
"halstead",
-- -- --- --- --- --- --- --- ------- ------- ------- |
"cyclomatic",
-- -- --- --- --- --- --- --- ------- ------- ------- |
"maintainability",
-- -- --- --- --- --- --- --- ------- ------- ------- |
"raw",
-- -- --- --- --- --- --- --- ------- ------- ------- |
"halstead,cyclomatic",
-- -- --- --- --- --- --- --- ------- ------- ------- |
"maintainability,raw",
-- -- --- --- --- --- --- --- ------- ------- ------- |
"halstead,cyclomatic,maintainability,raw",
-- -- --- --- --- --- --- --- ------- ------- ------- |
)
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows")
-- -- --- --- --- --- --- --- ------- ------- ------- |
@pytest.mark.parametrize("operator", operators)
-- 01 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
def test_operator(operator, gitdir):
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
runner = CliRunner()
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
result = runner.invoke(
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
main.cli,
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
["--debug", "--path", gitdir, "build", "src", "-o", operator],
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
catch_exceptions=False,
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
)
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
assert result.exit_code == 0, result.stdout
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
result = runner.invoke(
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
main.cli, ["--debug", "--path", gitdir, "report", _path], catch_exceptions=False
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
)
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
assert result.exit_code == 0, result.stdout
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
result = runner.invoke(
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
main.cli,
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
["--debug", "--path", gitdir, "diff", _path, "--all"],
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
catch_exceptions=False,
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
)
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
assert result.exit_code == 0, result.stdout
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
assert "test.py" in result.stdout
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
complex_test = """
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
import abc
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
foo = 1
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
def function1():
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
a = 1 + 1
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
if a == 2:
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
print(1)
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
class Class1(object):
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
def method(self):
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
b = 1 + 5
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
if b == 6:
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
if 1==2:
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
if 2==3:
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
print(1)
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
"""
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
with open(pathlib.Path(gitdir) / "src" / "test.py", "w") as test_py:
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
test_py.write(dedent(complex_test))
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
result = runner.invoke(
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
main.cli,
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
["--debug", "--path", gitdir, "diff", _path, "--all"],
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
catch_exceptions=False,
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
)
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
assert result.exit_code == 0, result.stdout
-- -- 003 007 008 016 010 024 0079.73 0273.35 0003.43 |
assert "test.py" in result.stdout
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
@pytest.mark.parametrize("operator", operators)
-- 01 002 004 002 004 006 006 0015.51 0015.51 0001.00 |
def test_operator_on_code_with_metric_named_objects(operator, tmpdir):
-- -- 002 004 002 004 006 006 0015.51 0015.51 0001.00 |
code_with_metric_named_objects = """
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
# CyclomaticComplexity
-- -- --- --- --- --- --- --- ------- ------- ------- |
def complexity(): pass
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
# Halstead
-- -- --- --- --- --- --- --- ------- ------- ------- |
def h1(): pass
-- -- --- --- --- --- --- --- ------- ------- ------- |
def h2(): pass
-- -- --- --- --- --- --- --- ------- ------- ------- |
def N1(): pass
-- -- --- --- --- --- --- --- ------- ------- ------- |
def N2(): pass
-- -- --- --- --- --- --- --- ------- ------- ------- |
def vocabulary(): pass
-- -- --- --- --- --- --- --- ------- ------- ------- |
def length(): pass
-- -- --- --- --- --- --- --- ------- ------- ------- |
def volume(): pass
-- -- --- --- --- --- --- --- ------- ------- ------- |
def difficulty(): pass
-- -- --- --- --- --- --- --- ------- ------- ------- |
def error(): pass
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
# Maintainability
-- -- --- --- --- --- --- --- ------- ------- ------- |
def rank(): pass
-- -- --- --- --- --- --- --- ------- ------- ------- |
def mi(): pass
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
# RawMetrics
-- -- --- --- --- --- --- --- ------- ------- ------- |
def loc(): pass
-- -- --- --- --- --- --- --- ------- ------- ------- |
def lloc(): pass
-- -- --- --- --- --- --- --- ------- ------- ------- |
def sloc(): pass
-- -- --- --- --- --- --- --- ------- ------- ------- |
def comments(): pass
-- -- --- --- --- --- --- --- ------- ------- ------- |
def multi(): pass
-- -- --- --- --- --- --- --- ------- ------- ------- |
def blank(): pass
-- -- --- --- --- --- --- --- ------- ------- ------- |
def single_comments(): pass
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
"""
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
testpath = pathlib.Path(tmpdir) / "test.py"
-- -- --- --- --- --- --- --- ------- ------- ------- |
author = Actor("An author", "author@example.com")
-- -- --- --- --- --- --- --- ------- ------- ------- |
committer = Actor("A committer", "committer@example.com")
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
with open(testpath, "w") as test_py:
-- -- --- --- --- --- --- --- ------- ------- ------- |
test_py.write(dedent(code_with_metric_named_objects))
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
with Repo.init(path=tmpdir) as repo:
-- -- --- --- --- --- --- --- ------- ------- ------- |
repo.index.add(["test.py"])
-- -- --- --- --- --- --- --- ------- ------- ------- |
repo.index.commit("add test.py", author=author, committer=committer)
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
runner = CliRunner()
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
result = runner.invoke(
-- -- --- --- --- --- --- --- ------- ------- ------- |
main.cli,
-- -- --- --- --- --- --- --- ------- ------- ------- |
["--debug", "--path", tmpdir, "build", str(testpath), "-o", operator],
-- -- --- --- --- --- --- --- ------- ------- ------- |
catch_exceptions=False,
-- -- --- --- --- --- --- --- ------- ------- ------- |
)
-- -- --- --- --- --- --- --- ------- ------- ------- |
assert result.exit_code == 0, result.stdout