# Path: diffoscope/comparators/utils/specialize.py
# def specialize(file):
#     for cls in ComparatorManager().classes:
#         if isinstance(file, cls):
#             return file
# 
#         # Does this file class match?
#         flag = False
#         if hasattr(cls, 'recognizes'):
#             with profile('recognizes', file):
#                 flag = cls.recognizes(file)
#         else:
#             re_tests = [(x, y) for x, y in (
#                 (cls.RE_FILE_TYPE, file.magic_file_type),
#                 (cls.RE_FILE_EXTENSION, file.name),
#             ) if x]
# 
#             # If neither are defined, it's *not* a match.
#             if re_tests:
#                 flag = all(x.search(y) for x, y in re_tests)
# 
#         if not flag:
#             continue
# 
#         # Found a match; perform type magic
#         logger.debug("Using %s for %s", cls.__name__, file.name)
#         new_cls = type(cls.__name__, (cls, type(file)), {})
#         file.__class__ = new_cls
# 
#         return file
# 
#     logger.debug("Unidentified file. Magic says: %s", file.magic_file_type)
# 
#     return file


import shutil
import pytest
from diffoscope.comparators.bzip2 import Bzip2File
from diffoscope.comparators.binary import FilesystemFile
from diffoscope.comparators.utils.specialize import specialize
from utils.data import load_fixture, get_data
from utils.tools import skip_unless_tools_exist
from utils.nonexisting import assert_non_existing



bzip1 = load_fixture('test1.bz2')
bzip2 = load_fixture('test2.bz2')


def test_identification(bzip1):
    assert isinstance(bzip1, Bzip2File)

def test_no_differences(bzip1):
    difference = bzip1.compare(bzip1)
    assert difference is None

@pytest.fixture
def differences(bzip1, bzip2):
    return bzip1.compare(bzip2).details

@skip_unless_tools_exist('bzip2')
def test_content_source(differences):
    assert differences[0].source1 == 'test1'
    assert differences[0].source2 == 'test2'

@skip_unless_tools_exist('bzip2')
def test_content_source_without_extension(tmpdir, bzip1, bzip2):
    path1 = str(tmpdir.join('test1'))
    path2 = str(tmpdir.join('test2'))
    shutil.copy(bzip1.path, path1)
    shutil.copy(bzip2.path, path2)
    bzip1 = specialize(FilesystemFile(path1))

# Path: diffoscope/comparators/utils/specialize.py
# def specialize(file):
#     for cls in ComparatorManager().classes:
#         if isinstance(file, cls):
#             return file
# 
#         # Does this file class match?
#         flag = False
#         if hasattr(cls, 'recognizes'):
#             with profile('recognizes', file):
#                 flag = cls.recognizes(file)
#         else:
#             re_tests = [(x, y) for x, y in (
#                 (cls.RE_FILE_TYPE, file.magic_file_type),
#                 (cls.RE_FILE_EXTENSION, file.name),
#             ) if x]
# 
#             # If neither are defined, it's *not* a match.
#             if re_tests:
#                 flag = all(x.search(y) for x, y in re_tests)
# 
#         if not flag:
#             continue
# 
#         # Found a match; perform type magic
#         logger.debug("Using %s for %s", cls.__name__, file.name)
#         new_cls = type(cls.__name__, (cls, type(file)), {})
#         file.__class__ = new_cls
# 
#         return file
# 
#     logger.debug("Unidentified file. Magic says: %s", file.magic_file_type)
# 
#     return file


# Path: tests/comparators/test_bzip2.py
import shutil
import pytest
from diffoscope.comparators.bzip2 import Bzip2File
from diffoscope.comparators.binary import FilesystemFile
from diffoscope.comparators.utils.specialize import specialize
from utils.data import load_fixture, get_data
from utils.tools import skip_unless_tools_exist
from utils.nonexisting import assert_non_existing




bzip1 = load_fixture('test1.bz2')
bzip2 = load_fixture('test2.bz2')


def test_identification(bzip1):
    assert isinstance(bzip1, Bzip2File)

def test_no_differences(bzip1):
    difference = bzip1.compare(bzip1)
    assert difference is None

@pytest.fixture
def differences(bzip1, bzip2):
    return bzip1.compare(bzip2).details

@skip_unless_tools_exist('bzip2')
def test_content_source(differences):
    assert differences[0].source1 == 'test1'
    assert differences[0].source2 == 'test2'

@skip_unless_tools_exist('bzip2')
def test_content_source_without_extension(tmpdir, bzip1, bzip2):
    path1 = str(tmpdir.join('test1'))
    path2 = str(tmpdir.join('test2'))
    shutil.copy(bzip1.path, path1)
    shutil.copy(bzip2.path, path2)
    bzip1 = specialize(FilesystemFile(path1))

    bzip2 = specialize(FilesystemFile(path2))