Completed
Push — master ( 5b30cc...d0fb6a )
by Ionel Cristian
53s
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
    # make an initial commit
70
    testdir.tmpdir.join('foo.txt').ensure(file=True)
71
    subprocess.check_call([scm, 'add', 'foo.txt'])
72
    subprocess.check_call([scm, 'commit', '-m', 'added foo.txt'])
73
    branch = get_branch_info()
74
    expected = 'master' if scm == 'git' else 'default'
75
    assert branch == expected
76
    #
77
    # switch to a branch
78
    if scm == 'git':
79
        subprocess.check_call(['git', 'checkout', '-b', 'mybranch'])
80
    else:
81
        subprocess.check_call(['hg', 'branch', 'mybranch'])
82
    branch = get_branch_info()
83
    assert branch == 'mybranch'
84
    #
85
    # git only: test detached head
86
    if scm == 'git':
87
        subprocess.check_call(['git', 'commit', '--allow-empty', '-m', '...'])
88
        subprocess.check_call(['git', 'commit', '--allow-empty', '-m', '...'])
89
        subprocess.check_call(['git', 'checkout', 'HEAD~1'])
90
        assert get_branch_info() == '(detached head)'
91
92
def test_parse_warmup():
93
    assert parse_warmup('yes') == True
94
    assert parse_warmup('on') == True
95
    assert parse_warmup('true') == True
96
    assert parse_warmup('off') == False
97
    assert parse_warmup('off') == False
98
    assert parse_warmup('no') == False
99
    assert parse_warmup('') == True
100
    assert parse_warmup('auto') in [True, False]
101
102
103
def test_parse_columns():
104
    assert parse_columns('min,max') == ['min', 'max']
105
    assert parse_columns('MIN, max  ') == ['min', 'max']
106
    with pytest.raises(argparse.ArgumentTypeError):
107
        parse_columns('min,max,x')
108
109
110
@mark.parametrize('scm', [None, 'git', 'hg'])
111
@mark.parametrize('set_remote', [True, False])
112
def test_get_project_name(scm, set_remote, testdir):
113
    if scm is None:
114
        assert get_project_name().startswith("test_get_project_name")
115
        return
116
    if not distutils.spawn.find_executable(scm):
117
        pytest.skip("%r not availabe on $PATH")
118
    subprocess.check_call([scm, 'init', '.'])
119
    if scm == 'git' and set_remote:
120
        subprocess.check_call('git config  remote.origin.url https://example.com/pytest_benchmark_repo.git'.split())
121
    elif scm == 'hg'and set_remote:
122
        testdir.tmpdir.join('.hg', 'hgrc').write("[ui]\n"
123
            "username = you <[email protected]>\n"
124
            "[paths]\n"
125
            "default = https://example.com/pytest_benchmark_repo\n"
126
                                                 )
127
    if set_remote:
128
        assert get_project_name() == "pytest_benchmark_repo"
129
    else:
130
        # use directory name if remote branch is not set
131
        assert get_project_name().startswith("test_get_project_name")
132
133
134
def test_parse_elasticsearch_storage():
135
    assert parse_elasticsearch_storage("http://localhost:9200") == (["http://localhost:9200"], "benchmark", "benchmark", "pytest-benchmark")
136
    assert parse_elasticsearch_storage("http://localhost:9200/benchmark2") == (["http://localhost:9200"], "benchmark2", "benchmark", "pytest-benchmark")
137
    assert parse_elasticsearch_storage("http://localhost:9200/benchmark2/benchmark2") == (["http://localhost:9200"], "benchmark2", "benchmark2", "pytest-benchmark")
138
    assert parse_elasticsearch_storage("http://host1:9200,host2:9200") == (["http://host1:9200", "http://host2:9200"], "benchmark", "benchmark", "pytest-benchmark")
139
    assert parse_elasticsearch_storage("http://host1:9200,host2:9200/benchmark2") == (["http://host1:9200", "http://host2:9200"], "benchmark2", "benchmark", "pytest-benchmark")
140
    assert parse_elasticsearch_storage("http://localhost:9200/benchmark2/benchmark2?project_name=project_name") == (["http://localhost:9200"], "benchmark2", "benchmark2", "project_name")
141