-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexperiment_testutils.py
44 lines (31 loc) · 1.15 KB
/
experiment_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
#!/usr/bin/python -S
"""Unit test utils for experiment.py."""
import logging
import os
import shutil
import tempfile
import experiment
logger = logging.getLogger(__name__)
def enable(name):
"""Enable an experiment. For unit tests only."""
open(os.path.join(experiment.EXPERIMENTS_TMP_DIR, name + '.available'), 'w')
open(os.path.join(experiment.EXPERIMENTS_DIR, name + '.active'), 'w')
logger.debug('Enabled %s for unit tests', name)
def disable(name):
"""Enable an experiment. For unit tests only."""
filename = os.path.join(experiment.EXPERIMENTS_DIR, name + '.active')
if os.path.exists(filename):
os.unlink(filename)
logger.debug('Disabled %s for unit tests', name)
class MakeExperimentDirs(object):
"""RAII class for tests which involve experiments.
Creates temporary experiment directories, and removes them
upon deletion.
"""
def __init__(self):
# pylint: disable=protected-access,missing-docstring
experiment.EXPERIMENTS_DIR = tempfile.mkdtemp()
experiment.EXPERIMENTS_TMP_DIR = tempfile.mkdtemp()
def __del__(self):
shutil.rmtree(experiment.EXPERIMENTS_DIR)
shutil.rmtree(experiment.EXPERIMENTS_TMP_DIR)