mirror of
https://github.com/grdl/git-get.git
synced 2026-02-08 06:59:15 +00:00
Rename test helpers file
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/libgit2/git2go/v30"
|
"fmt"
|
||||||
|
|
||||||
|
git "github.com/libgit2/git2go/v30"
|
||||||
)
|
)
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
options := &git.CloneOptions{
|
options := &git.CloneOptions{
|
||||||
|
|||||||
125
git-get_test.go
125
git-get_test.go
@@ -1,125 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
|
|
||||||
git "github.com/libgit2/git2go/v30"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
ReadmeFile = "README.md"
|
|
||||||
ReadmeContent = "I'm a readme file\n"
|
|
||||||
CommitterName = "Some Guy"
|
|
||||||
CommitterEmail = "someguy@example.com"
|
|
||||||
)
|
|
||||||
|
|
||||||
func cleanupRepo(t *testing.T, repo *git.Repository) {
|
|
||||||
err := os.RemoveAll(repo.Workdir())
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("failed cleaning up repo")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTempRepo() (*git.Repository, error) {
|
|
||||||
dir, err := ioutil.TempDir("", "test-repo-")
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "failed creating a temp repo")
|
|
||||||
}
|
|
||||||
|
|
||||||
repo, err := git.InitRepository(dir, false)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "failed initializing a temp repo")
|
|
||||||
}
|
|
||||||
|
|
||||||
return repo, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTempRepoWithUntracked() (*git.Repository, error) {
|
|
||||||
repo, err := newTempRepo()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = ioutil.WriteFile(path.Join(repo.Workdir(), ReadmeFile), []byte(ReadmeContent), 0644)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "failed writing a file")
|
|
||||||
}
|
|
||||||
|
|
||||||
return repo, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTempRepoWithStaged() (*git.Repository, error) {
|
|
||||||
repo, err := newTempRepoWithUntracked()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
index, err := repo.Index()
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "failed getting repo index")
|
|
||||||
}
|
|
||||||
|
|
||||||
err = index.AddByPath(ReadmeFile)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "failed adding file to index")
|
|
||||||
}
|
|
||||||
|
|
||||||
err = index.Write()
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "failed writing index")
|
|
||||||
}
|
|
||||||
|
|
||||||
return repo, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTempRepoWithCommit() (*git.Repository, error) {
|
|
||||||
repo, err := newTempRepoWithStaged()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
index, err := repo.Index()
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "failed creating a temp repo")
|
|
||||||
}
|
|
||||||
|
|
||||||
treeId, err := index.WriteTree()
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "failed building tree from index")
|
|
||||||
}
|
|
||||||
|
|
||||||
tree, err := repo.LookupTree(treeId)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "failed looking up tree")
|
|
||||||
}
|
|
||||||
|
|
||||||
signature := &git.Signature{
|
|
||||||
Name: CommitterName,
|
|
||||||
Email: CommitterEmail,
|
|
||||||
When: time.Date(2000, 01, 01, 16, 00, 00, 0, time.UTC),
|
|
||||||
}
|
|
||||||
message := "Initial commit"
|
|
||||||
|
|
||||||
_, err = repo.CreateCommit("HEAD", signature, signature, message, tree)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "failed creating commit")
|
|
||||||
}
|
|
||||||
|
|
||||||
return repo, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCreatingRepoWithCommit(t *testing.T) {
|
|
||||||
repo, err := newTempRepoWithCommit()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed creating test repository")
|
|
||||||
}
|
|
||||||
defer cleanupRepo(t, repo)
|
|
||||||
fmt.Println(repo.Path())
|
|
||||||
}
|
|
||||||
2
go.mod
2
go.mod
@@ -4,7 +4,7 @@ go 1.14
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/libgit2/git2go/v30 v30.0.3
|
github.com/libgit2/git2go/v30 v30.0.3
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1
|
||||||
)
|
)
|
||||||
|
|
||||||
replace github.com/libgit2/git2go/v30 => ./static/git2go
|
replace github.com/libgit2/git2go/v30 => ./static/git2go
|
||||||
|
|||||||
98
helpers_test.go
Normal file
98
helpers_test.go
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
|
git "github.com/libgit2/git2go/v30"
|
||||||
|
)
|
||||||
|
|
||||||
|
func checkFatal(t *testing.T, err error) {
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%+v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func cleanupRepo(t *testing.T, repo *git.Repository) {
|
||||||
|
err := os.RemoveAll(repo.Workdir())
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed cleaning up repo")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTestRepo() (*git.Repository, error) {
|
||||||
|
dir, err := ioutil.TempDir("", "test-repo-")
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "failed creating a temp repo")
|
||||||
|
}
|
||||||
|
|
||||||
|
repo, err := git.InitRepository(dir, false)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "failed initializing a temp repo")
|
||||||
|
}
|
||||||
|
|
||||||
|
return repo, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func createFile(repo *git.Repository, name string) error {
|
||||||
|
err := ioutil.WriteFile(path.Join(repo.Workdir(), name), []byte("I'm a file"), 0644)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed writing a file")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func stageFile(repo *git.Repository, name string) error {
|
||||||
|
index, err := repo.Index()
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed getting repo index")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = index.AddByPath(name)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed adding file to index")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = index.Write()
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed writing index")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func createCommit(repo *git.Repository, message string) error {
|
||||||
|
index, err := repo.Index()
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed creating a temp repo")
|
||||||
|
}
|
||||||
|
|
||||||
|
treeId, err := index.WriteTree()
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed building tree from index")
|
||||||
|
}
|
||||||
|
|
||||||
|
tree, err := repo.LookupTree(treeId)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed looking up tree")
|
||||||
|
}
|
||||||
|
|
||||||
|
signature := &git.Signature{
|
||||||
|
Name: "Some Guy",
|
||||||
|
Email: "someguy@example.com",
|
||||||
|
When: time.Date(2000, 01, 01, 16, 00, 00, 0, time.UTC),
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = repo.CreateCommit("HEAD", signature, signature, message, tree)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed creating commit")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user