# Path: snakemakelib/bio/ngs/utils.py
# def find_files(regexp, path=os.curdir, search=False, limit=None):
#     """Find files in path that comply with a regular expression.
# 
#     Args:
#       regexp (RegexpDict | str): regular expression object of class
#                                <RegexpDict> or <str>
#       path (str):   path to search
#       search (bool): use re.search instead of re.match for pattern matching
#       limit (dict): dictionary where keys correspond to regular expression
#              grouping labels and values are lists that limit the
#              returned pattern
# 
#     Returns:
#       flist: list of file names, prepended with root path
#     """
#     if isinstance(regexp, RegexpDict):
#         r = regexp.re
#     else:
#         if not regexp:
#             return []
#         r = re.compile(regexp)
#     if not limit is None and any(k not in r.groupindex.keys() for k in limit.keys()):
#         smllogger.warning("""Some limit keys '{}' not in regexp
#         groupindex '{}'; disregarding limit option for these
#         keys""".format(list(limit.keys()), list(r.groupindex.keys())))
#     re_fn = r.search if search else r.match
#     flist = []
#     for root, dirs, files in os.walk(path):
#         for x in files:
#             m = re_fn(x)
#             if m is None:
#                 continue
#             if limit:
#                 if any([m.group(k) in limit[k] for k in limit.keys() if k in m.groupdict().keys()]):
#                     flist += [os.path.join(root, x)]
#             else:
#                 flist += [os.path.join(root, x)]
#     return sorted(flist)


import unittest
import os
from unittest.mock import patch
from snakemakelib.bio.ngs.utils import find_files
# Copyright (C) 2015 by Per Unneberg
# pylint: disable=R0904, C0301, C0103

class TestFindFiles(unittest.TestCase):
    def setUp(self):
        """Setup text fixtures"""
        self.walk = [
            [os.curdir, ['foo', 'bar'], ['1_121023_FLOWCELL_FOO.fastq.gz', 'bar.txt']],
            ['./foo', [], ['foo.txt', '1_121023_FLOWCELL_BAR.fastq.gz']],
            ['./bar', [], ['bar.txt']],
            ]

    @patch('snakemakelib.bio.ngs.utils.os.walk')
    def test_find_fastq_files(self, mock_walk):
        """Find fastq files using match"""
        mock_walk.return_value = self.walk
        f = find_files(regexp="\w+.fastq.gz")
        self.assertListEqual(f, ['./1_121023_FLOWCELL_FOO.fastq.gz', './foo/1_121023_FLOWCELL_BAR.fastq.gz'])

    @patch('snakemakelib.bio.ngs.utils.os.walk')
    def test_find_files_search(self, mock_walk):
        """Find files using search"""
        mock_walk.return_value = self.walk

# Path: snakemakelib/bio/ngs/utils.py
# def find_files(regexp, path=os.curdir, search=False, limit=None):
#     """Find files in path that comply with a regular expression.
# 
#     Args:
#       regexp (RegexpDict | str): regular expression object of class
#                                <RegexpDict> or <str>
#       path (str):   path to search
#       search (bool): use re.search instead of re.match for pattern matching
#       limit (dict): dictionary where keys correspond to regular expression
#              grouping labels and values are lists that limit the
#              returned pattern
# 
#     Returns:
#       flist: list of file names, prepended with root path
#     """
#     if isinstance(regexp, RegexpDict):
#         r = regexp.re
#     else:
#         if not regexp:
#             return []
#         r = re.compile(regexp)
#     if not limit is None and any(k not in r.groupindex.keys() for k in limit.keys()):
#         smllogger.warning("""Some limit keys '{}' not in regexp
#         groupindex '{}'; disregarding limit option for these
#         keys""".format(list(limit.keys()), list(r.groupindex.keys())))
#     re_fn = r.search if search else r.match
#     flist = []
#     for root, dirs, files in os.walk(path):
#         for x in files:
#             m = re_fn(x)
#             if m is None:
#                 continue
#             if limit:
#                 if any([m.group(k) in limit[k] for k in limit.keys() if k in m.groupdict().keys()]):
#                     flist += [os.path.join(root, x)]
#             else:
#                 flist += [os.path.join(root, x)]
#     return sorted(flist)


# Path: snakemakelib/bio/ngs/tests/test_bio_ngs_utils.py
import unittest
import os
from unittest.mock import patch
from snakemakelib.bio.ngs.utils import find_files

# Copyright (C) 2015 by Per Unneberg
# pylint: disable=R0904, C0301, C0103

class TestFindFiles(unittest.TestCase):
    def setUp(self):
        """Setup text fixtures"""
        self.walk = [
            [os.curdir, ['foo', 'bar'], ['1_121023_FLOWCELL_FOO.fastq.gz', 'bar.txt']],
            ['./foo', [], ['foo.txt', '1_121023_FLOWCELL_BAR.fastq.gz']],
            ['./bar', [], ['bar.txt']],
            ]

    @patch('snakemakelib.bio.ngs.utils.os.walk')
    def test_find_fastq_files(self, mock_walk):
        """Find fastq files using match"""
        mock_walk.return_value = self.walk
        f = find_files(regexp="\w+.fastq.gz")
        self.assertListEqual(f, ['./1_121023_FLOWCELL_FOO.fastq.gz', './foo/1_121023_FLOWCELL_BAR.fastq.gz'])

    @patch('snakemakelib.bio.ngs.utils.os.walk')
    def test_find_files_search(self, mock_walk):
        """Find files using search"""
        mock_walk.return_value = self.walk

        f = find_files(regexp=".fastq", search=True)