-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
58 lines (42 loc) · 1.48 KB
/
__init__.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
import os
import shutil
import tempfile
from pathlib import Path
FIXTURES_DIR_PATH = Path(__file__).parent / "fixtures"
TEST_DIR_NAME = "test_dir"
TEST_DIR = Path.cwd() / TEST_DIR_NAME
def load_fixture(fixture_name, destination):
with (FIXTURES_DIR_PATH / fixture_name).open() as fixture:
with (Path.cwd() / destination).open("w") as dest:
dest.write(fixture.read())
def load_fixture_to(fixture_name, destination):
with (FIXTURES_DIR_PATH / fixture_name).open() as fixture:
with destination.open("w") as dest:
dest.write(fixture.read())
def get_test_file_content(name):
with (Path.cwd() / name).open() as test_file:
return test_file.read()
def get_fixture_content(name):
with (FIXTURES_DIR_PATH / name).open() as fixture:
return fixture.read()
def isolated_workdir(test_function):
def wrapped():
temp_dir = tempfile.mkdtemp()
original_working_dir = os.getcwd()
os.chdir(temp_dir)
try:
test_function()
finally:
os.chdir(original_working_dir)
shutil.rmtree(temp_dir)
return wrapped
def isolated_env(test_function):
def wrapped(*args, **kwargs):
temp_dir = tempfile.mkdtemp()
history_file = Path(temp_dir) / "HISTORY.rst"
history_dir = Path(temp_dir) / "history"
try:
test_function(history_dir, history_file, *args, **kwargs)
finally:
shutil.rmtree(temp_dir)
return wrapped