|
| 1 | +# Copyright 2010-2020 The pygit2 contributors |
| 2 | +# |
| 3 | +# This file is free software; you can redistribute it and/or modify |
| 4 | +# it under the terms of the GNU General Public License, version 2, |
| 5 | +# as published by the Free Software Foundation. |
| 6 | +# |
| 7 | +# In addition to the permissions in the GNU General Public License, |
| 8 | +# the authors give you unlimited permission to link the compiled |
| 9 | +# version of this file into combinations with other programs, |
| 10 | +# and to distribute those combinations without any restriction |
| 11 | +# coming from the use of this file. (The General Public License |
| 12 | +# restrictions do apply in other respects; for example, they cover |
| 13 | +# modification of the file, and distribution when not linked into |
| 14 | +# a combined executable.) |
| 15 | +# |
| 16 | +# This file is distributed in the hope that it will be useful, but |
| 17 | +# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | +# General Public License for more details. |
| 20 | +# |
| 21 | +# You should have received a copy of the GNU General Public License |
| 22 | +# along with this program; see the file COPYING. If not, write to |
| 23 | +# the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
| 24 | +# Boston, MA 02110-1301, USA. |
| 25 | + |
| 26 | +"""Tests for Index files.""" |
| 27 | + |
| 28 | +import os |
| 29 | + |
| 30 | +import pytest |
| 31 | + |
| 32 | +import pygit2 |
| 33 | +from pygit2 import PackBuilder |
| 34 | +from . import utils |
| 35 | +from .utils import rmtree |
| 36 | + |
| 37 | + |
| 38 | +def test_create_packbuilder(testrepo): |
| 39 | + # simple test of PackBuilder creation |
| 40 | + packbuilder = PackBuilder(testrepo) |
| 41 | + assert len(packbuilder) == 0 |
| 42 | + |
| 43 | + |
| 44 | +def test_add(testrepo): |
| 45 | + # Add a few objects and confirm that the count is correct |
| 46 | + packbuilder = PackBuilder(testrepo) |
| 47 | + objects_to_add = [obj for obj in testrepo] |
| 48 | + packbuilder.add(objects_to_add[0]) |
| 49 | + assert len(packbuilder) == 1 |
| 50 | + packbuilder.add(objects_to_add[1]) |
| 51 | + assert len(packbuilder) == 2 |
| 52 | + |
| 53 | + |
| 54 | +def test_add_recursively(testrepo): |
| 55 | + # Add the head object and referenced objects recursively and confirm that the count is correct |
| 56 | + packbuilder = PackBuilder(testrepo) |
| 57 | + packbuilder.add_recur(testrepo.head.target) |
| 58 | + |
| 59 | + #expect a count of 4 made up of the following referenced objects: |
| 60 | + # Commit |
| 61 | + # Tree |
| 62 | + # Blob: hello.txt |
| 63 | + # Blob: .gitignore |
| 64 | + |
| 65 | + assert len(packbuilder) == 4 |
| 66 | + |
| 67 | + |
| 68 | +def test_repo_pack(testrepo, tmp_path): |
| 69 | + # pack the repo with the default strategy |
| 70 | + confirm_same_repo_after_packing(testrepo, tmp_path, None) |
| 71 | + |
| 72 | + |
| 73 | +def test_pack_with_delegate(testrepo, tmp_path): |
| 74 | + # loop through all branches and add each commit to the packbuilder |
| 75 | + def pack_delegate(pb): |
| 76 | + for branch in pb._repo.branches: |
| 77 | + br = pb._repo.branches.get(branch) |
| 78 | + for commit in br.log(): |
| 79 | + pb.add_recur(commit.oid_new) |
| 80 | + confirm_same_repo_after_packing(testrepo, tmp_path, pack_delegate) |
| 81 | + |
| 82 | + |
| 83 | +def setup_second_repo(tmp_path): |
| 84 | + # helper method to set up a second repo for comparison |
| 85 | + tmp_path_2 = os.path.join(tmp_path, 'test_repo2') |
| 86 | + with utils.TemporaryRepository('testrepo.tar', tmp_path_2) as path: |
| 87 | + testrepo = pygit2.Repository(path) |
| 88 | + return testrepo |
| 89 | + |
| 90 | +def confirm_same_repo_after_packing(testrepo, tmp_path, pack_delegate): |
| 91 | + # Helper method to confirm the contents of two repos before and after packing |
| 92 | + pack_repo = setup_second_repo(tmp_path) |
| 93 | + |
| 94 | + objects_dir = os.path.join(pack_repo.path, 'objects') |
| 95 | + rmtree(objects_dir) |
| 96 | + pack_path = os.path.join(pack_repo.path, 'objects', 'pack') |
| 97 | + os.makedirs(pack_path) |
| 98 | + |
| 99 | + # assert that the number of written objects is the same as the number of objects in the repo |
| 100 | + written_objects = testrepo.pack(pack_path, pack_delegate=pack_delegate) |
| 101 | + assert written_objects == len([obj for obj in testrepo]) |
| 102 | + |
| 103 | + |
| 104 | + # assert that the number of objects in the pack repo is the same as the original repo |
| 105 | + orig_objects = [obj for obj in testrepo.odb] |
| 106 | + packed_objects = [obj for obj in pack_repo.odb] |
| 107 | + assert len(packed_objects) == len(orig_objects) |
| 108 | + |
| 109 | + # assert that the objects in the packed repo are the same objects as the original repo |
| 110 | + for i, obj in enumerate(orig_objects): |
| 111 | + assert pack_repo[obj].type == testrepo[obj].type |
| 112 | + assert pack_repo[obj].read_raw() == testrepo[obj].read_raw() |
0 commit comments