# Path: core/commands.py
# def get_commands(include=None, exclude=None):
# def build(name, ask=True, **kwargs):
#     def is_device_present():
# def clean(name, ask=True, **kwargs):
# def cooja(name, with_malicious=True, **kwargs):
# def __make(name, ask=True, **kwargs):
# def __remake(name, build=False, **kwargs):
# def report(name, theme=REPORT_THEME, silent=False, **kwargs):
# def __run(name, **kwargs):
# def clean_all(exp_file, **kwargs):
# def drop(exp_file, ask=True, **kwargs):
# def make_all(exp_file, **kwargs):
# def prepare(exp_file, ask=True, **kwargs):
# def remake_all(exp_file, **kwargs):
# def run_all(exp_file, **kwargs):
# def list(item_type, **kwargs):
# def config(contiki_folder='~/contiki', experiments_folder='~/Experiments', silent=False, **kwargs):
# def test(**kwargs):
# def setup(silent=False, **kwargs):
# def update(silent=False, **kwargs):
# def versions(**kwargs):
# def demo(**kwargs):
# 
# Path: core/conf/constants.py
# EXPERIMENT_FOLDER = abspath(expanduser(confparser.get("RPL Attacks Framework Configuration", "experiments_folder")))
# 
# Path: core/utils/rpla.py
# def check_structure(path, files=None, create=False, remove=False):
#     """
#     This function checks if the file structure given by the dictionary files exists at the input path.
# 
#     :param path: path to be checked for the file structure
#     :param files: file structure as a dictionary
#     :param create: create subfolders if they do not exist
#     :param remove: if this flag is True, non-matching files are removed
#     :return: True if the file structure is respected, otherwise False
#     """
#     if create and not exists(path):
#         makedirs(path)
#     files = deepcopy(EXPERIMENT_STRUCTURE) if files is None else files
#     if files.get('*'):
#         return True
#     items = listdir(path)
#     if create:
#         items = [i for i, f in files.items() if not isinstance(f, bool)] + items
#     for item in items:
#         wildcard = '{}.*'.format(splitext(item)[0])
#         match = item if item in files.keys() else (wildcard if wildcard in files.keys() else None)
#         if match is None:
#             if remove:
#                 remove_files(path, item)
#             continue
#         files[match] = True if isinstance(files[match], bool) else \
#             check_structure(join(path, match), deepcopy(files[match]), create, remove)
#     return all(files.values())


import sh
import unittest
from os.path import exists, expanduser, getmtime, join
from core.commands import clean, make, remake
from core.conf.constants import EXPERIMENT_FOLDER
from core.utils.rpla import check_structure
class ExperimentTestCase(unittest.TestCase):
    path = expanduser(join(EXPERIMENT_FOLDER, SIM))
    backup = path + '.backup'


class Test3Make(ExperimentTestCase):
    """ 3. Make an example experiment """

    @classmethod
    def setUpClass(cls):
        if exists(cls.path):
            sh.mv(cls.path, cls.backup)
        make(SIM, ask=False, silent=True)

    def test1_experiment_structure(self):
        """ > Is the new experiment correctly structured ? """
        self.assertTrue(check_structure(self.path))


class Test4Remake(ExperimentTestCase):
    """ 4. Remake the same example experiment """

    @classmethod
    def setUpClass(cls):
        cls.t1 = getmtime(join(cls.path, 'with-malicious', 'motes', 'malicious.z1'))
        remake(SIM, silent=True)
        cls.t2 = getmtime(join(cls.path, 'with-malicious', 'motes', 'malicious.z1'))

    def test1_experiment_structure_unchanged(self):
        """ > Is the new experiment still correctly structured ? """

# Path: core/commands.py
# def get_commands(include=None, exclude=None):
# def build(name, ask=True, **kwargs):
#     def is_device_present():
# def clean(name, ask=True, **kwargs):
# def cooja(name, with_malicious=True, **kwargs):
# def __make(name, ask=True, **kwargs):
# def __remake(name, build=False, **kwargs):
# def report(name, theme=REPORT_THEME, silent=False, **kwargs):
# def __run(name, **kwargs):
# def clean_all(exp_file, **kwargs):
# def drop(exp_file, ask=True, **kwargs):
# def make_all(exp_file, **kwargs):
# def prepare(exp_file, ask=True, **kwargs):
# def remake_all(exp_file, **kwargs):
# def run_all(exp_file, **kwargs):
# def list(item_type, **kwargs):
# def config(contiki_folder='~/contiki', experiments_folder='~/Experiments', silent=False, **kwargs):
# def test(**kwargs):
# def setup(silent=False, **kwargs):
# def update(silent=False, **kwargs):
# def versions(**kwargs):
# def demo(**kwargs):
# 
# Path: core/conf/constants.py
# EXPERIMENT_FOLDER = abspath(expanduser(confparser.get("RPL Attacks Framework Configuration", "experiments_folder")))
# 
# Path: core/utils/rpla.py
# def check_structure(path, files=None, create=False, remove=False):
#     """
#     This function checks if the file structure given by the dictionary files exists at the input path.
# 
#     :param path: path to be checked for the file structure
#     :param files: file structure as a dictionary
#     :param create: create subfolders if they do not exist
#     :param remove: if this flag is True, non-matching files are removed
#     :return: True if the file structure is respected, otherwise False
#     """
#     if create and not exists(path):
#         makedirs(path)
#     files = deepcopy(EXPERIMENT_STRUCTURE) if files is None else files
#     if files.get('*'):
#         return True
#     items = listdir(path)
#     if create:
#         items = [i for i, f in files.items() if not isinstance(f, bool)] + items
#     for item in items:
#         wildcard = '{}.*'.format(splitext(item)[0])
#         match = item if item in files.keys() else (wildcard if wildcard in files.keys() else None)
#         if match is None:
#             if remove:
#                 remove_files(path, item)
#             continue
#         files[match] = True if isinstance(files[match], bool) else \
#             check_structure(join(path, match), deepcopy(files[match]), create, remove)
#     return all(files.values())


# Path: tests/experiment.py
import sh
import unittest
from os.path import exists, expanduser, getmtime, join
from core.commands import clean, make, remake
from core.conf.constants import EXPERIMENT_FOLDER
from core.utils.rpla import check_structure

class ExperimentTestCase(unittest.TestCase):
    path = expanduser(join(EXPERIMENT_FOLDER, SIM))
    backup = path + '.backup'


class Test3Make(ExperimentTestCase):
    """ 3. Make an example experiment """

    @classmethod
    def setUpClass(cls):
        if exists(cls.path):
            sh.mv(cls.path, cls.backup)
        make(SIM, ask=False, silent=True)

    def test1_experiment_structure(self):
        """ > Is the new experiment correctly structured ? """
        self.assertTrue(check_structure(self.path))


class Test4Remake(ExperimentTestCase):
    """ 4. Remake the same example experiment """

    @classmethod
    def setUpClass(cls):
        cls.t1 = getmtime(join(cls.path, 'with-malicious', 'motes', 'malicious.z1'))
        remake(SIM, silent=True)
        cls.t2 = getmtime(join(cls.path, 'with-malicious', 'motes', 'malicious.z1'))

    def test1_experiment_structure_unchanged(self):
        """ > Is the new experiment still correctly structured ? """

        self.assertTrue(check_structure(self.path))