# Path: jacquard/config.py
# def load_config(source):
#     """
#     Load `Config` from file.
# 
#     `source` may be given either as a file-like or path-like object. In the
#     former case it should be opened for reading in text/unicode mode.
#     """
#     if hasattr(source, "read"):
#         return _load_config_from_fp(source)
#     else:
#         with pathlib.Path(source).open("r") as f:
#             return _load_config_from_fp(f)
# 
# Path: jacquard/directory/dummy.py
# class DummyDirectory(Directory):
#     """Dummy user directory."""
# 
#     def __init__(self, users=()):
#         """
#         Initialise.
# 
#         If `users` is given it should be an iterable of `UserEntry` instances.
#         This is likely only to be useful in tests, given that one cannot
#         construct `UserEntry` instances in a config file.
#         """
#         self.users = {str(x.id): x for x in users}
# 
#     def lookup(self, user_id):
#         """Look up user by ID."""
#         return self.users.get(str(user_id))


import io
import sys
import tempfile
import textwrap
from jacquard.config import load_config
from jacquard.directory.dummy import DummyDirectory



CONFIG_FILE = """
[storage]
engine = dummy
url = dummy

[directory]
engine = dummy

[test_section]
test_key = test_value
"""


def load_test_config(extra=""):
    f = io.StringIO(CONFIG_FILE + textwrap.dedent(extra))
    return load_config(f)


def test_load_config_smoke():
    load_test_config()


def test_load_config_from_file():
    with tempfile.NamedTemporaryFile("w") as f:
        f.write(CONFIG_FILE)
        f.flush()

# Path: jacquard/config.py
# def load_config(source):
#     """
#     Load `Config` from file.
# 
#     `source` may be given either as a file-like or path-like object. In the
#     former case it should be opened for reading in text/unicode mode.
#     """
#     if hasattr(source, "read"):
#         return _load_config_from_fp(source)
#     else:
#         with pathlib.Path(source).open("r") as f:
#             return _load_config_from_fp(f)
# 
# Path: jacquard/directory/dummy.py
# class DummyDirectory(Directory):
#     """Dummy user directory."""
# 
#     def __init__(self, users=()):
#         """
#         Initialise.
# 
#         If `users` is given it should be an iterable of `UserEntry` instances.
#         This is likely only to be useful in tests, given that one cannot
#         construct `UserEntry` instances in a config file.
#         """
#         self.users = {str(x.id): x for x in users}
# 
#     def lookup(self, user_id):
#         """Look up user by ID."""
#         return self.users.get(str(user_id))


# Path: jacquard/tests/test_config.py
import io
import sys
import tempfile
import textwrap
from jacquard.config import load_config
from jacquard.directory.dummy import DummyDirectory




CONFIG_FILE = """
[storage]
engine = dummy
url = dummy

[directory]
engine = dummy

[test_section]
test_key = test_value
"""


def load_test_config(extra=""):
    f = io.StringIO(CONFIG_FILE + textwrap.dedent(extra))
    return load_config(f)


def test_load_config_smoke():
    load_test_config()


def test_load_config_from_file():
    with tempfile.NamedTemporaryFile("w") as f:
        f.write(CONFIG_FILE)
        f.flush()

        load_config(f.name)