# Path: gears/assets.py
# class Dependency(object):
# 
#     def __init__(self, environment, absolute_path):
#         self.environment = environment
#         self.absolute_path = absolute_path
#         if self.expired:
#             self._save_to_cache()
# 
#     def __eq__(self, other):
#         return self.absolute_path == other.absolute_path
# 
#     def __hash__(self):
#         return hash(self.absolute_path)
# 
#     @cached_property
#     def source(self):
#         if os.path.isdir(self.absolute_path):
#             source = ', '.join(sorted(os.listdir(self.absolute_path)))
#             return source.encode('utf-8') if is_py3 else source
#         with open(self.absolute_path, 'rb') as f:
#             return f.read()
# 
#     @cached_property
#     def mtime(self):
#         return os.stat(self.absolute_path).st_mtime
# 
#     @cached_property
#     def hexdigest(self):
#         return hashlib.sha1(self.source).hexdigest()
# 
#     @cached_property
#     def expired(self):
#         data = self.environment.cache.get(self._get_cache_key())
#         return (data is None or
#                 self.mtime > data['mtime'] or
#                 self.hexdigest > data['hexdigest'])
# 
#     def to_dict(self):
#         return {'mtime': self.mtime, 'hexdigest': self.hexdigest}
# 
#     def _save_to_cache(self):
#         self.environment.cache.set(self._get_cache_key(), self.to_dict())
# 
#     def _get_cache_key(self):
#         return 'dependency:%s' % self.absolute_path
# 
# Path: tests/helpers.py
# class GearsTestCase(unittest2.TestCase):
# 
#     fixtures_root = None
# 
#     def get_fixture_path(self, fixture):
#         return os.path.join(FIXTURES_DIR, self.fixtures_root, fixture)
# 
#     def find_fixture_file(self, fixture, name):
#         fixture_path = self.get_fixture_path(fixture)
#         for dirpath, dirnames, filenames in os.walk(fixture_path):
#             for filename in filenames:
#                 if filename.split('.')[0] == name:
#                     return os.path.join(dirpath, filename)
#         return None
# 
#     def get_source_path(self, fixture):
#         return self.find_fixture_file(fixture, 'source')
# 
#     def get_source(self, fixture):
#         return read(self.get_source_path(fixture))
# 
#     def get_output(self, fixture, name='output'):
#         return read(self.find_fixture_file(fixture, name))
# 
#     def get_finder(self, fixture):
#         fixture_path = self.get_fixture_path(fixture)
#         return FileSystemFinder([fixture_path])
# 
#     def get_environment(self, fixture):
#         finder = self.get_finder(fixture)
#         environment = Environment(os.path.join(TESTS_DIR, 'static'))
#         environment.finders.register(finder)
#         environment.register_defaults()
#         return environment
# 
#     def get_asset(self, fixture, environment=None, asset_class=Asset):
#         source_path = self.get_source_path(fixture)
#         if environment is None:
#             environment = self.get_environment(fixture)
# 
#         fixture_path = self.get_fixture_path(fixture)
#         logical_path = os.path.relpath(source_path, fixture_path)
#         asset_attributes = AssetAttributes(environment, logical_path)
# 
#         return asset_class(asset_attributes, source_path)
# 
#     def get_static_asset(self, fixture, environment=None):
#         return self.get_asset(fixture, environment, asset_class=StaticAsset)


import os
from gears.assets import Dependency
from .helpers import GearsTestCase


class AssetDependencyTests(GearsTestCase):

    fixtures_root = 'asset_dependency'

    def test_dependencies_with_the_same_path_are_equal(self):
        fixture = 'dependencies_with_the_same_path_are_equal'
        environment = self.get_environment(fixture)
        fixture_file = self.find_fixture_file(fixture, 'dependency')
        dependency1 = Dependency(environment, fixture_file)
        dependency2 = Dependency(environment, fixture_file)
        self.assertEqual(dependency1, dependency1)

    def test_handles_directories(self):
        fixture = 'handles_directories'
        fixture_path = self.get_fixture_path(fixture)
        environment = self.get_environment(fixture)
        Dependency(environment, os.path.join(fixture_path, 'libs'))

    def test_handles_binary_files(self):
        fixture = 'handles_binary_files'
        fixture_path = self.get_fixture_path(fixture)
        environment = self.get_environment(fixture)

# Path: gears/assets.py
# class Dependency(object):
# 
#     def __init__(self, environment, absolute_path):
#         self.environment = environment
#         self.absolute_path = absolute_path
#         if self.expired:
#             self._save_to_cache()
# 
#     def __eq__(self, other):
#         return self.absolute_path == other.absolute_path
# 
#     def __hash__(self):
#         return hash(self.absolute_path)
# 
#     @cached_property
#     def source(self):
#         if os.path.isdir(self.absolute_path):
#             source = ', '.join(sorted(os.listdir(self.absolute_path)))
#             return source.encode('utf-8') if is_py3 else source
#         with open(self.absolute_path, 'rb') as f:
#             return f.read()
# 
#     @cached_property
#     def mtime(self):
#         return os.stat(self.absolute_path).st_mtime
# 
#     @cached_property
#     def hexdigest(self):
#         return hashlib.sha1(self.source).hexdigest()
# 
#     @cached_property
#     def expired(self):
#         data = self.environment.cache.get(self._get_cache_key())
#         return (data is None or
#                 self.mtime > data['mtime'] or
#                 self.hexdigest > data['hexdigest'])
# 
#     def to_dict(self):
#         return {'mtime': self.mtime, 'hexdigest': self.hexdigest}
# 
#     def _save_to_cache(self):
#         self.environment.cache.set(self._get_cache_key(), self.to_dict())
# 
#     def _get_cache_key(self):
#         return 'dependency:%s' % self.absolute_path
# 
# Path: tests/helpers.py
# class GearsTestCase(unittest2.TestCase):
# 
#     fixtures_root = None
# 
#     def get_fixture_path(self, fixture):
#         return os.path.join(FIXTURES_DIR, self.fixtures_root, fixture)
# 
#     def find_fixture_file(self, fixture, name):
#         fixture_path = self.get_fixture_path(fixture)
#         for dirpath, dirnames, filenames in os.walk(fixture_path):
#             for filename in filenames:
#                 if filename.split('.')[0] == name:
#                     return os.path.join(dirpath, filename)
#         return None
# 
#     def get_source_path(self, fixture):
#         return self.find_fixture_file(fixture, 'source')
# 
#     def get_source(self, fixture):
#         return read(self.get_source_path(fixture))
# 
#     def get_output(self, fixture, name='output'):
#         return read(self.find_fixture_file(fixture, name))
# 
#     def get_finder(self, fixture):
#         fixture_path = self.get_fixture_path(fixture)
#         return FileSystemFinder([fixture_path])
# 
#     def get_environment(self, fixture):
#         finder = self.get_finder(fixture)
#         environment = Environment(os.path.join(TESTS_DIR, 'static'))
#         environment.finders.register(finder)
#         environment.register_defaults()
#         return environment
# 
#     def get_asset(self, fixture, environment=None, asset_class=Asset):
#         source_path = self.get_source_path(fixture)
#         if environment is None:
#             environment = self.get_environment(fixture)
# 
#         fixture_path = self.get_fixture_path(fixture)
#         logical_path = os.path.relpath(source_path, fixture_path)
#         asset_attributes = AssetAttributes(environment, logical_path)
# 
#         return asset_class(asset_attributes, source_path)
# 
#     def get_static_asset(self, fixture, environment=None):
#         return self.get_asset(fixture, environment, asset_class=StaticAsset)


# Path: tests/test_asset_dependency.py
import os
from gears.assets import Dependency
from .helpers import GearsTestCase



class AssetDependencyTests(GearsTestCase):

    fixtures_root = 'asset_dependency'

    def test_dependencies_with_the_same_path_are_equal(self):
        fixture = 'dependencies_with_the_same_path_are_equal'
        environment = self.get_environment(fixture)
        fixture_file = self.find_fixture_file(fixture, 'dependency')
        dependency1 = Dependency(environment, fixture_file)
        dependency2 = Dependency(environment, fixture_file)
        self.assertEqual(dependency1, dependency1)

    def test_handles_directories(self):
        fixture = 'handles_directories'
        fixture_path = self.get_fixture_path(fixture)
        environment = self.get_environment(fixture)
        Dependency(environment, os.path.join(fixture_path, 'libs'))

    def test_handles_binary_files(self):
        fixture = 'handles_binary_files'
        fixture_path = self.get_fixture_path(fixture)
        environment = self.get_environment(fixture)

        Dependency(environment, os.path.join(fixture_path, 'image.png'))