PageRenderTime 46ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/GitSharp.Tests/GitSharp.Core/RepositoryConfigTest.cs

https://github.com/nestalk/GitSharp
C# | 285 lines | 206 code | 35 blank | 44 comment | 4 complexity | e6634a0ad7d9260713548839d200da87 MD5 | raw file
  1. /*
  2. * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
  3. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  5. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  6. *
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or
  10. * without modification, are permitted provided that the following
  11. * conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * - Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer in the documentation and/or other materials provided
  19. * with the distribution.
  20. *
  21. * - Neither the name of the Git Development Community nor the
  22. * names of its contributors may be used to endorse or promote
  23. * products derived from this software without specific prior
  24. * written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  27. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  28. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  31. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  37. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  38. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. */
  40. using System;
  41. using System.Collections.Generic;
  42. using System.Linq;
  43. using GitSharp.Core;
  44. using GitSharp.Core.Tests.Util;
  45. using NUnit.Framework;
  46. namespace GitSharp.Core.Tests
  47. {
  48. [TestFixture]
  49. public class RepositoryConfigTest
  50. {
  51. [Test]
  52. public void test001_ReadBareKey()
  53. {
  54. Core.Config c = parse("[foo]\nbar\n");
  55. Assert.AreEqual(true, c.getBoolean("foo", null, "bar", false));
  56. Assert.AreEqual(string.Empty, c.getString("foo", null, "bar"));
  57. }
  58. [Test]
  59. public void test002_ReadWithSubsection()
  60. {
  61. Core.Config c = parse("[foo \"zip\"]\nbar\n[foo \"zap\"]\nbar=false\nn=3\n");
  62. Assert.AreEqual(true, c.getBoolean("foo", "zip", "bar", false));
  63. Assert.AreEqual(string.Empty, c.getString("foo", "zip", "bar"));
  64. Assert.AreEqual(false, c.getBoolean("foo", "zap", "bar", true));
  65. Assert.AreEqual("false", c.getString("foo", "zap", "bar"));
  66. Assert.AreEqual(3, c.getInt("foo", "zap", "n", 4));
  67. Assert.AreEqual(4, c.getInt("foo", "zap", "m", 4));
  68. }
  69. [Test]
  70. public void test003_PutRemote()
  71. {
  72. Core.Config c = new Core.Config();
  73. c.setString("sec", "ext", "name", "value");
  74. c.setString("sec", "ext", "name2", "value2");
  75. string expText = "[sec \"ext\"]\n\tname = value\n\tname2 = value2\n";
  76. Assert.AreEqual(expText, c.toText());
  77. }
  78. [Test]
  79. public void test004_PutGetSimple()
  80. {
  81. Core.Config c = new Core.Config();
  82. c.setString("my", null, "somename", "false");
  83. Assert.AreEqual("false", c.getString("my", null, "somename"));
  84. Assert.AreEqual("[my]\n\tsomename = false\n", c.toText());
  85. }
  86. [Test]
  87. public void test005_PutGetStringList()
  88. {
  89. Core.Config c = new Core.Config();
  90. List<string> values = new List<string>();
  91. values.Add("value1");
  92. values.Add("value2");
  93. c.setStringList("my", null, "somename", values);
  94. object[] expArr = values.ToArray();
  95. string[] actArr = c.getStringList("my", null, "somename");
  96. Assert.IsTrue(expArr.SequenceEqual(actArr));
  97. string expText = "[my]\n\tsomename = value1\n\tsomename = value2\n";
  98. Assert.AreEqual(expText, c.toText());
  99. }
  100. [Test]
  101. public void test006_readCaseInsensitive()
  102. {
  103. Core.Config c = parse("[Foo]\nBar\n");
  104. Assert.AreEqual(true, c.getBoolean("foo", null, "bar", false));
  105. Assert.AreEqual(string.Empty, c.getString("foo", null, "bar"));
  106. }
  107. [Test]
  108. public void test007_readUserConfig()
  109. {
  110. MockSystemReader mockSystemReader = new MockSystemReader();
  111. SystemReader.setInstance(mockSystemReader);
  112. string hostname = mockSystemReader.getHostname();
  113. Core.Config userGitConfig = mockSystemReader.openUserConfig();
  114. Core.Config localConfig = new Core.Config(userGitConfig);
  115. mockSystemReader.clearProperties();
  116. string authorName;
  117. string authorEmail;
  118. // no values defined nowhere
  119. authorName = localConfig.get(UserConfig.KEY).getAuthorName();
  120. authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
  121. Assert.AreEqual(Constants.UNKNOWN_USER_DEFAULT, authorName);
  122. Assert.AreEqual(Constants.UNKNOWN_USER_DEFAULT + "@" + hostname, authorEmail);
  123. // the system user name is defined
  124. mockSystemReader.setProperty(Constants.OS_USER_NAME_KEY, "os user name");
  125. localConfig.uncache(UserConfig.KEY);
  126. authorName = localConfig.get(UserConfig.KEY).getAuthorName();
  127. Assert.AreEqual("os user name", authorName);
  128. if (hostname != null && hostname.Length != 0)
  129. {
  130. authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
  131. Assert.AreEqual("os user name@" + hostname, authorEmail);
  132. }
  133. // the git environment variables are defined
  134. mockSystemReader.setProperty(Constants.GIT_AUTHOR_NAME_KEY, "git author name");
  135. mockSystemReader.setProperty(Constants.GIT_AUTHOR_EMAIL_KEY, "author@email");
  136. localConfig.uncache(UserConfig.KEY);
  137. authorName = localConfig.get(UserConfig.KEY).getAuthorName();
  138. authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
  139. Assert.AreEqual("git author name", authorName);
  140. Assert.AreEqual("author@email", authorEmail);
  141. // the values are defined in the global configuration
  142. userGitConfig.setString("user", null, "name", "global username");
  143. userGitConfig.setString("user", null, "email", "author@globalemail");
  144. authorName = localConfig.get(UserConfig.KEY).getAuthorName();
  145. authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
  146. Assert.AreEqual("global username", authorName);
  147. Assert.AreEqual("author@globalemail", authorEmail);
  148. // the values are defined in the local configuration
  149. localConfig.setString("user", null, "name", "local username");
  150. localConfig.setString("user", null, "email", "author@localemail");
  151. authorName = localConfig.get(UserConfig.KEY).getAuthorName();
  152. authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
  153. Assert.AreEqual("local username", authorName);
  154. Assert.AreEqual("author@localemail", authorEmail);
  155. authorName = localConfig.get(UserConfig.KEY).getCommitterName();
  156. authorEmail = localConfig.get(UserConfig.KEY).getCommitterEmail();
  157. Assert.AreEqual("local username", authorName);
  158. Assert.AreEqual("author@localemail", authorEmail);
  159. }
  160. [Test]
  161. public void testReadBoolean_TrueFalse1()
  162. {
  163. Core.Config c = parse("[s]\na = true\nb = false\n");
  164. Assert.AreEqual("true", c.getString("s", null, "a"));
  165. Assert.AreEqual("false", c.getString("s", null, "b"));
  166. Assert.IsTrue(c.getBoolean("s", "a", false));
  167. Assert.IsFalse(c.getBoolean("s", "b", true));
  168. }
  169. [Test]
  170. public void testReadBoolean_TrueFalse2()
  171. {
  172. Core.Config c = parse("[s]\na = TrUe\nb = fAlSe\n");
  173. Assert.AreEqual("TrUe", c.getString("s", null, "a"));
  174. Assert.AreEqual("fAlSe", c.getString("s", null, "b"));
  175. Assert.IsTrue(c.getBoolean("s", "a", false));
  176. Assert.IsFalse(c.getBoolean("s", "b", true));
  177. }
  178. [Test]
  179. public void testReadBoolean_YesNo1()
  180. {
  181. Core.Config c = parse("[s]\na = yes\nb = no\n");
  182. Assert.AreEqual("yes", c.getString("s", null, "a"));
  183. Assert.AreEqual("no", c.getString("s", null, "b"));
  184. Assert.IsTrue(c.getBoolean("s", "a", false));
  185. Assert.IsFalse(c.getBoolean("s", "b", true));
  186. }
  187. [Test]
  188. public void testReadBoolean_YesNo2()
  189. {
  190. Core.Config c = parse("[s]\na = yEs\nb = NO\n");
  191. Assert.AreEqual("yEs", c.getString("s", null, "a"));
  192. Assert.AreEqual("NO", c.getString("s", null, "b"));
  193. Assert.IsTrue(c.getBoolean("s", "a", false));
  194. Assert.IsFalse(c.getBoolean("s", "b", true));
  195. }
  196. [Test]
  197. public void testReadBoolean_OnOff1()
  198. {
  199. Core.Config c = parse("[s]\na = on\nb = off\n");
  200. Assert.AreEqual("on", c.getString("s", null, "a"));
  201. Assert.AreEqual("off", c.getString("s", null, "b"));
  202. Assert.IsTrue(c.getBoolean("s", "a", false));
  203. Assert.IsFalse(c.getBoolean("s", "b", true));
  204. }
  205. [Test]
  206. public void testReadBoolean_OnOff2()
  207. {
  208. Core.Config c = parse("[s]\na = ON\nb = OFF\n");
  209. Assert.AreEqual("ON", c.getString("s", null, "a"));
  210. Assert.AreEqual("OFF", c.getString("s", null, "b"));
  211. Assert.IsTrue(c.getBoolean("s", "a", false));
  212. Assert.IsFalse(c.getBoolean("s", "b", true));
  213. }
  214. [Test]
  215. public void testReadLong()
  216. {
  217. assertReadLong(1L);
  218. assertReadLong(-1L);
  219. assertReadLong(long.MinValue);
  220. assertReadLong(long.MaxValue);
  221. assertReadLong(4L * 1024 * 1024 * 1024, "4g");
  222. assertReadLong(3L * 1024 * 1024, "3 m");
  223. assertReadLong(8L * 1024, "8 k");
  224. try
  225. {
  226. assertReadLong(-1, "1.5g");
  227. Assert.Fail("incorrectly accepted 1.5g");
  228. }
  229. catch (ArgumentException e)
  230. {
  231. Assert.AreEqual("Invalid long value: s.a=1.5g", e.Message);
  232. }
  233. }
  234. private void assertReadLong(long exp)
  235. {
  236. assertReadLong(exp, exp.ToString());
  237. }
  238. private void assertReadLong(long exp, string act)
  239. {
  240. Core.Config c = parse("[s]\na = " + act + "\n");
  241. Assert.AreEqual(exp, c.getLong("s", null, "a", 0L));
  242. }
  243. private Core.Config parse(string content)
  244. {
  245. Core.Config c = new Core.Config(null);
  246. c.fromText(content);
  247. return c;
  248. }
  249. }
  250. }