@@ -25,59 +25,44 @@ type ReceivePackSuite struct {
25
25
fixtures.Suite
26
26
27
27
base string
28
+ host string
29
+ port int
28
30
}
29
31
30
32
var _ = Suite (& ReceivePackSuite {})
31
33
32
34
func (s * ReceivePackSuite ) SetUpTest (c * C ) {
33
35
s .ReceivePackSuite .Client = DefaultClient
34
36
35
- port , err := freePort ( )
37
+ l , err := net . Listen ( "tcp" , "localhost:0" )
36
38
c .Assert (err , IsNil )
37
39
38
40
base , err := ioutil .TempDir (os .TempDir (), "go-git-http-backend-test" )
39
41
c .Assert (err , IsNil )
40
- s .base = base
41
42
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 )
60
46
61
- ep , err = transport . NewEndpoint ( fmt . Sprintf ( "http://localhost:%d/empty.git" , port ) )
47
+ err = os . MkdirAll ( s . base , 0755 )
62
48
c .Assert (err , IsNil )
63
- s .ReceivePackSuite .EmptyEndpoint = ep
64
49
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" )
68
53
69
54
cmd := exec .Command ("git" , "--exec-path" )
70
55
out , err := cmd .CombinedOutput ()
71
56
c .Assert (err , IsNil )
72
- p := filepath .Join (strings .Trim (string (out ), "\n " ), "git-http-backend" )
73
57
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
+ },
77
63
}
78
-
79
64
go func () {
80
- log .Fatal (http . ListenAndServe ( fmt . Sprintf ( ":%d" , port ), h ))
65
+ log .Fatal (server . Serve ( l ))
81
66
}()
82
67
}
83
68
@@ -86,37 +71,44 @@ func (s *ReceivePackSuite) TearDownTest(c *C) {
86
71
c .Assert (err , IsNil )
87
72
}
88
73
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 )
94
76
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 )
99
79
100
- return l .Addr ().(* net.TCPAddr ).Port , l .Close ()
80
+ s .setConfigToRepository (c , path )
81
+ return s .newEndpoint (c , name )
101
82
}
102
83
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
122
114
}
0 commit comments