-
Notifications
You must be signed in to change notification settings - Fork 83
/
test_utils.py
232 lines (180 loc) · 6.26 KB
/
test_utils.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
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
""" Provide abstractions over click testing of the
app and unittest.
"""
from __future__ import print_function
import contextlib
import os
from tempfile import mkdtemp
import shutil
from sys import version_info
import traceback
from click.testing import CliRunner
from planemo import cli
from planemo import shed
from planemo import io
from planemo.config import PLANEMO_CONFIG_ENV_PROP
from galaxy.tools.deps.commands import which
from .shed_app_test_utils import (
mock_shed,
setup_mock_shed,
)
if version_info < (2, 7):
from unittest2 import TestCase, skip
PRE_PYTHON_27 = True
else:
from unittest import TestCase, skip
PRE_PYTHON_27 = False
if version_info[0] == 2 and version_info[1] >= 7:
PYTHON_27 = True
else:
PYTHON_27 = False
TEST_DIR = os.path.dirname(__file__)
TEST_DATA_DIR = os.path.join(TEST_DIR, "data")
TEST_REPOS_DIR = os.path.join(TEST_DATA_DIR, "repos")
TEST_TOOLS_DIR = os.path.join(TEST_DATA_DIR, "tools")
PROJECT_TEMPLATES_DIR = os.path.join(TEST_DIR, os.path.pardir, "project_templates")
EXIT_CODE_MESSAGE = ("Planemo command [%s] resulted in unexpected exit code "
"[%s], expected exit code [%s]]. Command output [%s]")
# More information on testing click applications at following link.
# http://click.pocoo.org/3/testing/#basic-testing
class CliTestCase(TestCase):
def setUp(self): # noqa
self._runner = CliRunner()
self._home = mkdtemp()
self._old_config = os.environ.get(PLANEMO_CONFIG_ENV_PROP, None)
os.environ[PLANEMO_CONFIG_ENV_PROP] = self.planemo_yaml_path
def tearDown(self): # noqa
if self._old_config:
os.environ[PLANEMO_CONFIG_ENV_PROP] = self._old_config
else:
del os.environ[PLANEMO_CONFIG_ENV_PROP]
shutil.rmtree(self._home)
@property
def planemo_yaml_path(self):
return os.path.join(self._home, ".planemo.yml")
@property
def _cli(self):
return cli
def _isolate(self):
return self._runner.isolated_filesystem()
def _invoke(self, command_list):
planemo_cli = self._cli.planemo
return self._runner.invoke(planemo_cli, command_list)
def _check_exit_code(self, command_list, exit_code=0):
expected_exit_code = exit_code
result = self._invoke(command_list)
print(result.output)
result_exit_code = result.exit_code
if result_exit_code != expected_exit_code:
message = EXIT_CODE_MESSAGE % (
" ".join(command_list),
result_exit_code,
expected_exit_code,
result.output,
)
if result.exception:
message += " Exception [%s], " % str(result.exception)
exc_type, exc_value, exc_traceback = result.exc_info
tb = traceback.format_exception(exc_type, exc_value,
exc_traceback)
message += "Traceback [%s]" % tb
raise AssertionError(message)
return result
@contextlib.contextmanager
def _isolate_repo(self, name):
with self._isolate() as f:
self._copy_repo(name, f)
yield f
def _copy_repo(self, name, dest):
repo = os.path.join(TEST_REPOS_DIR, name)
io.shell("cp -r '%s/.' '%s'" % (repo, dest))
@property
def test_context(self):
return test_context()
class CliShedTestCase(CliTestCase):
def setUp(self): # noqa
super(CliShedTestCase, self).setUp()
self.mock_shed = setup_mock_shed()
def tearDown(self): # noqa
super(CliShedTestCase, self).tearDown()
self.mock_shed.shutdown()
def _shed_create(self, recursive=False):
create_command = ["shed_create"]
if recursive:
create_command.append("-r")
create_command.extend(self._shed_args())
self._check_exit_code(create_command)
def _shed_args(self, read_only=False):
args = [
"--shed_target", self.mock_shed.url,
]
if not read_only:
args.extend(["--shed_key", "ignored"])
return args
def _print_shed_info(self):
print(self._repositories)
@property
def _tsi(self):
tsi = shed.tool_shed_client(
None,
shed_target=self.mock_shed.url,
key="ignored",
)
return tsi
@property
def _repositories(self):
return self._tsi.repositories.get_repositories()
def repository_by_name(self, name):
return [r for r in self._repositories if r["name"] == name][0]
@contextlib.contextmanager
def mock_shed_context():
with mock_shed() as mock_shed_obj:
yield shed.get_shed_context(shed_target=mock_shed_obj.url)
class TempDirectoryTestCase(TestCase):
def setUp(self): # noqa
self.temp_directory = mkdtemp()
def tearDown(self): # noqa
shutil.rmtree(self.temp_directory)
class TempDirectoryContext(object):
def __init__(self):
self.temp_directory = mkdtemp()
def __enter__(self):
return self
def __exit__(self, type, value, tb):
shutil.rmtree(self.temp_directory)
def skip_unless_environ(var):
if var in os.environ:
return lambda func: func
template = "Environment variable %s not found, dependent test skipped."
return skip(template % var)
def skip_if_environ(var):
if var not in os.environ:
return lambda func: func
template = "Environment variable %s set, dependent test skipped."
return skip(template % var)
def skip_unless_module(module):
available = True
try:
__import__(module)
except ImportError:
available = False
if available:
return lambda func: func
template = "Module %s could not be loaded, dependent test skipped."
return skip(template % module)
def skip_unless_executable(executable):
if which(executable):
return lambda func: func
return skip("PATH doesn't contain executable %s" % executable)
def skip_unless_python_2_7():
if PYTHON_27:
return lambda func: func
return skip("Python 2.7 required for test.")
def test_context():
context = cli.Context()
context.planemo_directory = "/tmp/planemo-test-workspace"
return context
__all__ = [
"TestCase",
"CliTestCase",
]