Ejemplo n.º 1
0
def test_create_bindiff_for_files_unknown_hashes(data_path, media_path,
                                                 read_headers):
    file1 = os.path.join(data_path, 'old', 'bigger_file.txt')
    file2 = os.path.join(data_path, 'new', 'bigger_file.txt')
    file_patch = os.path.join(media_path, 'patch')

    with pytest.raises(ValueError):
        diff.create_bindiff_for_files(
            file1=file1,
            file2=file2,
            file_patch=file_patch,
            block_size_in=1,
            hashsums=('wtfsum', ),
        )

    assert not os.path.exists(file_patch)
Ejemplo n.º 2
0
def test_create_bindiff_for_files_complex(data_path, media_path, read_headers):
    file1 = os.path.join(data_path, 'old', 'bigger_file.txt')
    file2 = os.path.join(data_path, 'new', 'bigger_file.txt')
    file_patch = os.path.join(media_path, 'patch')

    result = diff.create_bindiff_for_files(
        file1=file1,
        file2=file2,
        file_patch=file_patch,
        block_size_in=1,
    )
    assert isinstance(result, dict)
    assert os.path.isfile(file_patch)

    with open(file_patch, 'rb') as fpp:
        assert read_headers(fpp) == {
            '1-mtime':
            datetime.utcfromtimestamp(
                os.stat(file1).st_mtime).strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
            '2-mtime':
            datetime.utcfromtimestamp(
                os.stat(file2).st_mtime).strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
            '1-mode':
            stat.filemode(os.stat(file1).st_mode)[1:],
            '2-mode':
            stat.filemode(os.stat(file2).st_mode)[1:],
            '1-sha256sum':
            '4795a1c2517089e4df569afd77c04e949139cf299c87f012b894fccf91df4594',
            '2-sha256sum':
            'dc4d8352e3da3129e1c85cb41ffdce1e2a307f308af9d3e1c770d80c5944e01f',
        }

        assert fpp.read(
        ) == b'= 3\n\n- 3\n\n456\n\n+ 3\n\nqqq\n\n= 4\n\n- 1\n\n\n\n\n+ 5\n\nabcd\n\n\n'
Ejemplo n.º 3
0
def test_create_bindiff_for_files_complex_fromempty_disallowed(
        data_path, media_path, read_headers):
    file1 = os.path.join(data_path, 'old', 'nonexisting_file.txt')
    file2 = os.path.join(data_path, 'new', 'bigger_file.txt')
    file_patch = os.path.join(media_path, 'patch')

    assert not os.path.exists(file1)

    with pytest.raises(OSError):
        diff.create_bindiff_for_files(
            file1=file1,
            file2=file2,
            file_patch=file_patch,
            block_size_in=1,
        )

    assert not os.path.exists(file_patch)
Ejemplo n.º 4
0
def test_create_bindiff_for_files_complex_gzip_manual(data_path, media_path,
                                                      read_headers):
    file1 = os.path.join(data_path, 'old', 'bigger_file.txt')
    file2 = os.path.join(data_path, 'new', 'bigger_file.txt')
    file_patch = os.path.join(media_path, 'patch')

    result = diff.create_bindiff_for_files(
        file1=file1,
        file2=file2,
        file_patch=file_patch,
        block_size_in=1,
        gzip_level=9,
    )
    assert isinstance(result, dict)
    assert os.path.isfile(file_patch)

    with gzip.open(file_patch, 'rb') as fpp:
        assert read_headers(fpp)
        assert fpp.read()
Ejemplo n.º 5
0
def test_create_bindiff_for_files_complex_gzip_auto_fromempty(
        data_path, media_path, read_headers):
    file1 = os.path.join(data_path, 'old', 'nonexisting_file.txt')
    file2 = os.path.join(data_path, 'new', 'bigger_file.txt')
    file_patch = os.path.join(media_path, 'patch.gz')

    result = diff.create_bindiff_for_files(
        file1=file1,
        file2=file2,
        file_patch=file_patch,
        block_size_in=1,
        allow_empty=True,
    )
    assert isinstance(result, dict)
    assert os.path.isfile(file_patch)

    with gzip.open(file_patch, 'rb') as fpp:
        assert read_headers(fpp)
        assert fpp.read()
Ejemplo n.º 6
0
def test_create_bindiff_for_files_complex_nogzip_manual(
        data_path, media_path, read_headers):
    file1 = os.path.join(data_path, 'old', 'bigger_file.txt')
    file2 = os.path.join(data_path, 'new', 'bigger_file.txt')
    file_patch = os.path.join(media_path, 'patch.gz')

    result = diff.create_bindiff_for_files(
        file1=file1,
        file2=file2,
        file_patch=file_patch,
        block_size_in=1,
        gzip_level=0,
    )
    assert isinstance(result, dict)
    assert os.path.isfile(file_patch)

    with open(file_patch, 'rb') as fpp:
        # Да-да, мы просили в .gz файл записать НЕ гзип :)
        assert read_headers(fpp)
        assert fpp.read()
Ejemplo n.º 7
0
def test_create_bindiff_for_files_complex_gzip_auto(data_path, media_path,
                                                    read_headers):
    file1 = os.path.join(data_path, 'old', 'bigger_file.txt')
    file2 = os.path.join(data_path, 'new', 'bigger_file.txt')
    file_patch = os.path.join(media_path, 'patch.gz')

    result = diff.create_bindiff_for_files(
        file1=file1,
        file2=file2,
        file_patch=file_patch,
        block_size_in=1,
    )
    assert isinstance(result, dict)
    assert os.path.isfile(file_patch)

    with gzip.open(file_patch, 'rb') as fpp:
        # Просто проверяем, что это действительно патч и действительно гзипнутый
        # (корректность содержимого патча уже проверили и перепроверили в тестах выше)
        assert read_headers(fpp)
        assert fpp.read()
Ejemplo n.º 8
0
def test_create_bindiff_for_files_complex_noheaders(data_path, media_path,
                                                    read_headers):
    file1 = os.path.join(data_path, 'old', 'bigger_file.txt')
    file2 = os.path.join(data_path, 'new', 'bigger_file.txt')
    file_patch = os.path.join(media_path, 'patch')

    result = diff.create_bindiff_for_files(
        file1=file1,
        file2=file2,
        file_patch=file_patch,
        block_size_in=1,
        mtime=False,
        modes=False,
        hashsums=(),
    )
    assert isinstance(result, dict)
    assert os.path.isfile(file_patch)

    with open(file_patch, 'rb') as fpp:
        assert read_headers(fpp) == {}
        assert fpp.read(
        ) == b'= 3\n\n- 3\n\n456\n\n+ 3\n\nqqq\n\n= 4\n\n- 1\n\n\n\n\n+ 5\n\nabcd\n\n\n'
Ejemplo n.º 9
0
def test_verbosity(data_path, media_path, read_headers):
    # Просто прогоняем всё с verbose, чтобы проверить, что ничего не упадёт

    file1 = os.path.join(data_path, 'old', 'bigger_file.txt')
    file2 = os.path.join(data_path, 'new', 'bigger_file.txt')
    file_patch = os.path.join(media_path, 'patch.gz')

    diff.create_bindiff_for_files(file1, file2, file_patch, verbose=2)
    diff.create_bindiff_for_files(file1,
                                  file2,
                                  file_patch,
                                  verbose=2,
                                  gzip_level=0)
    diff.create_bindiff_for_files(file1 + 'nonexist',
                                  file2,
                                  file_patch,
                                  verbose=2,
                                  allow_empty=True)
Ejemplo n.º 10
0
def test_create_bindiff_for_files_complex_fromempty(data_path, media_path,
                                                    read_headers):
    file1 = os.path.join(data_path, 'old', 'nonexisting_file.txt')
    file2 = os.path.join(data_path, 'new', 'bigger_file.txt')
    file_patch = os.path.join(media_path, 'patch')

    assert not os.path.exists(file1)

    result = diff.create_bindiff_for_files(
        file1=file1,
        file2=file2,
        file_patch=file_patch,
        block_size_in=1,
        allow_empty=True,
        block_size_out=2,  # должен игнорироваться при пустом первом файле
    )
    assert isinstance(result, dict)
    assert os.path.isfile(file_patch)

    with open(file_patch, 'rb') as fpp:
        assert read_headers(fpp) == {
            '1-mtime':
            datetime(1970, 1, 1, 0, 0, 0).strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
            '2-mtime':
            datetime.utcfromtimestamp(
                os.stat(file2).st_mtime).strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
            '1-mode':
            '---------',
            '2-mode':
            stat.filemode(os.stat(file2).st_mode)[1:],
            '1-sha256sum':
            'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
            '2-sha256sum':
            'dc4d8352e3da3129e1c85cb41ffdce1e2a307f308af9d3e1c770d80c5944e01f',
        }

        assert fpp.read() == b'+ 15\n\n123qqq7890abcd\n\n\n'