PageRenderTime 44ms CodeModel.GetById 11ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

/Mercurial.Net.Tests/CommitTests.cs

#
C# | 233 lines | 214 code | 18 blank | 1 comment | 0 complexity | 5393732850e4abf947d9c31d04261ee9 MD5 | raw file
  1using System;
  2using System.Collections.ObjectModel;
  3using System.IO;
  4using System.Linq;
  5using NUnit.Framework;
  6
  7namespace Mercurial.Tests
  8{
  9    [TestFixture]
 10    public class CommitTests : SingleRepositoryTestsBase
 11    {
 12        [TestCase((string)null)]
 13        [TestCase("")]
 14        [TestCase(" \t \n \r")]
 15        [Test]
 16        [Category("Integration")]
 17        public void CommitWithMessage_NullOrEmptyMessage_ThrowsArgumentNullException(string testCase)
 18        {
 19            Repo.Init();
 20            File.WriteAllText(Path.Combine(Repo.Path, "test1.txt"), "dummy content");
 21            Assert.Throws<ArgumentNullException>(
 22                () => Repo.Commit(
 23                    testCase, new CommitCommand
 24                    {
 25                        AddRemove = true,
 26                    }));
 27        }
 28
 29        [TestCase((string)null)]
 30        [TestCase("")]
 31        [TestCase(" \t \n \r")]
 32        [Test]
 33        [Category("Integration")]
 34        public void CommitWithMessageInOptions_NullOrEmptyMessage_ThrowsArgumentNullException(string testCase)
 35        {
 36            Repo.Init();
 37            File.WriteAllText(Path.Combine(Repo.Path, "test1.txt"), "dummy content");
 38            Assert.Throws<ArgumentNullException>(
 39                () => Repo.Commit(
 40                    new CommitCommand
 41                    {
 42                        Message = testCase,
 43                        AddRemove = true,
 44                    }));
 45        }
 46
 47        [Test]
 48        [Category("Integration")]
 49        public void CommitWithMessageInOptions_WithChanges_CommitsAChangeset()
 50        {
 51            Repo.Init();
 52            File.WriteAllText(Path.Combine(Repo.Path, "test1.txt"), "dummy content");
 53            Repo.Commit(
 54                new CommitCommand
 55                {
 56                    Message = "dummy",
 57                    AddRemove = true,
 58                });
 59            Assert.That(Repo.Log().Count(), Is.EqualTo(1));
 60        }
 61
 62        [Test]
 63        [Category("Integration")]
 64        public void Commit_ListingFile_OnlyCommitsThatFile()
 65        {
 66            Repo.Init();
 67            File.WriteAllText(Path.Combine(Repo.Path, "test1.txt"), "dummy");
 68            File.WriteAllText(Path.Combine(Repo.Path, "test2.txt"), "dummy");
 69            Repo.AddRemove();
 70
 71            Repo.Commit(new CommitCommand().WithMessage("dummy").WithPath("test1.txt"));
 72
 73            FileStatus[] status = Repo.Status().ToArray();
 74
 75            Assert.That(status.Length, Is.EqualTo(1));
 76            Assert.That(status[0].Path, Is.EqualTo("test2.txt"));
 77        }
 78
 79        [Test]
 80        [Category("Integration")]
 81        public void Commit_NoChanges_DoesNotAddAChangeset()
 82        {
 83            Repo.Init();
 84            try
 85            {
 86                Repo.Commit("dummy");
 87            }
 88            catch (MercurialExecutionException)
 89            {
 90                // Swallow this one
 91            }
 92            CollectionAssert.IsEmpty(Repo.Log());
 93        }
 94
 95        [Test]
 96        [Category("Integration")]
 97        public void Commit_NoChanges_ThrowsMercurialExecutionException()
 98        {
 99            Repo.Init();
100            Assert.Throws<MercurialExecutionException>(() => Repo.Commit("dummy"));
101        }
102
103        [Test]
104        [Category("Integration")]
105        public void Commit_NoRepository_ThrowsMercurialExecutionException()
106        {
107            Assert.Throws<MercurialExecutionException>(() => Repo.Commit("dummy"));
108        }
109
110        [Test]
111        [Category("API")]
112        public void Commit_NullOptions_ThrowsArgumentNullException()
113        {
114            Repo.Init();
115            File.WriteAllText(Path.Combine(Repo.Path, "test1.txt"), "dummy content");
116            Assert.Throws<ArgumentNullException>(() => Repo.Commit(null));
117        }
118
119        [Test]
120        [Category("Integration")]
121        public void Commit_WithChanges_CommitsAChangeset()
122        {
123            Repo.Init();
124            File.WriteAllText(Path.Combine(Repo.Path, "test1.txt"), "dummy content");
125            Repo.Commit(
126                "dummy", new CommitCommand
127                {
128                    AddRemove = true,
129                });
130            Assert.That(Repo.Log().Count(), Is.EqualTo(1));
131        }
132
133        [Test]
134        [Category("Integration")]
135        public void Commit_WithOneExcludePattern_DoesNotCommitThatFile()
136        {
137            Repo.Init();
138            File.WriteAllText(Path.Combine(Repo.Path, "test1.txt"), "dummy content");
139            File.WriteAllText(Path.Combine(Repo.Path, "test2.txt"), "dummy content");
140            Repo.Commit(
141                "dummy", new CommitCommand
142                {
143                    AddRemove = true,
144                    OverrideAuthor = "Dummy User <dummy@company.com>",
145                    ExcludePatterns =
146                        {
147                            "test1.txt",
148                        },
149                });
150            Collection<ChangesetPathAction> filesCommitted = Repo.Log(
151                new LogCommand
152                {
153                    IncludePathActions = true,
154                }).First().PathActions;
155
156            CollectionAssert.AreEqual(
157                filesCommitted, new[]
158                {
159                    new ChangesetPathAction
160                    {
161                        Action = ChangesetPathActionType.Add,
162                        Path = "test2.txt"
163                    },
164                });
165        }
166
167        [Test]
168        [Category("Integration")]
169        public void Commit_WithOneIncludePattern_CommitsJustThatFile()
170        {
171            Repo.Init();
172            File.WriteAllText(Path.Combine(Repo.Path, "test1.txt"), "dummy content");
173            File.WriteAllText(Path.Combine(Repo.Path, "test2.txt"), "dummy content");
174            Repo.Commit(
175                "dummy", new CommitCommand
176                {
177                    AddRemove = true,
178                    OverrideAuthor = "Dummy User <dummy@company.com>",
179                    IncludePatterns =
180                        {
181                            "test1.txt",
182                        },
183                });
184            Collection<ChangesetPathAction> filesCommitted = Repo.Log(
185                new LogCommand
186                {
187                    IncludePathActions = true,
188                }).First().PathActions;
189
190            CollectionAssert.AreEqual(
191                filesCommitted, new[]
192                {
193                    new ChangesetPathAction
194                    {
195                        Action = ChangesetPathActionType.Add,
196                        Path = "test1.txt"
197                    },
198                });
199        }
200
201        [Test]
202        [Category("Integration")]
203        public void Commit_WithOverriddenTimestamp_CommitsWithThatTimestamp()
204        {
205            Repo.Init();
206            File.WriteAllText(Path.Combine(Repo.Path, "test1.txt"), "dummy content");
207            var timestamp = new DateTime(2010, 1, 17, 18, 23, 59);
208            Repo.Commit(
209                "dummy", new CommitCommand
210                {
211                    AddRemove = true,
212                    OverrideTimestamp = timestamp,
213                });
214            Assert.That(Repo.Log().First().Timestamp, Is.EqualTo(timestamp));
215        }
216
217        [Test]
218        [Category("Integration")]
219        public void Commit_WithOverriddenUsername_CommitsWithThatUsername()
220        {
221            Repo.Init();
222            File.WriteAllText(Path.Combine(Repo.Path, "test1.txt"), "dummy content");
223            Repo.Commit(
224                "dummy", new CommitCommand
225                {
226                    AddRemove = true,
227                    OverrideAuthor = "Dummy User <dummy@company.com>",
228                });
229            Assert.That(Repo.Log().First().AuthorName, Is.EqualTo("Dummy User"));
230            Assert.That(Repo.Log().First().AuthorEmailAddress, Is.EqualTo("dummy@company.com"));
231        }
232    }
233}