shithub: furgit

ref: df1f2fb3daa1acd25c88510f259d5535fb482126
dir: /internal/testgit/repo_from_fixture.go/

View raw version
package testgit

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

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

// 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)
}