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
|
|
|
@mark.parametrize('scm', ['git', 'hg']) |
177
|
|
|
def test_get_project_name_broken(scm, testdir): |
178
|
|
|
testdir.tmpdir.join('.' + scm).ensure(dir=1) |
179
|
|
|
assert get_project_name() in ['test_get_project_name_broken0', 'test_get_project_name_broken1'] |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
def test_get_project_name_fallback(testdir, capfd): |
183
|
|
|
testdir.tmpdir.ensure('.hg', dir=1) |
184
|
|
|
project_name = get_project_name() |
185
|
|
|
assert project_name.startswith("test_get_project_name_fallback") |
186
|
|
|
assert capfd.readouterr() == ('', '') |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
def test_get_project_name_fallback_broken_hgrc(testdir, capfd): |
190
|
|
|
testdir.tmpdir.ensure('.hg', 'hgrc').write('[paths]\ndefault = /') |
191
|
|
|
project_name = get_project_name() |
192
|
|
|
assert project_name.startswith("test_get_project_name_fallback") |
193
|
|
|
assert capfd.readouterr() == ('', '') |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
def test_parse_elasticsearch_storage(): |
197
|
|
|
assert parse_elasticsearch_storage("http://localhost:9200") == ( |
198
|
|
|
["http://localhost:9200"], "benchmark", "benchmark", "pytest-benchmark") |
199
|
|
|
assert parse_elasticsearch_storage("http://localhost:9200/benchmark2") == ( |
200
|
|
|
["http://localhost:9200"], "benchmark2", "benchmark", "pytest-benchmark") |
201
|
|
|
assert parse_elasticsearch_storage("http://localhost:9200/benchmark2/benchmark2") == ( |
202
|
|
|
["http://localhost:9200"], "benchmark2", "benchmark2", "pytest-benchmark") |
203
|
|
|
assert parse_elasticsearch_storage("http://host1:9200,host2:9200") == ( |
204
|
|
|
["http://host1:9200", "http://host2:9200"], "benchmark", "benchmark", "pytest-benchmark") |
205
|
|
|
assert parse_elasticsearch_storage("http://host1:9200,host2:9200/benchmark2") == ( |
206
|
|
|
["http://host1:9200", "http://host2:9200"], "benchmark2", "benchmark", "pytest-benchmark") |
207
|
|
|
assert parse_elasticsearch_storage("http://localhost:9200/benchmark2/benchmark2?project_name=project_name") == ( |
208
|
|
|
["http://localhost:9200"], "benchmark2", "benchmark2", "project_name") |
209
|
|
|
|