| 1 | package gitrepo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | func TestMaintenanceArgsNeverPruneNow(t *testing.T) { |
| 14 | for _, args := range [][]string{MaintenanceArgs(), GCAutoArgs()} { |
| 15 | joined := strings.Join(args, " ") |
| 16 | if strings.Contains(joined, "--prune=now") || strings.Contains(joined, "prune=now") { |
| 17 | t.Fatalf("onderhoud mag nooit --prune=now gebruiken: %s", joined) |
| 18 | } |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | func TestMaintainKeepsReachableObjects(t *testing.T) { |
| 23 | bare, work := testRepo(t) |
| 24 | sha := gitSHA(t, bare, "refs/heads/master") |
| 25 | // Een paar losse objecten zodat gc iets te doen heeft, zonder |
| 26 | // onbereikbare refs te willen wissen. |
| 27 | if err := os.WriteFile(filepath.Join(work, "g"), []byte("meer\n"), 0o644); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | gitOK(t, work, "add", "g") |
| 31 | gitOK(t, work, "commit", "-qm", "meer") |
| 32 | gitOK(t, work, "push", "-q", "origin", "master") |
| 33 | head := gitSHA(t, bare, "refs/heads/master") |
| 34 | |
| 35 | ctx, cancel := context.WithTimeout(context.Background(), time.Minute) |
| 36 | defer cancel() |
| 37 | res := Maintain(ctx, "git", bare) |
| 38 | if !res.OK { |
| 39 | t.Fatalf("maintain: ok=false err=%s out=%s", res.Err, res.Output) |
| 40 | } |
| 41 | if got := gitSHA(t, bare, "refs/heads/master"); got != head { |
| 42 | t.Fatalf("head veranderde: %s -> %s", head, got) |
| 43 | } |
| 44 | if err := exec.Command("git", "-C", bare, "cat-file", "-e", sha).Run(); err != nil { |
| 45 | t.Fatalf("bereikbaar object %s is weg: %v", sha, err) |
| 46 | } |
| 47 | if err := exec.Command("git", "-C", bare, "cat-file", "-e", head).Run(); err != nil { |
| 48 | t.Fatalf("nieuwe head %s is weg: %v", head, err) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestMaintainMissingDirIsSkipped(t *testing.T) { |
| 53 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 54 | defer cancel() |
| 55 | res := Maintain(ctx, "git", filepath.Join(t.TempDir(), "ontbreekt.git")) |
| 56 | if res.OK { |
| 57 | t.Fatal("ontbrekende repo zou moeten falen (en overgeslagen worden)") |
| 58 | } |
| 59 | if res.Err == "" { |
| 60 | t.Fatal("verwacht een fouttekst") |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func TestMaintainEmptyPathFails(t *testing.T) { |
| 65 | res := Maintain(context.Background(), "git", "") |
| 66 | if res.OK || res.Err == "" { |
| 67 | t.Fatalf("leeg pad moet falen, kreeg %+v", res) |
| 68 | } |
| 69 | } |