Browse Source

Add git clone test on integration test (#1682)

Lunny Xiao 1 year ago
parent
commit
f70758dec9
2 changed files with 65 additions and 2 deletions
  1. 60 0
      integrations/git_test.go
  2. 5 2
      integrations/integration_test.go

+ 60 - 0
integrations/git_test.go

@@ -0,0 +1,60 @@
1
+// Copyright 2017 The Gitea Authors. All rights reserved.
2
+// Use of this source code is governed by a MIT-style
3
+// license that can be found in the LICENSE file.
4
+
5
+package integrations
6
+
7
+import (
8
+	"context"
9
+	"fmt"
10
+	"io/ioutil"
11
+	"net"
12
+	"net/http"
13
+	"os"
14
+	"path/filepath"
15
+	"testing"
16
+	"time"
17
+
18
+	"code.gitea.io/git"
19
+
20
+	"github.com/Unknwon/com"
21
+	"github.com/stretchr/testify/assert"
22
+)
23
+
24
+func onGiteaWebRun(t *testing.T, callback func(*testing.T, string)) {
25
+	s := http.Server{
26
+		Handler: mac,
27
+	}
28
+
29
+	listener, err := net.Listen("tcp", "")
30
+	assert.NoError(t, err)
31
+
32
+	defer func() {
33
+		ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
34
+		s.Shutdown(ctx)
35
+		cancel()
36
+	}()
37
+
38
+	go s.Serve(listener)
39
+
40
+	_, port, err := net.SplitHostPort(listener.Addr().String())
41
+	assert.NoError(t, err)
42
+
43
+	callback(t, fmt.Sprintf("http://localhost:%s/", port))
44
+}
45
+
46
+func TestClone_ViaHTTP_NoLogin(t *testing.T) {
47
+	prepareTestEnv(t)
48
+
49
+	onGiteaWebRun(t, func(t *testing.T, urlPrefix string) {
50
+		dstPath, err := ioutil.TempDir("", "repo1")
51
+		assert.NoError(t, err)
52
+		defer os.RemoveAll(dstPath)
53
+
54
+		err = git.Clone(fmt.Sprintf("%suser2/repo1.git", urlPrefix),
55
+			dstPath, git.CloneRepoOptions{})
56
+		assert.NoError(t, err)
57
+
58
+		assert.True(t, com.IsExist(filepath.Join(dstPath, "README.md")))
59
+	})
60
+}

+ 5 - 2
integrations/integration_test.go

@@ -16,6 +16,7 @@ import (
16 16
 	"net/url"
17 17
 	"os"
18 18
 	"path"
19
+	"path/filepath"
19 20
 	"strings"
20 21
 	"testing"
21 22
 
@@ -51,7 +52,7 @@ func TestMain(m *testing.M) {
51 52
 
52 53
 	err := models.InitFixtures(
53 54
 		helper,
54
-		"models/fixtures/",
55
+		path.Join(filepath.Dir(setting.AppPath), "models/fixtures/"),
55 56
 	)
56 57
 	if err != nil {
57 58
 		fmt.Printf("Error initializing test database: %v\n", err)
@@ -134,7 +135,9 @@ func initIntegrationTest() {
134 135
 func prepareTestEnv(t testing.TB) {
135 136
 	assert.NoError(t, models.LoadFixtures())
136 137
 	assert.NoError(t, os.RemoveAll(setting.RepoRootPath))
137
-	assert.NoError(t, com.CopyDir("integrations/gitea-repositories-meta", setting.RepoRootPath))
138
+
139
+	assert.NoError(t, com.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"),
140
+		setting.RepoRootPath))
138 141
 }
139 142
 
140 143
 type TestSession struct {