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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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.junit.RepositoryTestCase;
  61. import org.eclipse.jgit.lib.ConfigConstants;
  62. import org.eclipse.jgit.lib.Constants;
  63. import org.eclipse.jgit.lib.FileMode;
  64. import org.eclipse.jgit.lib.ObjectId;
  65. import org.eclipse.jgit.lib.Repository;
  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. try (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. ObjectId subCommit = repo.resolve(Constants.HEAD);
  125. repo.close();
  126. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  127. assertTrue(generator.next());
  128. assertEquals(path, generator.getPath());
  129. assertEquals(commit, generator.getObjectId());
  130. assertEquals(uri, generator.getModulesUrl());
  131. assertEquals(path, generator.getModulesPath());
  132. assertEquals(uri, generator.getConfigUrl());
  133. Repository subModRepo = generator.getRepository();
  134. assertNotNull(subModRepo);
  135. assertEquals(subCommit, commit);
  136. subModRepo.close();
  137. Status status = Git.wrap(db).status().call();
  138. assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES));
  139. assertTrue(status.getAdded().contains(path));
  140. }
  141. }
  142. @Test
  143. public void addExistentSubmodule() throws Exception {
  144. final ObjectId id = ObjectId
  145. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  146. final String path = "sub";
  147. DirCache cache = db.lockDirCache();
  148. DirCacheEditor editor = cache.editor();
  149. editor.add(new PathEdit(path) {
  150. @Override
  151. public void apply(DirCacheEntry ent) {
  152. ent.setFileMode(FileMode.GITLINK);
  153. ent.setObjectId(id);
  154. }
  155. });
  156. editor.commit();
  157. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  158. command.setPath(path);
  159. command.setURI("git://server/repo.git");
  160. try {
  161. command.call().close();
  162. fail("Exception not thrown");
  163. } catch (JGitInternalException e) {
  164. assertEquals(
  165. MessageFormat.format(JGitText.get().submoduleExists, path),
  166. e.getMessage());
  167. }
  168. }
  169. @Test
  170. public void addSubmoduleWithInvalidPath() throws Exception {
  171. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  172. command.setPath("-invalid-path");
  173. // TODO(ms) set name to a valid value in 5.1.0 and adapt expected
  174. // message below
  175. command.setURI("http://example.com/repo/x.git");
  176. try {
  177. command.call().close();
  178. fail("Exception not thrown");
  179. } catch (IllegalArgumentException e) {
  180. // TODO(ms) should check for submodule path, but can't set name
  181. // before 5.1.0
  182. assertEquals("Invalid submodule name '-invalid-path'",
  183. e.getMessage());
  184. }
  185. }
  186. @Test
  187. public void addSubmoduleWithInvalidUri() throws Exception {
  188. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  189. command.setPath("valid-path");
  190. command.setURI("-upstream");
  191. try {
  192. command.call().close();
  193. fail("Exception not thrown");
  194. } catch (IllegalArgumentException e) {
  195. assertEquals("Invalid submodule URL '-upstream'", e.getMessage());
  196. }
  197. }
  198. @Test
  199. public void addSubmoduleWithRelativeUri() throws Exception {
  200. try (Git git = new Git(db)) {
  201. writeTrashFile("file.txt", "content");
  202. git.add().addFilepattern("file.txt").call();
  203. RevCommit commit = git.commit().setMessage("create file").call();
  204. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  205. String path = "sub";
  206. String uri = "./.git";
  207. command.setPath(path);
  208. command.setURI(uri);
  209. Repository repo = command.call();
  210. assertNotNull(repo);
  211. addRepoToClose(repo);
  212. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  213. assertTrue(generator.next());
  214. assertEquals(path, generator.getPath());
  215. assertEquals(commit, generator.getObjectId());
  216. assertEquals(uri, generator.getModulesUrl());
  217. assertEquals(path, generator.getModulesPath());
  218. String fullUri = db.getDirectory().getAbsolutePath();
  219. if (File.separatorChar == '\\') {
  220. fullUri = fullUri.replace('\\', '/');
  221. }
  222. assertEquals(fullUri, generator.getConfigUrl());
  223. Repository subModRepo = generator.getRepository();
  224. assertNotNull(subModRepo);
  225. assertEquals(
  226. fullUri,
  227. subModRepo
  228. .getConfig()
  229. .getString(ConfigConstants.CONFIG_REMOTE_SECTION,
  230. Constants.DEFAULT_REMOTE_NAME,
  231. ConfigConstants.CONFIG_KEY_URL));
  232. subModRepo.close();
  233. assertEquals(commit, repo.resolve(Constants.HEAD));
  234. Status status = Git.wrap(db).status().call();
  235. assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES));
  236. assertTrue(status.getAdded().contains(path));
  237. }
  238. }
  239. @Test
  240. public void addSubmoduleWithExistingSubmoduleDefined() throws Exception {
  241. String path1 = "sub1";
  242. String url1 = "git://server/repo1.git";
  243. String path2 = "sub2";
  244. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  245. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  246. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  247. path1, ConfigConstants.CONFIG_KEY_PATH, path1);
  248. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  249. path1, ConfigConstants.CONFIG_KEY_URL, url1);
  250. modulesConfig.save();
  251. try (Git git = new Git(db)) {
  252. writeTrashFile("file.txt", "content");
  253. git.add().addFilepattern("file.txt").call();
  254. assertNotNull(git.commit().setMessage("create file").call());
  255. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  256. command.setPath(path2);
  257. String url2 = db.getDirectory().toURI().toString();
  258. command.setURI(url2);
  259. Repository r = command.call();
  260. assertNotNull(r);
  261. addRepoToClose(r);
  262. modulesConfig.load();
  263. assertEquals(path1, modulesConfig.getString(
  264. ConfigConstants.CONFIG_SUBMODULE_SECTION, path1,
  265. ConfigConstants.CONFIG_KEY_PATH));
  266. assertEquals(url1, modulesConfig.getString(
  267. ConfigConstants.CONFIG_SUBMODULE_SECTION, path1,
  268. ConfigConstants.CONFIG_KEY_URL));
  269. assertEquals(path2, modulesConfig.getString(
  270. ConfigConstants.CONFIG_SUBMODULE_SECTION, path2,
  271. ConfigConstants.CONFIG_KEY_PATH));
  272. assertEquals(url2, modulesConfig.getString(
  273. ConfigConstants.CONFIG_SUBMODULE_SECTION, path2,
  274. ConfigConstants.CONFIG_KEY_URL));
  275. }
  276. }
  277. }