Jelajahi Sumber

fix temp file generation

Darien Raymond 9 tahun lalu
induk
melakukan
7632618584

+ 28 - 7
testing/scenarios/server_env.go

@@ -9,19 +9,40 @@ import (
 
 	"v2ray.com/core/common/log"
 
+	"fmt"
+	"io/ioutil"
+	"sync"
 	_ "v2ray.com/core"
+	"v2ray.com/core/common/retry"
 )
 
 var (
-	runningServers = make([]*exec.Cmd, 0, 10)
+	runningServers    = make([]*exec.Cmd, 0, 10)
+	testBinaryPath    string
+	testBinaryPathGen sync.Once
 )
 
-func GetTestBinaryPath() string {
-	file := filepath.Join(os.Getenv("GOPATH"), "out", "v2ray", "v2ray.test")
-	if runtime.GOOS == "windows" {
-		file += ".exe"
-	}
-	return file
+func GenTestBinaryPath() {
+	testBinaryPathGen.Do(func() {
+		var tempDir string
+		err := retry.Timed(5, 100).On(func() error {
+			dir, err := ioutil.TempDir("", "v2ray")
+			if err != nil {
+				return err
+			}
+			tempDir = dir
+			return nil
+		})
+		if err != nil {
+			panic(err)
+		}
+		file := filepath.Join(tempDir, "v2ray.test")
+		if runtime.GOOS == "windows" {
+			file += ".exe"
+		}
+		testBinaryPath = file
+		fmt.Printf("Generated binary path: %s\n", file)
+	})
 }
 
 func GetSourcePath() string {

+ 5 - 5
testing/scenarios/server_env_coverage.go

@@ -11,21 +11,21 @@ import (
 )
 
 func BuildV2Ray() error {
-	binaryPath := GetTestBinaryPath()
-	if _, err := os.Stat(binaryPath); err == nil {
+	GenTestBinaryPath()
+	if _, err := os.Stat(testBinaryPath); err == nil {
 		return nil
 	}
 
-	cmd := exec.Command("go", "test", "-tags", "json coverage coveragemain", "-coverpkg", "v2ray.com/core/...", "-c", "-o", binaryPath, GetSourcePath())
+	cmd := exec.Command("go", "test", "-tags", "json coverage coveragemain", "-coverpkg", "v2ray.com/core/...", "-c", "-o", testBinaryPath, GetSourcePath())
 	return cmd.Run()
 }
 
 func RunV2Ray(configFile string) *exec.Cmd {
-	binaryPath := GetTestBinaryPath()
+	GenTestBinaryPath()
 
 	covDir := filepath.Join(os.Getenv("GOPATH"), "out", "v2ray", "cov")
 	profile := uuid.New().String() + ".out"
-	proc := exec.Command(binaryPath, "-config", configFile, "-test.run", "TestRunMainForCoverage", "-test.coverprofile", profile, "-test.outputdir", covDir)
+	proc := exec.Command(testBinaryPath, "-config", configFile, "-test.run", "TestRunMainForCoverage", "-test.coverprofile", profile, "-test.outputdir", covDir)
 	proc.Stderr = os.Stderr
 	proc.Stdout = os.Stdout
 

+ 7 - 5
testing/scenarios/server_env_regular.go

@@ -3,23 +3,25 @@
 package scenarios
 
 import (
+	"fmt"
 	"os"
 	"os/exec"
 )
 
 func BuildV2Ray() error {
-	binaryPath := GetTestBinaryPath()
-	if _, err := os.Stat(binaryPath); err == nil {
+	GenTestBinaryPath()
+	if _, err := os.Stat(testBinaryPath); err == nil {
 		return nil
 	}
 
-	cmd := exec.Command("go", "build", "-tags=json", "-o="+binaryPath, GetSourcePath())
+	fmt.Printf("Building V2Ray into path (%d)\n", testBinaryPath)
+	cmd := exec.Command("go", "build", "-tags=json", "-o="+testBinaryPath, GetSourcePath())
 	return cmd.Run()
 }
 
 func RunV2Ray(configFile string) *exec.Cmd {
-	binaryPath := GetTestBinaryPath()
-	proc := exec.Command(binaryPath, "-config", configFile)
+	GenTestBinaryPath()
+	proc := exec.Command(testBinaryPath, "-config", configFile)
 	proc.Stderr = os.Stderr
 	proc.Stdout = os.Stdout
 

+ 5 - 7
tools/build/build_test.go

@@ -2,6 +2,7 @@ package main
 
 import (
 	"fmt"
+	"io/ioutil"
 	"os"
 	"path/filepath"
 	"testing"
@@ -9,11 +10,6 @@ import (
 	"v2ray.com/core/testing/assert"
 )
 
-func cleanBinPath() {
-	os.RemoveAll(binPath)
-	os.Mkdir(binPath, os.ModeDir|0777)
-}
-
 func fileExists(file string) bool {
 	_, err := os.Stat(file)
 	return err == nil
@@ -32,8 +28,10 @@ func allFilesExists(files ...string) bool {
 
 func TestBuildMacOS(t *testing.T) {
 	assert := assert.On(t)
-	binPath = filepath.Join(os.Getenv("GOPATH"), "testing")
-	cleanBinPath()
+	tmpPath, err := ioutil.TempDir("", "v2ray")
+	assert.Error(err).IsNil()
+
+	binPath = tmpPath
 
 	build("macos", "amd64", true, "test", "metadata.txt")
 	assert.Bool(allFilesExists(