Completed
Push — master ( 3a231d...275b8a )
by Ionel Cristian
34s
created

test_missing_scm_bins()   A

Complexity

Conditions 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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