v2ray 9 лет назад
Родитель
Сommit
697d44abbd

+ 11 - 0
shell/point/main/main_test.go

@@ -0,0 +1,11 @@
+// +build coveragemain
+
+package main
+
+import (
+	"testing"
+)
+
+func TestRunMainForCoverage(t *testing.T) {
+	main()
+}

+ 1 - 1
testing/coverage/coverall

@@ -7,7 +7,7 @@ function test_package {
   DIR="github.com/v2ray/v2ray-core/$1"
   DEP=$(go list -f '{{ join .Deps "\n" }}' $DIR | grep v2ray | tr '\n' ',')
   DEP=${DEP}$DIR
-  go test -tags json -coverprofile=coversingle.out -coverpkg=$DEP $DIR || FAIL=1
+  go test -tags "json coverage" -coverprofile=coversingle.out -coverpkg=$DEP $DIR || FAIL=1
   if [ -f coversingle.out ]; then
     cat coversingle.out | grep -v "mode: set" >> ${COVERAGE_FILE}
     rm coversingle.out

+ 6 - 9
testing/scenarios/server_env.go

@@ -28,11 +28,11 @@ var (
 	binaryPath string
 )
 
-func BuildV2Ray() error {
-	if len(binaryPath) > 0 {
-		return nil
-	}
+func GetSourcePath() string {
+	return filepath.Join("github.com", "v2ray", "v2ray-core", "shell", "point", "main")
+}
 
+func FillBinaryPath() error {
 	dir, err := ioutil.TempDir("", "v2ray")
 	if err != nil {
 		return err
@@ -41,8 +41,7 @@ func BuildV2Ray() error {
 	if runtime.GOOS == "windows" {
 		binaryPath += ".exe"
 	}
-	cmd := exec.Command("go", "build", "-tags=json", "-o="+binaryPath, filepath.Join("github.com", "v2ray", "v2ray-core", "shell", "point", "main"))
-	return cmd.Run()
+	return nil
 }
 
 func TestFile(filename string) string {
@@ -73,9 +72,7 @@ func InitializeServer(configFile string) error {
 		return err
 	}
 
-	proc := exec.Command(binaryPath, "-config="+configFile)
-	proc.Stderr = os.Stderr
-	proc.Stdout = os.Stdout
+	proc := RunV2Ray(configFile)
 
 	err = proc.Start()
 	if err != nil {

+ 29 - 0
testing/scenarios/server_env_coverage.go

@@ -0,0 +1,29 @@
+// +build coverage
+
+package scenarios
+
+import (
+	"os"
+	"os/exec"
+)
+
+func BuildV2Ray() error {
+	if _, err := os.Stat(binaryPath); err == nil {
+		return nil
+	}
+
+	if err := FillBinaryPath(); err != nil {
+		return err
+	}
+
+	cmd := exec.Command("go", "test", "-tags", "json coverage coveragemain", "-coverpkg", "github.com/v2ray/v2ray-core/...", "-c", "-o", binaryPath, GetSourcePath())
+	return cmd.Run()
+}
+
+func RunV2Ray(configFile string) *exec.Cmd {
+	proc := exec.Command(binaryPath, "-config="+configFile, "-test.run=TestRunMainForCoverage", "-test.coverprofile=coversingle.out")
+	proc.Stderr = os.Stderr
+	proc.Stdout = os.Stdout
+
+	return proc
+}

+ 29 - 0
testing/scenarios/server_env_regular.go

@@ -0,0 +1,29 @@
+// +build !coverage
+
+package scenarios
+
+import (
+	"os"
+	"os/exec"
+)
+
+func BuildV2Ray() error {
+	if _, err := os.Stat(binaryPath); err == nil {
+		return nil
+	}
+
+	if err := FillBinaryPath(); err != nil {
+		return err
+	}
+
+	cmd := exec.Command("go", "build", "-tags=json", "-o="+binaryPath, GetSourcePath())
+	return cmd.Run()
+}
+
+func RunV2Ray(configFile string) *exec.Cmd {
+	proc := exec.Command(binaryPath, "-config="+configFile)
+	proc.Stderr = os.Stderr
+	proc.Stdout = os.Stdout
+
+	return proc
+}