mirror of
https://github.com/grdl/git-get.git
synced 2026-02-05 01:29:42 +00:00
Move test package inside the git package
It is not being used by other packages so it should belong to `git` package.
This commit is contained in:
94
pkg/git/test/helpers.go
Normal file
94
pkg/git/test/helpers.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git-get/pkg/run"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TempDir creates a temporary directory inside the parent dir.
|
||||
// If parent is empty, it will use a system default temp dir (usually /tmp).
|
||||
func TempDir(t *testing.T, parent string) string {
|
||||
dir, err := ioutil.TempDir(parent, "git-get-repo-")
|
||||
checkFatal(t, err)
|
||||
|
||||
// Automatically remove temp dir when the test is over.
|
||||
t.Cleanup(func() {
|
||||
err := os.RemoveAll(dir)
|
||||
if err != nil {
|
||||
t.Errorf("failed removing test repo %s", dir)
|
||||
}
|
||||
})
|
||||
|
||||
return dir
|
||||
}
|
||||
|
||||
func (r *Repo) init() {
|
||||
err := run.Git("init", "--quiet", r.path).AndShutUp()
|
||||
checkFatal(r.t, err)
|
||||
}
|
||||
|
||||
// writeFile writes the content string into a file. If file doesn't exists, it will create it.
|
||||
func (r *Repo) writeFile(filename string, content string) {
|
||||
path := filepath.Join(r.path, filename)
|
||||
|
||||
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
checkFatal(r.t, err)
|
||||
|
||||
_, err = file.Write([]byte(content))
|
||||
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 := TempDir(r.t, "")
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
193
pkg/git/test/testrepos.go
Normal file
193
pkg/git/test/testrepos.go
Normal file
@@ -0,0 +1,193 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"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
|
||||
}
|
||||
|
||||
// Path returns path to a repository.
|
||||
func (r *Repo) Path() string {
|
||||
return r.path
|
||||
}
|
||||
|
||||
// RepoEmpty creates an empty git repo.
|
||||
func RepoEmpty(t *testing.T) *Repo {
|
||||
return RepoEmptyInDir(t, "")
|
||||
}
|
||||
|
||||
// RepoEmptyInDir creates an empty git repo inside a given parent dir.
|
||||
func RepoEmptyInDir(t *testing.T, parent string) *Repo {
|
||||
r := &Repo{
|
||||
path: TempDir(t, parent),
|
||||
t: t,
|
||||
}
|
||||
|
||||
r.init()
|
||||
return r
|
||||
}
|
||||
|
||||
// RepoWithUntracked creates a git repo with a single untracked file.
|
||||
func RepoWithUntracked(t *testing.T) *Repo {
|
||||
r := RepoEmpty(t)
|
||||
r.writeFile("README.md", "I'm a readme file")
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// RepoWithStaged creates a git repo with a single staged file.
|
||||
func RepoWithStaged(t *testing.T) *Repo {
|
||||
r := RepoEmpty(t)
|
||||
r.writeFile("README.md", "I'm a readme file")
|
||||
r.stageFile("README.md")
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// RepoWithCommit creates a git repo with a single commit.
|
||||
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
|
||||
}
|
||||
|
||||
// RepoWithUncommittedAndUntracked creates a git repo with one staged but uncommitted file and one untracked file.
|
||||
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
|
||||
}
|
||||
|
||||
// RepoWithBranch creates a git repo with a new branch.
|
||||
func RepoWithBranch(t *testing.T) *Repo {
|
||||
r := RepoWithCommit(t)
|
||||
r.branch("feature/branch")
|
||||
r.checkout("feature/branch")
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// RepoWithTag creates a git repo with a new tag.
|
||||
func RepoWithTag(t *testing.T) *Repo {
|
||||
r := RepoWithCommit(t)
|
||||
r.tag("v0.0.1")
|
||||
r.checkout("v0.0.1")
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// RepoWithBranchWithUpstream creates a git repo by cloning another repo and checking out a remote branch.
|
||||
func RepoWithBranchWithUpstream(t *testing.T) *Repo {
|
||||
origin := RepoWithCommit(t)
|
||||
origin.branch("feature/branch")
|
||||
|
||||
r := origin.clone()
|
||||
r.checkout("feature/branch")
|
||||
return r
|
||||
}
|
||||
|
||||
// RepoWithBranchWithoutUpstream creates a git repo by cloning another repo and checking out a new local branch.
|
||||
func RepoWithBranchWithoutUpstream(t *testing.T) *Repo {
|
||||
origin := RepoWithCommit(t)
|
||||
|
||||
r := origin.clone()
|
||||
r.branch("feature/branch")
|
||||
r.checkout("feature/branch")
|
||||
return r
|
||||
}
|
||||
|
||||
// RepoWithBranchAhead creates a git repo with a branch being ahead of a remote branch by 1 commit.
|
||||
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
|
||||
}
|
||||
|
||||
// RepoWithBranchBehind creates a git repo with a branch being behind a remote branch by 1 commit.
|
||||
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
|
||||
}
|
||||
|
||||
// RepoWithBranchAheadAndBehind creates a git repo with a branch being 2 commits ahead and 1 behind a remote branch.
|
||||
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
|
||||
}
|
||||
|
||||
// RepoWithEmptyConfig creates a git repo with empty .git/config file
|
||||
func RepoWithEmptyConfig(t *testing.T) *Repo {
|
||||
r := RepoEmpty(t)
|
||||
r.writeFile(filepath.Join(".git", "config"), "")
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// RepoWithValidConfig creates a git repo with valid content in .git/config file
|
||||
func RepoWithValidConfig(t *testing.T) *Repo {
|
||||
r := RepoEmpty(t)
|
||||
|
||||
gitconfig := `
|
||||
[user]
|
||||
name = grdl
|
||||
[gitget]
|
||||
host = github.com
|
||||
`
|
||||
r.writeFile(filepath.Join(".git", "config"), gitconfig)
|
||||
|
||||
return r
|
||||
}
|
||||
Reference in New Issue
Block a user