diff --git a/testing/scenarios/server_env.go b/testing/scenarios/server_env.go index b211a0f2..d2200cc4 100644 --- a/testing/scenarios/server_env.go +++ b/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 { diff --git a/testing/scenarios/server_env_coverage.go b/testing/scenarios/server_env_coverage.go index 9d1b1073..4d37cf97 100644 --- a/testing/scenarios/server_env_coverage.go +++ b/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 diff --git a/testing/scenarios/server_env_regular.go b/testing/scenarios/server_env_regular.go index 307bb93e..29a6ee2e 100644 --- a/testing/scenarios/server_env_regular.go +++ b/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 diff --git a/tools/build/build_test.go b/tools/build/build_test.go index 82842d2c..20dc7299 100644 --- a/tools/build/build_test.go +++ b/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(