|
4 | 4 | "os" |
5 | 5 | "path/filepath" |
6 | 6 | "testing" |
| 7 | + |
| 8 | + "github.com/Astro-Han/diffpane/internal" |
7 | 9 | ) |
8 | 10 |
|
9 | 11 | // TestComputeDiffModifiedFile verifies tracked modifications become unified diffs. |
@@ -128,6 +130,68 @@ func TestComputeDiffFreshRepoUntrackedFile(t *testing.T) { |
128 | 130 | } |
129 | 131 | } |
130 | 132 |
|
| 133 | +// TestComputeDiffUntrackedFilePreservesInnerBlankLines verifies synthetic diffs |
| 134 | +// keep blank lines inside file content instead of dropping them. |
| 135 | +func TestComputeDiffUntrackedFilePreservesInnerBlankLines(t *testing.T) { |
| 136 | + root := initGitRepo(t) |
| 137 | + runGit(t, root, "commit", "--allow-empty", "-m", "init") |
| 138 | + |
| 139 | + baseline, err := GetHeadSHA(root) |
| 140 | + if err != nil { |
| 141 | + t.Fatalf("GetHeadSHA returned error: %v", err) |
| 142 | + } |
| 143 | + if err := os.WriteFile(filepath.Join(root, "blank.txt"), []byte("top\n\nbottom\n"), 0o600); err != nil { |
| 144 | + t.Fatalf("write file: %v", err) |
| 145 | + } |
| 146 | + |
| 147 | + files, err := ComputeDiff(root, baseline) |
| 148 | + if err != nil { |
| 149 | + t.Fatalf("ComputeDiff returned error: %v", err) |
| 150 | + } |
| 151 | + if len(files) != 1 { |
| 152 | + t.Fatalf("expected 1 file, got %d", len(files)) |
| 153 | + } |
| 154 | + lines := files[0].Hunks[0].Lines |
| 155 | + if len(lines) != 3 { |
| 156 | + t.Fatalf("line count = %d, want 3", len(lines)) |
| 157 | + } |
| 158 | + if lines[1].Content != "" { |
| 159 | + t.Fatalf("middle line = %q, want blank line", lines[1].Content) |
| 160 | + } |
| 161 | + if files[0].AddCount != 3 { |
| 162 | + t.Fatalf("add count = %d, want 3", files[0].AddCount) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +// TestComputeDiffUntrackedBinaryFile verifies untracked binary files are not |
| 167 | +// rendered as text hunks. |
| 168 | +func TestComputeDiffUntrackedBinaryFile(t *testing.T) { |
| 169 | + root := initGitRepo(t) |
| 170 | + runGit(t, root, "commit", "--allow-empty", "-m", "init") |
| 171 | + |
| 172 | + baseline, err := GetHeadSHA(root) |
| 173 | + if err != nil { |
| 174 | + t.Fatalf("GetHeadSHA returned error: %v", err) |
| 175 | + } |
| 176 | + if err := os.WriteFile(filepath.Join(root, "image.bin"), []byte{0x00, 0x01, 0x02}, 0o600); err != nil { |
| 177 | + t.Fatalf("write file: %v", err) |
| 178 | + } |
| 179 | + |
| 180 | + files, err := ComputeDiff(root, baseline) |
| 181 | + if err != nil { |
| 182 | + t.Fatalf("ComputeDiff returned error: %v", err) |
| 183 | + } |
| 184 | + if len(files) != 1 { |
| 185 | + t.Fatalf("expected 1 file, got %d", len(files)) |
| 186 | + } |
| 187 | + if !files[0].IsBinary || files[0].Status != internal.StatusBinary { |
| 188 | + t.Fatalf("expected binary status, got %#v", files[0]) |
| 189 | + } |
| 190 | + if len(files[0].Hunks) != 0 { |
| 191 | + t.Fatalf("binary file should not have text hunks, got %#v", files[0].Hunks) |
| 192 | + } |
| 193 | +} |
| 194 | + |
131 | 195 | // TestComputeDiffNoChanges verifies a clean worktree produces no diffs. |
132 | 196 | func TestComputeDiffNoChanges(t *testing.T) { |
133 | 197 | root := initGitRepo(t) |
|
0 commit comments