Files
the-littlest-jupyterhub/tests/test_config.py
yuvipanda 70785f9fd3 Add tljh-config command
We do not want users to hand-edit YAML files. This has been a
major source of bugs and confusion for users in z2jh. Doing so
in a terminal text editor makes it even worse.

This lets users type commands directly to modify config.yaml file
rather than edit files directly. This makes it a lot less error
prone and user friendly.

Advanced users can still edit config.yaml manually.

Fixes #38
2018-07-27 21:36:29 -07:00

79 lines
1.6 KiB
Python

"""
Test configuration commandline tools
"""
from tljh import config
from contextlib import redirect_stdout
import io
import tempfile
def test_set_no_mutate():
conf = {}
new_conf = config.set_item_in_config(conf, 'a.b', 'c')
assert new_conf['a']['b'] == 'c'
assert conf == {}
def test_set_one_level():
conf = {}
new_conf = config.set_item_in_config(conf, 'a', 'b')
assert new_conf['a'] == 'b'
def test_set_multi_level():
conf = {}
new_conf = config.set_item_in_config(conf, 'a.b', 'c')
new_conf = config.set_item_in_config(new_conf, 'a.d', 'e')
new_conf = config.set_item_in_config(new_conf, 'f', 'g')
assert new_conf == {
'a': {'b': 'c', 'd': 'e'},
'f': 'g'
}
def test_set_overwrite():
"""
We can overwrite already existing config items.
This might be surprising destructive behavior to some :D
"""
conf = {
'a': 'b'
}
new_conf = config.set_item_in_config(conf, 'a', 'c')
assert new_conf == {'a': 'c'}
new_conf = config.set_item_in_config(new_conf, 'a.b', 'd')
assert new_conf == {'a': {'b': 'd'}}
new_conf = config.set_item_in_config(new_conf, 'a', 'hi')
assert new_conf == {'a': 'hi'}
def test_show_config():
"""
Test stdout output when showing config
"""
conf = """
# Just some test YAML
a:
b:
- h
- 1
""".strip()
with tempfile.NamedTemporaryFile() as tmp:
tmp.write(conf.encode())
tmp.flush()
out = io.StringIO()
with redirect_stdout(out):
config.show_config(tmp.name)
assert out.getvalue().strip() == conf