-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathconftest.py
76 lines (63 loc) · 1.84 KB
/
conftest.py
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
import os
import shutil
import tempfile
import builtins
import subprocess
import pytest
import sys
from rever import environ
@pytest.fixture
def gitrepo(request):
"""A test fixture that creates and destroys a git repo in a temporary
directory.
This will yield the path to the repo.
"""
cwd = os.getcwd()
name = request.node.name
repo = os.path.join(tempfile.gettempdir(), name)
if os.path.exists(repo):
rmtree(repo)
subprocess.run(['git', 'init', repo])
os.chdir(repo)
with open('README', 'w') as f:
f.write('testing ' + name)
subprocess.run(['git', 'add', '.'])
subprocess.run(['git', 'commit', '-am', 'Initial readme'])
with environ.context():
yield repo
os.chdir(cwd)
rmtree(repo)
@pytest.fixture
def gitecho(request):
aliases = builtins.aliases
aliases['git'] = lambda args: 'Would have run: ' + ' '.join(args) + '\n'
yield None
del aliases['git']
@pytest.fixture
def gcloudecho(request):
aliases = builtins.aliases
aliases['gcloud'] = lambda args: 'Would have run: ' + ' '.join(args) + '@\n'
yield None
del aliases['gcloud']
@pytest.fixture
def kubectlecho(request):
aliases = builtins.aliases
aliases['kubectl'] = lambda args: 'Would have run: ' + ' '.join(args) + '\n'
yield None
del aliases['kubectl']
def rmtree(dirname):
"""Remove a directory, even if it has read-only files (Windows).
Git creates read-only files that must be removed on teardown. See
https://stackoverflow.com/questions/2656322 for more info.
Parameters
----------
dirname : str
Directory to be removed
"""
try:
shutil.rmtree(dirname)
except PermissionError:
if sys.platform == 'win32':
subprocess.check_call(['del', '/F/S/Q', dirname], shell=True)
else:
raise