aboutsummaryrefslogtreecommitdiff
path: root/repository/write_loose_test.go
blob: d1fa479c34ebbf592580540cea5bbe14ffb3d1a7 (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
package repository_test

import (
	"bytes"
	"testing"

	"codeberg.org/lindenii/furgit/internal/testgit"
	objectid "codeberg.org/lindenii/furgit/object/id"
	objecttype "codeberg.org/lindenii/furgit/object/type"
)

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

	testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
		repoHarness := testgit.NewRepo(t, testgit.RepoOptions{
			ObjectFormat: algo,
			Bare:         true,
			RefFormat:    "files",
		})

		repo := repoHarness.OpenRepository(t)

		content := []byte("write-loose-bytes-content\n")

		gotID, err := repo.Objects().WriteBytesContent(objecttype.TypeBlob, content)
		if err != nil {
			t.Fatalf("WriteLooseBytesContent: %v", err)
		}

		wantID := repoHarness.HashObject(t, "blob", content)
		if gotID != wantID {
			t.Fatalf("WriteLooseBytesContent id = %s, want %s", gotID, wantID)
		}

		ty, gotContent, err := repo.Objects().ReadBytesContent(gotID)
		if err != nil {
			t.Fatalf("ReadStoredBytesContent: %v", err)
		}

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

		if !bytes.Equal(gotContent, content) {
			t.Fatalf("ReadStoredBytesContent content mismatch")
		}
	})
}

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

	testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
		repoHarness := testgit.NewRepo(t, testgit.RepoOptions{
			ObjectFormat: algo,
			Bare:         true,
			RefFormat:    "files",
		})

		repo := repoHarness.OpenRepository(t)

		content := []byte("write-loose-reader-content\n")

		gotID, err := repo.Objects().WriteReaderContent(objecttype.TypeBlob, int64(len(content)), bytes.NewReader(content))
		if err != nil {
			t.Fatalf("WriteLooseReaderContent: %v", err)
		}

		wantID := repoHarness.HashObject(t, "blob", content)
		if gotID != wantID {
			t.Fatalf("WriteLooseReaderContent id = %s, want %s", gotID, wantID)
		}
	})
}

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

	testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
		repoHarness := testgit.NewRepo(t, testgit.RepoOptions{
			ObjectFormat: algo,
			Bare:         true,
			RefFormat:    "files",
		})
		_, _, commitID := repoHarness.MakeCommit(t, "write-loose-full")

		repo := repoHarness.OpenRepository(t)

		raw, err := repo.Objects().ReadBytesFull(commitID)
		if err != nil {
			t.Fatalf("ReadStoredBytesFull: %v", err)
		}

		idFromBytes, err := repo.Objects().WriteBytesFull(raw)
		if err != nil {
			t.Fatalf("WriteLooseBytesFull: %v", err)
		}

		if idFromBytes != commitID {
			t.Fatalf("WriteLooseBytesFull id = %s, want %s", idFromBytes, commitID)
		}

		idFromReader, err := repo.Objects().WriteReaderFull(bytes.NewReader(raw))
		if err != nil {
			t.Fatalf("WriteLooseReaderFull: %v", err)
		}

		if idFromReader != commitID {
			t.Fatalf("WriteLooseReaderFull id = %s, want %s", idFromReader, commitID)
		}
	})
}