# Path: pygopherd/logger.py
# def log_file(message):
# def setlogfile(file):
# def log_syslog(message):
# def log_none(message):
# def init(config):
# 
# Path: pygopherd/initialization.py
# def initconffile(conffile):
# def initlogger(config, conffile):
# def initexceptions(config):
# def initmimetypes(config):
#     def handle(self):
# def getserverobject(config):
#         def server_bind(self):
# def initsecurity(config):
# def initconditionaldetach(config):
# def initpidfile(config):
# def initpgrp(config):
# def initsighandlers(config, pgrp):
# def initeverything(conffile):
# class GopherRequestHandler(socketserver.StreamRequestHandler):
#     class MyServer(servertype):
# 
# Path: pygopherd/GopherExceptions.py
# def log(exception, protocol = None, handler = None):
# def init(backtraceenabled):
#     def __init__(self, arg):
#     def __str__(self):
# class FileNotFound(BaseException):
# 
# Path: pygopherd/testutil.py
# def getconfig():
# def getstringlogger():
# def gettestingserver(config = None):
# def gettestinghandler(rfile, wfile, config = None):
#         def __init__(self, rfile, wfile):
#         def makefile(self, mode, bufsize):
#         def __init__(self, request, client_address, server):
# def gettestingprotocol(request, config = None):
#     class requestClass:
#     class handlerClass(initialization.GopherRequestHandler):
# 
# Path: pygopherd/GopherExceptions.py
# class FileNotFound(BaseException):
#     def __init__(self, arg):
#         self.selector = arg
#         self.comments = ''
#         self.protocol = ''
# 
#         if type(arg) != bytes:
#             self.selector = arg[0]
#             self.comments = arg[1]
#             if len(arg) > 2 and arg[2]:
#                 self.protocol = arg[2]
# 
#         log(self, self.protocol, None)
# 
#     def __str__(self):
#         retval = "'%s' does not exist" % (self.selector)
#         if self.comments:
#             retval += " (%s)" % self.comments
# 
#         return retval
# 
# Path: pygopherd/protocols/rfc1436.py
# class GopherProtocol(BaseGopherProtocol):
#     def canhandlerequest(self):
#     def renderobjinfo(self, entry):


import unittest
from io import BytesIO
from pygopherd import logger, initialization, GopherExceptions, testutil
from pygopherd.GopherExceptions import FileNotFound
from pygopherd.protocols import rfc1436
# END OF COPYRIGHT #


class GopherExceptionsTestCase(unittest.TestCase):
    def setUp(self):
        self.stringfile = BytesIO()
        self.config = testutil.getconfig()
        self.stringfile = testutil.getstringlogger()
        GopherExceptions.tracebacks = 0

    def testlog_basic(self):
        try:
            raise IOError("foo")
        except IOError as e:
            GopherExceptions.log(e)
        self.assertEqual(self.stringfile.getvalue(),
                         "unknown-address [None/None] EXCEPTION OSError: foo\n")

    def testlog_proto_ip(self):
        rfile = BytesIO(b"/NONEXISTANT\n")
        wfile = BytesIO()
        handler = testutil.gettestinghandler(rfile, wfile, self.config)
        handler.handle()
        # handler.handle()
        self.assertEqual(self.stringfile.getvalue(),
             b"10.77.77.77 [GopherProtocol/None] EXCEPTION FileNotFound: '/NONEXISTANT' does not exist (no handler found)\n")

    def testFileNotFound(self):
        try:
            raise FileNotFound("TEST STRING")

# Path: pygopherd/logger.py
# def log_file(message):
# def setlogfile(file):
# def log_syslog(message):
# def log_none(message):
# def init(config):
# 
# Path: pygopherd/initialization.py
# def initconffile(conffile):
# def initlogger(config, conffile):
# def initexceptions(config):
# def initmimetypes(config):
#     def handle(self):
# def getserverobject(config):
#         def server_bind(self):
# def initsecurity(config):
# def initconditionaldetach(config):
# def initpidfile(config):
# def initpgrp(config):
# def initsighandlers(config, pgrp):
# def initeverything(conffile):
# class GopherRequestHandler(socketserver.StreamRequestHandler):
#     class MyServer(servertype):
# 
# Path: pygopherd/GopherExceptions.py
# def log(exception, protocol = None, handler = None):
# def init(backtraceenabled):
#     def __init__(self, arg):
#     def __str__(self):
# class FileNotFound(BaseException):
# 
# Path: pygopherd/testutil.py
# def getconfig():
# def getstringlogger():
# def gettestingserver(config = None):
# def gettestinghandler(rfile, wfile, config = None):
#         def __init__(self, rfile, wfile):
#         def makefile(self, mode, bufsize):
#         def __init__(self, request, client_address, server):
# def gettestingprotocol(request, config = None):
#     class requestClass:
#     class handlerClass(initialization.GopherRequestHandler):
# 
# Path: pygopherd/GopherExceptions.py
# class FileNotFound(BaseException):
#     def __init__(self, arg):
#         self.selector = arg
#         self.comments = ''
#         self.protocol = ''
# 
#         if type(arg) != bytes:
#             self.selector = arg[0]
#             self.comments = arg[1]
#             if len(arg) > 2 and arg[2]:
#                 self.protocol = arg[2]
# 
#         log(self, self.protocol, None)
# 
#     def __str__(self):
#         retval = "'%s' does not exist" % (self.selector)
#         if self.comments:
#             retval += " (%s)" % self.comments
# 
#         return retval
# 
# Path: pygopherd/protocols/rfc1436.py
# class GopherProtocol(BaseGopherProtocol):
#     def canhandlerequest(self):
#     def renderobjinfo(self, entry):


# Path: pygopherd/GopherExceptionsTest.py
import unittest
from io import BytesIO
from pygopherd import logger, initialization, GopherExceptions, testutil
from pygopherd.GopherExceptions import FileNotFound
from pygopherd.protocols import rfc1436

# END OF COPYRIGHT #


class GopherExceptionsTestCase(unittest.TestCase):
    def setUp(self):
        self.stringfile = BytesIO()
        self.config = testutil.getconfig()
        self.stringfile = testutil.getstringlogger()
        GopherExceptions.tracebacks = 0

    def testlog_basic(self):
        try:
            raise IOError("foo")
        except IOError as e:
            GopherExceptions.log(e)
        self.assertEqual(self.stringfile.getvalue(),
                         "unknown-address [None/None] EXCEPTION OSError: foo\n")

    def testlog_proto_ip(self):
        rfile = BytesIO(b"/NONEXISTANT\n")
        wfile = BytesIO()
        handler = testutil.gettestinghandler(rfile, wfile, self.config)
        handler.handle()
        # handler.handle()
        self.assertEqual(self.stringfile.getvalue(),
             b"10.77.77.77 [GopherProtocol/None] EXCEPTION FileNotFound: '/NONEXISTANT' does not exist (no handler found)\n")

    def testFileNotFound(self):
        try:
            raise FileNotFound("TEST STRING")

        except FileNotFound as e: