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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.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.util.FS;
  58. import org.eclipse.jgit.util.FileUtils;
  59. import org.junit.Test;
  60. /**
  61. * Tests for setting up the working directory when creating a Repository
  62. */
  63. public class RepositorySetupWorkDirTest extends LocalDiskRepositoryTestCase {
  64. @Test
  65. public void testIsBare_CreateRepositoryFromArbitraryGitDir()
  66. throws Exception {
  67. File gitDir = getFile("workdir");
  68. assertTrue(new FileRepository(gitDir).isBare());
  69. }
  70. @Test
  71. public void testNotBare_CreateRepositoryFromDotGitGitDir() throws Exception {
  72. File gitDir = getFile("workdir", Constants.DOT_GIT);
  73. Repository repo = new FileRepository(gitDir);
  74. assertFalse(repo.isBare());
  75. assertWorkdirPath(repo, "workdir");
  76. assertGitdirPath(repo, "workdir", Constants.DOT_GIT);
  77. }
  78. @Test
  79. public void testWorkdirIsParentDir_CreateRepositoryFromDotGitGitDir()
  80. throws Exception {
  81. File gitDir = getFile("workdir", Constants.DOT_GIT);
  82. Repository repo = new FileRepository(gitDir);
  83. String workdir = repo.getWorkTree().getName();
  84. assertEquals(workdir, "workdir");
  85. }
  86. @Test
  87. public void testNotBare_CreateRepositoryFromWorkDirOnly() throws Exception {
  88. File workdir = getFile("workdir", "repo");
  89. FileRepository repo = new FileRepositoryBuilder().setWorkTree(workdir).build();
  90. assertFalse(repo.isBare());
  91. assertWorkdirPath(repo, "workdir", "repo");
  92. assertGitdirPath(repo, "workdir", "repo", Constants.DOT_GIT);
  93. }
  94. @Test
  95. public void testWorkdirIsDotGit_CreateRepositoryFromWorkDirOnly()
  96. throws Exception {
  97. File workdir = getFile("workdir", "repo");
  98. FileRepository repo = new FileRepositoryBuilder().setWorkTree(workdir).build();
  99. assertGitdirPath(repo, "workdir", "repo", Constants.DOT_GIT);
  100. }
  101. @Test
  102. public void testNotBare_CreateRepositoryFromGitDirOnlyWithWorktreeConfig()
  103. throws Exception {
  104. File gitDir = getFile("workdir", "repoWithConfig");
  105. File workTree = getFile("workdir", "treeRoot");
  106. setWorkTree(gitDir, workTree);
  107. FileRepository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
  108. assertFalse(repo.isBare());
  109. assertWorkdirPath(repo, "workdir", "treeRoot");
  110. assertGitdirPath(repo, "workdir", "repoWithConfig");
  111. }
  112. @Test
  113. public void testBare_CreateRepositoryFromGitDirOnlyWithBareConfigTrue()
  114. throws Exception {
  115. File gitDir = getFile("workdir", "repoWithConfig");
  116. setBare(gitDir, true);
  117. FileRepository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
  118. assertTrue(repo.isBare());
  119. }
  120. @Test
  121. public void testWorkdirIsParent_CreateRepositoryFromGitDirOnlyWithBareConfigFalse()
  122. throws Exception {
  123. File gitDir = getFile("workdir", "repoWithBareConfigTrue", "child");
  124. setBare(gitDir, false);
  125. FileRepository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
  126. assertWorkdirPath(repo, "workdir", "repoWithBareConfigTrue");
  127. }
  128. @Test
  129. public void testNotBare_CreateRepositoryFromGitDirOnlyWithBareConfigFalse()
  130. throws Exception {
  131. File gitDir = getFile("workdir", "repoWithBareConfigFalse", "child");
  132. setBare(gitDir, false);
  133. FileRepository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
  134. assertFalse(repo.isBare());
  135. assertWorkdirPath(repo, "workdir", "repoWithBareConfigFalse");
  136. assertGitdirPath(repo, "workdir", "repoWithBareConfigFalse", "child");
  137. }
  138. @Test
  139. public void testExceptionThrown_BareRepoGetWorkDir() throws Exception {
  140. File gitDir = getFile("workdir");
  141. try {
  142. new FileRepository(gitDir).getWorkTree();
  143. fail("Expected NoWorkTreeException missing");
  144. } catch (NoWorkTreeException e) {
  145. // expected
  146. }
  147. }
  148. @Test
  149. public void testExceptionThrown_BareRepoGetIndex() throws Exception {
  150. File gitDir = getFile("workdir");
  151. try {
  152. new FileRepository(gitDir).getIndex();
  153. fail("Expected NoWorkTreeException missing");
  154. } catch (NoWorkTreeException e) {
  155. // expected
  156. }
  157. }
  158. @Test
  159. public void testExceptionThrown_BareRepoGetIndexFile() throws Exception {
  160. File gitDir = getFile("workdir");
  161. try {
  162. new FileRepository(gitDir).getIndexFile();
  163. fail("Expected NoWorkTreeException missing");
  164. } catch (NoWorkTreeException e) {
  165. // expected
  166. }
  167. }
  168. private File getFile(String... pathComponents) throws IOException {
  169. String rootPath = new File(new File("target"), "trash").getPath();
  170. for (String pathComponent : pathComponents)
  171. rootPath = rootPath + File.separatorChar + pathComponent;
  172. File result = new File(rootPath);
  173. FileUtils.mkdirs(result, true);
  174. return result;
  175. }
  176. private void setBare(File gitDir, boolean bare) throws IOException,
  177. ConfigInvalidException {
  178. FileBasedConfig cfg = configFor(gitDir);
  179. cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  180. ConfigConstants.CONFIG_KEY_BARE, bare);
  181. cfg.save();
  182. }
  183. private void setWorkTree(File gitDir, File workTree) throws IOException,
  184. ConfigInvalidException {
  185. String path = workTree.getAbsolutePath();
  186. FileBasedConfig cfg = configFor(gitDir);
  187. cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
  188. ConfigConstants.CONFIG_KEY_WORKTREE, path);
  189. cfg.save();
  190. }
  191. private FileBasedConfig configFor(File gitDir) throws IOException,
  192. ConfigInvalidException {
  193. File configPath = new File(gitDir, "config");
  194. FileBasedConfig cfg = new FileBasedConfig(configPath, FS.DETECTED);
  195. cfg.load();
  196. return cfg;
  197. }
  198. private void assertGitdirPath(Repository repo, String... expected)
  199. throws IOException {
  200. File exp = getFile(expected).getCanonicalFile();
  201. File act = repo.getDirectory().getCanonicalFile();
  202. assertEquals("Wrong Git Directory", exp, act);
  203. }
  204. private void assertWorkdirPath(Repository repo, String... expected)
  205. throws IOException {
  206. File exp = getFile(expected).getCanonicalFile();
  207. File act = repo.getWorkTree().getCanonicalFile();
  208. assertEquals("Wrong working Directory", exp, act);
  209. }
  210. }