diff --git a/shell/point/main/main_test.go b/shell/point/main/main_test.go new file mode 100644 index 000000000..63c18234e --- /dev/null +++ b/shell/point/main/main_test.go @@ -0,0 +1,11 @@ +// +build coveragemain + +package main + +import ( + "testing" +) + +func TestRunMainForCoverage(t *testing.T) { + main() +} diff --git a/testing/coverage/coverall b/testing/coverage/coverall index 87fc15624..5aa3823e2 100755 --- a/testing/coverage/coverall +++ b/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 diff --git a/testing/scenarios/server_env.go b/testing/scenarios/server_env.go index 7e49112bc..541d387d3 100644 --- a/testing/scenarios/server_env.go +++ b/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 { diff --git a/testing/scenarios/server_env_coverage.go b/testing/scenarios/server_env_coverage.go new file mode 100644 index 000000000..ae46fcb14 --- /dev/null +++ b/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 +} diff --git a/testing/scenarios/server_env_regular.go b/testing/scenarios/server_env_regular.go new file mode 100644 index 000000000..3ecfc5286 --- /dev/null +++ b/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 +}