Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

InitCommandTest.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
  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.api;
  44. import static org.junit.Assert.assertNotNull;
  45. import static org.junit.Assert.assertNull;
  46. import static org.junit.Assert.assertTrue;
  47. import java.io.File;
  48. import java.io.IOException;
  49. import org.eclipse.jgit.api.errors.GitAPIException;
  50. import org.eclipse.jgit.api.errors.JGitInternalException;
  51. import org.eclipse.jgit.errors.NoWorkTreeException;
  52. import org.eclipse.jgit.junit.MockSystemReader;
  53. import org.eclipse.jgit.junit.RepositoryTestCase;
  54. import org.eclipse.jgit.lib.Constants;
  55. import org.eclipse.jgit.lib.Repository;
  56. import org.eclipse.jgit.util.SystemReader;
  57. import org.junit.Before;
  58. import org.junit.Test;
  59. public class InitCommandTest extends RepositoryTestCase {
  60. @Override
  61. @Before
  62. public void setUp() throws Exception {
  63. super.setUp();
  64. }
  65. @Test
  66. public void testInitRepository() throws IOException, JGitInternalException,
  67. GitAPIException {
  68. File directory = createTempDirectory("testInitRepository");
  69. InitCommand command = new InitCommand();
  70. command.setDirectory(directory);
  71. Repository repository = command.call().getRepository();
  72. addRepoToClose(repository);
  73. assertNotNull(repository);
  74. }
  75. @Test
  76. public void testInitNonEmptyRepository() throws IOException,
  77. JGitInternalException, GitAPIException {
  78. File directory = createTempDirectory("testInitRepository2");
  79. File someFile = new File(directory, "someFile");
  80. someFile.createNewFile();
  81. assertTrue(someFile.exists());
  82. assertTrue(directory.listFiles().length > 0);
  83. InitCommand command = new InitCommand();
  84. command.setDirectory(directory);
  85. Repository repository = command.call().getRepository();
  86. addRepoToClose(repository);
  87. assertNotNull(repository);
  88. }
  89. @Test
  90. public void testInitBareRepository() throws IOException,
  91. JGitInternalException, GitAPIException {
  92. File directory = createTempDirectory("testInitBareRepository");
  93. InitCommand command = new InitCommand();
  94. command.setDirectory(directory);
  95. command.setBare(true);
  96. Repository repository = command.call().getRepository();
  97. addRepoToClose(repository);
  98. assertNotNull(repository);
  99. assertTrue(repository.isBare());
  100. }
  101. // non-bare repos where gitDir and directory is set. Same as
  102. // "git init --separate-git-dir /tmp/a /tmp/b"
  103. @Test
  104. public void testInitWithExplicitGitDir() throws IOException,
  105. JGitInternalException, GitAPIException {
  106. File wt = createTempDirectory("testInitRepositoryWT");
  107. File gitDir = createTempDirectory("testInitRepositoryGIT");
  108. InitCommand command = new InitCommand();
  109. command.setDirectory(wt);
  110. command.setGitDir(gitDir);
  111. Repository repository = command.call().getRepository();
  112. addRepoToClose(repository);
  113. assertNotNull(repository);
  114. assertEqualsFile(wt, repository.getWorkTree());
  115. assertEqualsFile(gitDir, repository.getDirectory());
  116. }
  117. // non-bare repos where only gitDir is set. Same as
  118. // "git init --separate-git-dir /tmp/a"
  119. @Test
  120. public void testInitWithOnlyExplicitGitDir() throws IOException,
  121. JGitInternalException, GitAPIException {
  122. MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
  123. reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
  124. .getAbsolutePath());
  125. File gitDir = createTempDirectory("testInitRepository/.git");
  126. InitCommand command = new InitCommand();
  127. command.setGitDir(gitDir);
  128. Repository repository = command.call().getRepository();
  129. addRepoToClose(repository);
  130. assertNotNull(repository);
  131. assertEqualsFile(gitDir, repository.getDirectory());
  132. assertEqualsFile(new File(reader.getProperty("user.dir")),
  133. repository.getWorkTree());
  134. }
  135. // Bare repos where gitDir and directory is set will only work if gitDir and
  136. // directory is pointing to same dir. Same as
  137. // "git init --bare --separate-git-dir /tmp/a /tmp/b"
  138. // (works in native git but I guess that's more a bug)
  139. @Test(expected = IllegalStateException.class)
  140. public void testInitBare_DirAndGitDirMustBeEqual() throws IOException,
  141. JGitInternalException, GitAPIException {
  142. File gitDir = createTempDirectory("testInitRepository.git");
  143. InitCommand command = new InitCommand();
  144. command.setBare(true);
  145. command.setDirectory(gitDir);
  146. command.setGitDir(new File(gitDir, ".."));
  147. command.call();
  148. }
  149. // If neither directory nor gitDir is set in a non-bare repo make sure
  150. // worktree and gitDir are set correctly. Standard case. Same as
  151. // "git init"
  152. @Test
  153. public void testInitWithDefaultsNonBare() throws JGitInternalException,
  154. GitAPIException, IOException {
  155. MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
  156. reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
  157. .getAbsolutePath());
  158. InitCommand command = new InitCommand();
  159. command.setBare(false);
  160. Repository repository = command.call().getRepository();
  161. addRepoToClose(repository);
  162. assertNotNull(repository);
  163. assertEqualsFile(new File(reader.getProperty("user.dir"), ".git"),
  164. repository.getDirectory());
  165. assertEqualsFile(new File(reader.getProperty("user.dir")),
  166. repository.getWorkTree());
  167. }
  168. // If neither directory nor gitDir is set in a bare repo make sure
  169. // worktree and gitDir are set correctly. Standard case. Same as
  170. // "git init --bare"
  171. @Test(expected = NoWorkTreeException.class)
  172. public void testInitWithDefaultsBare() throws JGitInternalException,
  173. GitAPIException, IOException {
  174. MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
  175. reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
  176. .getAbsolutePath());
  177. InitCommand command = new InitCommand();
  178. command.setBare(true);
  179. Repository repository = command.call().getRepository();
  180. addRepoToClose(repository);
  181. assertNotNull(repository);
  182. assertEqualsFile(new File(reader.getProperty("user.dir")),
  183. repository.getDirectory());
  184. assertNull(repository.getWorkTree());
  185. }
  186. // In a non-bare repo when directory and gitDir is set then they shouldn't
  187. // point to the same dir. Same as
  188. // "git init --separate-git-dir /tmp/a /tmp/a"
  189. // (works in native git but I guess that's more a bug)
  190. @Test(expected = IllegalStateException.class)
  191. public void testInitNonBare_GitdirAndDirShouldntBeSame()
  192. throws JGitInternalException, GitAPIException, IOException {
  193. File gitDir = createTempDirectory("testInitRepository.git");
  194. InitCommand command = new InitCommand();
  195. command.setBare(false);
  196. command.setGitDir(gitDir);
  197. command.setDirectory(gitDir);
  198. command.call().getRepository();
  199. }
  200. }