You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SubmoduleAddTest.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (C) 2011, GitHub Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.submodule;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertNotNull;
  46. import static org.junit.Assert.assertTrue;
  47. import static org.junit.Assert.fail;
  48. import java.io.File;
  49. import java.text.MessageFormat;
  50. import org.eclipse.jgit.api.Git;
  51. import org.eclipse.jgit.api.Status;
  52. import org.eclipse.jgit.api.SubmoduleAddCommand;
  53. import org.eclipse.jgit.api.errors.GitAPIException;
  54. import org.eclipse.jgit.api.errors.JGitInternalException;
  55. import org.eclipse.jgit.dircache.DirCache;
  56. import org.eclipse.jgit.dircache.DirCacheEditor;
  57. import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
  58. import org.eclipse.jgit.dircache.DirCacheEntry;
  59. import org.eclipse.jgit.internal.JGitText;
  60. import org.eclipse.jgit.lib.ConfigConstants;
  61. import org.eclipse.jgit.lib.Constants;
  62. import org.eclipse.jgit.lib.FileMode;
  63. import org.eclipse.jgit.lib.ObjectId;
  64. import org.eclipse.jgit.lib.Repository;
  65. import org.eclipse.jgit.lib.RepositoryTestCase;
  66. import org.eclipse.jgit.revwalk.RevCommit;
  67. import org.eclipse.jgit.storage.file.FileBasedConfig;
  68. import org.junit.Test;
  69. /**
  70. * Unit tests of {@link org.eclipse.jgit.api.SubmoduleAddCommand}
  71. */
  72. public class SubmoduleAddTest extends RepositoryTestCase {
  73. @Test
  74. public void commandWithNullPath() throws GitAPIException {
  75. try {
  76. new SubmoduleAddCommand(db).setURI("uri").call().close();
  77. fail("Exception not thrown");
  78. } catch (IllegalArgumentException e) {
  79. assertEquals(JGitText.get().pathNotConfigured, e.getMessage());
  80. }
  81. }
  82. @Test
  83. public void commandWithEmptyPath() throws GitAPIException {
  84. try {
  85. new SubmoduleAddCommand(db).setPath("").setURI("uri").call()
  86. .close();
  87. fail("Exception not thrown");
  88. } catch (IllegalArgumentException e) {
  89. assertEquals(JGitText.get().pathNotConfigured, e.getMessage());
  90. }
  91. }
  92. @Test
  93. public void commandWithNullUri() throws GitAPIException {
  94. try {
  95. new SubmoduleAddCommand(db).setPath("sub").call().close();
  96. fail("Exception not thrown");
  97. } catch (IllegalArgumentException e) {
  98. assertEquals(JGitText.get().uriNotConfigured, e.getMessage());
  99. }
  100. }
  101. @Test
  102. public void commandWithEmptyUri() throws GitAPIException {
  103. try {
  104. new SubmoduleAddCommand(db).setPath("sub").setURI("").call()
  105. .close();
  106. fail("Exception not thrown");
  107. } catch (IllegalArgumentException e) {
  108. assertEquals(JGitText.get().uriNotConfigured, e.getMessage());
  109. }
  110. }
  111. @Test
  112. public void addSubmodule() throws Exception {
  113. Git git = new Git(db);
  114. writeTrashFile("file.txt", "content");
  115. git.add().addFilepattern("file.txt").call();
  116. RevCommit commit = git.commit().setMessage("create file").call();
  117. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  118. String path = "sub";
  119. command.setPath(path);
  120. String uri = db.getDirectory().toURI().toString();
  121. command.setURI(uri);
  122. Repository repo = command.call();
  123. assertNotNull(repo);
  124. addRepoToClose(repo);
  125. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  126. assertTrue(generator.next());
  127. assertEquals(path, generator.getPath());
  128. assertEquals(commit, generator.getObjectId());
  129. assertEquals(uri, generator.getModulesUrl());
  130. assertEquals(path, generator.getModulesPath());
  131. assertEquals(uri, generator.getConfigUrl());
  132. Repository subModRepo = generator.getRepository();
  133. addRepoToClose(subModRepo);
  134. assertNotNull(subModRepo);
  135. assertEquals(commit, repo.resolve(Constants.HEAD));
  136. Status status = Git.wrap(db).status().call();
  137. assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES));
  138. assertTrue(status.getAdded().contains(path));
  139. }
  140. @Test
  141. public void addExistentSubmodule() throws Exception {
  142. final ObjectId id = ObjectId
  143. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  144. final String path = "sub";
  145. DirCache cache = db.lockDirCache();
  146. DirCacheEditor editor = cache.editor();
  147. editor.add(new PathEdit(path) {
  148. public void apply(DirCacheEntry ent) {
  149. ent.setFileMode(FileMode.GITLINK);
  150. ent.setObjectId(id);
  151. }
  152. });
  153. editor.commit();
  154. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  155. command.setPath(path);
  156. command.setURI("git://server/repo.git");
  157. try {
  158. command.call().close();
  159. fail("Exception not thrown");
  160. } catch (JGitInternalException e) {
  161. assertEquals(
  162. MessageFormat.format(JGitText.get().submoduleExists, path),
  163. e.getMessage());
  164. }
  165. }
  166. @Test
  167. public void addSubmoduleWithRelativeUri() throws Exception {
  168. Git git = new Git(db);
  169. writeTrashFile("file.txt", "content");
  170. git.add().addFilepattern("file.txt").call();
  171. RevCommit commit = git.commit().setMessage("create file").call();
  172. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  173. String path = "sub";
  174. String uri = "./.git";
  175. command.setPath(path);
  176. command.setURI(uri);
  177. Repository repo = command.call();
  178. assertNotNull(repo);
  179. addRepoToClose(repo);
  180. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  181. assertTrue(generator.next());
  182. assertEquals(path, generator.getPath());
  183. assertEquals(commit, generator.getObjectId());
  184. assertEquals(uri, generator.getModulesUrl());
  185. assertEquals(path, generator.getModulesPath());
  186. String fullUri = db.getDirectory().getAbsolutePath();
  187. if (File.separatorChar == '\\')
  188. fullUri = fullUri.replace('\\', '/');
  189. assertEquals(fullUri, generator.getConfigUrl());
  190. Repository subModRepo = generator.getRepository();
  191. addRepoToClose(subModRepo);
  192. assertNotNull(subModRepo);
  193. assertEquals(
  194. fullUri,
  195. subModRepo
  196. .getConfig()
  197. .getString(ConfigConstants.CONFIG_REMOTE_SECTION,
  198. Constants.DEFAULT_REMOTE_NAME,
  199. ConfigConstants.CONFIG_KEY_URL));
  200. assertEquals(commit, repo.resolve(Constants.HEAD));
  201. Status status = Git.wrap(db).status().call();
  202. assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES));
  203. assertTrue(status.getAdded().contains(path));
  204. }
  205. @Test
  206. public void addSubmoduleWithExistingSubmoduleDefined() throws Exception {
  207. String path1 = "sub1";
  208. String url1 = "git://server/repo1.git";
  209. String path2 = "sub2";
  210. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  211. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  212. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  213. path1, ConfigConstants.CONFIG_KEY_PATH, path1);
  214. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  215. path1, ConfigConstants.CONFIG_KEY_URL, url1);
  216. modulesConfig.save();
  217. Git git = new Git(db);
  218. writeTrashFile("file.txt", "content");
  219. git.add().addFilepattern("file.txt").call();
  220. assertNotNull(git.commit().setMessage("create file").call());
  221. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  222. command.setPath(path2);
  223. String url2 = db.getDirectory().toURI().toString();
  224. command.setURI(url2);
  225. Repository r = command.call();
  226. assertNotNull(r);
  227. addRepoToClose(r);
  228. modulesConfig.load();
  229. assertEquals(path1, modulesConfig.getString(
  230. ConfigConstants.CONFIG_SUBMODULE_SECTION, path1,
  231. ConfigConstants.CONFIG_KEY_PATH));
  232. assertEquals(url1, modulesConfig.getString(
  233. ConfigConstants.CONFIG_SUBMODULE_SECTION, path1,
  234. ConfigConstants.CONFIG_KEY_URL));
  235. assertEquals(path2, modulesConfig.getString(
  236. ConfigConstants.CONFIG_SUBMODULE_SECTION, path2,
  237. ConfigConstants.CONFIG_KEY_PATH));
  238. assertEquals(url2, modulesConfig.getString(
  239. ConfigConstants.CONFIG_SUBMODULE_SECTION, path2,
  240. ConfigConstants.CONFIG_KEY_URL));
  241. }
  242. }