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

LfsPointerFilterTest.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (C) 2015, Dariusz Luksza <dariusz@luksza.org> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.lfs.lib;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertFalse;
  13. import static org.junit.Assert.assertNotNull;
  14. import static org.junit.Assert.assertNull;
  15. import static org.junit.Assert.assertTrue;
  16. import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
  17. import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
  18. import org.eclipse.jgit.junit.TestRepository;
  19. import org.eclipse.jgit.revwalk.ObjectWalk;
  20. import org.eclipse.jgit.revwalk.RevCommit;
  21. import org.eclipse.jgit.revwalk.RevTree;
  22. import org.eclipse.jgit.treewalk.TreeWalk;
  23. import org.junit.Test;
  24. public class LfsPointerFilterTest {
  25. private static final int SIZE = 12345;
  26. private static final String OID = "4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393";
  27. private static final String[] NOT_VALID_LFS_FILES = { "", // empty file
  28. // simulate java file
  29. "package org.eclipse.jgit;",
  30. // invalid LFS pointer, no oid and version
  31. "version https://hawser.github.com/spec/v1\n",
  32. // invalid LFS pointer, no version
  33. "version https://hawser.github.com/spec/v1\n"
  34. + "oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\n",
  35. // invalid LFS pointer, no id
  36. "version https://hawser.github.com/spec/v1\n" + "size 12345\n",
  37. // invalid LFS pointer, wrong order of oid and size
  38. "version https://hawser.github.com/spec/v1\n" + "size 12345\n"
  39. + "oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\n" };
  40. private static final String[] LFS_VERSION_DOMAINS = {
  41. "hawser", "git-lfs"
  42. };
  43. private static final String[] VALID_LFS_FILES = {
  44. // valid LFS pointer
  45. "version https://%s.github.com/spec/v1\n"
  46. + "oid sha256:" + OID + "\n"
  47. + "size " + SIZE + "\n",
  48. // valid LFS pointer with "custom" key
  49. "version https://%s.github.com/spec/v1\n"
  50. + "custom key with value\n"
  51. + "oid sha256:" + OID + "\n"
  52. + "size " + SIZE + "\n",
  53. // valid LFS pointer with key with "."
  54. "version https://%s.github.com/spec/v1\n"
  55. + "oid sha256:" + OID + "\n"
  56. + "r.key key with .\n"
  57. + "size " + SIZE + "\n",
  58. // valid LFS pointer with key with "-"
  59. "version https://%s.github.com/spec/v1\n"
  60. + "oid sha256:" + OID + "\n"
  61. + "size " + SIZE + "\n"
  62. + "valid-name another valid key\n" };
  63. @Test
  64. public void testRegularFilesInRepositoryRoot() throws Exception {
  65. for (String file : NOT_VALID_LFS_FILES) {
  66. assertLfs("file.bin", file).withRecursive(false).shouldBe(false);
  67. }
  68. }
  69. @Test
  70. public void testNestedRegularFiles() throws Exception {
  71. for (String file : NOT_VALID_LFS_FILES) {
  72. assertLfs("a/file.bin", file).withRecursive(true).shouldBe(false);
  73. }
  74. }
  75. @Test
  76. public void testValidPointersInRepositoryRoot() throws Exception {
  77. for (String domain : LFS_VERSION_DOMAINS) {
  78. for (String file : VALID_LFS_FILES) {
  79. assertLfs("file.bin", String.format(file, domain))
  80. .withRecursive(true).shouldBe(true)
  81. .check();
  82. }
  83. }
  84. }
  85. @Test
  86. public void testValidNestedPointers() throws Exception {
  87. for (String domain : LFS_VERSION_DOMAINS) {
  88. for (String file : VALID_LFS_FILES) {
  89. assertLfs("a/file.bin", String.format(file, domain))
  90. .withRecursive(true).shouldBe(true).check();
  91. }
  92. }
  93. }
  94. @Test
  95. public void testValidNestedPointersWithoutRecurrence() throws Exception {
  96. for (String domain : LFS_VERSION_DOMAINS) {
  97. for (String file : VALID_LFS_FILES) {
  98. assertLfs("file.bin", String.format(file, domain))
  99. .withRecursive(false).shouldBe(true).check();
  100. assertLfs("a/file.bin", String.format(file, domain))
  101. .withRecursive(false).shouldBe(false).check();
  102. }
  103. }
  104. }
  105. private static LfsTreeWalk assertLfs(String path, String content) {
  106. return new LfsTreeWalk(path, content);
  107. }
  108. private static class LfsTreeWalk {
  109. private final String path;
  110. private final String content;
  111. private boolean state;
  112. private boolean recursive;
  113. private TestRepository<InMemoryRepository> tr;
  114. LfsTreeWalk(String path, String content) {
  115. this.path = path;
  116. this.content = content;
  117. }
  118. LfsTreeWalk withRecursive(boolean shouldBeRecursive) {
  119. this.recursive = shouldBeRecursive;
  120. return this;
  121. }
  122. LfsTreeWalk shouldBe(boolean shouldBeValid) {
  123. this.state = shouldBeValid;
  124. return this;
  125. }
  126. void check() throws Exception {
  127. tr = new TestRepository<>(new InMemoryRepository(
  128. new DfsRepositoryDescription("test")));
  129. RevCommit commit = tr.branch("master").commit().add(path, content)
  130. .message("initial commit").create();
  131. RevTree tree = parseCommit(commit);
  132. LfsPointerFilter filter = new LfsPointerFilter();
  133. try (TreeWalk treeWalk = new TreeWalk(tr.getRepository())) {
  134. treeWalk.addTree(tree);
  135. treeWalk.setRecursive(recursive);
  136. treeWalk.setFilter(filter);
  137. if (state) {
  138. assertTrue(treeWalk.next());
  139. assertEquals(path, treeWalk.getPathString());
  140. assertNotNull(filter.getPointer());
  141. assertEquals(SIZE, filter.getPointer().getSize());
  142. assertEquals(OID, filter.getPointer().getOid().name());
  143. } else {
  144. assertFalse(treeWalk.next());
  145. assertNull(filter.getPointer());
  146. }
  147. }
  148. }
  149. private RevTree parseCommit(RevCommit commit) throws Exception {
  150. try (ObjectWalk ow = new ObjectWalk(tr.getRepository())) {
  151. return ow.parseCommit(commit).getTree();
  152. }
  153. }
  154. }
  155. }