|
| 1 | +/* |
| 2 | + * Copyright 2026 java-diff-utils. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.github.difflib.unifieddiff; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import com.github.difflib.patch.PatchFailedException; |
| 21 | +import java.io.ByteArrayInputStream; |
| 22 | +import java.io.IOException; |
| 23 | +import java.nio.charset.StandardCharsets; |
| 24 | +import java.util.Collections; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +public class UnifiedDiffReaderFileBoundaryTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + public void testParseHeaderOnlyFilesBeforeTextChange() throws IOException, PatchFailedException { |
| 31 | + UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(getClass().getResourceAsStream("header_only_files.diff")); |
| 32 | + |
| 33 | + assertThat(diff.getFiles()).hasSize(5); |
| 34 | + UnifiedDiffFile binary = diff.getFiles().get(0); |
| 35 | + assertThat(binary.getFromFile()).isEqualTo("a-image.bin"); |
| 36 | + assertThat(binary.getToFile()).isEqualTo("a-image.bin"); |
| 37 | + assertThat(binary.getBinaryEdited()).isEqualTo("a-image.bin"); |
| 38 | + assertThat(binary.getPatch().getDeltas()).isEmpty(); |
| 39 | + |
| 40 | + UnifiedDiffFile empty = diff.getFiles().get(1); |
| 41 | + assertThat(empty.getToFile()).isEqualTo("b-empty.txt"); |
| 42 | + assertThat(empty.getNewFileMode()).isEqualTo("100644"); |
| 43 | + assertThat(empty.getBinaryEdited()).isNull(); |
| 44 | + assertThat(empty.getPatch().getDeltas()).isEmpty(); |
| 45 | + |
| 46 | + UnifiedDiffFile mode = diff.getFiles().get(2); |
| 47 | + assertThat(mode.getFromFile()).isEqualTo("c-script.sh"); |
| 48 | + assertThat(mode.getOldMode()).isEqualTo("100644"); |
| 49 | + assertThat(mode.getNewMode()).isEqualTo("100755"); |
| 50 | + assertThat(mode.getNewFileMode()).isNull(); |
| 51 | + assertThat(mode.getPatch().getDeltas()).isEmpty(); |
| 52 | + |
| 53 | + UnifiedDiffFile rename = diff.getFiles().get(3); |
| 54 | + assertThat(rename.getFromFile()).isEqualTo("d-old.txt"); |
| 55 | + assertThat(rename.getToFile()).isEqualTo("e-renamed.txt"); |
| 56 | + assertThat(rename.getSimilarityIndex()).isEqualTo(100); |
| 57 | + assertThat(rename.getRenameFrom()).isEqualTo("d-old.txt"); |
| 58 | + assertThat(rename.getRenameTo()).isEqualTo("e-renamed.txt"); |
| 59 | + assertThat(rename.getOldMode()).isNull(); |
| 60 | + assertThat(rename.getPatch().getDeltas()).isEmpty(); |
| 61 | + |
| 62 | + UnifiedDiffFile text = diff.getFiles().get(4); |
| 63 | + assertThat(text.getFromFile()).isEqualTo("z-text.txt"); |
| 64 | + assertThat(text.getToFile()).isEqualTo("z-text.txt"); |
| 65 | + assertThat(text.getBinaryEdited()).isNull(); |
| 66 | + assertThat(text.getNewFileMode()).isNull(); |
| 67 | + assertThat(text.getNewMode()).isNull(); |
| 68 | + assertThat(text.getRenameFrom()).isNull(); |
| 69 | + assertThat(text.getPatch().applyTo(Collections.singletonList("old"))).containsExactly("new"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testParseConsecutiveHeaderOnlyFilesAtEndOfDiff() throws IOException { |
| 74 | + String text = "diff --git a/first.sh b/first.sh\n" |
| 75 | + + "old mode 100644\nnew mode 100755\n" |
| 76 | + + "diff --git a/second.sh b/second.sh\n" |
| 77 | + + "old mode 100755\nnew mode 100644\n"; |
| 78 | + UnifiedDiff diff = |
| 79 | + UnifiedDiffReader.parseUnifiedDiff(new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8))); |
| 80 | + |
| 81 | + assertThat(diff.getFiles()).hasSize(2); |
| 82 | + assertThat(diff.getFiles().get(0).getFromFile()).isEqualTo("first.sh"); |
| 83 | + assertThat(diff.getFiles().get(0).getNewMode()).isEqualTo("100755"); |
| 84 | + assertThat(diff.getFiles().get(1).getFromFile()).isEqualTo("second.sh"); |
| 85 | + assertThat(diff.getFiles().get(1).getNewMode()).isEqualTo("100644"); |
| 86 | + assertThat(diff.getFiles()) |
| 87 | + .allSatisfy(file -> assertThat(file.getPatch().getDeltas()).isEmpty()); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testParseTextChangeBeforeHeaderOnlyAndBinaryFiles() throws IOException, PatchFailedException { |
| 92 | + String text = "diff --git a/text.txt b/text.txt\n" |
| 93 | + + "--- a/text.txt\n+++ b/text.txt\n@@ -1 +1 @@\n-old\n+new\n" |
| 94 | + + "diff --git a/script.sh b/script.sh\n" |
| 95 | + + "old mode 100644\nnew mode 100755\n" |
| 96 | + + "diff --git a/image.bin b/image.bin\n" |
| 97 | + + "Binary files a/image.bin and b/image.bin differ\n"; |
| 98 | + UnifiedDiff diff = |
| 99 | + UnifiedDiffReader.parseUnifiedDiff(new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8))); |
| 100 | + |
| 101 | + assertThat(diff.getFiles()).hasSize(3); |
| 102 | + UnifiedDiffFile changed = diff.getFiles().get(0); |
| 103 | + assertThat(changed.getFromFile()).isEqualTo("text.txt"); |
| 104 | + assertThat(changed.getNewMode()).isNull(); |
| 105 | + assertThat(changed.getBinaryEdited()).isNull(); |
| 106 | + assertThat(changed.getPatch().applyTo(Collections.singletonList("old"))).containsExactly("new"); |
| 107 | + UnifiedDiffFile mode = diff.getFiles().get(1); |
| 108 | + assertThat(mode.getFromFile()).isEqualTo("script.sh"); |
| 109 | + assertThat(mode.getNewMode()).isEqualTo("100755"); |
| 110 | + assertThat(mode.getBinaryEdited()).isNull(); |
| 111 | + assertThat(mode.getPatch().getDeltas()).isEmpty(); |
| 112 | + UnifiedDiffFile binary = diff.getFiles().get(2); |
| 113 | + assertThat(binary.getFromFile()).isEqualTo("image.bin"); |
| 114 | + assertThat(binary.getBinaryEdited()).isEqualTo("image.bin"); |
| 115 | + assertThat(binary.getNewMode()).isNull(); |
| 116 | + assertThat(binary.getPatch().getDeltas()).isEmpty(); |
| 117 | + } |
| 118 | +} |
0 commit comments