aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/transport/file/server_test.go
blob: ff462e282e5a41ee4ae635ddd1091011e9967237 (plain) (blame)
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
package file

import (
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"

	"gopkg.in/src-d/go-git.v4/fixtures"

	. "gopkg.in/check.v1"
)

type ServerSuite struct {
	CommonSuite
	RemoteName string
	SrcPath    string
	DstPath    string
	DstURL     string
}

var _ = Suite(&ServerSuite{})

func (s *ServerSuite) SetUpSuite(c *C) {
	s.CommonSuite.SetUpSuite(c)

	s.RemoteName = "test"

	fixture := fixtures.Basic().One()
	s.SrcPath = fixture.DotGit().Base()

	fixture = fixtures.ByTag("empty").One()
	s.DstPath = fixture.DotGit().Base()
	s.DstURL = fmt.Sprintf("file://%s", s.DstPath)

	cmd := exec.Command("git", "remote", "add", s.RemoteName, s.DstURL)
	cmd.Dir = s.SrcPath
	c.Assert(cmd.Run(), IsNil)
}

func (s *ServerSuite) TestPush(c *C) {
	// git <2.0 cannot push to an empty repository without a refspec.
	cmd := exec.Command("git", "push",
		"--receive-pack", s.ReceivePackBin,
		s.RemoteName, "refs/heads/*:refs/heads/*",
	)
	cmd.Dir = s.SrcPath
	cmd.Env = os.Environ()
	cmd.Env = append(cmd.Env, "GIT_TRACE=true", "GIT_TRACE_PACKET=true")
	stdout, stderr, err := execAndGetOutput(c, cmd)
	c.Assert(err, IsNil, Commentf("STDOUT:\n%s\nSTDERR:\n%s\n", stdout, stderr))
}

func (s *ServerSuite) TestClone(c *C) {
	pathToClone := c.MkDir()

	cmd := exec.Command("git", "clone",
		"--upload-pack", s.UploadPackBin,
		s.SrcPath, pathToClone,
	)
	cmd.Env = os.Environ()
	cmd.Env = append(cmd.Env, "GIT_TRACE=true", "GIT_TRACE_PACKET=true")
	stdout, stderr, err := execAndGetOutput(c, cmd)
	c.Assert(err, IsNil, Commentf("STDOUT:\n%s\nSTDERR:\n%s\n", stdout, stderr))
}

func execAndGetOutput(c *C, cmd *exec.Cmd) (stdout, stderr string, err error) {
	sout, err := cmd.StdoutPipe()
	c.Assert(err, IsNil)
	serr, err := cmd.StderrPipe()
	c.Assert(err, IsNil)

	outChan, outErr := readAllAsync(sout)
	errChan, errErr := readAllAsync(serr)

	c.Assert(cmd.Start(), IsNil)

	if err = cmd.Wait(); err != nil {
		return <-outChan, <-errChan, err
	}

	if err := <-outErr; err != nil {
		return <-outChan, <-errChan, err
	}

	return <-outChan, <-errChan, <-errErr
}

func readAllAsync(r io.Reader) (out chan string, err chan error) {
	out = make(chan string, 1)
	err = make(chan error, 1)
	go func() {
		b, e := ioutil.ReadAll(r)
		if e != nil {
			err <- e
		} else {
			err <- nil
		}

		out <- string(b)
	}()

	return out, err
}