Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 65b4be3

Browse files
committed
transport: http, fixes random failing test, #644
Signed-off-by: Máximo Cuadros <[email protected]>
1 parent 63f4806 commit 65b4be3

File tree

1 file changed

+52
-60
lines changed

1 file changed

+52
-60
lines changed

plumbing/transport/http/receive_pack_test.go

+52-60
Original file line numberDiff line numberDiff line change
@@ -25,59 +25,44 @@ type ReceivePackSuite struct {
2525
fixtures.Suite
2626

2727
base string
28+
host string
29+
port int
2830
}
2931

3032
var _ = Suite(&ReceivePackSuite{})
3133

3234
func (s *ReceivePackSuite) SetUpTest(c *C) {
3335
s.ReceivePackSuite.Client = DefaultClient
3436

35-
port, err := freePort()
37+
l, err := net.Listen("tcp", "localhost:0")
3638
c.Assert(err, IsNil)
3739

3840
base, err := ioutil.TempDir(os.TempDir(), "go-git-http-backend-test")
3941
c.Assert(err, IsNil)
40-
s.base = base
4142

42-
host := fmt.Sprintf("localhost_%d", port)
43-
interpolatedBase := filepath.Join(base, host)
44-
err = os.MkdirAll(interpolatedBase, 0755)
45-
c.Assert(err, IsNil)
46-
47-
dotgit := fixtures.Basic().One().DotGit().Root()
48-
prepareRepo(c, dotgit)
49-
err = os.Rename(dotgit, filepath.Join(interpolatedBase, "basic.git"))
50-
c.Assert(err, IsNil)
51-
52-
ep, err := transport.NewEndpoint(fmt.Sprintf("http://localhost:%d/basic.git", port))
53-
c.Assert(err, IsNil)
54-
s.ReceivePackSuite.Endpoint = ep
55-
56-
dotgit = fixtures.ByTag("empty").One().DotGit().Root()
57-
prepareRepo(c, dotgit)
58-
err = os.Rename(dotgit, filepath.Join(interpolatedBase, "empty.git"))
59-
c.Assert(err, IsNil)
43+
s.port = l.Addr().(*net.TCPAddr).Port
44+
s.host = fmt.Sprintf("localhost_%d", s.port)
45+
s.base = filepath.Join(base, s.host)
6046

61-
ep, err = transport.NewEndpoint(fmt.Sprintf("http://localhost:%d/empty.git", port))
47+
err = os.MkdirAll(s.base, 0755)
6248
c.Assert(err, IsNil)
63-
s.ReceivePackSuite.EmptyEndpoint = ep
6449

65-
ep, err = transport.NewEndpoint(fmt.Sprintf("http://localhost:%d/non-existent.git", port))
66-
c.Assert(err, IsNil)
67-
s.ReceivePackSuite.NonExistentEndpoint = ep
50+
s.ReceivePackSuite.Endpoint = s.prepareRepository(c, fixtures.Basic().One(), "basic.git")
51+
s.ReceivePackSuite.EmptyEndpoint = s.prepareRepository(c, fixtures.ByTag("empty").One(), "empty.git")
52+
s.ReceivePackSuite.NonExistentEndpoint = s.newEndpoint(c, "non-existent.git")
6853

6954
cmd := exec.Command("git", "--exec-path")
7055
out, err := cmd.CombinedOutput()
7156
c.Assert(err, IsNil)
72-
p := filepath.Join(strings.Trim(string(out), "\n"), "git-http-backend")
7357

74-
h := &cgi.Handler{
75-
Path: p,
76-
Env: []string{"GIT_HTTP_EXPORT_ALL=true", fmt.Sprintf("GIT_PROJECT_ROOT=%s", interpolatedBase)},
58+
server := &http.Server{
59+
Handler: &cgi.Handler{
60+
Path: filepath.Join(strings.Trim(string(out), "\n"), "git-http-backend"),
61+
Env: []string{"GIT_HTTP_EXPORT_ALL=true", fmt.Sprintf("GIT_PROJECT_ROOT=%s", s.base)},
62+
},
7763
}
78-
7964
go func() {
80-
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), h))
65+
log.Fatal(server.Serve(l))
8166
}()
8267
}
8368

@@ -86,37 +71,44 @@ func (s *ReceivePackSuite) TearDownTest(c *C) {
8671
c.Assert(err, IsNil)
8772
}
8873

89-
func freePort() (int, error) {
90-
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
91-
if err != nil {
92-
return 0, err
93-
}
74+
func (s *ReceivePackSuite) prepareRepository(c *C, f *fixtures.Fixture, name string) transport.Endpoint {
75+
path := filepath.Join(s.base, name)
9476

95-
l, err := net.ListenTCP("tcp", addr)
96-
if err != nil {
97-
return 0, err
98-
}
77+
err := os.Rename(f.DotGit().Root(), path)
78+
c.Assert(err, IsNil)
9979

100-
return l.Addr().(*net.TCPAddr).Port, l.Close()
80+
s.setConfigToRepository(c, path)
81+
return s.newEndpoint(c, name)
10182
}
10283

103-
const bareConfig = `[core]
104-
repositoryformatversion = 0
105-
filemode = true
106-
bare = true
107-
[http]
108-
receivepack = true`
109-
110-
func prepareRepo(c *C, path string) {
111-
// git-receive-pack refuses to update refs/heads/master on non-bare repo
112-
// so we ensure bare repo config.
113-
config := filepath.Join(path, "config")
114-
if _, err := os.Stat(config); err == nil {
115-
f, err := os.OpenFile(config, os.O_TRUNC|os.O_WRONLY, 0)
116-
c.Assert(err, IsNil)
117-
content := strings.NewReader(bareConfig)
118-
_, err = io.Copy(f, content)
119-
c.Assert(err, IsNil)
120-
c.Assert(f.Close(), IsNil)
121-
}
84+
// git-receive-pack refuses to update refs/heads/master on non-bare repo
85+
// so we ensure bare repo config.
86+
func (s *ReceivePackSuite) setConfigToRepository(c *C, path string) {
87+
cfgPath := filepath.Join(path, "config")
88+
_, err := os.Stat(cfgPath)
89+
c.Assert(err, IsNil)
90+
91+
cfg, err := os.OpenFile(cfgPath, os.O_TRUNC|os.O_WRONLY, 0)
92+
c.Assert(err, IsNil)
93+
94+
content := strings.NewReader("" +
95+
"[core]\n" +
96+
"repositoryformatversion = 0\n" +
97+
"filemode = true\n" +
98+
"bare = true\n" +
99+
"[http]\n" +
100+
"receivepack = true\n",
101+
)
102+
103+
_, err = io.Copy(cfg, content)
104+
c.Assert(err, IsNil)
105+
106+
c.Assert(cfg.Close(), IsNil)
107+
}
108+
109+
func (s *ReceivePackSuite) newEndpoint(c *C, name string) transport.Endpoint {
110+
ep, err := transport.NewEndpoint(fmt.Sprintf("http://localhost:%d/%s", s.port, name))
111+
c.Assert(err, IsNil)
112+
113+
return ep
122114
}

0 commit comments

Comments
 (0)