mirror of
https://github.com/grdl/git-get.git
synced 2026-02-06 06:27:58 +00:00
Refactor repoFinder
- Remove io package and move finder to git package - Move tempDir and writeFile into test package. They are only used for testing purposes anyway. - Add a way to specify parent folder for tempDir. Useful for testing nested repos. - Add new test repos with .git/config files
This commit is contained in:
@@ -2,20 +2,26 @@ package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git-get/pkg/io"
|
||||
"git-get/pkg/run"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func (r *Repo) writeFile(filename string, content string) {
|
||||
path := filepath.Join(r.path, filename)
|
||||
err := io.Write(path, content)
|
||||
func (r *Repo) init() {
|
||||
err := run.Git("init", "--quiet", r.path).AndShutUp()
|
||||
checkFatal(r.t, err)
|
||||
}
|
||||
|
||||
func (r *Repo) init() {
|
||||
err := run.Git("init", "--quiet", r.path).AndShutUp()
|
||||
// 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)
|
||||
}
|
||||
|
||||
@@ -45,7 +51,7 @@ func (r *Repo) checkout(name string) {
|
||||
}
|
||||
|
||||
func (r *Repo) clone() *Repo {
|
||||
dir, err := io.TempDir()
|
||||
dir, err := tempDir("")
|
||||
checkFatal(r.t, err)
|
||||
|
||||
url := fmt.Sprintf("file://%s/.git", r.path)
|
||||
@@ -66,6 +72,14 @@ func (r *Repo) fetch() {
|
||||
checkFatal(r.t, err)
|
||||
}
|
||||
|
||||
// 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(parent string) (string, error) {
|
||||
dir, err := ioutil.TempDir(parent, "git-get-repo-")
|
||||
|
||||
return dir, err
|
||||
}
|
||||
|
||||
func checkFatal(t *testing.T, err error) {
|
||||
if err != nil {
|
||||
t.Fatalf("failed making test repo: %+v", err)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"git-get/pkg/io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -29,7 +29,7 @@ func (r *Repo) cleanup() {
|
||||
|
||||
// RepoEmpty creates an empty git repo.
|
||||
func RepoEmpty(t *testing.T) *Repo {
|
||||
dir, err := io.TempDir()
|
||||
dir, err := tempDir("")
|
||||
checkFatal(t, err)
|
||||
|
||||
r := &Repo{
|
||||
@@ -178,3 +178,26 @@ func RepoWithBranchAheadAndBehind(t *testing.T) *Repo {
|
||||
|
||||
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