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.

RepositorySetupWorkDirTest.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
  3. *
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.internal.storage.file;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assert.fail;
  49. import java.io.File;
  50. import java.io.IOException;
  51. import org.eclipse.jgit.errors.ConfigInvalidException;
  52. import org.eclipse.jgit.errors.NoWorkTreeException;
  53. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  54. import org.eclipse.jgit.lib.ConfigConstants;
  55. import org.eclipse.jgit.lib.Constants;
  56. import org.eclipse.jgit.lib.Repository;
  57. import org.eclipse.jgit.storage.file.FileBasedConfig;
  58. import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
  59. import org.eclipse.jgit.util.FS;
  60. import org.eclipse.jgit.util.FileUtils;
  61. import org.junit.Test;
  62. /**
  63. * Tests for setting up the working directory when creating a Repository
  64. */
  65. public class RepositorySetupWorkDirTest extends LocalDiskRepositoryTestCase {
  66. @Test
  67. public void testIsBare_CreateRepositoryFromArbitraryGitDir()
  68. throws Exception {
  69. File gitDir = getFile("workdir");
  70. assertTrue(new FileRepository(gitDir).isBare());
  71. }
  72. @Test
  73. public void testNotBare_CreateRepositoryFromDotGitGitDir() throws Exception {
  74. File gitDir = getFile("workdir", Constants.DOT_GIT);
  75. Repository repo = new FileRepository(gitDir);
  76. assertFalse(repo.isBare());
  77. assertWorkdirPath(repo, "workdir");
  78. assertGitdirPath(repo, "workdir", Constants.DOT_GIT);
  79. }
  80. @Test
  81. public void testWorkdirIsParentDir_CreateRepositoryFromDotGitGitDir()
  82. throws Exception {
  83. File gitDir = getFile("workdir", Constants.DOT_GIT);
  84. Repository repo = new FileRepository(gitDir);
  85. String workdir = repo.getWorkTree().getName();
  86. assertEquals(workdir, "workdir");
  87. }
  88. @Test
  89. public void testNotBare_CreateRepositoryFromWorkDirOnly() throws Exception {
  90. File workdir = getFile("workdir", "repo");
  91. Repository repo = new FileRepositoryBuilder().setWorkTree(workdir)
  92. .build();
  93. assertFalse(repo.isBare());
  94. assertWorkdirPath(repo, "workdir", "repo");
  95. assertGitdirPath(repo, "workdir", "repo", Constants.DOT_GIT);
  96. }
  97. @Test
  98. public void testWorkdirIsDotGit_CreateRepositoryFromWorkDirOnly()
  99. throws Exception {
  100. File workdir = getFile("workdir", "repo");
  101. Repository repo = new FileRepositoryBuilder().setWorkTree(workdir)
  102. .build();
  103. assertGitdirPath(repo, "workdir", "repo", Constants.DOT_GIT);
  104. }
  105. @Test
  106. public void testNotBare_CreateRepositoryFromGitDirOnlyWithWorktreeConfig()
  107. throws Exception {
  108. File gitDir = getFile("workdir", "repoWithConfig");
  109. File workTree = getFile("workdir", "treeRoot");
  110. setWorkTree(gitDir, workTree);
  111. Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
  112. assertFalse(repo.isBare());
  113. assertWorkdirPath(repo, "workdir", "treeRoot");
  114. assertGitdirPath(repo, "workdir", "repoWithConfig");
  115. }
  116. @Test
  117. public void testBare_CreateRepositoryFromGitDirOnlyWithBareConfigTrue()
  118. throws Exception {
  119. File gitDir = getFile("workdir", "repoWithConfig");
  120. setBare(gitDir, true);
  121. Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
  122. assertTrue(repo.isBare());
  123. }
  124. @Test
  125. public void testWorkdirIsParent_CreateRepositoryFromGitDirOnlyWithBareConfigFalse()
  126. throws Exception {
  127. File gitDir = getFile("workdir", "repoWithBareConfigTrue", "child");
  128. setBare(gitDir, false);
  129. Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
  130. assertWorkdirPath(repo, "workdir", "repoWithBareConfigTrue");
  131. }
  132. @Test
  133. public void testNotBare_CreateRepositoryFromGitDirOnlyWithBareConfigFalse()
  134. throws Exception {
  135. File gitDir = getFile("workdir", "repoWithBareConfigFalse", "child");
  136. setBare(gitDir, false);
  137. Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
  138. assertFalse(repo.isBare());
  139. assertWorkdirPath(repo, "workdir", "repoWithBareConfigFalse");
  140. assertGitdirPath(repo, "workdir", "repoWithBareConfigFalse", "child");
  141. }
  142. @Test
  143. public void testExceptionThrown_BareRepoGetWorkDir() throws Exception {
  144. File gitDir = getFile("workdir");
  145. try {
  146. new FileRepository(gitDir).getWorkTree();
  147. fail("Expected NoWorkTreeException missing");
  148. } catch (NoWorkTreeException e) {
  149. // expected
  150. }
  151. }
  152. @Test
  153. public void testExceptionThrown_BareRepoGetIndex() throws Exception {
  154. File gitDir = getFile("workdir");
  155. try {
  156. new FileRepository(gitDir).readDirCache();
  157. fail("Expected NoWorkTreeException missing");
  158. } catch (NoWorkTreeException e) {
  159. // expected
  160. }
  161. }
  162. @Test
  163. public void testExceptionThrown_BareRepoGetIndexFile() throws Exception {
  164. File gitDir = getFile("workdir");
  165. try {
  166. new FileRepository(gitDir).getIndexFile();
  167. fail("Expected NoWorkTreeException missing");
  168. } catch (NoWorkTreeException e) {
  169. // expected
  170. }
  171. }
  172. private File getFile(String... pathComponents) throws IOException {
  173. File dir = getTemporaryDirectory();
  174. for (String pathComponent : pathComponents)
  175. dir = new File(dir, pathComponent);
  176. FileUtils.mkdirs(dir, true);
  177. return dir;
  178. }
  179. private void setBare(File gitDir, boolean bare) throws IOException,
  180. ConfigInvalidException {
  181. FileBasedConfig cfg = configFor(gitDir);
  182. cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  183. ConfigConstants.CONFIG_KEY_BARE, bare);
  184. cfg.save();
  185. }
  186. private void setWorkTree(File gitDir, File workTree)
  187. throws IOException,
  188. ConfigInvalidException {
  189. String path = workTree.getAbsolutePath();
  190. FileBasedConfig cfg = configFor(gitDir);
  191. cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
  192. ConfigConstants.CONFIG_KEY_WORKTREE, path);
  193. cfg.save();
  194. }
  195. private FileBasedConfig configFor(File gitDir) throws IOException,
  196. ConfigInvalidException {
  197. File configPath = new File(gitDir, Constants.CONFIG);
  198. FileBasedConfig cfg = new FileBasedConfig(configPath, FS.DETECTED);
  199. cfg.load();
  200. return cfg;
  201. }
  202. private void assertGitdirPath(Repository repo, String... expected)
  203. throws IOException {
  204. File exp = getFile(expected).getCanonicalFile();
  205. File act = repo.getDirectory().getCanonicalFile();
  206. assertEquals("Wrong Git Directory", exp, act);
  207. }
  208. private void assertWorkdirPath(Repository repo, String... expected)
  209. throws IOException {
  210. File exp = getFile(expected).getCanonicalFile();
  211. File act = repo.getWorkTree().getCanonicalFile();
  212. assertEquals("Wrong working Directory", exp, act);
  213. }
  214. }