mirror of
https://github.com/grdl/git-get.git
synced 2026-02-06 04:07:57 +00:00
Add a run package responsible for running git commands
- Add better git error handling - Move repo helpers into a separate package
This commit is contained in:
73
pkg/test/helpers.go
Normal file
73
pkg/test/helpers.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git-get/pkg/io"
|
||||
"git-get/pkg/run"
|
||||
"path"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func (r *Repo) writeFile(filename string, content string) {
|
||||
path := path.Join(r.path, filename)
|
||||
err := io.Write(path, content)
|
||||
checkFatal(r.t, err)
|
||||
}
|
||||
|
||||
func (r *Repo) init() {
|
||||
err := run.Git("init", "--quiet", r.path).AndShutUp()
|
||||
checkFatal(r.t, err)
|
||||
}
|
||||
|
||||
func (r *Repo) stageFile(path string) {
|
||||
err := run.Git("add", path).OnRepo(r.path).AndShutUp()
|
||||
checkFatal(r.t, err)
|
||||
}
|
||||
|
||||
func (r *Repo) commit(msg string) {
|
||||
err := run.Git("commit", "-m", fmt.Sprintf("%q", msg), "--author=\"user <user@example.com>\"").OnRepo(r.path).AndShutUp()
|
||||
checkFatal(r.t, err)
|
||||
}
|
||||
|
||||
func (r *Repo) branch(name string) {
|
||||
err := run.Git("branch", name).OnRepo(r.path).AndShutUp()
|
||||
checkFatal(r.t, err)
|
||||
}
|
||||
|
||||
func (r *Repo) tag(name string) {
|
||||
err := run.Git("tag", "-a", name, "-m", name).OnRepo(r.path).AndShutUp()
|
||||
checkFatal(r.t, err)
|
||||
}
|
||||
|
||||
func (r *Repo) checkout(name string) {
|
||||
err := run.Git("checkout", name).OnRepo(r.path).AndShutUp()
|
||||
checkFatal(r.t, err)
|
||||
}
|
||||
|
||||
func (r *Repo) clone() *Repo {
|
||||
dir, err := io.TempDir()
|
||||
checkFatal(r.t, err)
|
||||
|
||||
url := fmt.Sprintf("file://%s/.git", r.path)
|
||||
err = run.Git("clone", url, dir).AndShutUp()
|
||||
checkFatal(r.t, err)
|
||||
|
||||
clone := &Repo{
|
||||
path: dir,
|
||||
t: r.t,
|
||||
}
|
||||
|
||||
clone.t.Cleanup(r.cleanup)
|
||||
return clone
|
||||
}
|
||||
|
||||
func (r *Repo) fetch() {
|
||||
err := run.Git("fetch", "--all").OnRepo(r.path).AndShutUp()
|
||||
checkFatal(r.t, err)
|
||||
}
|
||||
|
||||
func checkFatal(t *testing.T, err error) {
|
||||
if err != nil {
|
||||
t.Fatalf("failed making test repo: %+v", err)
|
||||
}
|
||||
}
|
||||
168
pkg/test/testrepos.go
Normal file
168
pkg/test/testrepos.go
Normal file
@@ -0,0 +1,168 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"git-get/pkg/io"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Repo represents a test repository.
|
||||
// It embeds testing.T so that any error thrown while creating a test repo will cause a t.Fatal call.
|
||||
type Repo struct {
|
||||
path string
|
||||
t *testing.T
|
||||
}
|
||||
|
||||
func (r *Repo) Path() string {
|
||||
return r.path
|
||||
}
|
||||
|
||||
// TODO: this should be a method of a tempDir, not a repo
|
||||
// Automatically remove test repo when the test is over
|
||||
func (r *Repo) cleanup() {
|
||||
err := os.RemoveAll(r.path)
|
||||
if err != nil {
|
||||
r.t.Errorf("failed removing test repo directory %s", r.path)
|
||||
}
|
||||
}
|
||||
|
||||
func RepoEmpty(t *testing.T) *Repo {
|
||||
dir, err := io.TempDir()
|
||||
checkFatal(t, err)
|
||||
|
||||
r := &Repo{
|
||||
path: dir,
|
||||
t: t,
|
||||
}
|
||||
|
||||
t.Cleanup(r.cleanup)
|
||||
|
||||
r.init()
|
||||
return r
|
||||
}
|
||||
|
||||
func RepoWithUntracked(t *testing.T) *Repo {
|
||||
r := RepoEmpty(t)
|
||||
r.writeFile("README.md", "I'm a readme file")
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func RepoWithStaged(t *testing.T) *Repo {
|
||||
r := RepoEmpty(t)
|
||||
r.writeFile("README.md", "I'm a readme file")
|
||||
r.stageFile("README.md")
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func RepoWithCommit(t *testing.T) *Repo {
|
||||
r := RepoEmpty(t)
|
||||
r.writeFile("README.md", "I'm a readme file")
|
||||
r.stageFile("README.md")
|
||||
r.commit("Initial commit")
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func RepoWithUncommittedAndUntracked(t *testing.T) *Repo {
|
||||
r := RepoEmpty(t)
|
||||
r.writeFile("README.md", "I'm a readme file")
|
||||
r.stageFile("README.md")
|
||||
r.commit("Initial commit")
|
||||
r.writeFile("README.md", "These changes won't be committed")
|
||||
r.writeFile("untracked.txt", "I'm untracked")
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func RepoWithBranch(t *testing.T) *Repo {
|
||||
r := RepoWithCommit(t)
|
||||
r.branch("feature/branch")
|
||||
r.checkout("feature/branch")
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func RepoWithTag(t *testing.T) *Repo {
|
||||
r := RepoWithCommit(t)
|
||||
r.tag("v0.0.1")
|
||||
r.checkout("v0.0.1")
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func RepoWithBranchWithUpstream(t *testing.T) *Repo {
|
||||
origin := RepoWithCommit(t)
|
||||
origin.branch("feature/branch")
|
||||
|
||||
r := origin.clone()
|
||||
r.checkout("feature/branch")
|
||||
return r
|
||||
}
|
||||
|
||||
func RepoWithBranchWithoutUpstream(t *testing.T) *Repo {
|
||||
origin := RepoWithCommit(t)
|
||||
|
||||
r := origin.clone()
|
||||
r.branch("feature/branch")
|
||||
r.checkout("feature/branch")
|
||||
return r
|
||||
}
|
||||
|
||||
func RepoWithBranchAhead(t *testing.T) *Repo {
|
||||
origin := RepoWithCommit(t)
|
||||
origin.branch("feature/branch")
|
||||
|
||||
r := origin.clone()
|
||||
r.checkout("feature/branch")
|
||||
|
||||
r.writeFile("local.new", "local.new")
|
||||
r.stageFile("local.new")
|
||||
r.commit("local.new")
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func RepoWithBranchBehind(t *testing.T) *Repo {
|
||||
origin := RepoWithCommit(t)
|
||||
origin.branch("feature/branch")
|
||||
origin.checkout("feature/branch")
|
||||
|
||||
r := origin.clone()
|
||||
r.checkout("feature/branch")
|
||||
|
||||
origin.writeFile("origin.new", "origin.new")
|
||||
origin.stageFile("origin.new")
|
||||
origin.commit("origin.new")
|
||||
|
||||
r.fetch()
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// returns a repo with 2 commits ahead and 1 behind
|
||||
func RepoWithBranchAheadAndBehind(t *testing.T) *Repo {
|
||||
origin := RepoWithCommit(t)
|
||||
origin.branch("feature/branch")
|
||||
origin.checkout("feature/branch")
|
||||
|
||||
r := origin.clone()
|
||||
r.checkout("feature/branch")
|
||||
|
||||
origin.writeFile("origin.new", "origin.new")
|
||||
origin.stageFile("origin.new")
|
||||
origin.commit("origin.new")
|
||||
|
||||
r.writeFile("local.new", "local.new")
|
||||
r.stageFile("local.new")
|
||||
r.commit("local.new")
|
||||
|
||||
r.writeFile("local.new2", "local.new2")
|
||||
r.stageFile("local.new2")
|
||||
r.commit("local.new2")
|
||||
|
||||
r.fetch()
|
||||
|
||||
return r
|
||||
}
|
||||
Reference in New Issue
Block a user