|
| 1 | +using System.IO; |
| 2 | +using System.Text; |
| 3 | +using LibGit2Sharp.Tests.TestHelpers; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace LibGit2Sharp.Tests |
| 7 | +{ |
| 8 | + public class AttributesFixture : BaseFixture |
| 9 | + { |
| 10 | + [Fact] |
| 11 | + public void StagingHonorsTheAttributesFiles() |
| 12 | + { |
| 13 | + TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath); |
| 14 | + using (var repo = new Repository(path.RepositoryPath)) |
| 15 | + { |
| 16 | + CreateAttributesFile(repo); |
| 17 | + |
| 18 | + AssertNormalization(repo, "text.txt", true, "22c74203bace3c2e950278c7ab08da0fca9f4e9b"); |
| 19 | + AssertNormalization(repo, "huh.dunno", true, "22c74203bace3c2e950278c7ab08da0fca9f4e9b"); |
| 20 | + AssertNormalization(repo, "binary.data", false, "66eeff1fcbacf589e6d70aa70edd3fce5be2b37c"); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + private void AssertNormalization(Repository repo, string filename, bool shouldHaveBeenNormalized, string expectedSha) |
| 25 | + { |
| 26 | + var sb = new StringBuilder(); |
| 27 | + sb.Append("I'm going to be dynamically processed\r\n"); |
| 28 | + sb.Append("And my line endings...\r\n"); |
| 29 | + sb.Append("...are going to be\n"); |
| 30 | + sb.Append("normalized!\r\n"); |
| 31 | + |
| 32 | + File.WriteAllText(Path.Combine(repo.Info.WorkingDirectory, filename), sb.ToString()); |
| 33 | + |
| 34 | + repo.Index.Stage(filename); |
| 35 | + |
| 36 | + IndexEntry entry = repo.Index[filename]; |
| 37 | + Assert.NotNull(entry); |
| 38 | + |
| 39 | + Assert.Equal(expectedSha, entry.Id.Sha); |
| 40 | + |
| 41 | + var blob = repo.Lookup<Blob>(entry.Id); |
| 42 | + Assert.NotNull(blob); |
| 43 | + |
| 44 | + Assert.Equal(!shouldHaveBeenNormalized, blob.ContentAsUtf8().Contains("\r")); |
| 45 | + } |
| 46 | + |
| 47 | + private void CreateAttributesFile(Repository repo) |
| 48 | + { |
| 49 | + const string relativePath =<
8000
/span> ".gitattributes"; |
| 50 | + string fullFilePath = Path.Combine(repo.Info.WorkingDirectory, relativePath); |
| 51 | + |
| 52 | + var sb = new StringBuilder(); |
| 53 | + sb.Append("* text=auto\n"); |
| 54 | + sb.Append("*.txt text\n"); |
| 55 | + sb.Append("*.data binary\n"); |
| 56 | + |
| 57 | + File.WriteAllText(fullFilePath, sb.ToString()); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments