# Path: hornet/core/fs_wrapper.py
# class SandboxedFS(OSFS):
# 
#     def __init__(self, *args, **kwargs):
#         create_fs = kwargs.pop('create_fs') if 'create_fs' in kwargs else False
#         super(SandboxedFS, self).__init__(*args, **kwargs)
#         if create_fs:
#             for each in directories:
#                 try:
#                     self.makedirs(each)
#                 except fs.errors.DirectoryExists as e:
#                     logging.debug('Directory creation skipped for: %s', each)
# 
#     def isfile(self, path):
#         if not isinstance(path, unicode):
#             path = unicode(path)
#         return super(SandboxedFS, self).isfile(path)
# 
#     def open(self,
#              path,
#              mode="r",
#              buffering=-1,
#              encoding=None,
#              errors=None,
#              newline='',
#              line_buffering=False,
#              **options):
#         if not isinstance(path, unicode):
#             path = unicode(path)
#         logging.debug('Opening a new path: {}, mode={}'.format(path, mode))
#         return super(SandboxedFS, self).open(
#             path, mode, buffering, encoding, errors, newline, **options
#         )
# 
#     def exists(self, path):
#         if not isinstance(path, unicode):
#             path = unicode(path)
#         return super(SandboxedFS, self).exists(path)
# 
#     def makedir(self, path, permissions=None, recreate=False):
#         if not isinstance(path, unicode):
#             path = unicode(path)
#         return super(SandboxedFS, self).makedir(path, permissions, recreate)
# 
#     def create(self, path, wipe=False):
#         if not isinstance(path, unicode):
#             path = unicode(path)
#         return super(SandboxedFS, self).create(path, wipe)
# 
#     def listdir(self, path='/'):
#         if not isinstance(path, unicode):
#             path = unicode(path)
#         return super(SandboxedFS, self).listdir(path)
# 
#     def isdir(self, path):
#         if not isinstance(path, unicode):
#             path = unicode(path)
#         return super(SandboxedFS, self).isdir(path)


import gevent.monkey
import random
import string
import unittest
import tempfile
import os
from hornet.core.fs_wrapper import SandboxedFS


class HornetTests(unittest.TestCase):

    def test_create(self):
        """ Test basic 'ls' """

        testfs = self.create_filesystem()
        new_filename = self.random_name()

        res = testfs.create(new_filename)

        self.assertTrue(res)

        # ensure file exists on disk
        self.assertTrue(os.path.isfile(os.path.join(testfs.root_path, new_filename)))

    def test_makedir(self):
        testfs = self.create_filesystem()
        new_dirname = self.random_name()

        res = testfs.makedir(new_dirname)

        self.assertIsNotNone(res)

        # ensure directory exists on disk
        self.assertTrue(os.path.isdir(os.path.join(testfs.root_path, new_dirname)))

    def create_filesystem(self):
        temp_dir = tempfile.mkdtemp(prefix='test_hornet_')

# Path: hornet/core/fs_wrapper.py
# class SandboxedFS(OSFS):
# 
#     def __init__(self, *args, **kwargs):
#         create_fs = kwargs.pop('create_fs') if 'create_fs' in kwargs else False
#         super(SandboxedFS, self).__init__(*args, **kwargs)
#         if create_fs:
#             for each in directories:
#                 try:
#                     self.makedirs(each)
#                 except fs.errors.DirectoryExists as e:
#                     logging.debug('Directory creation skipped for: %s', each)
# 
#     def isfile(self, path):
#         if not isinstance(path, unicode):
#             path = unicode(path)
#         return super(SandboxedFS, self).isfile(path)
# 
#     def open(self,
#              path,
#              mode="r",
#              buffering=-1,
#              encoding=None,
#              errors=None,
#              newline='',
#              line_buffering=False,
#              **options):
#         if not isinstance(path, unicode):
#             path = unicode(path)
#         logging.debug('Opening a new path: {}, mode={}'.format(path, mode))
#         return super(SandboxedFS, self).open(
#             path, mode, buffering, encoding, errors, newline, **options
#         )
# 
#     def exists(self, path):
#         if not isinstance(path, unicode):
#             path = unicode(path)
#         return super(SandboxedFS, self).exists(path)
# 
#     def makedir(self, path, permissions=None, recreate=False):
#         if not isinstance(path, unicode):
#             path = unicode(path)
#         return super(SandboxedFS, self).makedir(path, permissions, recreate)
# 
#     def create(self, path, wipe=False):
#         if not isinstance(path, unicode):
#             path = unicode(path)
#         return super(SandboxedFS, self).create(path, wipe)
# 
#     def listdir(self, path='/'):
#         if not isinstance(path, unicode):
#             path = unicode(path)
#         return super(SandboxedFS, self).listdir(path)
# 
#     def isdir(self, path):
#         if not isinstance(path, unicode):
#             path = unicode(path)
#         return super(SandboxedFS, self).isdir(path)


# Path: hornet/tests/test_fs_wrapper.py
import gevent.monkey
import random
import string
import unittest
import tempfile
import os
from hornet.core.fs_wrapper import SandboxedFS



class HornetTests(unittest.TestCase):

    def test_create(self):
        """ Test basic 'ls' """

        testfs = self.create_filesystem()
        new_filename = self.random_name()

        res = testfs.create(new_filename)

        self.assertTrue(res)

        # ensure file exists on disk
        self.assertTrue(os.path.isfile(os.path.join(testfs.root_path, new_filename)))

    def test_makedir(self):
        testfs = self.create_filesystem()
        new_dirname = self.random_name()

        res = testfs.makedir(new_dirname)

        self.assertIsNotNone(res)

        # ensure directory exists on disk
        self.assertTrue(os.path.isdir(os.path.join(testfs.root_path, new_dirname)))

    def create_filesystem(self):
        temp_dir = tempfile.mkdtemp(prefix='test_hornet_')

        return SandboxedFS(temp_dir)