PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Mercurial.Net.Tests/CommitTests.cs

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