Completed
Push — master ( 2c286b...0bb808 )
by Ionel Cristian
01:04
created

test_no_branch_info()   A

Complexity

Conditions 2

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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