Completed
Push — master ( b341e8...9d6e1e )
by Ionel Cristian
01:10
created

test_get_commit_info()   C

Complexity

Conditions 9

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 9
c 2
b 0
f 0
dl 0
loc 18
rs 6.4615
1
import argparse
2
import distutils.spawn
3
import subprocess
4
5
import pytest
6
from pytest import mark
7
from pytest_benchmark.utils import clonefunc
8
from pytest_benchmark.utils import get_branch_info
9
from pytest_benchmark.utils import get_commit_info
10
from pytest_benchmark.utils import get_project_name
11
from pytest_benchmark.utils import parse_columns
12
from pytest_benchmark.utils import parse_elasticsearch_storage
13
from pytest_benchmark.utils import parse_warmup
14
15
pytest_plugins = 'pytester',
16
17
f1 = lambda a: a
18
19
20
def f2(a):
21
    return a
22
23
24
@mark.parametrize('f', [f1, f2])
25
def test_clonefunc(f):
26
    assert clonefunc(f)(1) == f(1)
27
    assert clonefunc(f)(1) == f(1)
28
29
30
def test_clonefunc_not_function():
31
    assert clonefunc(1) == 1
32
33
34
@pytest.yield_fixture(params=(True, False))
35
def crazytestdir(request, testdir):
36
    if request.param:
37
        testdir.tmpdir.join('foo', 'bar').ensure(dir=1).chdir()
38
39
    yield testdir
40
41
42
@pytest.fixture(params=('git', 'hg'))
43
def scm(request, testdir):
44
    scm = request.param
45
    if not distutils.spawn.find_executable(scm):
46
        pytest.skip("%r not availabe on $PATH")
47
    subprocess.check_call([scm, 'init', '.'])
48
    if scm == 'git':
49
        subprocess.check_call('git config user.email [email protected]'.split())
50
        subprocess.check_call('git config user.name you'.split())
51
    else:
52
        testdir.tmpdir.join('.hg', 'hgrc').write("""
53
[ui]
54
username = you <[email protected]>
55
""")
56
    return scm
57
58
59
def test_get_commit_info(scm, crazytestdir):
60
    with open('test_get_commit_info.py', 'w') as fh:
61
        fh.write('asdf')
62
    subprocess.check_call([scm, 'add', 'test_get_commit_info.py'])
63
    subprocess.check_call([scm, 'commit', '-m', 'asdf'])
64
    out = get_commit_info()
65
    branch = 'master' if scm == 'git' else 'default'
66
    assert out['branch'] == branch
67
68
    assert out.get('dirty') == False
69
    assert 'id' in out
70
71
    with open('test_get_commit_info.py', 'w') as fh:
72
        fh.write('sadf')
73
    out = get_commit_info()
74
75
    assert out.get('dirty') == True
76
    assert 'id' in out
77
78
79
def test_get_branch_info(scm, testdir):
80
    # make an initial commit
81
    testdir.tmpdir.join('foo.txt').ensure(file=True)
82
    subprocess.check_call([scm, 'add', 'foo.txt'])
83
    subprocess.check_call([scm, 'commit', '-m', 'added foo.txt'])
84
    branch = get_branch_info()
85
    expected = 'master' if scm == 'git' else 'default'
86
    assert branch == expected
87
    #
88
    # switch to a branch
89
    if scm == 'git':
90
        subprocess.check_call(['git', 'checkout', '-b', 'mybranch'])
91
    else:
92
        subprocess.check_call(['hg', 'branch', 'mybranch'])
93
    branch = get_branch_info()
94
    assert branch == 'mybranch'
95
    #
96
    # git only: test detached head
97
    if scm == 'git':
98
        subprocess.check_call(['git', 'commit', '--allow-empty', '-m', '...'])
99
        subprocess.check_call(['git', 'commit', '--allow-empty', '-m', '...'])
100
        subprocess.check_call(['git', 'checkout', 'HEAD~1'])
101
        assert get_branch_info() == '(detached head)'
102
103
104
def test_no_branch_info(testdir):
105
    assert get_branch_info() == '(unknown vcs)'
106
107
108
def test_branch_info_error(testdir):
109
    testdir.mkdir('.git')
110
    assert get_branch_info() == '(error: fatal: Not a git repository (or any of the parent directories): .git)'
111
112
113
def test_parse_warmup():
114
    assert parse_warmup('yes') == True
115
    assert parse_warmup('on') == True
116
    assert parse_warmup('true') == True
117
    assert parse_warmup('off') == False
118
    assert parse_warmup('off') == False
119
    assert parse_warmup('no') == False
120
    assert parse_warmup('') == True
121
    assert parse_warmup('auto') in [True, False]
122
123
124
def test_parse_columns():
125
    assert parse_columns('min,max') == ['min', 'max']
126
    assert parse_columns('MIN, max  ') == ['min', 'max']
127
    with pytest.raises(argparse.ArgumentTypeError):
128
        parse_columns('min,max,x')
129
130
131
@mark.parametrize('scm', [None, 'git', 'hg'])
132
@mark.parametrize('set_remote', [
133
    False,
134
    'https://example.com/pytest_benchmark_repo',
135
    'https://example.com/pytest_benchmark_repo.git',
136
    '[email protected]:pytest_benchmark_repo.git'])
137
def test_get_project_name(scm, set_remote, testdir):
138
    if scm is None:
139
        assert get_project_name().startswith("test_get_project_name")
140
        return
141
    if not distutils.spawn.find_executable(scm):
142
        pytest.skip("%r not availabe on $PATH")
143
    subprocess.check_call([scm, 'init', '.'])
144
    if scm == 'git' and set_remote:
145
        subprocess.check_call(['git', 'config', 'remote.origin.url', set_remote])
146
    elif scm == 'hg' and set_remote:
147
        set_remote = set_remote.replace('.git', '')
148
        set_remote = set_remote.replace('.com:', '/')
149
        testdir.tmpdir.join('.hg', 'hgrc').write(
150
            "[ui]\n"
151
            "username = you <[email protected]>\n"
152
            "[paths]\n"
153
            "default = %s\n" % set_remote)
154
    if set_remote:
155
        assert get_project_name() == "pytest_benchmark_repo"
156
    else:
157
        # use directory name if remote branch is not set
158
        assert get_project_name().startswith("test_get_project_name")
159
160
161
def test_parse_elasticsearch_storage():
162
    assert parse_elasticsearch_storage("http://localhost:9200") == (
163
    ["http://localhost:9200"], "benchmark", "benchmark", "pytest-benchmark")
164
    assert parse_elasticsearch_storage("http://localhost:9200/benchmark2") == (
165
    ["http://localhost:9200"], "benchmark2", "benchmark", "pytest-benchmark")
166
    assert parse_elasticsearch_storage("http://localhost:9200/benchmark2/benchmark2") == (
167
    ["http://localhost:9200"], "benchmark2", "benchmark2", "pytest-benchmark")
168
    assert parse_elasticsearch_storage("http://host1:9200,host2:9200") == (
169
    ["http://host1:9200", "http://host2:9200"], "benchmark", "benchmark", "pytest-benchmark")
170
    assert parse_elasticsearch_storage("http://host1:9200,host2:9200/benchmark2") == (
171
    ["http://host1:9200", "http://host2:9200"], "benchmark2", "benchmark", "pytest-benchmark")
172
    assert parse_elasticsearch_storage("http://localhost:9200/benchmark2/benchmark2?project_name=project_name") == (
173
    ["http://localhost:9200"], "benchmark2", "benchmark2", "project_name")
174