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

ResetFixture.cs « LibGit2Sharp.Tests - github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cfe0791dca720265d435c28340f525a517a01ca7 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.IO;
using LibGit2Sharp.Tests.TestHelpers;
using NUnit.Framework;

namespace LibGit2Sharp.Tests
{
    [TestFixture]
    public class ResetFixture : BaseFixture
    {
        [TestCase(true)]
        [TestCase(false)]
        public void ResetANewlyInitializedRepositoryThrows(bool isBare)
        {
            SelfCleaningDirectory scd = BuildSelfCleaningDirectory();

            using (var repo = Repository.Init(scd.DirectoryPath, isBare))
            {
                Assert.Throws<LibGit2Exception>(() => repo.Reset(ResetOptions.Soft, repo.Head.CanonicalName));
            }
        }

        [Test]
        public void SoftResetToTheHeadOfARepositoryDoesNotChangeTheTargetOfTheHead()
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                Branch oldHead = repo.Head;

                repo.Reset(ResetOptions.Soft, oldHead.CanonicalName);

                repo.Head.ShouldEqual(oldHead);
            }
        }

        [Test]
        public void SoftResetSetsTheHeadToTheDereferencedCommitOfAChainedTag()
        {
            TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();

            using (var repo = new Repository(path.RepositoryPath))
            {
                Tag tag = repo.Tags["test"];
                repo.Reset(ResetOptions.Soft, tag.CanonicalName);
                repo.Head.Tip.Sha.ShouldEqual("e90810b8df3e80c413d903f631643c716887138d");
            }
        }

        [Test]
        public void ResettingWithBadParamsThrows()
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                Assert.Throws<ArgumentNullException>(() => repo.Reset(ResetOptions.Soft, null));
                Assert.Throws<ArgumentException>(() => repo.Reset(ResetOptions.Soft, ""));
                Assert.Throws<LibGit2Exception>(() => repo.Reset(ResetOptions.Soft, Constants.UnknownSha));
            }
        }

        [Test]
        public void SoftResetSetsTheHeadToTheSpecifiedCommit()
        {
            /* Make the Head point to a branch through its name */
            AssertSoftReset(b => b.Name, false, b => b.Name);
        }

        [Test]
        public void SoftResetSetsTheDetachedHeadToTheSpecifiedCommit()
        {
            /* Make the Head point to a commit through its sha (Detaches the Head) */
            AssertSoftReset(b => b.Tip.Sha, true, b => "(no branch)");
        }

        private void AssertSoftReset(Func<Branch, string> branchIdentifierRetriever, bool shouldHeadBeDetached, Func<Branch, string> expectedHeadNameRetriever)
        {
            SelfCleaningDirectory scd = BuildSelfCleaningDirectory();

            using (var repo = Repository.Init(scd.DirectoryPath)) 
            {
                FeedTheRepository(repo);

                Tag tag = repo.Tags["mytag"];
                Branch branch = repo.Branches["mybranch"];

                string branchIdentifier = branchIdentifierRetriever(branch);
                repo.Branches.Checkout(branchIdentifier);
                repo.Info.IsHeadDetached.ShouldEqual(shouldHeadBeDetached);

                string expectedHeadName = expectedHeadNameRetriever(branch);
                repo.Head.Name.ShouldEqual(expectedHeadName);
                repo.Head.Tip.Sha.ShouldEqual(branch.Tip.Sha);

                /* Reset --soft the Head to a tag through its canonical name */
                repo.Reset(ResetOptions.Soft, tag.CanonicalName);
                repo.Head.Name.ShouldEqual(expectedHeadName);
                repo.Head.Tip.Id.ShouldEqual(tag.Target.Id);

                repo.Index.RetrieveStatus("a.txt").ShouldEqual(FileStatus.Staged);

                /* Reset --soft the Head to a commit through its sha */
                repo.Reset(ResetOptions.Soft, branch.Tip.Sha);
                repo.Head.Name.ShouldEqual(expectedHeadName);
                repo.Head.Tip.Sha.ShouldEqual(branch.Tip.Sha);

                repo.Index.RetrieveStatus("a.txt").ShouldEqual(FileStatus.Unaltered);
            }
        }

        private static void FeedTheRepository(Repository repo)
        {
            string fullPath = Path.Combine(repo.Info.WorkingDirectory, "a.txt");
            File.WriteAllText(fullPath, "Hello\n");
            repo.Index.Stage(fullPath);
            repo.Commit("Initial commit", Constants.Signature, Constants.Signature);
            repo.ApplyTag("mytag");

            File.AppendAllText(fullPath, "World\n");
            repo.Index.Stage(fullPath);

            Signature shiftedSignature = Constants.Signature.TimeShift(TimeSpan.FromMinutes(1));
            repo.Commit("Update file", shiftedSignature, shiftedSignature);
            repo.CreateBranch("mybranch");

            repo.Branches.Checkout("mybranch");

            repo.Index.RetrieveStatus().IsDirty.ShouldBeFalse();
        }

        [Test]
        public void MixedResetRefreshesTheIndex()
        {
            SelfCleaningDirectory scd = BuildSelfCleaningDirectory();

            using (var repo = Repository.Init(scd.DirectoryPath))
            {
                FeedTheRepository(repo);

                Tag tag = repo.Tags["mytag"];

                repo.Reset(ResetOptions.Mixed, tag.CanonicalName);

                repo.Index.RetrieveStatus("a.txt").ShouldEqual(FileStatus.Modified);
            }
        }

        [Test]
        public void MixedResetInABareRepositoryThrows()
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                Assert.Throws<LibGit2Exception>(() => repo.Reset(ResetOptions.Mixed, repo.Head.Tip.Sha));
            }
        }
    }
}