File: fixtures_test.go

package info (click to toggle)
git-lfs 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,808 kB
  • sloc: sh: 21,256; makefile: 507; ruby: 417
file content (229 lines) | stat: -rw-r--r-- 5,926 bytes parent folder | download
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package githistory

import (
	"encoding/hex"
	"fmt"
	"io"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"

	"github.com/git-lfs/gitobj/v2"
	"github.com/stretchr/testify/assert"
)

// DatabaseFromFixture returns a *gitobj.ObjectDatabase instance that is safely
// mutable and created from a template equivalent to the fixture that you
// provided it.
//
// If any error was encountered, it will call t.Fatalf() immediately.
func DatabaseFromFixture(t *testing.T, name string) *gitobj.ObjectDatabase {
	path, err := copyToTmp(filepath.Join("fixtures", name))
	if err != nil {
		t.Fatalf("gitobj: could not copy fixture %s: %v", name, err)
	}

	db, err := gitobj.FromFilesystem(filepath.Join(path, "objects"), "")
	if err != nil {
		t.Fatalf("gitobj: could not create object database: %v", err)
	}
	return db
}

// AssertBlobContents asserts that the blob contents given by loading the path
// starting from the root tree "tree" has the given "contents".
func AssertBlobContents(t *testing.T, db *gitobj.ObjectDatabase, tree, path, contents string) {
	// First, load the root tree.
	root, err := db.Tree(HexDecode(t, tree))
	if err != nil {
		t.Fatalf("gitobj: cannot load tree: %s: %s", tree, err)
	}

	// Then, iterating through each part of the filepath (i.e., a/b/c.txt ->
	// []string{"a", "b", "c.txt"}).
	parts := strings.Split(path, "/")
	for i := 0; i < len(parts)-1; i++ {
		part := parts[i]

		// Load the subtree given by that name.
		var subtree *gitobj.Tree
		for _, entry := range root.Entries {
			if entry.Name != part {
				continue
			}

			subtree, err = db.Tree(entry.Oid)
			if err != nil {
				t.Fatalf("gitobj: cannot load subtree %s: %s", filepath.Join(parts[:i]...), err)
			}
			break
		}

		if subtree == nil {
			t.Fatalf("gitobj: subtree %s does not exist", path)
		}

		// And re-assign it to root, creating a sort of pseudo-recursion.
		root = subtree
	}

	filename := parts[len(parts)-1]

	// Find the blob given by the last entry in parts (the filename).
	var blob *gitobj.Blob
	for _, entry := range root.Entries {
		if entry.Name == filename {
			blob, err = db.Blob(entry.Oid)
			if err != nil {
				t.Fatalf("gitobj: cannot load blob %x: %s", entry.Oid, err)
			}
		}
	}

	// If we couldn't find the blob, fail immediately.
	if blob == nil {
		t.Fatalf("gitobj: blob at %s in %s does not exist", path, tree)
	}

	// Perform an assertion on the blob's contents.
	got, err := io.ReadAll(blob.Contents)
	if err != nil {
		t.Fatalf("gitobj: cannot read contents from blob %s: %s", path, err)
	}

	assert.Equal(t, contents, string(got))
}

// AssertCommitParent asserts that the given commit has a parent equivalent to
// the one provided.
func AssertCommitParent(t *testing.T, db *gitobj.ObjectDatabase, sha, parent string) {
	commit, err := db.Commit(HexDecode(t, sha))
	if err != nil {
		t.Fatalf("gitobj: expected to read commit: %s, couldn't: %v", sha, err)
	}

	decoded, err := hex.DecodeString(parent)
	if err != nil {
		t.Fatalf("gitobj: expected to decode parent SHA: %s, couldn't: %v", parent, err)
	}

	assert.Contains(t, commit.ParentIDs, decoded,
		"gitobj: expected parents of commit: %s to contain: %s", sha, parent)
}

// AssertCommitTree asserts that the given commit has a tree equivalent to the
// one provided.
func AssertCommitTree(t *testing.T, db *gitobj.ObjectDatabase, sha, tree string) {
	commit, err := db.Commit(HexDecode(t, sha))
	if err != nil {
		t.Fatalf("gitobj: expected to read commit: %s, couldn't: %v", sha, err)
	}

	decoded, err := hex.DecodeString(tree)
	if err != nil {
		t.Fatalf("gitobj: expected to decode tree SHA: %s, couldn't: %v", tree, err)
	}

	assert.Equal(t, decoded, commit.TreeID, "gitobj: expected tree ID: %s (got: %x)", tree, commit.TreeID)
}

// AssertRef asserts that a given refname points at the expected commit.
func AssertRef(t *testing.T, db *gitobj.ObjectDatabase, ref string, expected []byte) {
	root, ok := db.Root()
	assert.True(t, ok, "gitobj: expected *odb.ObjectDatabase to have Root()")

	cmd := exec.Command("git", "rev-parse", ref)
	cmd.Dir = root
	out, err := cmd.Output()

	assert.Nil(t, err)

	assert.Equal(t, hex.EncodeToString(expected), strings.TrimSpace(string(out)))
}

// HexDecode decodes the given ASCII hex-encoded string into []byte's, or fails
// the test immediately if the given "sha" wasn't a valid hex-encoded sequence.
func HexDecode(t *testing.T, sha string) []byte {
	b, err := hex.DecodeString(sha)
	if err != nil {
		t.Fatalf("gitobj: could not decode string: %q, %v", sha, err)
	}

	return b
}

// copyToTmp copies the given fixture to a folder in /tmp.
func copyToTmp(fixture string) (string, error) {
	p, err := os.MkdirTemp("", fmt.Sprintf("git-lfs-fixture-%s", filepath.Dir(fixture)))
	if err != nil {
		return "", err
	}

	if err = copyDir(fixture, p); err != nil {
		return "", err
	}
	return p, nil
}

// copyDir copies a directory (and recursively all files and subdirectories)
// from "from" to "to" preserving permissions and ownership.
func copyDir(from, to string) error {
	stat, err := os.Stat(from)
	if err != nil {
		return err
	}

	if err := os.MkdirAll(to, stat.Mode()); err != nil {
		return err
	}

	entries, err := os.ReadDir(from)
	if err != nil {
		return err
	}

	for _, entry := range entries {
		sp := filepath.Join(from, entry.Name())
		dp := filepath.Join(to, entry.Name())

		if entry.IsDir() {
			err = copyDir(sp, dp)
		} else {
			err = copyFile(sp, dp)
		}

		if err != nil {
			return err
		}
	}
	return nil
}

// copyFile copies a file from "from" to "to" preserving permissions and
// ownership.
func copyFile(from, to string) error {
	src, err := os.Open(from)
	if err != nil {
		return err
	}
	defer src.Close()

	dst, err := os.Create(to)
	if err != nil {
		return err
	}
	defer dst.Close()

	if _, err = io.Copy(dst, src); err != nil {
		return err
	}

	stat, err := os.Stat(from)
	if err != nil {
		return err
	}

	return os.Chmod(to, stat.Mode())
}