-
Notifications
You must be signed in to change notification settings - Fork 230
/
testutils.py
88 lines (66 loc) · 2.36 KB
/
testutils.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
77
78
79
80
81
82
83
84
85
86
87
88
import stat
from functools import wraps
import os
import tempfile
from pathlib import Path
import shutil
from glob import iglob
import pytest
def fixture_backup(source):
"""
Similar to fixture_tmp_dir but backups the content instead
"""
def decorator(function):
@wraps(function)
def wrapper():
old = os.getcwd()
backup = tempfile.mkdtemp()
root = _path_to_tests() / 'assets' / source
shutil.copytree(str(root), str(Path(backup, source)))
os.chdir(root)
yield root
os.chdir(old)
shutil.rmtree(str(root))
shutil.copytree(str(Path(backup, source)), str(root))
shutil.rmtree(backup)
return pytest.fixture(wrapper)
return decorator
def fixture_tmp_dir(source, **kwargs):
"""
A lot of our fixtures are copying a few files into a temporary location,
making that location the current working directory and deleting after
the test is done. This decorator allows us to build such fixture
"""
# NOTE: I tried not making this a decorator and just do:
# some_fixture = factory('some/path')
# but didn't work
def decorator(function):
@wraps(function)
def wrapper():
old = os.getcwd()
tmp_dir = tempfile.mkdtemp()
tmp = Path(tmp_dir, 'content')
# we have to add extra folder content/, otherwise copytree
# complains
shutil.copytree(str(source), str(tmp))
os.chdir(str(tmp))
yield tmp
# some tests create sample git repos, if we are on windows, we
# need to change permissions to be able to delete the files
_fix_all_dot_git_permissions(tmp)
os.chdir(old)
shutil.rmtree(tmp_dir)
return pytest.fixture(wrapper, **kwargs)
return decorator
def _path_to_tests():
return Path(__file__).resolve().parent.parent / 'tests'
def _fix_all_dot_git_permissions(tmp):
if os.name == 'nt':
for path in iglob(f'{tmp}/**/.git', recursive=True):
_fix_dot_git_permissions(path)
def _fix_dot_git_permissions(path):
for root, dirs, files in os.walk(path):
for dir_ in dirs:
os.chmod(Path(root, dir_), stat.S_IRWXU)
for file_ in files:
os.chmod(Path(root, file_), stat.S_IRWXU)