Debian Codegraph @

plumbing/transport/git/common_test.go

raw ◀ ▶
1 Debian patch touch this file
1 package git
2
3 import (
4 "bytes"
5 "fmt"
6 "net"
7 "os"
8 "os/exec"
9 "path/filepath"
10 "runtime"
11 "testing"
12 "time"
13
14 "github.com/go-git/go-git/v5/plumbing/transport"
15
16 fixtures "github.com/go-git/go-git-fixtures/v4"
17 . "gopkg.in/check.v1"
18 )
19
20 func Test(t *testing.T) { TestingT(t) }
21
22 type BaseSuite struct {
23 fixtures.Suite
24
25 base string
26 port int
27 daemon *exec.Cmd
28 }
29
30 func (s *BaseSuite) SetUpTest(c *C) {
31 if runtime.GOOS == "windows" {
32 c.Skip(`git for windows has issues with write operations through git:// protocol.
33 See https://github.com/git-for-windows/git/issues/907`)
34 }
35
36 cmd := exec.Command("git", "daemon", "--help")
37 output, err := cmd.CombinedOutput()
38 if err != nil && bytes.Contains(output, []byte("'daemon' is not a git command")) {
39 c.Fatal("git daemon cannot be found")
40 }
41
42 s.port, err = freePort()
43 c.Assert(err, IsNil)
44
45 s.base, err = os.MkdirTemp(c.MkDir(), fmt.Sprintf("go-git-protocol-%d", s.port))
46 c.Assert(err, IsNil)
47 }
48
49 func (s *BaseSuite) StartDaemon(c *C) {
50 s.daemon = exec.Command(
51 "git",
52 "daemon",
53 fmt.Sprintf("--base-path=%s", s.base),
54 "--export-all",
55 "--enable=receive-pack",
56 "--reuseaddr",
57 fmt.Sprintf("--port=%d", s.port),
58 // Unless max-connections is limited to 1, a git-receive-pack
59 // might not be seen by a subsequent operation.
60 "--max-connections=1",
61 )
62
63 // Environment must be inherited in order to acknowledge GIT_EXEC_PATH if set.
64 s.daemon.Env = os.Environ()
65
66 err := s.daemon.Start()
67 c.Assert(err, IsNil)
68
69 // Connections might be refused if we start sending request too early.
70 time.Sleep(time.Millisecond * 500)
71 }
72
73 func (s *BaseSuite) newEndpoint(c *C, name string) *transport.Endpoint {
•74 ep, err := transport.NewEndpoint(fmt.Sprintf("git://127.0.0.1:%d/%s", s.port, name))
75 c.Assert(err, IsNil)
76
77 return ep
78 }
79
80 func (s *BaseSuite) prepareRepository(c *C, f *fixtures.Fixture, name string) *transport.Endpoint {
81 fs := f.DotGit()
82
83 err := fixtures.EnsureIsBare(fs)
84 c.Assert(err, IsNil)
85
86 path := filepath.Join(s.base, name)
87 err = os.Rename(fs.Root(), path)
88 c.Assert(err, IsNil)
89
90 return s.newEndpoint(c, name)
91 }
92
93 func (s *BaseSuite) TearDownTest(c *C) {
94 if s.daemon != nil {
95 _ = s.daemon.Process.Signal(os.Kill)
96 _ = s.daemon.Wait()
97 }
98 }
99
100 func freePort() (int, error) {
•101 addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
102 if err != nil {
103 return 0, err
104 }
105
106 l, err := net.ListenTCP("tcp", addr)
107 if err != nil {
108 return 0, err
109 }
110
111 return l.Addr().(*net.TCPAddr).Port, l.Close()
112 }