| 1 | package rebase |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | // A pair entry with more conflicted paths than fit is cut to the first in |
| 12 | // sorted order, keeps the full count, and reads back as it was returned, |
| 13 | // with its heads in either order. Its file fits what an entry is read to, |
| 14 | // and a damaged one is a miss. |
| 15 | func TestAPairEntryIsCutToWhatFits(t *testing.T) { |
| 16 | dir := t.TempDir() |
| 17 | tip, a, b := strings.Repeat("1", 40), strings.Repeat("2", 40), strings.Repeat("3", 40) |
| 18 | var paths []string |
| 19 | for i := 300; i > 0; i-- { |
| 20 | paths = append(paths, fmt.Sprintf("docs/a-long-directory-name/file-%03d.md", i)) |
| 21 | } |
| 22 | kept := RememberPair(dir, tip, b, a, Pair{Total: len(paths), Paths: paths}) |
| 23 | if kept.Total != 300 || len(kept.Paths) == 0 || len(kept.Paths) >= 300 || kept.Paths[0] != "docs/a-long-directory-name/file-001.md" { |
| 24 | t.Fatalf("RememberPair kept %d of %d paths, the first %q", len(kept.Paths), kept.Total, kept.Paths) |
| 25 | } |
| 26 | got, ok := RememberedPair(dir, tip, a, b) |
| 27 | if !ok || got.Total != kept.Total || strings.Join(got.Paths, "\n") != strings.Join(kept.Paths, "\n") { |
| 28 | t.Fatalf("RememberedPair = %d paths of %d, %v; want what RememberPair kept", len(got.Paths), got.Total, ok) |
| 29 | } |
| 30 | entries, _ := filepath.Glob(filepath.Join(dir, "pairs", "*")) |
| 31 | if len(entries) != 1 { |
| 32 | t.Fatalf("the pair is %d entries", len(entries)) |
| 33 | } |
| 34 | data, err := os.ReadFile(entries[0]) |
| 35 | if err != nil || len(data) > entryLimit { |
| 36 | t.Fatalf("the entry is %d bytes, more than %d, or does not read: %v", len(data), entryLimit, err) |
| 37 | } |
| 38 | os.WriteFile(entries[0], []byte(strings.Replace(string(data), "file-001", "file-000", 1)), 0o600) |
| 39 | if _, ok := RememberedPair(dir, tip, a, b); ok { |
| 40 | t.Error("a damaged pair entry is found") |
| 41 | } |
| 42 | if clean := RememberPair(dir, tip, a, b, Pair{}); clean.Total != 0 { |
| 43 | t.Errorf("a clean pair kept %d paths", clean.Total) |
| 44 | } |
| 45 | if got, ok := RememberedPair(dir, tip, b, a); !ok || got.Total != 0 || len(got.Paths) != 0 { |
| 46 | t.Errorf("a clean pair reads back as %+v, %v", got, ok) |
| 47 | } |
| 48 | } |