aboutsummaryrefslogtreecommitdiff
path: root/internal/testgit/repo_from_fixture.go
blob: 887bf9a3c712bdf15e6930a63d333f5baba1cb65 (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
package testgit

import (
	"io/fs"
	"os"
	"testing"

	"codeberg.org/lindenii/furgit/objectid"
)

// NewRepoFromFixture copies one existing repository fixture into a temp dir.
func NewRepoFromFixture(tb testing.TB, algo objectid.Algorithm, fixtureDir string) *TestRepo {
	tb.Helper()

	if algo.Size() == 0 {
		tb.Fatalf("invalid algorithm: %v", algo)
	}

	dst := tb.TempDir()
	srcFS := os.DirFS(fixtureDir)
	err := copyFS(dst, srcFS)
	if err != nil {
		tb.Fatalf("copy fixture %q: %v", fixtureDir, err)
	}

	return &TestRepo{
		dir:  dst,
		algo: algo,
		env:  defaultEnv(),
	}
}

func copyFS(dst string, src fs.FS) error {
	return os.CopyFS(dst, src)
}