# Path: gears/exceptions.py
# class ImproperlyConfigured(Exception):
#     pass
# 
# class FileNotFound(Exception):
#     pass
# 
# Path: gears/finders.py
# class FileSystemFinder(BaseFinder):
# 
#     def __init__(self, directories):
#         self.locations = []
#         if not isinstance(directories, (list, tuple)):
#             raise ImproperlyConfigured(
#                 "FileSystemFinder's 'directories' parameter is not a "
#                 "tuple or list; perhaps you forgot a trailing comma?")
#         for directory in directories:
#             if directory not in self.locations:
#                 self.locations.append(directory)
# 
#     @property
#     def paths(self):
#         return self.locations
# 
#     def find(self, path):
#         for matched_path in self.find_all(path):
#             return matched_path
#         raise FileNotFound(path)
# 
#     def find_all(self, path):
#         for root in self.locations:
#             matched_path = self.find_location(root, path)
#             if matched_path:
#                 yield matched_path
# 
#     def find_location(self, root, path):
#         path = safe_join(root, path)
#         if os.path.exists(path):
#             return path
# 
#     def list(self, path):
#         for root in self.locations:
#             for absolute_path in glob2.iglob(safe_join(root, path)):
#                 if os.path.isfile(absolute_path):
#                     logical_path = os.path.relpath(absolute_path, root)
#                     yield logical_path, absolute_path


import os
from gears.exceptions import ImproperlyConfigured, FileNotFound
from gears.finders import FileSystemFinder
from mock import patch, Mock
from unittest2 import TestCase

ASSETS_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), 'assets'))


class FileSystemFinderTests(TestCase):

    def test_initialization(self):
        finder = FileSystemFinder(('/first', '/second', '/third', '/first'))
        self.assertEqual(finder.locations, ['/first', '/second', '/third'])

    def test_if_directories_is_not_iterable(self):
        with self.assertRaises(ImproperlyConfigured):
            finder = FileSystemFinder('/first')

    @patch('os.path.exists')
    def test_find_location_if_exists(self, exists):
        exists.return_value = True
        finder = FileSystemFinder(('/assets',))
        location = finder.find_location('/assets', 'js/script.js')
        self.assertEqual(location, '/assets/js/script.js')
        exists.assert_called_once_with('/assets/js/script.js')

    @patch('os.path.exists')
    def test_find_location_if_does_not_exist(self, exists):
        exists.return_value = False
        finder = FileSystemFinder(('/assets',))
        self.assertIsNone(finder.find_location('/assets', 'js/script.js'))
        exists.assert_called_once_with('/assets/js/script.js')

    def test_find_if_exists(self):

# Path: gears/exceptions.py
# class ImproperlyConfigured(Exception):
#     pass
# 
# class FileNotFound(Exception):
#     pass
# 
# Path: gears/finders.py
# class FileSystemFinder(BaseFinder):
# 
#     def __init__(self, directories):
#         self.locations = []
#         if not isinstance(directories, (list, tuple)):
#             raise ImproperlyConfigured(
#                 "FileSystemFinder's 'directories' parameter is not a "
#                 "tuple or list; perhaps you forgot a trailing comma?")
#         for directory in directories:
#             if directory not in self.locations:
#                 self.locations.append(directory)
# 
#     @property
#     def paths(self):
#         return self.locations
# 
#     def find(self, path):
#         for matched_path in self.find_all(path):
#             return matched_path
#         raise FileNotFound(path)
# 
#     def find_all(self, path):
#         for root in self.locations:
#             matched_path = self.find_location(root, path)
#             if matched_path:
#                 yield matched_path
# 
#     def find_location(self, root, path):
#         path = safe_join(root, path)
#         if os.path.exists(path):
#             return path
# 
#     def list(self, path):
#         for root in self.locations:
#             for absolute_path in glob2.iglob(safe_join(root, path)):
#                 if os.path.isfile(absolute_path):
#                     logical_path = os.path.relpath(absolute_path, root)
#                     yield logical_path, absolute_path


# Path: tests/test_file_system_finder.py
import os
from gears.exceptions import ImproperlyConfigured, FileNotFound
from gears.finders import FileSystemFinder
from mock import patch, Mock
from unittest2 import TestCase


ASSETS_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), 'assets'))


class FileSystemFinderTests(TestCase):

    def test_initialization(self):
        finder = FileSystemFinder(('/first', '/second', '/third', '/first'))
        self.assertEqual(finder.locations, ['/first', '/second', '/third'])

    def test_if_directories_is_not_iterable(self):
        with self.assertRaises(ImproperlyConfigured):
            finder = FileSystemFinder('/first')

    @patch('os.path.exists')
    def test_find_location_if_exists(self, exists):
        exists.return_value = True
        finder = FileSystemFinder(('/assets',))
        location = finder.find_location('/assets', 'js/script.js')
        self.assertEqual(location, '/assets/js/script.js')
        exists.assert_called_once_with('/assets/js/script.js')

    @patch('os.path.exists')
    def test_find_location_if_does_not_exist(self, exists):
        exists.return_value = False
        finder = FileSystemFinder(('/assets',))
        self.assertIsNone(finder.find_location('/assets', 'js/script.js'))
        exists.assert_called_once_with('/assets/js/script.js')

    def test_find_if_exists(self):

        finder = FileSystemFinder(('/first', '/second', '/third'))