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.

PatchErrorTest.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc. 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.patch;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertNull;
  13. import static org.junit.Assert.assertSame;
  14. import static org.junit.Assert.assertTrue;
  15. import static org.junit.Assert.fail;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import org.eclipse.jgit.junit.JGitTestUtil;
  19. import org.junit.Test;
  20. public class PatchErrorTest {
  21. @Test
  22. public void testError_DisconnectedHunk() throws IOException {
  23. final Patch p = parseTestPatchFile();
  24. assertEquals(1, p.getFiles().size());
  25. {
  26. final FileHeader fh = p.getFiles().get(0);
  27. assertEquals(
  28. "org.eclipse.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java",
  29. fh.getNewPath());
  30. assertEquals(1, fh.getHunks().size());
  31. }
  32. assertEquals(1, p.getErrors().size());
  33. final FormatError e = p.getErrors().get(0);
  34. assertSame(FormatError.Severity.ERROR, e.getSeverity());
  35. assertEquals("Hunk disconnected from file", e.getMessage());
  36. assertEquals(18, e.getOffset());
  37. assertTrue(e.getLineText().startsWith("@@ -109,4 +109,11 @@ assert"));
  38. }
  39. @Test
  40. public void testError_TruncatedOld() throws IOException {
  41. final Patch p = parseTestPatchFile();
  42. assertEquals(1, p.getFiles().size());
  43. assertEquals(1, p.getErrors().size());
  44. final FormatError e = p.getErrors().get(0);
  45. assertSame(FormatError.Severity.ERROR, e.getSeverity());
  46. assertEquals("Truncated hunk, at least 1 old lines is missing", e
  47. .getMessage());
  48. assertEquals(313, e.getOffset());
  49. assertTrue(e.getLineText().startsWith("@@ -236,9 +236,9 @@ protected "));
  50. }
  51. @Test
  52. public void testError_TruncatedNew() throws IOException {
  53. final Patch p = parseTestPatchFile();
  54. assertEquals(1, p.getFiles().size());
  55. assertEquals(1, p.getErrors().size());
  56. final FormatError e = p.getErrors().get(0);
  57. assertSame(FormatError.Severity.ERROR, e.getSeverity());
  58. assertEquals("Truncated hunk, at least 1 new lines is missing", e
  59. .getMessage());
  60. assertEquals(313, e.getOffset());
  61. assertTrue(e.getLineText().startsWith("@@ -236,9 +236,9 @@ protected "));
  62. }
  63. @Test
  64. public void testError_BodyTooLong() throws IOException {
  65. final Patch p = parseTestPatchFile();
  66. assertEquals(1, p.getFiles().size());
  67. assertEquals(1, p.getErrors().size());
  68. final FormatError e = p.getErrors().get(0);
  69. assertSame(FormatError.Severity.WARNING, e.getSeverity());
  70. assertEquals("Hunk header 4:11 does not match body line count of 4:12",
  71. e.getMessage());
  72. assertEquals(349, e.getOffset());
  73. assertTrue(e.getLineText().startsWith("@@ -109,4 +109,11 @@ assert"));
  74. }
  75. @Test
  76. public void testError_GarbageBetweenFiles() throws IOException {
  77. final Patch p = parseTestPatchFile();
  78. assertEquals(2, p.getFiles().size());
  79. {
  80. final FileHeader fh = p.getFiles().get(0);
  81. assertEquals(
  82. "org.eclipse.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java",
  83. fh.getNewPath());
  84. assertEquals(1, fh.getHunks().size());
  85. }
  86. {
  87. final FileHeader fh = p.getFiles().get(1);
  88. assertEquals(
  89. "org.eclipse.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java",
  90. fh.getNewPath());
  91. assertEquals(1, fh.getHunks().size());
  92. }
  93. assertEquals(1, p.getErrors().size());
  94. final FormatError e = p.getErrors().get(0);
  95. assertSame(FormatError.Severity.WARNING, e.getSeverity());
  96. assertEquals("Unexpected hunk trailer", e.getMessage());
  97. assertEquals(926, e.getOffset());
  98. assertEquals("I AM NOT HERE\n", e.getLineText());
  99. }
  100. @Test
  101. public void testError_GitBinaryNoForwardHunk() throws IOException {
  102. final Patch p = parseTestPatchFile();
  103. assertEquals(2, p.getFiles().size());
  104. {
  105. final FileHeader fh = p.getFiles().get(0);
  106. assertEquals("org.spearce.egit.ui/icons/toolbar/fetchd.png", fh
  107. .getNewPath());
  108. assertSame(FileHeader.PatchType.GIT_BINARY, fh.getPatchType());
  109. assertTrue(fh.getHunks().isEmpty());
  110. assertNull(fh.getForwardBinaryHunk());
  111. }
  112. {
  113. final FileHeader fh = p.getFiles().get(1);
  114. assertEquals("org.spearce.egit.ui/icons/toolbar/fetche.png", fh
  115. .getNewPath());
  116. assertSame(FileHeader.PatchType.UNIFIED, fh.getPatchType());
  117. assertTrue(fh.getHunks().isEmpty());
  118. assertNull(fh.getForwardBinaryHunk());
  119. }
  120. assertEquals(1, p.getErrors().size());
  121. final FormatError e = p.getErrors().get(0);
  122. assertSame(FormatError.Severity.ERROR, e.getSeverity());
  123. assertEquals("Missing forward-image in GIT binary patch", e
  124. .getMessage());
  125. assertEquals(297, e.getOffset());
  126. assertEquals("\n", e.getLineText());
  127. }
  128. private Patch parseTestPatchFile() throws IOException {
  129. final String patchFile = JGitTestUtil.getName() + ".patch";
  130. try (InputStream in = getClass().getResourceAsStream(patchFile)) {
  131. if (in == null) {
  132. fail("No " + patchFile + " test vector");
  133. return null; // Never happens
  134. }
  135. final Patch p = new Patch();
  136. p.parse(in);
  137. return p;
  138. }
  139. }
  140. }