選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

RepositorySetupWorkDirTest.java 8.3KB

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