1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
package command
import (
"io/ioutil"
"os"
"testing"
"time"
"github.com/isacikgoz/gitbatch/core/git"
ggit "gopkg.in/src-d/go-git.v4"
)
var (
testRepoDir, _ = ioutil.TempDir("", "dirty-repo")
)
func TestRun(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatalf("Test Failed.")
}
var tests = []struct {
inp1 string
inp2 string
inp3 []string
}{
{wd, "git", []string{"status"}},
}
for _, test := range tests {
if output, err := Run(test.inp1, test.inp2, test.inp3); err != nil || len(output) <= 0 {
t.Errorf("Test Failed. {%s, %s, %s} inputted, output: %s", test.inp1, test.inp2, test.inp3, output)
}
}
}
func TestReturn(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatalf("Test Failed.")
}
var tests = []struct {
inp1 string
inp2 string
inp3 []string
expected int
}{
{wd, "foo", []string{}, -1},
}
for _, test := range tests {
if output, _ := Return(test.inp1, test.inp2, test.inp3); output != test.expected {
t.Errorf("Test Failed. {%s, %s, %s} inputted, output: %d, expected : %d", test.inp1, test.inp2, test.inp3, output, test.expected)
}
}
}
func TestTrimTrailingNewline(t *testing.T) {
var tests = []struct {
input string
expected string
}{
{"foo", "foo"},
{"foo\n", "foo"},
{"foo\r", "foo"},
}
for _, test := range tests {
if output := trimTrailingNewline(test.input); output != test.expected {
t.Errorf("Test Failed. %s inputted, output: %s, expected: %s", test.input, output, test.expected)
}
}
}
func testRepo() (*git.Repository, error) {
testRepoURL := "https://gitlab.com/isacikgoz/dirty-repo.git"
_, err := ggit.PlainClone(testRepoDir, false, &ggit.CloneOptions{
URL: testRepoURL,
RecurseSubmodules: ggit.DefaultSubmoduleRecursionDepth,
})
time.Sleep(time.Second)
if err != nil && err != ggit.NoErrAlreadyUpToDate {
return nil, err
}
return git.InitializeRepo(testRepoDir)
}
func testFile(name string) (*git.File, error) {
_, err := os.Create(testRepoDir + string(os.PathSeparator) + name)
if err != nil {
return nil, err
}
f := &git.File{
Name: name,
AbsPath: testRepoDir + string(os.PathSeparator) + name,
X: git.StatusUntracked,
Y: git.StatusUntracked,
}
return f, nil
}
func cleanRepo() error {
return os.RemoveAll(testRepoDir)
}
|