|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | +using LibGit2Sharp.Tests.TestHelpers; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace LibGit2Sharp.Tests |
| 8 | +{ |
| 9 | + public class PackBuilderFixture : BaseFixture |
| 10 | + { |
| 11 | + [Fact] |
| 12 | + public void TestDefaultPackDelegate() |
| 13 | + { |
| 14 | + TestIfSameRepoAfterPacking(null); |
| 15 | + } |
| 16 | + |
| 17 | + [Fact] |
| 18 | + public void TestCommitsPerBranchPackDelegate() |
| 19 | + { |
| 20 | + TestIfSameRepoAfterPacking(AddingObjectIdsTestDelegate); |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public void TestCommitsPerBranchIdsPackDelegate() |
| 25 | + { |
| 26 | + TestIfSameRepoAfterPacking(AddingObjectsTestDelegate); |
| 27 | + } |
| 28 | + |
| 29 | + internal void TestIfSameRepoAfterPacking(Action<IRepository, PackBuilder> packDelegate) |
| 30 | + { |
| 31 | + // read a repo |
| 32 | + // pack with the provided action |
| 33 | + // write the pack file in a mirror repo |
| 34 | + // read new repo |
| 35 | + // compare |
| 36 | + |
| 37 | + string orgRepoPath = SandboxPackBuilderTestRepo(); |
| 38 | + string mrrRepoPath = SandboxPackBuilderTestRepo(); |
| 39 | + string mrrRepoPackDirPath = Path.Combine(mrrRepoPath + "/.git/objects"); |
| 40 | + |
| 41 | + DirectoryHelper.DeleteDirectory(mrrRepoPackDirPath); |
| 42 | + Directory.CreateDirectory(mrrRepoPackDirPath + "/pack"); |
| 43 | + |
| 44 | + PackBuilderOptions packBuilderOptions = new PackBuilderOptions(mrrRepoPackDirPath + "/pack"); |
| 45 | + |
| 46 | + using (Repository orgRepo = new Repository(orgRepoPath)) |
| 47 | + { |
| 48 | + PackBuilderResults results; |
| 49 | + if (packDelegate != null) |
| 50 | + results = orgRepo.ObjectDatabase.Pack(packBuilderOptions, b => packDelegate(orgRepo, b)); |
| 51 | + else |
| 52 | + results = orgRepo.ObjectDatabase.Pack(packBuilderOptions); |
| 53 | + |
| 54 | + // written objects count is the same as in objects database |
| 55 | + Assert.Equal(orgRepo.ObjectDatabase.Count(), results.WrittenObjectsCount); |
| 56 | + |
| 57 | + // loading a repo from the written pack file. |
| 58 | + using (Repository mrrRepo = new Repository(mrrRepoPath)) |
| 59 | + { |
| 60 | + // make sure the objects of the original repo are the same as the ones in the mirror repo |
| 61 | + // doing that by making sure the count is the same, and the set difference is empty |
| 62 | + Assert.True(mrrRepo.ObjectDatabase.Count() == orgRepo.ObjectDatabase.Count() && !mrrRepo.ObjectDatabase.Except(orgRepo.ObjectDatabase).Any()); |
| 63 | + |
| 64 | + Assert.Equal(orgRepo.Commits.Count(), mrrRepo.Commits.Count()); |
| 65 | + Assert.Equal(orgRepo.Branches.Count(), mrrRepo.Branches.Count()); |
| 66 | + Assert.Equal(orgRepo.Refs.Count(), mrrRepo.Refs.Count()); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + internal void AddingObjectIdsTestDelegate(IRepository repo, PackBuilder builder) |
| 72 | + { |
| 73 | + foreach (Branch branch in repo.Branches) |
| 74 | + { |
| 75 | + foreach (Commit commit in branch.Commits) |
| 76 | + { |
| 77 | + builder.AddRecursively(commit.Id); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + foreach (Tag tag in repo.Tags) |
| 82 | + { |
| 83 | + builder.Add(tag.Target.Id); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + internal void AddingObjectsTestDelegate(IRepository repo, PackBuilder builder) |
| 88 | + { |
| 89 | + foreach (Branch branch in repo.Branches) |
| 90 | + { |
| 91 | + foreach (Commit commit in branch.Commits) |
| 92 | + { |
| 93 | + builder.AddRecursively(commit); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + foreach (Tag tag in repo.Tags) |
| 98 | + { |
| 99 | + builder.Add(tag.Target); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + [Fact] |
| 104 | + public void ExceptionIfPathDoesNotExist() |
| 105 | + { |
| 106 | + PackBuilderOptions pbo; |
| 107 | + |
| 108 | + Assert.Throws<DirectoryNotFoundException>(() => |
| 109 | + { |
| 110 | + pbo = new PackBuilderOptions("aaa"); |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + [Fact] |
| 115 | + public void ExceptionIfPathEqualsNull() |
| 116 | + { |
| 117 | + PackBuilderOptions pbo; |
| 118 | + |
| 119 | + Assert.Throws<ArgumentNullException>(() => |
| 120 | + { |
| 121 | + pbo = new PackBuilderOptions(null); |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + [Fact] |
| 126 | + public void ExceptionIfOptionsEqualsNull() |
| 127 | + { |
| 128 | + string orgRepoPath = SandboxPackBuilderTestRepo(); |
| 129 | + |
| 130 | + using (Repository orgRepo = new Repository(orgRepoPath)) |
| 131 | + { |
| 132 | + Assert.Throws<ArgumentNullException>(() => |
| 133 | + { |
| 134 | + orgRepo.ObjectDatabase.Pack(null); |
| 135 | + }); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + [Fact] |
| 140 | + public void ExceptionIfBuildDelegateEqualsNull() |
| 141 | + { |
| 142 | + string orgRepoPath = SandboxPackBuilderTestRepo(); |
| 143 | + PackBuilderOptions packBuilderOptions = new PackBuilderOptions(orgRepoPath); |
| 144 | + |
| 145 | + using (Repository orgRepo = new Repository(orgRepoPath)) |
| 146 | + { |
| 147 | + Assert.Throws<ArgumentNullException>(() => |
| 148 | + { |
| 149 | + orgRepo.ObjectDatabase.Pack(packBuilderOptions,
9920
null); |
| 150 | + }); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + [Fact] |
| 155 | + public void ExceptionIfNegativeNumberOfThreads() |
| 156 | + { |
| 157 | + string orgRepoPath = SandboxPackBuilderTestRepo(); |
| 158 | + PackBuilderOptions packBuilderOptions = new PackBuilderOptions(orgRepoPath); |
| 159 | + |
| 160 | + Assert.Throws<ArgumentException>(() => |
| 161 | + { |
| 162 | + packBuilderOptions.MaximumNumberOfThreads = -1; |
| 163 | + }); |
| 164 | + } |
| 165 | + |
| 166 | + [Fact] |
| 167 | + public void ExceptionIfAddNullObjectID() |
| 168 | + { |
| 169 | + string orgRepoPath = SandboxPackBuilderTestRepo(); |
| 170 | + PackBuilderOptions packBuilderOptions = new PackBuilderOptions(orgRepoPath); |
| 171 | + |
| 172 | + using (Repository orgRepo = new Repository(orgRepoPath)) |
| 173 | + { |
| 174 | + Assert.Throws<ArgumentNullException>(() => |
| 175 | + { |
| 176 | + orgRepo.ObjectDatabase.Pack(packBuilderOptions, builder => |
| 177 | + { |
| 178 | + builder.Add(null); |
| 179 | + }); |
| 180 | + }); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + [Fact] |
| 185 | + public void ExceptionIfAddRecursivelyNullObjectID() |
| 186 | + { |
| 187 | + string orgRepoPath = SandboxPackBuilderTestRepo(); |
| 188 | + PackBuilderOptions packBuilderOptions = new PackBuilderOptions(orgRepoPath); |
| 189 | + |
| 190 | + using (Repository orgRepo = new Repository(orgRepoPath)) |
| 191 | + { |
| 192 | + Assert.Throws<ArgumentNullException>(() => |
| 193 | + { |
| 194 | + orgRepo.ObjectDatabase.Pack(packBuilderOptions, builder => |
| 195 | + { |
| 196 | + builder.AddRecursively(null); |
| 197 | + }); |
| 198 | + }); |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments