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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
package gitcha
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestGitRepoForPath(t *testing.T) {
if _, err := os.Stat(".git"); os.IsNotExist(err) {
t.Skip("No .git directory, skipping", t.Name())
}
abs, _ := filepath.Abs(".")
tt := []struct {
path string
exp string
}{
{"/", ""},
{".", abs},
{"gitcha.go", abs},
}
for _, test := range tt {
r, err := GitRepoForPath(test.path)
if err != nil {
t.Error(err)
}
if test.exp != r {
t.Errorf("Expected %v, got %v for %s", test.exp, r, test.path)
}
}
}
func TestFindAllFiles(t *testing.T) {
tmp := t.TempDir()
gitignore, err := os.Create(filepath.Join(tmp, ".gitignore"))
if err != nil {
t.Fatal(err)
}
defer gitignore.Close()
_, err = gitignore.WriteString("*.test")
if err != nil {
t.Fatal(err)
}
tt := []struct {
path string
list []string
exp string
}{
{tmp, []string{"*.test"}, "ignore.test"},
}
for _, test := range tt {
f, err := os.Create(filepath.Join(tmp, test.exp))
if err != nil {
t.Fatal(err)
}
defer f.Close()
ch, err := FindAllFiles(test.path, test.list)
if err != nil {
t.Fatal(err)
}
var counter int
for v := range ch {
counter++
if test.exp != v.Info.Name() {
t.Errorf("Expected %v, got %v for %s", test.exp, v.Path, test.path)
}
}
if counter != 1 {
t.Errorf("Expected 1 file found, got %d for %s", counter, test.path)
}
}
}
func TestFindFiles(t *testing.T) {
tlink, err := tempLink(".")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tlink)
tt := []struct {
path string
list []string
exp string
}{
{"../", []string{"gitcha.go"}, "gitcha.go"},
{".", []string{"gitcha_test.go"}, "gitcha_test.go"},
{".", []string{"README.MD"}, "README.md"},
{".", []string{"*.md"}, "README.md"},
{".", []string{"*.MD"}, "README.md"},
{tlink, []string{"gitcha.go"}, "gitcha.go"},
}
for _, test := range tt {
ch, err := FindFiles(test.path, test.list)
if err != nil {
t.Fatal(err)
}
for v := range ch {
var err error
test.exp, err = filepath.Abs(test.exp)
if err != nil {
t.Fatal(err)
}
if test.exp != v.Path {
t.Errorf("Expected %v, got %v for %s", test.exp, v, test.path)
}
}
}
}
func TestFindFirstFile(t *testing.T) {
tt := []struct {
path string
list []string
exp string
expErr bool
}{
{"../", []string{"gitcha.go"}, "gitcha.go", false},
{".", []string{"gitcha_test.go"}, "gitcha_test.go", false},
{".", []string{"README.MD"}, "README.md", false},
{".", []string{"*.md"}, "README.md", false},
{".", []string{"*.MD"}, "README.md", false},
}
for _, test := range tt {
r, err := FindFirstFile(test.path, test.list)
if err != nil && !test.expErr {
t.Error(err)
}
if err == nil && test.expErr {
t.Errorf("Expected error, got none for %s", test.path)
}
if err != nil && test.expErr {
continue
}
test.exp, err = filepath.Abs(test.exp)
if err != nil {
t.Fatal(err)
}
if test.exp != r.Path {
t.Errorf("Expected %v, got %v for %s", test.exp, r, test.path)
}
}
}
// tempLink creates a temporary symbolic link pointing to "dest".
func tempLink(dest string) (string, error) {
// Use tempfile just to create a name and remove it afterwards.
tmp, err := ioutil.TempFile("", "gitcha_test")
if err != nil {
return "", err
}
tmp.Close()
if err := os.Remove(tmp.Name()); err != nil {
return "", err
}
d, err := filepath.Abs(dest)
if err != nil {
return "", err
}
if err := os.Symlink(d, tmp.Name()); err != nil {
return "", err
}
return tmp.Name(), nil
}
|