aboutsummaryrefslogtreecommitdiff
path: root/object/store/loose/quarantine_test.go
blob: 2aab6566d00d7866a7e661494baf33b850dd2690 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package loose_test

import (
	"bytes"
	"errors"
	"testing"

	"lindenii.org/go/furgit/internal/testgit"
	"lindenii.org/go/furgit/object/id"
	"lindenii.org/go/furgit/object/store"
	"lindenii.org/go/furgit/object/typ"
)

func TestQuarantinePromote(t *testing.T) {
	t.Parallel()

	for _, objectFormat := range id.SupportedObjectFormats() {
		t.Run(objectFormat.String(), func(t *testing.T) {
			t.Parallel()

			repo, err := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: objectFormat})
			if err != nil {
				t.Fatalf("NewRepo: %v", err)
			}

			looseStore := openLooseStore(t, repo)

			quarantine, err := looseStore.BeginObjectQuarantine(store.ObjectQuarantineOptions{})
			if err != nil {
				t.Fatalf("BeginObjectQuarantine: %v", err)
			}

			content := []byte("quarantined object\n")

			objectID, err := quarantine.WriteBytesContent(typ.TypeBlob, content)
			if err != nil {
				t.Fatalf("quarantine.WriteBytesContent: %v", err)
			}

			ty, got, err := quarantine.ReadBytesContent(objectID)
			if err != nil {
				t.Fatalf("quarantine.ReadBytesContent: %v", err)
			}

			if ty != typ.TypeBlob {
				t.Fatalf("quarantine type = %v, want %v", ty, typ.TypeBlob)
			}

			if !bytes.Equal(got, content) {
				t.Fatalf("quarantine body mismatch")
			}

			_, _, err = looseStore.ReadBytesContent(objectID)
			if !errors.Is(err, store.ErrObjectNotFound) {
				t.Fatalf("parent saw quarantined object before promote: %v", err)
			}

			err = quarantine.Promote()
			if err != nil {
				t.Fatalf("Promote: %v", err)
			}

			ty, got, err = looseStore.ReadBytesContent(objectID)
			if err != nil {
				t.Fatalf("parent ReadBytesContent after promote: %v", err)
			}

			if ty != typ.TypeBlob {
				t.Fatalf("parent type = %v, want %v", ty, typ.TypeBlob)
			}

			if !bytes.Equal(got, content) {
				t.Fatalf("parent body mismatch")
			}
		})
	}
}

func TestQuarantineDiscard(t *testing.T) {
	t.Parallel()

	for _, objectFormat := range id.SupportedObjectFormats() {
		t.Run(objectFormat.String(), func(t *testing.T) {
			t.Parallel()

			repo, err := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: objectFormat})
			if err != nil {
				t.Fatalf("NewRepo: %v", err)
			}

			looseStore := openLooseStore(t, repo)

			quarantine, err := looseStore.BeginObjectQuarantine(store.ObjectQuarantineOptions{})
			if err != nil {
				t.Fatalf("BeginObjectQuarantine: %v", err)
			}

			content := []byte("discarded object\n")

			objectID, err := quarantine.WriteBytesContent(typ.TypeBlob, content)
			if err != nil {
				t.Fatalf("quarantine.WriteBytesContent: %v", err)
			}

			err = quarantine.Discard()
			if err != nil {
				t.Fatalf("Discard: %v", err)
			}

			_, _, err = looseStore.ReadBytesContent(objectID)
			if !errors.Is(err, store.ErrObjectNotFound) {
				t.Fatalf("parent saw discarded object: %v", err)
			}
		})
	}
}