# Path: diffoscope/comparators/zip.py
# class ZipFile(File):
#     CONTAINER_CLASS = ZipContainer
#     RE_FILE_TYPE = re.compile(r'^(Zip archive|Java archive|EPUB document|OpenDocument (Text|Spreadsheet|Presentation|Drawing|Formula|Template|Text Template))\b')
# 
#     def compare_details(self, other, source=None):
#         zipinfo_difference = Difference.from_command(Zipinfo, self.path, other.path) or \
#                              Difference.from_command(ZipinfoVerbose, self.path, other.path)
#         return [zipinfo_difference]
# 
# class MozillaZipFile(File):
#     CONTAINER_CLASS = MozillaZipContainer
# 
#     @staticmethod
#     def recognizes(file):
#         # Mozilla-optimized ZIPs start with a 32-bit little endian integer
#         # indicating the amount of data to preload, followed by the ZIP
#         # central directory (with a PK\x01\x02 signature)
#         with open(file.path, 'rb') as f:
#             preload = f.read(4)
#             if len(preload) == 4:
#                 signature = f.read(4)
#                 return signature == b'PK\x01\x02'
# 
#     def compare_details(self, other, source=None):
#         zipinfo_difference = Difference.from_command(MozillaZipinfo, self.path, other.path) or \
#                              Difference.from_command(MozillaZipinfoVerbose, self.path, other.path)
#         return [zipinfo_difference]


import pytest
from diffoscope.comparators.zip import ZipFile, MozillaZipFile
from utils.data import load_fixture, get_data
from utils.tools import skip_unless_tools_exist
from utils.nonexisting import assert_non_existing

@skip_unless_tools_exist('zipinfo')
def test_compressed_files(differences):
    assert differences[1].source1 == 'dir/text'
    assert differences[1].source2 == 'dir/text'
    expected_diff = get_data('text_ascii_expected_diff')
    assert differences[1].unified_diff == expected_diff

@skip_unless_tools_exist('zipinfo')
def test_compare_non_existing(monkeypatch, zip1):
    assert_non_existing(monkeypatch, zip1)

def test_mozzip_identification(mozzip1):
    assert isinstance(mozzip1, MozillaZipFile)

def test_mozzip_no_differences(mozzip1):
    difference = mozzip1.compare(mozzip1)
    assert difference is None

@pytest.fixture
def mozzip_differences(mozzip1, mozzip2):
    return mozzip1.compare(mozzip2).details

@skip_unless_tools_exist('zipinfo')
def test_mozzip_metadata(mozzip_differences, mozzip1, mozzip2):
    expected_diff = get_data('mozzip_zipinfo_expected_diff')
    diff = mozzip_differences[0].unified_diff
    assert (diff.replace(mozzip1.path, 'test1.mozzip')
                .replace(mozzip2.path, 'test2.mozzip')) == expected_diff


# Path: diffoscope/comparators/zip.py
# class ZipFile(File):
#     CONTAINER_CLASS = ZipContainer
#     RE_FILE_TYPE = re.compile(r'^(Zip archive|Java archive|EPUB document|OpenDocument (Text|Spreadsheet|Presentation|Drawing|Formula|Template|Text Template))\b')
# 
#     def compare_details(self, other, source=None):
#         zipinfo_difference = Difference.from_command(Zipinfo, self.path, other.path) or \
#                              Difference.from_command(ZipinfoVerbose, self.path, other.path)
#         return [zipinfo_difference]
# 
# class MozillaZipFile(File):
#     CONTAINER_CLASS = MozillaZipContainer
# 
#     @staticmethod
#     def recognizes(file):
#         # Mozilla-optimized ZIPs start with a 32-bit little endian integer
#         # indicating the amount of data to preload, followed by the ZIP
#         # central directory (with a PK\x01\x02 signature)
#         with open(file.path, 'rb') as f:
#             preload = f.read(4)
#             if len(preload) == 4:
#                 signature = f.read(4)
#                 return signature == b'PK\x01\x02'
# 
#     def compare_details(self, other, source=None):
#         zipinfo_difference = Difference.from_command(MozillaZipinfo, self.path, other.path) or \
#                              Difference.from_command(MozillaZipinfoVerbose, self.path, other.path)
#         return [zipinfo_difference]


# Path: tests/comparators/test_zip.py
import pytest
from diffoscope.comparators.zip import ZipFile, MozillaZipFile
from utils.data import load_fixture, get_data
from utils.tools import skip_unless_tools_exist
from utils.nonexisting import assert_non_existing


@skip_unless_tools_exist('zipinfo')
def test_compressed_files(differences):
    assert differences[1].source1 == 'dir/text'
    assert differences[1].source2 == 'dir/text'
    expected_diff = get_data('text_ascii_expected_diff')
    assert differences[1].unified_diff == expected_diff

@skip_unless_tools_exist('zipinfo')
def test_compare_non_existing(monkeypatch, zip1):
    assert_non_existing(monkeypatch, zip1)

def test_mozzip_identification(mozzip1):
    assert isinstance(mozzip1, MozillaZipFile)

def test_mozzip_no_differences(mozzip1):
    difference = mozzip1.compare(mozzip1)
    assert difference is None

@pytest.fixture
def mozzip_differences(mozzip1, mozzip2):
    return mozzip1.compare(mozzip2).details

@skip_unless_tools_exist('zipinfo')
def test_mozzip_metadata(mozzip_differences, mozzip1, mozzip2):
    expected_diff = get_data('mozzip_zipinfo_expected_diff')
    diff = mozzip_differences[0].unified_diff
    assert (diff.replace(mozzip1.path, 'test1.mozzip')
                .replace(mozzip2.path, 'test2.mozzip')) == expected_diff


@skip_unless_tools_exist('zipinfo')