Browse Source

More test case for building

V2Ray 10 năm trước cách đây
mục cha
commit
581e6b7104
2 tập tin đã thay đổi với 72 bổ sung25 xóa
  1. 28 17
      tools/build/build.go
  2. 44 8
      tools/build/build_test.go

+ 28 - 17
tools/build/build.go

@@ -11,16 +11,17 @@ import (
 )
 
 var (
-	targetOS   = flag.String("os", runtime.GOOS, "Target OS of this build.")
-	targetArch = flag.String("arch", runtime.GOARCH, "Target CPU arch of this build.")
-	archive    = flag.Bool("zip", false, "Whether to make an archive of files or not.")
+	flagTargetOS   = flag.String("os", runtime.GOOS, "Target OS of this build.")
+	flagTargetArch = flag.String("arch", runtime.GOARCH, "Target CPU arch of this build.")
+	flagArchive    = flag.Bool("zip", false, "Whether to make an archive of files or not.")
+
+	binPath string
 )
 
 func createTargetDirectory(version string, goOS GoOS, goArch GoArch) (string, error) {
 	suffix := getSuffix(goOS, goArch)
-	GOPATH := os.Getenv("GOPATH")
 
-	targetDir := filepath.Join(GOPATH, "bin", "v2ray-"+version+suffix)
+	targetDir := filepath.Join(binPath, "v2ray-"+version+suffix)
 	if version != "custom" {
 		os.RemoveAll(targetDir)
 	}
@@ -36,19 +37,31 @@ func getTargetFile(goOS GoOS) string {
 	return "v2ray" + suffix
 }
 
+func getBinPath() string {
+	GOPATH := os.Getenv("GOPATH")
+	return filepath.Join(GOPATH, "bin")
+}
+
 func main() {
 	flag.Parse()
+	binPath = getBinPath()
+	build(*flagTargetOS, *flagTargetArch, *flagArchive, "")
+}
 
-	v2rayOS := parseOS(*targetOS)
-	v2rayArch := parseArch(*targetArch)
+func build(targetOS, targetArch string, archive bool, version string) {
+	v2rayOS := parseOS(targetOS)
+	v2rayArch := parseArch(targetArch)
 
-	version, err := git.RepoVersionHead()
-	if version == git.VersionUndefined {
-		version = "custom"
-	}
-	if err != nil {
-		fmt.Println("Unable to detect V2Ray version: " + err.Error())
-		return
+	if len(version) == 0 {
+		v, err := git.RepoVersionHead()
+		if v == git.VersionUndefined {
+			v = "custom"
+		}
+		if err != nil {
+			fmt.Println("Unable to detect V2Ray version: " + err.Error())
+			return
+		}
+    version = v
 	}
 	fmt.Printf("Building V2Ray (%s) for %s %s\n", version, v2rayOS, v2rayArch)
 
@@ -68,9 +81,7 @@ func main() {
 		fmt.Println("Unable to copy config files: " + err.Error())
 	}
 
-	if *archive {
-		GOPATH := os.Getenv("GOPATH")
-		binPath := filepath.Join(GOPATH, "bin")
+	if archive {
 		err := os.Chdir(binPath)
 		if err != nil {
 			fmt.Printf("Unable to switch to directory (%s): %v\n", binPath, err)

+ 44 - 8
tools/build/build_test.go

@@ -1,23 +1,59 @@
 package main
 
 import (
+	"fmt"
 	"os"
+	"path/filepath"
 	"testing"
 
 	"github.com/v2ray/v2ray-core/testing/unit"
 )
 
+func cleanBinPath() {
+	os.RemoveAll(binPath)
+	os.Mkdir(binPath, os.ModeDir|0777)
+}
+
+func fileExists(file string) bool {
+	_, err := os.Stat(file)
+	return err == nil
+}
+
+func allFilesExists(files ...string) bool {
+	for _, file := range files {
+		fullPath := filepath.Join(binPath, file)
+		if !fileExists(fullPath) {
+			fmt.Println(fullPath + " doesn't exist.")
+			return false
+		}
+	}
+	return true
+}
+
 func TestBuildMacOS(t *testing.T) {
 	assert := unit.Assert(t)
+	binPath = filepath.Join(os.Getenv("GOPATH"), "testing")
+	cleanBinPath()
 
-	targetFile := os.ExpandEnv("$GOPATH/bin/v2ray-macos.zip")
-	os.Remove(targetFile)
+	build("macos", "amd64", true, "test")
+	assert.Bool(allFilesExists(
+		"v2ray-macos.zip",
+		"v2ray-test-macos",
+		filepath.Join("v2ray-test-macos", "config.json"),
+		filepath.Join("v2ray-test-macos", "v2ray"))).IsTrue()
 
-	*targetOS = "macos"
-	*targetArch = "amd64"
-	*archive = true
-	main()
+	build("windows", "amd64", true, "test")
+	assert.Bool(allFilesExists(
+		"v2ray-windows-64.zip",
+		"v2ray-test-windows-64",
+		filepath.Join("v2ray-test-windows-64", "config.json"),
+		filepath.Join("v2ray-test-windows-64", "v2ray.exe"))).IsTrue()
 
-	_, err := os.Stat(targetFile)
-	assert.Error(err).IsNil()
+	build("linux", "amd64", true, "test")
+	assert.Bool(allFilesExists(
+		"v2ray-linux-64.zip",
+		"v2ray-test-linux-64",
+		filepath.Join("v2ray-test-linux-64", "vpoint_socks_vmess.json"),
+		filepath.Join("v2ray-test-linux-64", "vpoint_vmess_freedom.json"),
+		filepath.Join("v2ray-test-linux-64", "v2ray"))).IsTrue()
 }