@@ -12,6 +12,7 @@ import (
12
12
"os/exec"
13
13
"path/filepath"
14
14
"strings"
15
+ "time"
15
16
16
17
"gopkg.in/src-d/go-git.v4/plumbing/transport"
17
18
"gopkg.in/src-d/go-git.v4/plumbing/transport/test"
@@ -25,59 +26,44 @@ type ReceivePackSuite struct {
25
26
fixtures.Suite
26
27
27
28
base string
29
+ host string
30
+ port int
28
31
}
29
32
30
33
var _ = Suite (& ReceivePackSuite {})
31
34
32
35
func (s * ReceivePackSuite ) SetUpTest (c * C ) {
33
36
s .ReceivePackSuite .Client = DefaultClient
34
37
35
- port , err := freePort ( )
38
+ l , err := net . Listen ( "tcp" , "localhost:0" )
36
39
c .Assert (err , IsNil )
37
40
38
41
base , err := ioutil .TempDir (os .TempDir (), "go-git-http-backend-test" )
39
42
c .Assert (err , IsNil )
40
- s .base = base
41
43
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 )
44
+ s .port = l .Addr ().(* net.TCPAddr ).Port
45
+ s .host = fmt .Sprintf ("localhost_%d" , s .port )
46
+ s .base = filepath .Join (base , s .host )
60
47
61
- ep , err = transport . NewEndpoint ( fmt . Sprintf ( "http://localhost:%d/empty.git" , port ) )
48
+ err = os . MkdirAll ( s . base , 0755 )
62
49
c .Assert (err , IsNil )
63
- s .ReceivePackSuite .EmptyEndpoint = ep
64
50
65
- ep , err = transport . NewEndpoint ( fmt . Sprintf ( "http://localhost:%d/non-existent .git", port ) )
66
- c . Assert ( err , IsNil )
67
- s .ReceivePackSuite .NonExistentEndpoint = ep
51
+ s . ReceivePackSuite . Endpoint = s . prepareRepository ( c , fixtures . Basic (). One (), "basic .git" )
52
+ s . ReceivePackSuite . EmptyEndpoint = s . prepareRepository ( c , fixtures . ByTag ( "empty" ). One (), "empty.git" )
53
+ s .ReceivePackSuite .NonExistentEndpoint = s . newEndpoint ( c , "non-existent.git" )
68
54
69
55
cmd := exec .Command ("git" , "--exec-path" )
70
56
out , err := cmd .CombinedOutput ()
71
57
c .Assert (err , IsNil )
72
- p := filepath .Join (strings .Trim (string (out ), "\n " ), "git-http-backend" )
73
58
74
- h := & cgi.Handler {
75
- Path : p ,
76
- Env : []string {"GIT_HTTP_EXPORT_ALL=true" , fmt .Sprintf ("GIT_PROJECT_ROOT=%s" , interpolatedBase )},
59
+ server := & http.Server {
60
+ Handler : & cgi.Handler {
61
+ Path : filepath .Join (strings .Trim (string (out ), "\n " ), "git-http-backend" ),
62
+ Env : []string {"GIT_HTTP_EXPORT_ALL=true" , fmt .Sprintf ("GIT_PROJECT_ROOT=%s" , s .base )},
63
+ },
77
64
}
78
-
79
65
go func () {
80
- log .Fatal (http . ListenAndServe ( fmt . Sprintf ( ":%d" , port ), h ))
66
+ log .Fatal (server . Serve ( l ))
81
67
}()
82
68
}
83
69
@@ -86,37 +72,44 @@ func (s *ReceivePackSuite) TearDownTest(c *C) {
86
72
c .Assert (err , IsNil )
87
73
}
88
74
89
- func freePort () (int , error ) {
90
- addr , err := net .ResolveTCPAddr ("tcp" , "localhost:0" )
91
- if err != nil {
92
- return 0 , err
93
- }
75
+ func (s * ReceivePackSuite ) prepareRepository (c * C , f * fixtures.Fixture , name string ) transport.Endpoint {
76
+ path := filepath .Join (s .base , name )
94
77
95
- l , err := net .ListenTCP ("tcp" , addr )
96
- if err != nil {
97
- return 0 , err
98
- }
78
+ err := os .Rename (f .DotGit ().Root (), path )
79
+ c .Assert (err , IsNil )
99
80
100
- return l .Addr ().(* net.TCPAddr ).Port , l .Close ()
81
+ s .setConfigToRepository (c , path )
82
+ return s .newEndpoint (c , name )
101
83
}
102
84
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
- }
85
+ // git-receive-pack refuses to update refs/heads/master on non-bare repo
86
+ // so we ensure bare repo config.
87
+ func (s * ReceivePackSuite ) setConfigToRepository (c * C , path string ) {
88
+ cfgPath := filepath .Join (path , "config" )
89
+ _ , err := os .Stat (cfgPath )
90
+ c .Assert (err , IsNil )
91
+
92
+ cfg , err := os .OpenFile (cfgPath , os .O_TRUNC | os .O_WRONLY , 0 )
93
+ c .Assert (err , IsNil )
94
+
95
+ content := strings .NewReader ("" +
96
+ "[core]\n " +
97
+ "repositoryformatversion = 0\n " +
98
+ "filemode = true\n " +
99
+ "bare = true\n " +
100
+ "[http]\n " +
101
+ "receivepack = true\n " ,
102
+ )
103
+
104
+ _ , err = io .Copy (cfg , content )
105
+ c .Assert (err , IsNil )
106
+
107
+ c .Assert (cfg .Close (), IsNil )
108
+ }
109
+
110
+ func (s * ReceivePackSuite ) newEndpoint (c * C , name string ) transport.Endpoint {
111
+ ep , err := transport .NewEndpoint (fmt .Sprintf ("http://localhost:%d/%s" , s .port , name ))
112
+ c .Assert (err , IsNil )
113
+
114
+ return ep
122
115
}
0 commit comments