# Path: tests/util.py
# class WsgiDavTestServer:
#     """Run wsgidav in a separate process."""
# 
#     def __init__(
#         self, config=None, with_auth=False, with_ssl=False, provider=None, profile=False
#     ):
#         self.config = config
#         self.with_auth = with_auth
#         self.with_ssl = with_ssl
#         self.provider = provider
#         # self.start_delay = 2
#         self.startup_event = multiprocessing.Event()
#         self.startup_timeout = 5
#         self.proc = None
#         assert not profile, "Not yet implemented"
# 
#     def __enter__(self):
#         self.start()
#         return self
# 
#     def __exit__(self, exc_type, exc_value, traceback):
#         self.stop()
# 
#     def __del__(self):
#         try:
#             self.stop()
#         except Exception:
#             pass
# 
#     def start(self):
#         kwargs = {
#             "with_auth": self.with_auth,
#             "with_ssl": self.with_ssl,
#             "provider": self.provider,
#             "startup_event": self.startup_event,
#             "startup_timeout": self.startup_timeout,
#         }
#         print("Starting WsgiDavTestServer...")
#         self.proc = multiprocessing.Process(target=run_wsgidav_server, kwargs=kwargs)
#         self.proc.daemon = True
#         self.proc.start()
# 
#         print("Starting WsgiDavTestServer... waiting for request loop...")
#         # time.sleep(self.start_delay)
#         if not self.startup_event.wait(self.startup_timeout):
#             raise RuntimeError(
#                 "WsgiDavTestServer start() timed out after {} seconds".format(
#                     self.startup_timeout
#                 )
#             )
#         print("Starting WsgiDavTestServer... running.")
#         return self
# 
#     def stop(self):
#         if self.proc:
#             print("Stopping WsgiDAVAppTestServer...")
#             self.proc.terminate()
#             self.proc.join()
#             self.proc = None
#         print("Stopping WsgiDavTestServer... done.")


import subprocess
import unittest
from tests.util import WsgiDavTestServer
    def setUp(self):
        if WsgiDAVLitmusTest._litmus_connect_failed:
            self._report_missing_litmus(" (again)")

    def tearDown(self):
        pass

    def _report_missing_litmus(self, extra=""):
        WsgiDAVLitmusTest._litmus_connect_failed = True
        print("*" * 70)
        print("This test requires the litmus test suite.")
        print("See https://github.com/mar10/docker-litmus")
        print("*" * 70)
        raise unittest.SkipTest(f"Test requires litmus test suite{extra}")

    def test_litmus_with_authentication(self):
        """Run litmus test suite on HTTP with authentification."""
        with WsgiDavTestServer(with_auth=True, with_ssl=False):
            try:
                res = subprocess.call(
                    ["litmus", "http://127.0.0.1:8080/", "tester", "secret"]
                )
                self.assertEqual(res, 0, "litmus suite failed: check the log")
            except OSError:
                self._report_missing_litmus()
                raise
        return

    def test_litmus_anonymous(self):
        """Run litmus test suite on HTTP with authentification."""

# Path: tests/util.py
# class WsgiDavTestServer:
#     """Run wsgidav in a separate process."""
# 
#     def __init__(
#         self, config=None, with_auth=False, with_ssl=False, provider=None, profile=False
#     ):
#         self.config = config
#         self.with_auth = with_auth
#         self.with_ssl = with_ssl
#         self.provider = provider
#         # self.start_delay = 2
#         self.startup_event = multiprocessing.Event()
#         self.startup_timeout = 5
#         self.proc = None
#         assert not profile, "Not yet implemented"
# 
#     def __enter__(self):
#         self.start()
#         return self
# 
#     def __exit__(self, exc_type, exc_value, traceback):
#         self.stop()
# 
#     def __del__(self):
#         try:
#             self.stop()
#         except Exception:
#             pass
# 
#     def start(self):
#         kwargs = {
#             "with_auth": self.with_auth,
#             "with_ssl": self.with_ssl,
#             "provider": self.provider,
#             "startup_event": self.startup_event,
#             "startup_timeout": self.startup_timeout,
#         }
#         print("Starting WsgiDavTestServer...")
#         self.proc = multiprocessing.Process(target=run_wsgidav_server, kwargs=kwargs)
#         self.proc.daemon = True
#         self.proc.start()
# 
#         print("Starting WsgiDavTestServer... waiting for request loop...")
#         # time.sleep(self.start_delay)
#         if not self.startup_event.wait(self.startup_timeout):
#             raise RuntimeError(
#                 "WsgiDavTestServer start() timed out after {} seconds".format(
#                     self.startup_timeout
#                 )
#             )
#         print("Starting WsgiDavTestServer... running.")
#         return self
# 
#     def stop(self):
#         if self.proc:
#             print("Stopping WsgiDAVAppTestServer...")
#             self.proc.terminate()
#             self.proc.join()
#             self.proc = None
#         print("Stopping WsgiDavTestServer... done.")


# Path: tests/test_litmus.py
import subprocess
import unittest
from tests.util import WsgiDavTestServer

    def setUp(self):
        if WsgiDAVLitmusTest._litmus_connect_failed:
            self._report_missing_litmus(" (again)")

    def tearDown(self):
        pass

    def _report_missing_litmus(self, extra=""):
        WsgiDAVLitmusTest._litmus_connect_failed = True
        print("*" * 70)
        print("This test requires the litmus test suite.")
        print("See https://github.com/mar10/docker-litmus")
        print("*" * 70)
        raise unittest.SkipTest(f"Test requires litmus test suite{extra}")

    def test_litmus_with_authentication(self):
        """Run litmus test suite on HTTP with authentification."""
        with WsgiDavTestServer(with_auth=True, with_ssl=False):
            try:
                res = subprocess.call(
                    ["litmus", "http://127.0.0.1:8080/", "tester", "secret"]
                )
                self.assertEqual(res, 0, "litmus suite failed: check the log")
            except OSError:
                self._report_missing_litmus()
                raise
        return

    def test_litmus_anonymous(self):
        """Run litmus test suite on HTTP with authentification."""

        with WsgiDavTestServer(with_auth=False, with_ssl=False):