Skip to content

Commit 1b33d35

Browse files
authored
Merge pull request #7 from dirkmc/feat/testing
Test push / fetch
2 parents f2fc4db + 30b3616 commit 1b33d35

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+939
-27
lines changed

cmd/git-remote-ipld/git-remote-ipld

17.2 MB
Binary file not shown.

cmd/git-remote-ipld/ipld.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import (
66
"fmt"
77
"github.com/magik6k/git-remote-ipld/core"
88
"gopkg.in/src-d/go-git.v4/plumbing"
9-
"os"
109
)
1110

1211
type IpldHandler struct {
13-
// remoteHash is hash form remote name
12+
// remoteHash is hash from remote name
1413
remoteHash string
14+
// command line arguments
15+
osArgs []string
1516
}
1617

1718
func (h *IpldHandler) List(remote *core.Remote, forPush bool) ([]string, error) {
@@ -38,7 +39,7 @@ func (h *IpldHandler) List(remote *core.Remote, forPush bool) ([]string, error)
3839
}
3940

4041
// pull ipld::hash, we only want to update HEAD
41-
if !forPush && headRef.Target() == ref.Name() && headRef.Type() == plumbing.SymbolicReference && len(os.Args) >= 3 {
42+
if !forPush && headRef.Target() == ref.Name() && headRef.Type() == plumbing.SymbolicReference && len(h.osArgs) >= 3 {
4243
sha, err := hex.DecodeString(h.remoteHash)
4344
if err != nil {
4445
return err
@@ -61,7 +62,7 @@ func (h *IpldHandler) List(remote *core.Remote, forPush bool) ([]string, error)
6162
}
6263

6364
// For clone
64-
if n == 0 && !forPush && len(os.Args) >= 3 {
65+
if n == 0 && !forPush && len(h.osArgs) >= 3 {
6566
sha, err := hex.DecodeString(h.remoteHash)
6667
if err != nil {
6768
return nil, err

cmd/git-remote-ipld/ipld_test.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"github.com/magik6k/git-remote-ipld/util"
6+
"io"
7+
"io/ioutil"
8+
"log"
9+
"os"
10+
"path/filepath"
11+
"strings"
12+
"testing"
13+
)
14+
15+
func TestCapabilities(t *testing.T) {
16+
tmpdir := setupTest(t)
17+
defer os.RemoveAll(tmpdir)
18+
19+
// git clone ipld::d5b0d08c180fd7a9bf4f684a37e60ceeb4d25ec8
20+
args := []string{"git-remote-ipld", "origin", "d5b0d08c180fd7a9bf4f684a37e60ceeb4d25ec8"}
21+
22+
listExp := []string{
23+
"0000000000000000000000000000000000000000 refs/heads/french",
24+
"0000000000000000000000000000000000000000 refs/heads/italian",
25+
"d5b0d08c180fd7a9bf4f684a37e60ceeb4d25ec8 refs/heads/master",
26+
"@refs/heads/master HEAD",
27+
}
28+
listForPushExp := []string{
29+
"0000000000000000000000000000000000000000 refs/heads/french",
30+
"0000000000000000000000000000000000000000 refs/heads/italian",
31+
"0000000000000000000000000000000000000000 refs/heads/master",
32+
"@refs/heads/master HEAD",
33+
}
34+
35+
testCase(t, args, "capabilities", []string{"push", "fetch"})
36+
testCase(t, args, "list", listExp)
37+
testCase(t, args, "list for-push", listForPushExp)
38+
39+
// mock/git> git push --set-upstream ipld:: master
40+
testCase(t, args, "push refs/heads/master:refs/heads/master", []string{})
41+
42+
testCase(t, args, "fetch d5b0d08c180fd7a9bf4f684a37e60ceeb4d25ec8 refs/heads/master\n", []string{""})
43+
comparePullToMock(t, tmpdir, "git")
44+
}
45+
46+
func testCase(t *testing.T, args []string, input string, expected []string) {
47+
reader := strings.NewReader(input + "\n")
48+
var writer bytes.Buffer
49+
logger := log.New(ioutil.Discard, "", 0)
50+
err := Main(args, reader, &writer, logger)
51+
if err != nil && err != io.EOF {
52+
t.Fatal(err)
53+
}
54+
55+
response := writer.String()
56+
exp := strings.Join(expected, "\n")
57+
if strings.TrimSpace(response) != exp {
58+
t.Fatalf("Args: %s\nInput:\n%s\nExpected:\n%s\nActual:\n'%s'\n", args, input, exp, response)
59+
}
60+
}
61+
62+
func comparePullToMock(t *testing.T, tmpdir, mock string) {
63+
wd, _ := os.Getwd()
64+
mockdir := filepath.Join(wd, "..", "..", "mock", mock)
65+
compareContents(t, filepath.Join(tmpdir, ".git"), mockdir)
66+
}
67+
68+
func compareContents(t *testing.T, src, dst string) {
69+
src = filepath.Clean(src)
70+
dst = filepath.Clean(dst)
71+
err := util.CompareDirs(src, dst, []string{"ipld"})
72+
if err != nil {
73+
t.Fatal(err)
74+
}
75+
}
76+
77+
func setupTest(t *testing.T) string {
78+
wd, _ := os.Getwd()
79+
src := filepath.Join(wd, "..", "..", "mock", "git")
80+
si, err := os.Stat(src)
81+
if err != nil {
82+
t.Fatal(err)
83+
}
84+
if !si.IsDir() {
85+
t.Fatal("source is not a directory")
86+
}
87+
88+
tmpdir, err := ioutil.TempDir("", "git-test")
89+
if err != nil {
90+
t.Fatal(err)
91+
}
92+
93+
dst := filepath.Join(tmpdir, ".git")
94+
err = util.CopyDir(src, dst)
95+
if err != nil {
96+
t.Fatal(err)
97+
}
98+
99+
os.Setenv("GIT_DIR", dst)
100+
return tmpdir
101+
}

cmd/git-remote-ipld/main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"io"
56
"log"
67
"os"
78
"strings"
@@ -14,27 +15,26 @@ const (
1415
IPFS_PREFIX = "ipfs://"
1516
)
1617

17-
func Main() error {
18-
if len(os.Args) < 3 {
18+
func Main(args []string, reader io.Reader, writer io.Writer, logger *log.Logger) error {
19+
if len(args) < 3 {
1920
return fmt.Errorf("Usage: git-remote-ipld remote-name url")
2021
}
2122

22-
hashArg := os.Args[2]
23+
hashArg := args[2]
2324
if strings.HasPrefix(hashArg, IPLD_PREFIX) || strings.HasPrefix(hashArg, IPFS_PREFIX) {
2425
hashArg = hashArg[len(IPLD_PREFIX):]
2526
}
2627

27-
remote, err := core.NewRemote(&IpldHandler{remoteHash: hashArg})
28+
remote, err := core.NewRemote(&IpldHandler{remoteHash: hashArg, osArgs: args}, reader, writer, logger)
2829
if err != nil {
2930
return err
3031
}
3132
defer remote.Close()
32-
3333
return remote.ProcessCommands()
3434
}
3535

3636
func main() {
37-
if err := Main(); err != nil {
37+
if err := Main(os.Args, nil, nil, nil); err != nil {
3838
fmt.Fprintf(os.Stderr, "\x1b[K")
3939
log.Fatal(err)
4040
}

cmd/git-remote-ipns/git-remote-ipns

17.2 MB
Binary file not shown.

cmd/git-remote-ipns/ipns.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package main
22

33
import (
4-
"path"
54
"fmt"
5+
"path"
66
"strings"
77

88
ipfs "github.com/ipfs/go-ipfs-api"
99
"github.com/magik6k/git-remote-ipld/core"
1010

11-
cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
12-
"gopkg.in/src-d/go-git.v4/plumbing"
13-
"encoding/hex"
1411
"bytes"
12+
"encoding/hex"
13+
"gopkg.in/src-d/go-git.v4/plumbing"
14+
cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
1515
)
1616

1717
const (
@@ -20,7 +20,7 @@ const (
2020
)
2121

2222
type refPath struct {
23-
path string
23+
path string
2424
rType int
2525

2626
hash string

cmd/git-remote-ipns/main.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ package main
22

33
import (
44
"fmt"
5+
"github.com/magik6k/git-remote-ipld/core"
6+
"io"
57
"log"
68
"os"
79
"strings"
8-
"github.com/magik6k/git-remote-ipld/core"
910
)
1011

1112
const (
1213
IPNS_PREFIX = "ipns://"
1314
)
1415

15-
func Main() error {
16+
func Main(reader io.Reader, writer io.Writer, logger *log.Logger) error {
1617
if len(os.Args) < 3 {
1718
return fmt.Errorf("Usage: git-remote-ipns remote-name url")
1819
}
@@ -22,7 +23,7 @@ func Main() error {
2223
remoteName = remoteName[len(IPNS_PREFIX):]
2324
}
2425

25-
remote, err := core.NewRemote(&IpnsHandler{remoteName:remoteName})
26+
remote, err := core.NewRemote(&IpnsHandler{remoteName: remoteName}, reader, writer, logger)
2627
if err != nil {
2728
return err
2829
}
@@ -31,7 +32,7 @@ func Main() error {
3132
}
3233

3334
func main() {
34-
if err := Main(); err != nil {
35+
if err := Main(nil, nil, nil); err != nil {
3536
fmt.Fprintf(os.Stderr, "\x1b[K")
3637
log.Fatal(err)
3738
}

core/remote.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"encoding/hex"
66
"fmt"
7+
"io"
78
"log"
89
"os"
910
"path"
@@ -18,6 +19,8 @@ type RemoteHandler interface {
1819
}
1920

2021
type Remote struct {
22+
reader io.Reader
23+
writer io.Writer
2124
Logger *log.Logger
2225
localDir string
2326

@@ -29,7 +32,7 @@ type Remote struct {
2932
todo []func() (string, error)
3033
}
3134

32-
func NewRemote(handler RemoteHandler) (*Remote, error) {
35+
func NewRemote(handler RemoteHandler, reader io.Reader, writer io.Writer, logger *log.Logger) (*Remote, error) {
3336
localDir, err := GetLocalDir()
3437
if err != nil {
3538
return nil, err
@@ -50,20 +53,32 @@ func NewRemote(handler RemoteHandler) (*Remote, error) {
5053
return nil, fmt.Errorf("fetch: %v", err)
5154
}
5255

56+
if reader == nil {
57+
reader = os.Stdin
58+
}
59+
if writer == nil {
60+
writer = os.Stdout
61+
}
62+
if logger == nil {
63+
logger = log.New(os.Stderr, "", 0)
64+
}
65+
5366
return &Remote{
54-
Logger: log.New(os.Stderr, "", 0),
67+
reader: reader,
68+
writer: writer,
69+
Logger: logger,
5570
localDir: localDir,
5671

5772
Repo: repo,
5873
Tracker: tracker,
5974

6075
Handler: handler,
61-
}, nil
76+
}, nil
6277
}
6378

6479
func (r *Remote) Printf(format string, a ...interface{}) (n int, err error) {
6580
r.Logger.Printf("> "+format, a...)
66-
return fmt.Printf(format, a...)
81+
return fmt.Fprintf(r.writer, format, a...)
6782
}
6883

6984
func (r *Remote) NewPush() *Push {
@@ -108,9 +123,9 @@ func (r *Remote) fetch(sha, ref string) {
108123
}
109124

110125
func (r *Remote) ProcessCommands() error {
111-
stdinReader := bufio.NewReader(os.Stdin)
126+
reader := bufio.NewReader(r.reader)
112127
for {
113-
command, err := stdinReader.ReadString('\n')
128+
command, err := reader.ReadString('\n')
114129
if err != nil {
115130
return err
116131
}
@@ -141,7 +156,7 @@ func (r *Remote) ProcessCommands() error {
141156
case command == "":
142157
fallthrough
143158
case command == "\n":
144-
r.Logger.Println("Processsing tasks")
159+
r.Logger.Println("Processing tasks")
145160
for _, task := range r.todo {
146161
resp, err := task()
147162
if err != nil {

core/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"os"
77
"path"
88

9+
"fmt"
910
cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
1011
mh "gx/ipfs/QmU9a9NV9RdPNwZQDYd5uKsm6N6LJLSvLbywDDYFbaaC6P/go-multihash"
11-
"fmt"
1212
)
1313

1414
func compressObject(in []byte) []byte {

mock/git/COMMIT_EDITMSG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
English -> Italian

mock/git/HEAD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master

mock/git/config

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = false
5+
logallrefupdates = true
6+
ignorecase = true
7+
precomposeunicode = true

mock/git/description

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Unnamed repository; edit this file 'description' to name the repository.

mock/git/hooks/applypatch-msg.sample

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to check the commit log message taken by
4+
# applypatch from an e-mail message.
5+
#
6+
# The hook should exit with non-zero status after issuing an
7+
# appropriate message if it wants to stop the commit. The hook is
8+
# allowed to edit the commit message file.
9+
#
10+
# To enable this hook, rename this file to "applypatch-msg".
11+
12+
. git-sh-setup
13+
commitmsg="$(git rev-parse --git-path hooks/commit-msg)"
14+
test -x "$commitmsg" && exec "$commitmsg" ${1+"$@"}
15+
:

mock/git/hooks/commit-msg.sample

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to check the commit log message.
4+
# Called by "git commit" with one argument, the name of the file
5+
# that has the commit message. The hook should exit with non-zero
6+
# status after issuing an appropriate message if it wants to stop the
7+
# commit. The hook is allowed to edit the commit message file.
8+
#
9+
# To enable this hook, rename this file to "commit-msg".
10+
11+
# Uncomment the below to add a Signed-off-by line to the message.
12+
# Doing this in a hook is a bad idea in general, but the prepare-commit-msg
13+
# hook is more suited to it.
14+
#
15+
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
16+
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
17+
18+
# This example catches duplicate Signed-off-by lines.
19+
20+
test "" = "$(grep '^Signed-off-by: ' "$1" |
21+
sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || {
22+
echo >&2 Duplicate Signed-off-by lines.
23+
exit 1
24+
}

0 commit comments

Comments
 (0)