|
| 1 | +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 2 | +using System; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using TestUtilities; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace Microsoft.Build.Tasks.Git.UnitTests |
| 9 | +{ |
| 10 | + public class GitReferenceResolverTests |
| 11 | + { |
| 12 | + [Fact] |
| 13 | + public void ResolveReference() |
| 14 | + { |
| 15 | + using var temp = new TempRoot(); |
| 16 | + |
| 17 | + var gitDir = temp.CreateDirectory(); |
| 18 | + |
| 19 | + var commonDir = temp.CreateDirectory(); |
| 20 | + var refsHeadsDir = commonDir.CreateDirectory("refs").CreateDirectory("heads"); |
| 21 | + |
| 22 | + refsHeadsDir.CreateFile("master").WriteAllText("0000000000000000000000000000000000000000"); |
| 23 | + refsHeadsDir.CreateFile("br1").WriteAllText("ref: refs/heads/br2"); |
| 24 | + refsHeadsDir.CreateFile("br2").WriteAllText("ref: refs/heads/master"); |
| 25 | + |
| 26 | + var resolver = new GitReferenceResolver(gitDir.Path, commonDir.Path); |
| 27 | + |
| 28 | + Assert.Equal("0123456789ABCDEFabcdef000000000000000000", resolver.ResolveReference("0123456789ABCDEFabcdef000000000000000000")); |
| 29 | + |
| 30 | + Assert.Equal("0000000000000000000000000000000000000000", resolver.ResolveReference("ref: refs/heads/master")); |
| 31 | + Assert.Equal("0000000000000000000000000000000000000000", resolver.ResolveReference("ref: refs/heads/br1")); |
| 32 | + Assert.Equal("0000000000000000000000000000000000000000", resolver.ResolveReference("ref: refs/heads/br2")); |
| 33 | + |
| 34 | + // branch without commits (emtpy repository) will have not file in refs/heads: |
| 35 | + Assert.Null(resolver.ResolveReference("ref: refs/heads/none")); |
| 36 | + |
| 37 | + Assert.Null(resolver.ResolveReference("ref: refs/heads/rec1 ")); |
| 38 | + Assert.Null(resolver.ResolveReference("ref: refs/heads/none" + string.Join("/", Path.GetInvalidPathChars()))); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void ResolveReference_Errors() |
| 43 | + { |
| 44 | + using var temp = new TempRoot(); |
| 45 | + |
| 46 | + var gitDir = temp.CreateDirectory(); |
| 47 | + |
| 48 | + var commonDir = temp.CreateDirectory(); |
| 49 | + var refsHeadsDir = commonDir.CreateDirectory("refs").CreateDirectory("heads"); |
| 50 | + |
| 51 | + refsHeadsDir.CreateFile("rec1").WriteAllText("ref: refs/heads/rec2"); |
| 52 | + refsHeadsDir.CreateFile("rec2").WriteAllText("ref: refs/heads/rec1"); |
| 53 | + |
| 54 | + var resolver = new GitReferenceResolver(gitDir.Path, commonDir.Path); |
| 55 | + |
| 56 | + Assert.Throws<InvalidDataException>(() => resolver.ResolveReference("ref: refs/heads/rec1")); |
| 57 | + Assert.Throws<InvalidDataException>(() => resolver.ResolveReference("ref: xyz/heads/rec1")); |
| 58 | + Assert.Throws<InvalidDataException>(() => resolver.ResolveReference("ref:refs/heads/rec1")); |
| 59 | + Assert.Throws<InvalidDataException>(() => resolver.ResolveReference("refs/heads/rec1")); |
| 60 | + Assert.Throws<InvalidDataException>(() => resolver.ResolveReference(new string('0', 39))); |
| 61 | + Assert.Throws<InvalidDataException>(() => resolver.ResolveReference(new string('0', 41))); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void ResolveReference_Packed() |
| 66 | + { |
| 67 | + using var temp = new TempRoot(); |
| 68 | + |
| 69 | + var gitDir = temp.CreateDirectory(); |
| 70 | + |
| 71 | + gitDir.CreateFile("packed-refs").WriteAllText( |
| 72 | +@"# pack-refs with: peeled fully-peeled sorted |
| 73 | +1111111111111111111111111111111111111111 refs/heads/master |
| 74 | +2222222222222222222222222222222222222222 refs/heads/br2 |
| 75 | +"); |
| 76 | + var commonDir = temp.CreateDirectory(); |
| 77 | + var refsHeadsDir = commonDir.CreateDirectory("refs").CreateDirectory("heads"); |
| 78 | + |
| 79 | + refsHeadsDir.CreateFile("br1").WriteAllText("ref: refs/heads/br2"); |
| 80 | + |
| 81 | + var resolver = new GitReferenceResolver(gitDir.Path, commonDir.Path); |
| 82 | + |
| 83 | + Assert.Equal("1111111111111111111111111111111111111111", resolver.ResolveReference("ref: refs/heads/master")); |
| 84 | + Assert.Equal("2222222222222222222222222222222222222222", resolver.ResolveReference("ref: refs/heads/br1")); |
| 85 | + Assert.Equal("2222222222222222222222222222222222222222", resolver.ResolveReference("ref: refs/heads/br2")); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void ReadPackedReferences() |
| 90 | + { |
| 91 | + var packedRefs = |
| 92 | +@"# pack-refs with: |
| 93 | +1111111111111111111111111111111111111111 refs/heads/master |
| 94 | +2222222222222222222222222222222222222222 refs/heads/br |
| 95 | +^3333333333333333333333333333333333333333 |
| 96 | +4444444444444444444444444444444444444444 x |
| 97 | +5555555555555555555555555555555555555555 y |
| 98 | +6666666666666666666666666666666666666666 y z |
| 99 | +7777777777777777777777777777777777777777 refs/heads/br |
| 100 | +"; |
| 101 | + |
| 102 | + var actual = GitReferenceResolver.ReadPackedReferences(new StringReader(packedRefs), "<path>"); |
| 103 | + |
| 104 | + AssertEx.SetEqual(new[] |
| 105 | + { |
| 106 | + "refs/heads/br:2222222222222222222222222222222222222222", |
| 107 | + "refs/heads/master:1111111111111111111111111111111111111111" |
| 108 | + }, actual.Select(e => $"{e.Key}:{e.Value}")); |
| 109 | + } |
| 110 | + |
| 111 | + [Theory] |
| 112 | + [InlineData("# pack-refs with:")] |
| 113 | + [InlineData("# pack-refs with:xyz")] |
| 114 | + [InlineData("# pack-refs with:xyz\n")] |
| 115 | + public void ReadPackedReferences_Empty(string content) |
| 116 | + { |
| 117 | + Assert.Empty(GitReferenceResolver.ReadPackedReferences(new StringReader(content), "<path>")); |
| 118 | + } |
| 119 | + |
| 120 | + [Theory] |
| 121 | + [InlineData("")] // missing header |
| 122 | + [InlineData("# pack-refs with")] // invalid header prefix |
| 123 | + [InlineData("# pack-refs with:xyz\n1")] // bad object id |
| 124 | + [InlineData("# pack-refs with:xyz\n1111111111111111111111111111111111111111")] // no reference name |
| 125 | + [InlineData("# pack-refs with:xyz\n^1111111111111111111111111111111111111111")] // tag dereference without previous ref |
| 126 | + [InlineData("# pack-refs with:xyz\n1111111111111111111111111111111111111111 x\n^1")] // bad object id |
| 127 | + [InlineData("# pack-refs with:xyz\n^1111111111111111111111111111111111111111\n^2222222222222222222222222222222222222222")] // tag dereference without previous ref |
| 128 | + public void ReadPackedReferences_Errors(string content) |
| 129 | + { |
| 130 | + Assert.Throws<InvalidDataException>(() => GitReferenceResolver.ReadPackedReferences(new StringReader(content), "<path>")); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments