forked from sloria/konch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_konch.py
395 lines (272 loc) · 9.5 KB
/
test_konch.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import sys
import os
import pytest
from docopt import DocoptExit
from scripttest import TestFileEnvironment
import konch
try:
import ptpython # noqa: F401
except ImportError:
HAS_PTPYTHON = False
else:
HAS_PTPYTHON = True
def assert_in_output(s, res, message=None):
"""Assert that a string is in either stdout or std err.
Included because banners are sometimes outputted to stderr.
"""
assert any(
[s in res.stdout, s in res.stderr]
), message or "{0} not in output".format(s)
@pytest.fixture
def env():
return TestFileEnvironment(ignore_hidden=False)
def teardown_function(func):
konch.reset_config()
def test_make_banner_custom():
text = "I want to be the very best"
result = konch.make_banner(text)
assert text in result
assert sys.version in result
def test_full_formatter():
class Foo(object):
def __repr__(self):
return "<Foo>"
context = {"foo": Foo(), "bar": 42}
assert (
konch.format_context(context, formatter="full")
== "\nContext:\nbar: 42\nfoo: <Foo>"
)
def test_short_formatter():
class Foo(object):
def __repr__(self):
return "<Foo>"
context = {"foo": Foo(), "bar": 42}
assert konch.format_context(context, formatter="short") == "\nContext:\nbar, foo"
def test_custom_formatter():
context = {"foo": 42, "bar": 24}
def my_formatter(ctx):
return "*".join(sorted(ctx.keys()))
assert konch.format_context(context, formatter=my_formatter) == "bar*foo"
def test_make_banner_includes_full_context_by_default():
context = {"foo": 42}
result = konch.make_banner(context=context)
assert konch.format_context(context, formatter="full") in result
def test_make_banner_hide_context():
context = {"foo": 42}
result = konch.make_banner(context=context, context_format="hide")
assert konch.format_context(context) not in result
def test_make_banner_custom_format():
context = {"foo": 42}
result = konch.make_banner(context=context, context_format=lambda ctx: repr(ctx))
assert repr(context) in result
def test_cfg_defaults():
assert konch._cfg["shell"] == konch.AutoShell
assert konch._cfg["banner"] is None
assert konch._cfg["context"] == {}
assert konch._cfg["context_format"] == "full"
def test_config():
assert konch._cfg == konch.Config()
konch.config({"banner": "Foo bar"})
assert konch._cfg["banner"] == "Foo bar"
def test_reset_config():
assert konch._cfg == konch.Config()
konch.config({"banner": "Foo bar"})
konch.reset_config()
assert konch._cfg == konch.Config()
def test_parse_args():
try:
args = konch.parse_args()
assert "--shell" in args
assert "init" in args
assert "<config_file>" in args
assert "--name" in args
except DocoptExit:
pass
def test_context_list2dict():
import math
class MyClass:
pass
def my_func():
pass
my_objects = [math, MyClass, my_func]
expected = {"my_func": my_func, "MyClass": MyClass, "math": math}
assert konch.context_list2dict(my_objects) == expected
def test_config_list():
assert konch._cfg == konch.Config()
def my_func():
return
konch.config({"context": [my_func]})
assert konch._cfg["context"]["my_func"] == my_func
def test_config_converts_list_context():
import math
config = konch.Config(context=[math])
assert config["context"] == {"math": math}
def test_config_set_context_converts_list():
import math
config = konch.Config()
config["context"] = [math]
assert config["context"] == {"math": math}
def test_config_update_context_converts_list():
import math
config = konch.Config()
config.update({"context": [math]})
assert config["context"] == {"math": math}
def test_named_config_adds_to_registry():
assert konch._config_registry["default"] == konch._cfg
assert len(konch._config_registry.keys()) == 1
konch.named_config("mynamespace", {"context": {"foo": 42}})
assert len(konch._config_registry.keys()) == 2
# reset config_registry
konch._config_registry = {"default": konch._cfg}
def test_context_can_be_callable():
def get_context():
return {"foo": 42}
shell = konch.Shell(context=get_context)
assert shell.context == {"foo": 42}
##### Command tests #####
def test_init_creates_config_file(env):
res = env.run("konch", "init")
assert res.returncode == 0
assert konch.CONFIG_FILE in res.files_created
def test_init_with_filename(env):
res = env.run("konch", "init", "myconfig")
assert "myconfig" in res.files_created
@pytest.mark.skipif(HAS_PTPYTHON, reason="test incompatible with ptpython")
def test_konch_with_no_config_file(env):
res = env.run("konch", "-f", "notfound", expect_stderr=True, cwd=env.base_path)
assert res.returncode == 0
def test_konch_init_when_config_file_exists(env):
env.run("konch", "init")
res = env.run("konch", "init", expect_error=True)
assert "already exists" in res.stderr
assert res.returncode == 1
@pytest.mark.skipif(HAS_PTPYTHON, reason="test incompatible with ptpython")
def test_default_banner(env):
env.run("konch", "init")
res = env.run("konch", expect_stderr=True)
assert_in_output(str(sys.version), res)
@pytest.mark.skipif(HAS_PTPYTHON, reason="test incompatible with ptpython")
def test_config_file_not_found(env):
res = env.run("konch", "-f", "notfound", expect_stderr=True)
assert "not found" in res.stderr
assert res.returncode == 0
TEST_CONFIG = """
import konch
konch.config({
'banner': 'Test banner',
'prompt': 'myprompt >>>'
})
"""
@pytest.fixture
def fileenv(request, env):
fpath = os.path.join(env.base_path, "testrc")
with open(fpath, "w") as fp:
fp.write(TEST_CONFIG)
def finalize():
os.remove(fpath)
request.addfinalizer(finalize)
return env
@pytest.mark.skipif(HAS_PTPYTHON, reason="test incompatible with ptpython")
def test_custom_banner(fileenv):
res = fileenv.run("konch", "-f", "testrc", expect_stderr=True)
assert_in_output("Test banner", res)
# TODO: Get this test working with IPython
def test_custom_prompt(fileenv):
res = fileenv.run("konch", "-f", "testrc", "-s", "py", expect_stderr=True)
assert_in_output("myprompt >>>", res)
def test_version(env):
res = env.run("konch", "--version")
assert konch.__version__ in res.stdout
res = env.run("konch", "-v")
assert konch.__version__ in res.stdout
TEST_CONFIG_WITH_NAMES = """
import konch
konch.config({
'context': {
'foo': 42,
},
'banner': 'Default'
})
konch.named_config('conf2', {
'context': {
'bar': 24
},
'banner': 'Conf2'
})
konch.named_config(['conf3', 'c3'], {
'context': {
'baz': 424,
},
'banner': 'Conf3',
})
"""
TEST_CONFIG_WITH_SETUP_AND_TEARDOWN = """
import konch
def setup():
print('setup!')
def teardown():
print('teardown!')
"""
@pytest.fixture
def names_env(request, env):
fpath = os.path.join(env.base_path, ".konchrc")
with open(fpath, "w") as fp:
fp.write(TEST_CONFIG_WITH_NAMES)
def finalize():
os.remove(fpath)
request.addfinalizer(finalize)
return env
@pytest.fixture
def setup_env(request, env):
fpath = os.path.join(env.base_path, ".konchrc")
with open(fpath, "w") as fp:
fp.write(TEST_CONFIG_WITH_SETUP_AND_TEARDOWN)
def finalize():
os.remove(fpath)
request.addfinalizer(finalize)
return env
@pytest.fixture
def folderenv(request, env):
folder = os.path.abspath(os.path.join(env.base_path, "testdir"))
os.makedirs(folder)
def finalize():
os.removedirs(folder)
request.addfinalizer(finalize)
return env
@pytest.mark.skipif(HAS_PTPYTHON, reason="test incompatible with ptpython")
def test_default_config(names_env):
# Explicitly specify ipython shell because test isn't compatible with ptpython
res = names_env.run("konch", expect_stderr=True)
assert_in_output("Default", res)
assert_in_output("foo", res)
@pytest.mark.skipif(HAS_PTPYTHON, reason="test incompatible with ptpython")
def test_setup_teardown(setup_env):
res = setup_env.run("konch", expect_stderr=True)
assert_in_output("setup!", res)
assert_in_output("teardown!", res)
@pytest.mark.skipif(HAS_PTPYTHON, reason="test incompatible with ptpython")
def test_selecting_named_config(names_env):
res = names_env.run("konch", "-n", "conf2", expect_stderr=True)
assert_in_output("Conf2", res)
assert_in_output("bar", res)
@pytest.mark.skipif(HAS_PTPYTHON, reason="test incompatible with ptpython")
def test_named_config_with_multiple_names(names_env):
res = names_env.run("konch", "-n", "conf3", expect_stderr=True)
assert_in_output("Conf3", res)
assert_in_output("baz", res)
res = names_env.run("konch", "-n", "c3", expect_stderr=True)
assert_in_output("Conf3", res)
assert_in_output("baz", res)
@pytest.mark.skipif(HAS_PTPYTHON, reason="test incompatible with ptpython")
def test_selecting_name_that_doesnt_exist(names_env):
res = names_env.run("konch", "-n", "doesntexist", expect_stderr=True)
assert_in_output("Default", res)
def test_resolve_path(folderenv):
folderenv.run("konch", "init")
fpath = os.path.abspath(os.path.join(folderenv.base_path, ".konchrc"))
assert os.path.exists(fpath)
folder = os.path.abspath(os.path.join(folderenv.base_path, "testdir"))
os.chdir(folder)
assert konch.resolve_path(".konchrc") == fpath