1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
import argparse
import os
import shutil
import subprocess
import pytest
from pytest import mark
from pytest_benchmark.utils import clonefunc
from pytest_benchmark.utils import get_commit_info
from pytest_benchmark.utils import get_project_name
from pytest_benchmark.utils import parse_columns
from pytest_benchmark.utils import parse_elasticsearch_storage
from pytest_benchmark.utils import parse_warmup
pytest_plugins = ('pytester',)
f1 = lambda a: a # noqa
def f2(a):
return a
@mark.parametrize('f', [f1, f2])
def test_clonefunc(f):
assert clonefunc(f)(1) == f(1)
assert clonefunc(f)(1) == f(1)
def test_clonefunc_not_function():
assert clonefunc(1) == 1
@pytest.fixture(params=(True, False))
def crazytestdir(request, testdir):
if request.param:
testdir.tmpdir.join('foo', 'bar').ensure(dir=1).chdir()
return testdir
@pytest.fixture(params=('git', 'hg'))
def scm(request, testdir):
scm = request.param
if not shutil.which(scm):
pytest.skip(f'{scm!r} not available on $PATH')
subprocess.check_call([scm, 'init', '.'])
if scm == 'git':
subprocess.check_call('git config user.email you@example.com'.split())
subprocess.check_call('git config user.name you'.split())
else:
testdir.tmpdir.join('.hg', 'hgrc').write(
"""
[ui]
username = you <you@example.com>
"""
)
return scm
def test_get_commit_info(scm, crazytestdir):
with open('test_get_commit_info.py', 'w') as fh:
fh.write('asdf')
subprocess.check_call([scm, 'add', 'test_get_commit_info.py'])
subprocess.check_call([scm, 'commit', '-m', 'asdf'])
out = get_commit_info()
branch = 'master' if scm == 'git' else 'default'
assert out['branch'] == branch
assert out.get('dirty') is False
assert 'id' in out
with open('test_get_commit_info.py', 'w') as fh:
fh.write('sadf')
out = get_commit_info()
assert out.get('dirty') is True
assert 'id' in out
def test_missing_scm_bins(scm, crazytestdir, monkeypatch):
with open('test_get_commit_info.py', 'w') as fh:
fh.write('asdf')
subprocess.check_call([scm, 'add', 'test_get_commit_info.py'])
subprocess.check_call([scm, 'commit', '-m', 'asdf'])
monkeypatch.setenv('PATH', os.getcwd())
out = get_commit_info()
assert (
'No such file or directory' in out['error']
or 'The system cannot find the file specified' in out['error']
or 'FileNotFoundError' in out['error']
)
def test_get_branch_info(scm, testdir):
# make an initial commit
testdir.tmpdir.join('foo.txt').ensure(file=True)
subprocess.check_call([scm, 'add', 'foo.txt'])
subprocess.check_call([scm, 'commit', '-m', 'added foo.txt'])
branch = get_commit_info()['branch']
expected = 'master' if scm == 'git' else 'default'
assert branch == expected
#
# switch to a branch
if scm == 'git':
subprocess.check_call(['git', 'checkout', '-b', 'mybranch'])
else:
subprocess.check_call(['hg', 'branch', 'mybranch'])
branch = get_commit_info()['branch']
assert branch == 'mybranch'
#
# git only: test detached head
if scm == 'git':
subprocess.check_call(['git', 'commit', '--allow-empty', '-m', '...'])
subprocess.check_call(['git', 'commit', '--allow-empty', '-m', '...'])
subprocess.check_call(['git', 'checkout', 'HEAD~1'])
assert get_commit_info()['branch'] == '(detached head)'
def test_no_branch_info(testdir):
assert get_commit_info()['branch'] == '(unknown)'
def test_commit_info_error(testdir):
testdir.mkdir('.git')
info = get_commit_info()
assert info['branch'].lower() == '(unknown)'.lower()
assert info['error'].lower().startswith("calledprocesserror(128, 'fatal: not a git repository")
def test_parse_warmup():
assert parse_warmup('yes') is True
assert parse_warmup('on') is True
assert parse_warmup('true') is True
assert parse_warmup('off') is False
assert parse_warmup('off') is False
assert parse_warmup('no') is False
assert parse_warmup('') is True
assert parse_warmup('auto') in [True, False]
def test_parse_columns():
assert parse_columns('min,max') == ['min', 'max']
assert parse_columns('MIN, max ') == ['min', 'max']
with pytest.raises(argparse.ArgumentTypeError):
parse_columns('min,max,x')
@mark.parametrize('scm', [None, 'git', 'hg'])
@mark.parametrize(
'set_remote',
[
False,
'https://example.com/pytest_benchmark_repo',
'https://example.com/pytest_benchmark_repo.git',
'c:\\foo\\bar\\pytest_benchmark_repo.git' 'foo@example.com:pytest_benchmark_repo.git',
],
)
def test_get_project_name(scm, set_remote, testdir):
if scm is None:
assert get_project_name().startswith('test_get_project_name')
return
if not shutil.which(scm):
pytest.skip(f'{scm!r} not available on $PATH')
subprocess.check_call([scm, 'init', '.'])
if scm == 'git' and set_remote:
subprocess.check_call(['git', 'config', 'remote.origin.url', set_remote])
elif scm == 'hg' and set_remote:
set_remote = set_remote.replace('.git', '')
set_remote = set_remote.replace('.com:', '/')
testdir.tmpdir.join('.hg', 'hgrc').write(f"""
[ui]
username = you <you@example.com>
[paths]
default = {set_remote}
""")
if set_remote:
assert get_project_name() == 'pytest_benchmark_repo'
else:
# use directory name if remote branch is not set
assert get_project_name().startswith('test_get_project_name')
@mark.parametrize('scm', ['git', 'hg'])
def test_get_project_name_broken(scm, testdir):
testdir.tmpdir.join('.' + scm).ensure(dir=1)
assert get_project_name() in ['test_get_project_name_broken0', 'test_get_project_name_broken1']
def test_get_project_name_fallback(testdir, capfd):
testdir.tmpdir.ensure('.hg', dir=1)
project_name = get_project_name()
assert project_name.startswith('test_get_project_name_fallback')
assert capfd.readouterr() == ('', '')
def test_get_project_name_fallback_broken_hgrc(testdir, capfd):
testdir.tmpdir.ensure('.hg', 'hgrc').write('[paths]\ndefault = /')
project_name = get_project_name()
assert project_name.startswith('test_get_project_name_fallback')
assert capfd.readouterr() == ('', '')
def test_parse_elasticsearch_storage():
benchdir = os.path.basename(os.getcwd())
assert parse_elasticsearch_storage('http://localhost:9200') == (['http://localhost:9200'], 'benchmark', 'benchmark', benchdir)
assert parse_elasticsearch_storage('http://localhost:9200/benchmark2') == (
['http://localhost:9200'],
'benchmark2',
'benchmark',
benchdir,
)
assert parse_elasticsearch_storage('http://localhost:9200/benchmark2/benchmark2') == (
['http://localhost:9200'],
'benchmark2',
'benchmark2',
benchdir,
)
assert parse_elasticsearch_storage('http://host1:9200,host2:9200') == (
['http://host1:9200', 'http://host2:9200'],
'benchmark',
'benchmark',
benchdir,
)
assert parse_elasticsearch_storage('http://host1:9200,host2:9200/benchmark2') == (
['http://host1:9200', 'http://host2:9200'],
'benchmark2',
'benchmark',
benchdir,
)
assert parse_elasticsearch_storage('http://localhost:9200/benchmark2/benchmark2?project_name=project_name') == (
['http://localhost:9200'],
'benchmark2',
'benchmark2',
'project_name',
)
|