|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.IO; |
| 4 | +using LibGit2Sharp.Tests.TestHelpers; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace LibGit2Sharp.Tests |
| 8 | +{ |
| 9 | + public class ArchiveFixture : BaseFixture |
| 10 | + { |
| 11 | + [Fact] |
| 12 | + public void CanArchiveATree() |
| 13 | + { |
| 14 | + using (var repo = new Repository(BareTestRepoPath)) |
| 15 | + { |
| 16 | + var tree = repo.Lookup<Tree>("581f9824ecaf824221bd36edf5430f2739a7c4f5"); |
| 17 | + |
| 18 | + var archiver = new MockArchiver(); |
| 19 | + |
| 20 | + repo.ObjectDatabase.Archive(tree, archiver); |
| 21 | + |
| 22 | + var expected = new ArrayList |
| 23 | + { |
| 24 | + new { Path = "1", Sha = "7f76480d939dc401415927ea7ef25c676b8ddb8f" }, |
| 25 | + new { Path = Path.Combine("1", "branch_file.txt"), Sha = "45b983be36b73c0788dc9cbcb76cbb80fc7bb057" }, |
| 26 | + new { Path = "README", Sha = "a8233120f6ad708f843d861ce2b7228ec4e3dec6" }, |
| 27 | + new { Path = "branch_file.txt", Sha = "45b983be36b73c0788dc9cbcb76cbb80fc7bb057" }, |
| 28 | + new { Path = "new.txt", Sha = "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd" }, |
| 29 | + }; |
| 30 | + Assert.Equal(expected, archiver.Files); |
| 31 | + Assert.Null(archiver.ReceivedCommitSha); |
| 32 | + Assert.InRange(archiver.ModificationTime, DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMilliseconds(100)), DateTimeOffset.UtcNow); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void CanArchiveACommit() |
| 38 | + { |
| 39 | + using (var repo = new Repository(BareTestRepoPath)) |
| 40 | + { |
| 41 | + var commit = repo.Lookup<Commit>("4c062a6361ae6959e06292c1fa5e2822d9c96345"); |
| 42 | + |
| 43 | + var archiver = new MockArchiver(); |
| 44 | + |
| 45 | + repo.ObjectDatabase.Archive(commit, archiver); |
| 46 | + |
| 47 | + var expected = new ArrayList |
| 48 | + { |
| 49 | + new { Path = "1", Sha = "7f76480d939dc401415927ea7ef25c676b8ddb8f" }, |
| 50 | + new { Path = Path.Combine("1", "branch_file.txt"), Sha = "45b983be36b73c0788dc9cbcb76cbb80fc7bb057" }, |
| 51 | + new { Path = "README", Sha = "a8233120f6ad708f843d861ce2b7228ec4e3dec6" }, |
| 52 | + new { Path = "branch_file.txt", Sha = "45b983be36b73c0788dc9cbcb76cbb80fc7bb057" }, |
| 53 | + new { Path = "new.txt", Sha = "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd" }, |
| 54 | + }; |
| 55 | + Assert.Equal(expected, archiver.Files); |
| 56 | + Assert.Equal(commit.Sha, archiver.ReceivedCommitSha); |
| 57 | + Assert.Equal(commit.Committer.When, archiver.ModificationTime); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public void ArchivingANullTreeOrCommitThrows() |
| 63 | + { |
| 64 | + using (var repo = new Repository(BareTestRepoPath)) |
| 65 | + { |
| 66 | + Assert.Throws<ArgumentNullException>(() => repo.ObjectDatabase.Archive((Commit)null, null)); |
| 67 | + Assert.Throws<ArgumentNullException>(() => repo.ObjectDatabase.Archive((Tree)null, null)); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + #region MockArchiver |
| 72 | + |
| 73 | + private class MockArchiver : ArchiverBase |
| 74 | + { |
| 75 | + public readonly ArrayList Files = new ArrayList(); |
| 76 | + public string ReceivedCommitSha; |
| 77 | + public DateTimeOffset ModificationTime; |
| 78 | + |
| 79 | + #region Overrides of ArchiverBase |
| 80 | + |
| 81 | + public override void BeforeArchiving(Tree tree, ObjectId oid, DateTimeOffset modificationTime) |
| 82 | + { |
| 83 | + if (oid != null) |
| 84 | + { |
| 85 | + ReceivedCommitSha = oid.Sha; |
| 86 | + } |
| 87 | + ModificationTime = modificationTime; |
| 88 | + } |
| 89 | + |
| 90 | + protected override void AddTreeEntry(string path, TreeEntry entry, DateTimeOffset modificationTime) |
| 91 | + { |
| 92 | + Files.Add(new { Path = path, entry.Target.Sha }); |
| 93 | + } |
| 94 | + |
| 95 | + #endregion |
| 96 | + } |
| 97 | + |
| 98 | + #endregion |
| 99 | + } |
| 100 | +} |
0 commit comments