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

scm()   A

Complexity

Conditions 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
dl 0
loc 15
rs 9.4285
c 1
b 0
f 0
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_commit_info
10
from pytest_benchmark.utils import get_branch_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
def test_get_commit_info(scm, testdir):
52
    testdir.makepyfile('asdf')
53
    subprocess.check_call([scm, 'add', 'test_get_commit_info.py'])
54
    subprocess.check_call([scm, 'commit', '-m', 'asdf'])
55
    out = get_commit_info()
56
    branch = 'master' if scm == 'git' else 'default'
57
    assert out['branch'] == branch
58
59
    assert out.get('dirty') == False
60
    assert 'id' in out
61
62
    testdir.makepyfile('sadf')
63
    out = get_commit_info()
64
65
    assert out.get('dirty') == True
66
    assert 'id' in out
67
68
def test_get_branch_info(scm, testdir):
69
    branch = get_branch_info()
70
    expected = 'master' if scm == 'git' else 'default'
71
    assert branch == expected
72
    #
73
    # switch to a branch
74
    if scm == 'git':
75
        subprocess.check_call(['git', 'checkout', '-b', 'mybranch'])
76
    else:
77
        subprocess.check_call(['hg', 'branch', 'mybranch'])
78
    branch = get_branch_info()
79
    assert branch == 'mybranch'
80
    #
81
    # git only: test detached head
82
    if scm == 'git':
83
        subprocess.check_call(['git', 'commit', '--allow-empty', '-m', '...'])
84
        subprocess.check_call(['git', 'commit', '--allow-empty', '-m', '...'])
85
        subprocess.check_call(['git', 'checkout', 'HEAD~1'])
86
        assert get_branch_info() == '(detached head)'
87
88
def test_parse_warmup():
89
    assert parse_warmup('yes') == True
90
    assert parse_warmup('on') == True
91
    assert parse_warmup('true') == True
92
    assert parse_warmup('off') == False
93
    assert parse_warmup('off') == False
94
    assert parse_warmup('no') == False
95
    assert parse_warmup('') == True
96
    assert parse_warmup('auto') in [True, False]
97
98
99
def test_parse_columns():
100
    assert parse_columns('min,max') == ['min', 'max']
101
    assert parse_columns('MIN, max  ') == ['min', 'max']
102
    with pytest.raises(argparse.ArgumentTypeError):
103
        parse_columns('min,max,x')
104
105
106
@mark.parametrize('scm', [None, 'git', 'hg'])
107
@mark.parametrize('set_remote', [True, False])
108
def test_get_project_name(scm, set_remote, testdir):
109
    if scm is None:
110
        assert get_project_name().startswith("test_get_project_name")
111
        return
112
    if not distutils.spawn.find_executable(scm):
113
        pytest.skip("%r not availabe on $PATH")
114
    subprocess.check_call([scm, 'init', '.'])
115
    if scm == 'git' and set_remote:
116
        subprocess.check_call('git config  remote.origin.url https://example.com/pytest_benchmark_repo.git'.split())
117
    elif scm == 'hg'and set_remote:
118
        testdir.tmpdir.join('.hg', 'hgrc').write("[ui]\n"
119
            "username = you <[email protected]>\n"
120
            "[paths]\n"
121
            "default = https://example.com/pytest_benchmark_repo\n"
122
                                                 )
123
    if set_remote:
124
        assert get_project_name() == "pytest_benchmark_repo"
125
    else:
126
        # use directory name if remote branch is not set
127
        assert get_project_name().startswith("test_get_project_name")
128
129
130
def test_parse_elasticsearch_storage():
131
    assert parse_elasticsearch_storage("http://localhost:9200") == (["http://localhost:9200"], "benchmark", "benchmark", "pytest-benchmark")
132
    assert parse_elasticsearch_storage("http://localhost:9200/benchmark2") == (["http://localhost:9200"], "benchmark2", "benchmark", "pytest-benchmark")
133
    assert parse_elasticsearch_storage("http://localhost:9200/benchmark2/benchmark2") == (["http://localhost:9200"], "benchmark2", "benchmark2", "pytest-benchmark")
134
    assert parse_elasticsearch_storage("http://host1:9200,host2:9200") == (["http://host1:9200", "http://host2:9200"], "benchmark", "benchmark", "pytest-benchmark")
135
    assert parse_elasticsearch_storage("http://host1:9200,host2:9200/benchmark2") == (["http://host1:9200", "http://host2:9200"], "benchmark2", "benchmark", "pytest-benchmark")
136
    assert parse_elasticsearch_storage("http://localhost:9200/benchmark2/benchmark2?project_name=project_name") == (["http://localhost:9200"], "benchmark2", "benchmark2", "project_name")
137