# Path: pyFileFixity/tests/aux_tests.py
# def get_marker(type=1):
#     """Helper function to store the usual entry and fields markers in ecc files"""
#     if type == 1:
#         return "\xFE\xFF\xFE\xFF\xFE\xFF\xFE\xFF\xFE\xFF"
#     elif type == 2:
#         return "\xFA\xFF\xFA\xFF\xFA"
#     else:
#         return ''
# 
# def dummy_ecc_file_gen(nb_files=1):
#     """ Generate a dummy ecc file, following the specs (of course the ecc tracks are fake!) """
#     # Create header comments
#     fcontent = '''**SCRIPT_CODE_NAMEv111...000...000**\n** Comment 2\n** Yet another comment\n'''
#     # Create files entries
#     for i in range(1, nb_files+1):
#         fcontent += get_marker(1)+("file%i.ext"%i)+get_marker(2)+("filesize%i"%i)+get_marker(2)+("relfilepath%i_ecc"%i)+get_marker(2)+("filesize%i_ecc"%i)+get_marker(2)+"hash-ecc-entry_"*(i*3)
#     return fcontent
# 
# def path_sample_files(type=None, path=None, createdir=False):
#     """ Helper function to return the full path to the test files """
#     subdir = ''
#     if not type:
#         return ''
#     elif type == 'input':
#         subdir = 'files'
#     elif type == 'results':
#         subdir = 'results'
#     elif type == 'output':
#         subdir = 'out'
# 
#     dirpath = ''
#     scriptpath = os.path.dirname(os.path.realpath(__file__))
#     if path:
#         dirpath = fullpath(os.path.join(scriptpath, subdir, path))
#     else:
#         dirpath = fullpath(os.path.join(scriptpath, subdir))
# 
#     if createdir:
#         create_dir_if_not_exist(dirpath)
# 
#     return dirpath
# 
# def create_dir_if_not_exist(path):
#     """Create a directory if it does not already exist, else nothing is done and no error is return"""
#     if not os.path.exists(path):
#         os.makedirs(path)
# 
# Path: pyFileFixity/lib/aux_funcs.py
# def is_file(dirname):
# def is_dir(dirname):
# def is_dir_or_file(dirname):
# def fullpath(relpath):
# def recwalk(inputpath, sorting=True):
# def sizeof_fmt(num, suffix='B', mod=1024.0):
# def path2unix(path, nojoin=False, fromwinpath=False):
# def get_next_entry(file, entrymarker="\xFE\xFF\xFE\xFF\xFE\xFF\xFE\xFF\xFE\xFF", only_coord=True, blocksize=65535):
# def create_dir_if_not_exist(path):  # pragma: no cover
# def remove_if_exist(path):  # pragma: no cover
# def copy_any(src, dst, only_missing=False):  # pragma: no cover
# def grouper(n, iterable, fillvalue=None):
# def group_files_by_size(fileslist, multi):  # pragma: no cover
# def group_files_by_size_fast(fileslist, nbgroups, mode=1):  # pragma: no cover
# def group_files_by_size_simple(fileslist, nbgroups):  # pragma: no cover
# def grouped_count_sizes(fileslist, fgrouped):  # pragma: no cover
# def gen_rand_fileslist(nbfiles=100, maxvalue=100):  # pragma: no cover
# def gen_rand_fileslist2(nbfiles=100, maxvalue=100):  # pragma: no cover
# def grouped_test(nbfiles=100, nbgroups=3):  # pragma: no cover
# def grouped_fast_test(nbfiles=100, nbgroups=3, mode=1):  # pragma: no cover
# def grouped_simple_test(nbfiles=100, nbgroups=3):  # pragma: no cover


from nose.tools import assert_raises
from StringIO import StringIO
from .aux_tests import get_marker, dummy_ecc_file_gen, path_sample_files, create_dir_if_not_exist
from ..lib import aux_funcs as auxf
from ..lib.argparse import ArgumentTypeError
import sys
import os
import shutil
from __future__ import print_function





def setup_module():
    """ Initialize the tests by emptying the out directory """
    outfolder = path_sample_files('output')
    shutil.rmtree(outfolder, ignore_errors=True)
    create_dir_if_not_exist(outfolder)

def test_get_next_entry():
    """ aux: test detection of next entry """
    entries = [
            '''file1.ext\xfa\xff\xfa\xff\xfafilesize1\xfa\xff\xfa\xff\xfarelfilepath1_ecc\xfa\xff\xfa\xff\xfafilesize1_ecc\xfa\xff\xfa\xff\xfahash-ecc-entry_hash-ecc-entry_hash-ecc-entry_''',
            '''file2.ext\xfa\xff\xfa\xff\xfafilesize2\xfa\xff\xfa\xff\xfarelfilepath2_ecc\xfa\xff\xfa\xff\xfafilesize2_ecc\xfa\xff\xfa\xff\xfahash-ecc-entry_hash-ecc-entry_hash-ecc-entry_hash-ecc-entry_hash-ecc-entry_hash-ecc-entry_'''
          ]
    entries_pos = [
                    [83, 195],
                    [205, 362]
                  ]

    filecontent = dummy_ecc_file_gen(2)
    fp1 = StringIO(filecontent)
    entry = auxf.get_next_entry(fp1, entrymarker=get_marker(1), only_coord=False, blocksize=len(get_marker(1))+1)
    assert entry == entries[0]
    entry = auxf.get_next_entry(fp1, entrymarker=get_marker(1), only_coord=False, blocksize=len(get_marker(1))+1)
    assert entry == entries[1]
    fp2 = StringIO(filecontent)

# Path: pyFileFixity/tests/aux_tests.py
# def get_marker(type=1):
#     """Helper function to store the usual entry and fields markers in ecc files"""
#     if type == 1:
#         return "\xFE\xFF\xFE\xFF\xFE\xFF\xFE\xFF\xFE\xFF"
#     elif type == 2:
#         return "\xFA\xFF\xFA\xFF\xFA"
#     else:
#         return ''
# 
# def dummy_ecc_file_gen(nb_files=1):
#     """ Generate a dummy ecc file, following the specs (of course the ecc tracks are fake!) """
#     # Create header comments
#     fcontent = '''**SCRIPT_CODE_NAMEv111...000...000**\n** Comment 2\n** Yet another comment\n'''
#     # Create files entries
#     for i in range(1, nb_files+1):
#         fcontent += get_marker(1)+("file%i.ext"%i)+get_marker(2)+("filesize%i"%i)+get_marker(2)+("relfilepath%i_ecc"%i)+get_marker(2)+("filesize%i_ecc"%i)+get_marker(2)+"hash-ecc-entry_"*(i*3)
#     return fcontent
# 
# def path_sample_files(type=None, path=None, createdir=False):
#     """ Helper function to return the full path to the test files """
#     subdir = ''
#     if not type:
#         return ''
#     elif type == 'input':
#         subdir = 'files'
#     elif type == 'results':
#         subdir = 'results'
#     elif type == 'output':
#         subdir = 'out'
# 
#     dirpath = ''
#     scriptpath = os.path.dirname(os.path.realpath(__file__))
#     if path:
#         dirpath = fullpath(os.path.join(scriptpath, subdir, path))
#     else:
#         dirpath = fullpath(os.path.join(scriptpath, subdir))
# 
#     if createdir:
#         create_dir_if_not_exist(dirpath)
# 
#     return dirpath
# 
# def create_dir_if_not_exist(path):
#     """Create a directory if it does not already exist, else nothing is done and no error is return"""
#     if not os.path.exists(path):
#         os.makedirs(path)
# 
# Path: pyFileFixity/lib/aux_funcs.py
# def is_file(dirname):
# def is_dir(dirname):
# def is_dir_or_file(dirname):
# def fullpath(relpath):
# def recwalk(inputpath, sorting=True):
# def sizeof_fmt(num, suffix='B', mod=1024.0):
# def path2unix(path, nojoin=False, fromwinpath=False):
# def get_next_entry(file, entrymarker="\xFE\xFF\xFE\xFF\xFE\xFF\xFE\xFF\xFE\xFF", only_coord=True, blocksize=65535):
# def create_dir_if_not_exist(path):  # pragma: no cover
# def remove_if_exist(path):  # pragma: no cover
# def copy_any(src, dst, only_missing=False):  # pragma: no cover
# def grouper(n, iterable, fillvalue=None):
# def group_files_by_size(fileslist, multi):  # pragma: no cover
# def group_files_by_size_fast(fileslist, nbgroups, mode=1):  # pragma: no cover
# def group_files_by_size_simple(fileslist, nbgroups):  # pragma: no cover
# def grouped_count_sizes(fileslist, fgrouped):  # pragma: no cover
# def gen_rand_fileslist(nbfiles=100, maxvalue=100):  # pragma: no cover
# def gen_rand_fileslist2(nbfiles=100, maxvalue=100):  # pragma: no cover
# def grouped_test(nbfiles=100, nbgroups=3):  # pragma: no cover
# def grouped_fast_test(nbfiles=100, nbgroups=3, mode=1):  # pragma: no cover
# def grouped_simple_test(nbfiles=100, nbgroups=3):  # pragma: no cover


# Path: pyFileFixity/tests/test_aux_funcs.py
from nose.tools import assert_raises
from StringIO import StringIO
from .aux_tests import get_marker, dummy_ecc_file_gen, path_sample_files, create_dir_if_not_exist
from ..lib import aux_funcs as auxf
from ..lib.argparse import ArgumentTypeError
import sys
import os
import shutil

from __future__ import print_function





def setup_module():
    """ Initialize the tests by emptying the out directory """
    outfolder = path_sample_files('output')
    shutil.rmtree(outfolder, ignore_errors=True)
    create_dir_if_not_exist(outfolder)

def test_get_next_entry():
    """ aux: test detection of next entry """
    entries = [
            '''file1.ext\xfa\xff\xfa\xff\xfafilesize1\xfa\xff\xfa\xff\xfarelfilepath1_ecc\xfa\xff\xfa\xff\xfafilesize1_ecc\xfa\xff\xfa\xff\xfahash-ecc-entry_hash-ecc-entry_hash-ecc-entry_''',
            '''file2.ext\xfa\xff\xfa\xff\xfafilesize2\xfa\xff\xfa\xff\xfarelfilepath2_ecc\xfa\xff\xfa\xff\xfafilesize2_ecc\xfa\xff\xfa\xff\xfahash-ecc-entry_hash-ecc-entry_hash-ecc-entry_hash-ecc-entry_hash-ecc-entry_hash-ecc-entry_'''
          ]
    entries_pos = [
                    [83, 195],
                    [205, 362]
                  ]

    filecontent = dummy_ecc_file_gen(2)
    fp1 = StringIO(filecontent)
    entry = auxf.get_next_entry(fp1, entrymarker=get_marker(1), only_coord=False, blocksize=len(get_marker(1))+1)
    assert entry == entries[0]
    entry = auxf.get_next_entry(fp1, entrymarker=get_marker(1), only_coord=False, blocksize=len(get_marker(1))+1)
    assert entry == entries[1]
    fp2 = StringIO(filecontent)

    entry = auxf.get_next_entry(fp2, entrymarker=get_marker(1), only_coord=True, blocksize=len(get_marker(1))+1)