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

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