Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AttributesFixture.cs « LibGit2Sharp.Tests - github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: df0282b4f9ce51ecdbd452029937587e430ef9a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System.IO;
using System.Text;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;

namespace LibGit2Sharp.Tests
{
    public class AttributesFixture : BaseFixture
    {
        [Fact]
        public void StagingHonorsTheAttributesFiles()
        {
            string path = CloneStandardTestRepo();
            using (var repo = new Repository(path))
            {
                CreateAttributesFile(repo);

                AssertNormalization(repo, "text.txt", true, "22c74203bace3c2e950278c7ab08da0fca9f4e9b");
                AssertNormalization(repo, "huh.dunno", true, "22c74203bace3c2e950278c7ab08da0fca9f4e9b");
                AssertNormalization(repo, "binary.data", false, "66eeff1fcbacf589e6d70aa70edd3fce5be2b37c");
            }
        }

        private static void AssertNormalization(Repository repo, string filename, bool shouldHaveBeenNormalized, string expectedSha)
        {
            var sb = new StringBuilder();
            sb.Append("I'm going to be dynamically processed\r\n");
            sb.Append("And my line endings...\r\n");
            sb.Append("...are going to be\n");
            sb.Append("normalized!\r\n");

            File.WriteAllText(Path.Combine(repo.Info.WorkingDirectory, filename), sb.ToString());

            repo.Index.Stage(filename);

            IndexEntry entry = repo.Index[filename];
            Assert.NotNull(entry);

            Assert.Equal(expectedSha, entry.Id.Sha);

            var blob = repo.Lookup<Blob>(entry.Id);
            Assert.NotNull(blob);

            Assert.Equal(!shouldHaveBeenNormalized, blob.ContentAsUtf8().Contains("\r"));
        }

        private static void CreateAttributesFile(Repository repo)
        {
            const string relativePath = ".gitattributes";
            string fullFilePath = Path.Combine(repo.Info.WorkingDirectory, relativePath);

            var sb = new StringBuilder();
            sb.Append("* text=auto\n");
            sb.Append("*.txt text\n");
            sb.Append("*.data binary\n");

            File.WriteAllText(fullFilePath, sb.ToString());
        }
    }
}