# Path: tests/testlib/testcase_with_tmpdir.py
# class TestCase(unittest.TestCase):
#     """Adds setUpClass an tearDownClass that create and destroy tmpdir."""
# 
#     _tmpdir = None
# 
#     @classmethod
#     def setUpClass(cls):
#         """Create tmp dir to write to and check."""
#         cls._tmpdir = tempfile.mkdtemp()
#         if (not os.path.isdir(cls._tmpdir)):
#             raise Exception("Failed to create tempdir to use for dump tests")
#         try:
#             cls.extraSetUpClass()
#         except Exception:
#             pass
# 
#     @classmethod
#     def tearDownClass(cls):
#         """Cleanup."""
#         if (not os.path.isdir(cls._tmpdir)):
#             raise Exception("Ooops, no tempdir (%s) to clean up?" % (cls._tmpdir))
#         shutil.rmtree(cls._tmpdir)
#         try:
#             cls.extraTearUpClass()
#         except Exception:
#             pass
# 
#     @property
#     def tmpdir(self):
#         """Read-only access to _tmpdir, just in case... The rmtree scares me."""
#         return(self._tmpdir)
# 
# Path: resync/client_state.py
# class ClientState(object):
#     """Read and store client state on disk."""
# 
#     def __init__(self):
#         """Initialize ClientState object with default status file name."""
#         self.status_file = '.resync-client-status.cfg'
# 
#     def set_state(self, site, timestamp=None):
#         """Write status dict to client status file.
# 
#         FIXME - should have some file lock to avoid race
#         """
#         parser = ConfigParser()
#         parser.read(self.status_file)
#         status_section = 'incremental'
#         if (not parser.has_section(status_section)):
#             parser.add_section(status_section)
#         if (timestamp is None):
#             parser.remove_option(
#                 status_section,
#                 self.config_site_to_name(site))
#         else:
#             parser.set(
#                 status_section,
#                 self.config_site_to_name(site),
#                 str(timestamp))
#         with open(self.status_file, 'w') as configfile:
#             parser.write(configfile)
#             configfile.close()
# 
#     def get_state(self, site):
#         """Read client status file and return dict."""
#         parser = ConfigParser()
#         status_section = 'incremental'
#         parser.read(self.status_file)
#         timestamp = None
#         try:
#             timestamp = float(
#                 parser.get(
#                     status_section,
#                     self.config_site_to_name(site)))
#         except NoSectionError as e:
#             pass
#         except NoOptionError as e:
#             pass
#         return(timestamp)
# 
#     def config_site_to_name(self, name):
#         """Convert site name to safe string for config.
# 
#         Simply replaces any non-word chars with underscore. This could
#         lead to multiple site names mapping to one config name but
#         probably not too likely.
#         """
#         return(re.sub(r"[^\w]", '_', name))


from .testlib import TestCase
from resync.client_state import ClientState
import os.path



class TestClientState(TestCase):

    def test01_set_and_get(self):
        cs = ClientState()
        self.assertEqual(cs.status_file, '.resync-client-status.cfg')
        cs.status_file = os.path.join(self.tmpdir, cs.status_file)
        # Get state
        site = 'https://this.site/'
        self.assertEqual(cs.get_state(site), None)

# Path: tests/testlib/testcase_with_tmpdir.py
# class TestCase(unittest.TestCase):
#     """Adds setUpClass an tearDownClass that create and destroy tmpdir."""
# 
#     _tmpdir = None
# 
#     @classmethod
#     def setUpClass(cls):
#         """Create tmp dir to write to and check."""
#         cls._tmpdir = tempfile.mkdtemp()
#         if (not os.path.isdir(cls._tmpdir)):
#             raise Exception("Failed to create tempdir to use for dump tests")
#         try:
#             cls.extraSetUpClass()
#         except Exception:
#             pass
# 
#     @classmethod
#     def tearDownClass(cls):
#         """Cleanup."""
#         if (not os.path.isdir(cls._tmpdir)):
#             raise Exception("Ooops, no tempdir (%s) to clean up?" % (cls._tmpdir))
#         shutil.rmtree(cls._tmpdir)
#         try:
#             cls.extraTearUpClass()
#         except Exception:
#             pass
# 
#     @property
#     def tmpdir(self):
#         """Read-only access to _tmpdir, just in case... The rmtree scares me."""
#         return(self._tmpdir)
# 
# Path: resync/client_state.py
# class ClientState(object):
#     """Read and store client state on disk."""
# 
#     def __init__(self):
#         """Initialize ClientState object with default status file name."""
#         self.status_file = '.resync-client-status.cfg'
# 
#     def set_state(self, site, timestamp=None):
#         """Write status dict to client status file.
# 
#         FIXME - should have some file lock to avoid race
#         """
#         parser = ConfigParser()
#         parser.read(self.status_file)
#         status_section = 'incremental'
#         if (not parser.has_section(status_section)):
#             parser.add_section(status_section)
#         if (timestamp is None):
#             parser.remove_option(
#                 status_section,
#                 self.config_site_to_name(site))
#         else:
#             parser.set(
#                 status_section,
#                 self.config_site_to_name(site),
#                 str(timestamp))
#         with open(self.status_file, 'w') as configfile:
#             parser.write(configfile)
#             configfile.close()
# 
#     def get_state(self, site):
#         """Read client status file and return dict."""
#         parser = ConfigParser()
#         status_section = 'incremental'
#         parser.read(self.status_file)
#         timestamp = None
#         try:
#             timestamp = float(
#                 parser.get(
#                     status_section,
#                     self.config_site_to_name(site)))
#         except NoSectionError as e:
#             pass
#         except NoOptionError as e:
#             pass
#         return(timestamp)
# 
#     def config_site_to_name(self, name):
#         """Convert site name to safe string for config.
# 
#         Simply replaces any non-word chars with underscore. This could
#         lead to multiple site names mapping to one config name but
#         probably not too likely.
#         """
#         return(re.sub(r"[^\w]", '_', name))


# Path: tests/test_client_state.py
from .testlib import TestCase
from resync.client_state import ClientState
import os.path




class TestClientState(TestCase):

    def test01_set_and_get(self):
        cs = ClientState()
        self.assertEqual(cs.status_file, '.resync-client-status.cfg')
        cs.status_file = os.path.join(self.tmpdir, cs.status_file)
        # Get state
        site = 'https://this.site/'
        self.assertEqual(cs.get_state(site), None)

        cs.set_state(site, 123)