2014-06-04 18:53:57 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"github.com/bmizerany/assert"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSmudge(t *testing.T) {
|
|
|
|
repo := NewRepository(t, "empty")
|
2014-06-04 19:03:47 +00:00
|
|
|
defer repo.Test()
|
2014-06-04 18:53:57 +00:00
|
|
|
|
2014-06-04 19:08:19 +00:00
|
|
|
prePushHookFile := filepath.Join(repo.Path, ".git", "hooks", "pre-push")
|
2014-08-07 14:53:13 +00:00
|
|
|
progressFile := filepath.Join(repo.Path, ".git", "progress")
|
2014-06-04 19:08:19 +00:00
|
|
|
|
2014-07-25 17:16:57 +00:00
|
|
|
// simple smudge example
|
2014-08-07 14:53:13 +00:00
|
|
|
cmd := repo.Command("smudge", "somefile")
|
2015-01-30 16:56:12 +00:00
|
|
|
cmd.Input = bytes.NewBufferString("version http://hawser.github.com/spec/v1\noid sha256:SOMEOID\nsize 9\n")
|
2014-06-04 18:53:57 +00:00
|
|
|
cmd.Output = "whatever"
|
2014-10-06 15:40:34 +00:00
|
|
|
cmd.Env = append(cmd.Env, "GIT_MEDIA_PROGRESS="+progressFile)
|
2014-06-04 18:53:57 +00:00
|
|
|
|
|
|
|
cmd.Before(func() {
|
|
|
|
path := filepath.Join(repo.Path, ".git", "media", "SO", "ME")
|
|
|
|
file := filepath.Join(path, "SOMEOID")
|
|
|
|
assert.Equal(t, nil, os.MkdirAll(path, 0755))
|
|
|
|
assert.Equal(t, nil, ioutil.WriteFile(file, []byte("whatever\n"), 0755))
|
|
|
|
})
|
2014-06-04 19:03:47 +00:00
|
|
|
|
|
|
|
cmd.After(func() {
|
2014-07-25 17:16:57 +00:00
|
|
|
// assert hook is created
|
2014-06-04 19:03:47 +00:00
|
|
|
stat, err := os.Stat(prePushHookFile)
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
assert.Equal(t, false, stat.IsDir())
|
2014-08-07 14:53:13 +00:00
|
|
|
|
2014-08-07 17:01:06 +00:00
|
|
|
// assert progress file
|
2014-08-07 14:53:13 +00:00
|
|
|
progress, err := ioutil.ReadFile(progressFile)
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
progLines := bytes.Split(progress, []byte("\n"))
|
2014-08-14 16:39:35 +00:00
|
|
|
assert.Equal(t, 2, len(progLines))
|
|
|
|
assert.Equal(t, "smudge 1/1 9/9 somefile", string(progLines[0]))
|
|
|
|
assert.Equal(t, "", string(progLines[1]))
|
2014-06-04 19:03:47 +00:00
|
|
|
})
|
2014-06-04 19:08:19 +00:00
|
|
|
|
2014-07-25 17:16:57 +00:00
|
|
|
// smudge with custom hook
|
2014-06-04 19:08:19 +00:00
|
|
|
cmd = repo.Command("smudge")
|
2014-10-04 16:28:12 +00:00
|
|
|
cmd.Input = bytes.NewBufferString("# git-media\n4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393")
|
2014-06-04 19:08:19 +00:00
|
|
|
cmd.Output = "whatever"
|
|
|
|
customHook := []byte("echo 'yo'")
|
|
|
|
|
|
|
|
cmd.Before(func() {
|
2014-10-04 16:28:12 +00:00
|
|
|
path := filepath.Join(repo.Path, ".git", "media", "4d", "7a")
|
|
|
|
file := filepath.Join(path, "4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393")
|
2014-06-04 19:08:19 +00:00
|
|
|
assert.Equal(t, nil, os.MkdirAll(path, 0755))
|
|
|
|
assert.Equal(t, nil, ioutil.WriteFile(file, []byte("whatever\n"), 0755))
|
|
|
|
assert.Equal(t, nil, ioutil.WriteFile(prePushHookFile, customHook, 0755))
|
|
|
|
})
|
|
|
|
|
|
|
|
cmd.After(func() {
|
2014-07-25 17:16:57 +00:00
|
|
|
// assert custom hook is not overwritten
|
2014-06-04 19:08:19 +00:00
|
|
|
by, err := ioutil.ReadFile(prePushHookFile)
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
assert.Equal(t, string(customHook), string(by))
|
|
|
|
})
|
2014-06-04 18:53:57 +00:00
|
|
|
}
|
2014-07-25 17:16:57 +00:00
|
|
|
|
|
|
|
func TestSmudgeInfo(t *testing.T) {
|
|
|
|
repo := NewRepository(t, "empty")
|
|
|
|
defer repo.Test()
|
|
|
|
|
2014-10-04 16:28:12 +00:00
|
|
|
mediaPath := filepath.Join(repo.Path, ".git", "media", "4d", "7a")
|
|
|
|
mediaFile := filepath.Join(mediaPath, "4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393")
|
2014-07-25 17:16:57 +00:00
|
|
|
|
|
|
|
// smudge --info with old pointer format, without local file
|
|
|
|
cmd := repo.Command("smudge", "--info")
|
2014-10-04 16:28:12 +00:00
|
|
|
cmd.Input = bytes.NewBufferString("# git-media\n4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393")
|
2014-07-25 17:16:57 +00:00
|
|
|
cmd.Output = "0 --"
|
|
|
|
|
|
|
|
// smudge --info with old pointer format, with local file
|
|
|
|
cmd = repo.Command("smudge", "--info")
|
2014-10-04 16:28:12 +00:00
|
|
|
cmd.Input = bytes.NewBufferString("# git-media\n4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393")
|
2014-07-25 17:26:01 +00:00
|
|
|
cmd.Output = "9 " + mediaFile
|
2014-07-25 17:16:57 +00:00
|
|
|
|
|
|
|
cmd.Before(func() {
|
|
|
|
assert.Equal(t, nil, os.MkdirAll(mediaPath, 0755))
|
|
|
|
assert.Equal(t, nil, ioutil.WriteFile(mediaFile, []byte("whatever\n"), 0755))
|
|
|
|
})
|
|
|
|
|
|
|
|
// smudge --info with ini pointer format, without local file
|
|
|
|
cmd = repo.Command("smudge", "--info")
|
2014-10-04 16:28:12 +00:00
|
|
|
cmd.Input = bytes.NewBufferString("version http://git-media.io/v/2\noid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\nsize 123\n")
|
2014-07-25 17:16:57 +00:00
|
|
|
cmd.Output = "123 --"
|
|
|
|
|
|
|
|
// smudge --info with ini pointer format, with local file
|
|
|
|
cmd = repo.Command("smudge", "--info")
|
2014-10-04 16:28:12 +00:00
|
|
|
cmd.Input = bytes.NewBufferString("version http://git-media.io/v/2\noid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\nsize 123\n")
|
2014-07-25 17:26:01 +00:00
|
|
|
cmd.Output = "9 " + mediaFile
|
2014-07-25 17:16:57 +00:00
|
|
|
|
|
|
|
cmd.Before(func() {
|
|
|
|
assert.Equal(t, nil, os.MkdirAll(mediaPath, 0755))
|
|
|
|
assert.Equal(t, nil, ioutil.WriteFile(mediaFile, []byte("whatever\n"), 0755))
|
|
|
|
})
|
2015-01-30 16:56:12 +00:00
|
|
|
|
|
|
|
// smudge --info with hawser pointer format, without local file
|
|
|
|
cmd = repo.Command("smudge", "--info")
|
|
|
|
cmd.Input = bytes.NewBufferString("version http://hawser.github.com/spec/v1\noid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\nsize 123\n")
|
|
|
|
cmd.Output = "123 --"
|
|
|
|
|
|
|
|
// smudge --info with hawser pointer format, with local file
|
|
|
|
cmd = repo.Command("smudge", "--info")
|
|
|
|
cmd.Input = bytes.NewBufferString("version http://hawser.github.com/spec/v1\noid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\nsize 123\n")
|
|
|
|
cmd.Output = "9 " + mediaFile
|
|
|
|
|
|
|
|
cmd.Before(func() {
|
|
|
|
assert.Equal(t, nil, os.MkdirAll(mediaPath, 0755))
|
|
|
|
assert.Equal(t, nil, ioutil.WriteFile(mediaFile, []byte("whatever\n"), 0755))
|
|
|
|
})
|
2014-07-25 17:16:57 +00:00
|
|
|
}
|
2014-09-05 16:38:14 +00:00
|
|
|
|
|
|
|
func TestSmudgePassesInvalidData(t *testing.T) {
|
|
|
|
repo := NewRepository(t, "empty")
|
|
|
|
defer repo.Test()
|
|
|
|
|
|
|
|
cmd := repo.Command("smudge", "somefile")
|
|
|
|
cmd.Input = bytes.NewBufferString("this is not a pointer file\n")
|
|
|
|
cmd.Output = "this is not a pointer file"
|
|
|
|
}
|