Completed
Pull Request — master (#58)
by
unknown
01:17
created

test_get_project_name()   D

Complexity

Conditions 11

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
c 1
b 0
f 0
dl 0
loc 22
rs 4.0714

How to fix   Complexity   

Complexity

Complex classes like test_get_project_name() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import distutils.spawn
2
import subprocess
3
import argparse
4
5
import pytest
6
from pytest import mark
7
8
from pytest_benchmark.utils import clonefunc
9
from pytest_benchmark.utils import get_commit_info
10
from pytest_benchmark.utils import parse_warmup
11
from pytest_benchmark.utils import parse_columns
12
from pytest_benchmark.utils import get_project_name
13
14
pytest_plugins = 'pytester',
15
16
f1 = lambda a: a
17
18
19
def f2(a):
20
    return a
21
22
23
@mark.parametrize('f', [f1, f2])
24
def test_clonefunc(f):
25
    assert clonefunc(f)(1) == f(1)
26
    assert clonefunc(f)(1) == f(1)
27
28
29
def test_clonefunc_not_function():
30
    assert clonefunc(1) == 1
31
32
33
@mark.parametrize('scm', ['git', 'hg'])
34
def test_get_commit_info(scm, testdir):
35
    if not distutils.spawn.find_executable(scm):
36
        pytest.skip("%r not availabe on $PATH")
37
    subprocess.check_call([scm, 'init', '.'])
38
    if scm == 'git':
39
        subprocess.check_call('git config user.email [email protected]'.split())
40
        subprocess.check_call('git config user.name you'.split())
41
    else:
42
        testdir.tmpdir.join('.hg', 'hgrc').write("""
43
[ui]
44
username = you <[email protected]>
45
""")
46
47
    testdir.makepyfile('asdf')
48
    subprocess.check_call([scm, 'add', 'test_get_commit_info.py'])
49
    subprocess.check_call([scm, 'commit', '-m', 'asdf'])
50
    out = get_commit_info()
51
52
    assert out.get('dirty') == False
53
    assert 'id' in out
54
55
    testdir.makepyfile('sadf')
56
    out = get_commit_info()
57
58
    assert out.get('dirty') == True
59
    assert 'id' in out
60
61
62
def test_parse_warmup():
63
    assert parse_warmup('yes') == True
64
    assert parse_warmup('on') == True
65
    assert parse_warmup('true') == True
66
    assert parse_warmup('off') == False
67
    assert parse_warmup('off') == False
68
    assert parse_warmup('no') == False
69
    assert parse_warmup('') == True
70
    assert parse_warmup('auto') in [True, False]
71
72
73
def test_parse_columns():
74
    assert parse_columns('min,max') == ['min', 'max']
75
    assert parse_columns('MIN, max  ') == ['min', 'max']
76
    with pytest.raises(argparse.ArgumentTypeError):
77
        parse_columns('min,max,x')
78
79
80
@mark.parametrize('scm', [None, 'git', 'hg'])
81
@mark.parametrize('set_remote', [True, False])
82
def test_get_project_name(scm, set_remote, testdir):
83
    if scm is None:
84
        assert get_project_name().startswith("test_get_project_name")
85
        return
86
    if not distutils.spawn.find_executable(scm):
87
        pytest.skip("%r not availabe on $PATH")
88
    subprocess.check_call([scm, 'init', '.'])
89
    if scm == 'git' and set_remote:
90
        subprocess.check_call('git config  remote.origin.url https://example.com/pytest_benchmark_repo.git'.split())
91
    elif scm == 'hg'and set_remote:
92
        testdir.tmpdir.join('.hg', 'hgrc').write("[ui]\n"
93
            "username = you <[email protected]>\n"
94
            "[paths]\n"
95
            "default = https://example.com/pytest_benchmark_repo\n"
96
                                                 )
97
    if set_remote:
98
        assert get_project_name() == "pytest_benchmark_repo"
99
    else:
100
        # use directory name if remote branch is not set
101
        assert get_project_name().startswith("test_get_project_name")
102