|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +const baseBlobOID = "df967b96a579e45a18b8251732d16804b2e56a55" // sha1 of "blob 5\0base\n" |
| 10 | + |
| 11 | +func setupRepoWithBaseBlob(t *testing.T) string { |
| 12 | + t.Helper() |
| 13 | + |
| 14 | + repo := t.TempDir() |
| 15 | + |
| 16 | + if _, _, err := runGogit(t, repo, "init"); err != nil { |
| 17 | + t.Fatalf("init: %v", err) |
| 18 | + } |
| 19 | + |
| 20 | + if err := os.WriteFile(filepath.Join(repo, "file0"), []byte("base\n"), 0o644); err != nil { |
| 21 | + t.Fatal(err) |
| 22 | + } |
| 23 | + |
| 24 | + if _, _, err := runGogit(t, repo, "add", "file0"); err != nil { |
| 25 | + t.Fatalf("add: %v", err) |
| 26 | + } |
| 27 | + |
| 28 | + if _, _, err := runGogitEnv(t, repo, gitIdentityEnv(repo), "commit", "-m", "x"); err != nil { |
| 29 | + t.Fatalf("commit: %v", err) |
| 30 | + } |
| 31 | + |
| 32 | + return repo |
| 33 | +} |
| 34 | + |
| 35 | +func TestCatFileExistsExitsZero(t *testing.T) { |
| 36 | + t.Parallel() |
| 37 | + |
| 38 | + repo := setupRepoWithBaseBlob(t) |
| 39 | + |
| 40 | + if _, _, err := runGogit(t, repo, "cat-file", "-e", baseBlobOID); err != nil { |
| 41 | + t.Fatalf("cat-file -e <existing>: expected exit 0, got %v", err) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestCatFileMissingExitsOne(t *testing.T) { |
| 46 | + t.Parallel() |
| 47 | + |
| 48 | + repo := t.TempDir() |
| 49 | + |
| 50 | + if _, _, err := runGogit(t, repo, "init"); err != nil { |
| 51 | + t.Fatalf("init: %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + stdout, _, err := runGogit(t, repo, "cat-file", "-e", "0000000000000000000000000000000000000000") |
| 55 | + if err == nil { |
| 56 | + t.Fatalf("expected non-zero exit, got success") |
| 57 | + } |
| 58 | + |
| 59 | + if stdout != "" { |
| 60 | + t.Fatalf("expected no stdout, got %q", stdout) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestCatFileBatchCheck(t *testing.T) { |
| 65 | + t.Parallel() |
| 66 | + |
| 67 | + repo := setupRepoWithBaseBlob(t) |
| 68 | + |
| 69 | + const missingOID = "0000000000000000000000000000000000000000" |
| 70 | + |
| 71 | + input := baseBlobOID + "\n" + missingOID + "\n" |
| 72 | + want := baseBlobOID + " blob 5\n" + missingOID + " missing\n" |
| 73 | + |
| 74 | + stdout, stderr, err := runGogitStdin(t, repo, input, "cat-file", "--batch-check") |
| 75 | + if err != nil { |
| 76 | + t.Fatalf("cat-file --batch-check failed: %v\nstderr: %s", err, stderr) |
| 77 | + } |
| 78 | + |
| 79 | + if stdout != want { |
| 80 | + t.Fatalf("batch-check output mismatch:\n got: %q\nwant: %q", stdout, want) |
| 81 | + } |
| 82 | +} |
0 commit comments