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
|
package git
import (
"bytes"
"fmt"
"net"
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/go-git/go-git/v5/plumbing/transport"
fixtures "github.com/go-git/go-git-fixtures/v4"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) { TestingT(t) }
type BaseSuite struct {
fixtures.Suite
base string
port int
daemon *exec.Cmd
}
func (s *BaseSuite) SetUpTest(c *C) {
if runtime.GOOS == "windows" {
c.Skip(`git for windows has issues with write operations through git:// protocol.
See https://github.com/git-for-windows/git/issues/907`)
}
cmd := exec.Command("git", "daemon", "--help")
output, err := cmd.CombinedOutput()
if err != nil && bytes.Contains(output, []byte("'daemon' is not a git command")) {
c.Fatal("git daemon cannot be found")
}
s.port, err = freePort()
c.Assert(err, IsNil)
s.base, err = os.MkdirTemp(c.MkDir(), fmt.Sprintf("go-git-protocol-%d", s.port))
c.Assert(err, IsNil)
}
func (s *BaseSuite) StartDaemon(c *C) {
s.daemon = exec.Command(
"git",
"daemon",
fmt.Sprintf("--base-path=%s", s.base),
"--export-all",
"--enable=receive-pack",
"--reuseaddr",
fmt.Sprintf("--port=%d", s.port),
// Unless max-connections is limited to 1, a git-receive-pack
// might not be seen by a subsequent operation.
"--max-connections=1",
)
// Environment must be inherited in order to acknowledge GIT_EXEC_PATH if set.
s.daemon.Env = os.Environ()
err := s.daemon.Start()
c.Assert(err, IsNil)
// Connections might be refused if we start sending request too early.
time.Sleep(time.Millisecond * 500)
}
func (s *BaseSuite) newEndpoint(c *C, name string) *transport.Endpoint {
ep, err := transport.NewEndpoint(fmt.Sprintf("git://localhost:%d/%s", s.port, name))
c.Assert(err, IsNil)
return ep
}
func (s *BaseSuite) prepareRepository(c *C, f *fixtures.Fixture, name string) *transport.Endpoint {
fs := f.DotGit()
err := fixtures.EnsureIsBare(fs)
c.Assert(err, IsNil)
path := filepath.Join(s.base, name)
err = os.Rename(fs.Root(), path)
c.Assert(err, IsNil)
return s.newEndpoint(c, name)
}
func (s *BaseSuite) TearDownTest(c *C) {
if s.daemon != nil {
_ = s.daemon.Process.Signal(os.Kill)
_ = s.daemon.Wait()
}
}
func freePort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return 0, err
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, err
}
return l.Addr().(*net.TCPAddr).Port, l.Close()
}
|