|
| 1 | +package integrity_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/githubnext/apm/internal/cache/integrity" |
| 9 | +) |
| 10 | + |
| 11 | +func TestReadHeadSHA_EmptyDir(t *testing.T) { |
| 12 | + dir := t.TempDir() |
| 13 | + got := integrity.ReadHeadSHA(dir) |
| 14 | + if got != "" { |
| 15 | + t.Errorf("expected empty for dir without .git, got %q", got) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +func TestReadHeadSHA_DirectSHA(t *testing.T) { |
| 20 | + sha := "cafebabe00000000cafebabe00000000cafebabe" |
| 21 | + root := t.TempDir() |
| 22 | + gitDir := filepath.Join(root, ".git") |
| 23 | + _ = os.MkdirAll(gitDir, 0o700) |
| 24 | + _ = os.WriteFile(filepath.Join(gitDir, "HEAD"), []byte(sha+"\n"), 0o600) |
| 25 | + got := integrity.ReadHeadSHA(root) |
| 26 | + if got != sha { |
| 27 | + t.Errorf("ReadHeadSHA direct SHA: got %q want %q", got, sha) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func TestReadHeadSHA_RefPointingToMissingFile(t *testing.T) { |
| 32 | + root := t.TempDir() |
| 33 | + gitDir := filepath.Join(root, ".git") |
| 34 | + _ = os.MkdirAll(gitDir, 0o700) |
| 35 | + _ = os.WriteFile(filepath.Join(gitDir, "HEAD"), []byte("ref: refs/heads/missing\n"), 0o600) |
| 36 | + got := integrity.ReadHeadSHA(root) |
| 37 | + if got != "" { |
| 38 | + t.Errorf("expected empty for dangling ref, got %q", got) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestVerifyCheckout_EmptyExpected(t *testing.T) { |
| 43 | + sha := "aabbccddaabbccddaabbccddaabbccddaabbccdd" |
| 44 | + root := makeGitDir(t, sha) |
| 45 | + if integrity.VerifyCheckout(root, "") { |
| 46 | + t.Error("VerifyCheckout should be false when expectedSHA is empty") |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestVerifyCheckout_EmptyActual(t *testing.T) { |
| 51 | + root := t.TempDir() |
| 52 | + if integrity.VerifyCheckout(root, "anySHA") { |
| 53 | + t.Error("VerifyCheckout should be false when ReadHeadSHA returns empty") |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestReadHeadSHA_PackedRefsWithCommentLine(t *testing.T) { |
| 58 | + sha := "deadcafedeadcafedeadcafedeadcafedeadcafe" |
| 59 | + root := t.TempDir() |
| 60 | + gitDir := filepath.Join(root, ".git") |
| 61 | + _ = os.MkdirAll(gitDir, 0o700) |
| 62 | + _ = os.WriteFile(filepath.Join(gitDir, "HEAD"), []byte("ref: refs/heads/main\n"), 0o600) |
| 63 | + packedRefs := "# pack-refs with: peeled fully-peeled sorted\n^peeled-sha\n" + sha + " refs/heads/main\n" |
| 64 | + _ = os.WriteFile(filepath.Join(gitDir, "packed-refs"), []byte(packedRefs), 0o600) |
| 65 | + got := integrity.ReadHeadSHA(root) |
| 66 | + if got != sha { |
| 67 | + t.Errorf("ReadHeadSHA packed-refs with comment: got %q want %q", got, sha) |
| 68 | + } |
| 69 | +} |
0 commit comments