2014-06-04 17:40:09 +00:00
|
|
|
package commands
|
2014-05-31 00:00:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/bmizerany/assert"
|
2014-06-03 15:11:31 +00:00
|
|
|
"github.com/github/git-media/gitmedia"
|
2014-06-04 18:47:11 +00:00
|
|
|
"io"
|
2014-05-31 00:00:29 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
Root string
|
|
|
|
Bin string
|
|
|
|
TempDir string
|
|
|
|
GitEnv []string
|
|
|
|
JoinedGitEnv string
|
|
|
|
Version = gitmedia.Version
|
2014-06-11 18:24:54 +00:00
|
|
|
configKeys = [...]string{"Endpoint", "LocalWorkingDir", "LocalGitDir", "LocalMediaDir", "TempDir"}
|
2014-05-31 00:00:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewRepository(t *testing.T, name string) *Repository {
|
|
|
|
path := filepath.Join(TempDir, name)
|
|
|
|
r := &Repository{
|
|
|
|
T: t,
|
|
|
|
Name: name,
|
|
|
|
Path: path,
|
|
|
|
Paths: []string{path},
|
2014-06-04 17:40:09 +00:00
|
|
|
Commands: make([]*TestCommand, 0),
|
2014-05-31 00:00:29 +00:00
|
|
|
}
|
|
|
|
r.clone()
|
|
|
|
r.Path = expand(path)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func AssertIncludeString(t *testing.T, expected string, actual []string) {
|
|
|
|
found := false
|
|
|
|
for _, line := range actual {
|
|
|
|
if line == expected {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Tf(t, found, "%s not included.", expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GlobalGitConfig(t *testing.T) []string {
|
|
|
|
o := cmd(t, "git", "config", "-l", "--global")
|
|
|
|
return strings.Split(o, "\n")
|
|
|
|
}
|
|
|
|
|
2014-06-04 17:40:09 +00:00
|
|
|
func SetConfigOutput(c *TestCommand, keys map[string]string) {
|
2014-05-31 00:00:29 +00:00
|
|
|
pieces := make([]string, 0, len(keys))
|
2014-06-11 18:24:54 +00:00
|
|
|
|
|
|
|
for _, key := range configKeys {
|
|
|
|
if v, ok := keys[key]; ok {
|
|
|
|
pieces = append(pieces, key+"="+v)
|
|
|
|
}
|
2014-05-31 00:00:29 +00:00
|
|
|
}
|
2014-06-11 18:24:54 +00:00
|
|
|
|
2014-05-31 00:00:29 +00:00
|
|
|
c.Output = strings.Join(pieces, "\n")
|
|
|
|
|
|
|
|
if len(JoinedGitEnv) > 0 {
|
|
|
|
c.Output += "\n" + JoinedGitEnv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Repository struct {
|
|
|
|
T *testing.T
|
|
|
|
Name string
|
|
|
|
Path string
|
|
|
|
Paths []string
|
2014-06-04 17:40:09 +00:00
|
|
|
Commands []*TestCommand
|
2014-05-31 00:00:29 +00:00
|
|
|
expandedTempPath bool
|
|
|
|
}
|
|
|
|
|
2014-06-04 17:44:07 +00:00
|
|
|
func (r *Repository) AddPath(paths ...string) {
|
|
|
|
r.Paths = append(r.Paths, filepath.Join(paths...))
|
2014-05-31 00:00:29 +00:00
|
|
|
}
|
|
|
|
|
2014-06-04 17:40:09 +00:00
|
|
|
func (r *Repository) Command(args ...string) *TestCommand {
|
|
|
|
cmd := &TestCommand{
|
2014-05-31 00:00:29 +00:00
|
|
|
T: r.T,
|
|
|
|
Args: args,
|
|
|
|
BeforeCallbacks: make([]func(), 0),
|
|
|
|
AfterCallbacks: make([]func(), 0),
|
|
|
|
}
|
|
|
|
r.Commands = append(r.Commands, cmd)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Repository) ReadFile(paths ...string) string {
|
|
|
|
args := make([]string, 1, len(paths)+1)
|
|
|
|
args[0] = r.Path
|
|
|
|
args = append(args, paths...)
|
|
|
|
by, err := ioutil.ReadFile(filepath.Join(args...))
|
|
|
|
assert.Equal(r.T, nil, err)
|
|
|
|
return string(by)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Repository) WriteFile(filename, output string) {
|
|
|
|
r.e(ioutil.WriteFile(filename, []byte(output), 0755))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Repository) MediaCmd(args ...string) string {
|
|
|
|
return r.cmd(Bin, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Repository) Test() {
|
|
|
|
for _, path := range r.Paths {
|
|
|
|
r.test(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Repository) test(path string) {
|
2014-06-04 17:40:09 +00:00
|
|
|
fmt.Println("Command tests for\n", path)
|
2014-05-31 00:00:29 +00:00
|
|
|
for _, cmd := range r.Commands {
|
|
|
|
r.clone()
|
2014-06-05 23:13:42 +00:00
|
|
|
cmd.Run(path)
|
2014-05-31 00:00:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Repository) clone() {
|
|
|
|
clone(r.T, r.Name, r.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Repository) e(err error) {
|
|
|
|
e(r.T, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Repository) cmd(name string, args ...string) string {
|
|
|
|
return cmd(r.T, name, args...)
|
|
|
|
}
|
|
|
|
|
2014-06-04 17:40:09 +00:00
|
|
|
type TestCommand struct {
|
2014-05-31 00:00:29 +00:00
|
|
|
T *testing.T
|
|
|
|
Args []string
|
2014-06-04 18:47:11 +00:00
|
|
|
Input io.Reader
|
2014-05-31 00:00:29 +00:00
|
|
|
Output string
|
|
|
|
BeforeCallbacks []func()
|
|
|
|
AfterCallbacks []func()
|
|
|
|
}
|
|
|
|
|
2014-06-05 23:13:42 +00:00
|
|
|
func (c *TestCommand) Run(path string) {
|
2014-05-31 00:00:29 +00:00
|
|
|
fmt.Println("$ git media", strings.Join(c.Args, " "))
|
|
|
|
|
|
|
|
for _, cb := range c.BeforeCallbacks {
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
|
2014-06-05 23:13:42 +00:00
|
|
|
c.e(os.Chdir(path))
|
|
|
|
|
2014-05-31 00:00:29 +00:00
|
|
|
cmd := exec.Command(Bin, c.Args...)
|
2014-06-04 18:47:11 +00:00
|
|
|
cmd.Stdin = c.Input
|
2014-06-05 18:48:23 +00:00
|
|
|
outputBytes, err := cmd.CombinedOutput()
|
2014-05-31 00:00:29 +00:00
|
|
|
c.e(err)
|
|
|
|
|
|
|
|
if len(c.Output) > 0 {
|
|
|
|
assert.Equal(c.T, c.Output+"\n", string(outputBytes))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cb := range c.AfterCallbacks {
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 17:40:09 +00:00
|
|
|
func (c *TestCommand) Before(f func()) {
|
2014-05-31 00:00:29 +00:00
|
|
|
c.BeforeCallbacks = append(c.BeforeCallbacks, f)
|
|
|
|
}
|
|
|
|
|
2014-06-04 17:40:09 +00:00
|
|
|
func (c *TestCommand) After(f func()) {
|
2014-05-31 00:00:29 +00:00
|
|
|
c.AfterCallbacks = append(c.AfterCallbacks, f)
|
|
|
|
}
|
|
|
|
|
2014-06-04 17:40:09 +00:00
|
|
|
func (c *TestCommand) e(err error) {
|
2014-05-31 00:00:29 +00:00
|
|
|
e(c.T, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmd(t *testing.T, name string, args ...string) string {
|
|
|
|
cmd := exec.Command(name, args...)
|
|
|
|
o, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(
|
|
|
|
"Error running command:\n$ %s\n\n%s",
|
|
|
|
strings.Join(cmd.Args, " "),
|
|
|
|
string(o),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return string(o)
|
|
|
|
}
|
|
|
|
|
|
|
|
func e(t *testing.T, err error) {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func expand(path string) string {
|
|
|
|
p, err := filepath.EvalSymlinks(path)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func clone(t *testing.T, name, path string) {
|
|
|
|
e(t, os.RemoveAll(path))
|
|
|
|
|
2014-06-04 17:40:09 +00:00
|
|
|
reposPath := filepath.Join(Root, "commands", "repos")
|
2014-05-31 00:00:29 +00:00
|
|
|
e(t, os.Chdir(reposPath))
|
|
|
|
cmd(t, "git", "clone", name, path)
|
|
|
|
e(t, os.Chdir(path))
|
|
|
|
cmd(t, "git", "remote", "remove", "origin")
|
|
|
|
cmd(t, "git", "remote", "add", "origin", "https://example.com/git/media")
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
Root = filepath.Join(wd, "..")
|
|
|
|
Bin = filepath.Join(Root, "bin", "git-media")
|
|
|
|
TempDir = filepath.Join(os.TempDir(), "git-media-tests")
|
|
|
|
|
|
|
|
env := os.Environ()
|
|
|
|
GitEnv = make([]string, 0, len(env))
|
|
|
|
for _, e := range env {
|
|
|
|
if !strings.Contains(e, "GIT_") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
GitEnv = append(GitEnv, e)
|
|
|
|
}
|
|
|
|
JoinedGitEnv = strings.Join(GitEnv, "\n")
|
|
|
|
}
|