ztf.go 2.5 KB
Newer Older
aaronchen2k2k's avatar
aaronchen2k2k 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
package commonTestHelper

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"os/exec"
	"runtime"
	"strconv"
	"strings"
	"time"

	constTestHelper "github.com/easysoft/zentaoatf/test/helper/conf"
	uiTest "github.com/easysoft/zentaoatf/test/helper/zentao/ui"
)

func BuildCli() (err error) {
	outPath := fmt.Sprintf("%s%s", constTestHelper.RootPath, "ztf")
	cliPath := `./cmd/command/main.go`
	if runtime.GOOS == "windows" {
		cliPath = `.\cmd\command\main.go`
		outPath += ".exe"
	}

	_, err = os.Stat(outPath)
	if err != nil && os.IsExist(err) {
		os.Remove(outPath)
	}

	var cmd *exec.Cmd
	cmd = exec.Command("go", "build", "-o", outPath, cliPath)
	cmd.Dir = constTestHelper.RootPath
	fmt.Println(cmd.String())

	_, err = cmd.CombinedOutput()
	if err != nil {
		return
	}

	return
}

func RunServer() (err error) {
	ztfPath := GetZtfPath()

	var cmd *exec.Cmd
	cmd = exec.Command(ztfPath, "-P", "8085", "-uuid=ui_auto_test")
	cmd.Dir = constTestHelper.RootPath

	fmt.Println(cmd.String())
	err = cmd.Start()
	if err != nil {
		return
	}

	return
}

func RunUi() (err error) {
	var cmd *exec.Cmd
	cmd = exec.Command("yarn", "run", "serve", "--port", strconv.Itoa(constTestHelper.UiPort), "-uuid=ui_auto_test")
	cmd.Dir = constTestHelper.RootPath + constTestHelper.FilePthSep + "ui"

	fmt.Println(cmd.String())
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		return
	}

	err = cmd.Start()
	if err != nil {
		return
	}

	reader1 := bufio.NewReader(stdout)
	go func() {
		for {
			line, err2 := reader1.ReadString('\n')
			line = strings.TrimSpace(line)
			fmt.Println(line)
			if err2 != nil {
				return
			}

			if err != nil || io.EOF == err {
				break
			}
		}
	}()

	WaitZtfAccessed()
	return
}

func GetZtfPath() string {
	ztfPath := fmt.Sprintf("%s%s", constTestHelper.RootPath, "ztf")
	if runtime.GOOS == "windows" {
		ztfPath += ".exe"
	}
	return ztfPath
}

func GetZtfProductPath() string {
	return fmt.Sprintf("%s%s%s%s%s%s%s%s", constTestHelper.RootPath, "test", constTestHelper.FilePthSep, "demo", constTestHelper.FilePthSep, "php", constTestHelper.FilePthSep, "product1")
}

func GetPhpWorkspacePath() string {
	return fmt.Sprintf("%s%s%s%s%s%s%s", constTestHelper.RootPath, "test", constTestHelper.FilePthSep, "demo", constTestHelper.FilePthSep, "php", constTestHelper.FilePthSep)
}

func WaitZtfAccessed() {
	isTimeout := false
	time.AfterFunc(120*time.Second, func() {
		isTimeout = true
	})

	for {
		status := uiTest.GetStatus(constTestHelper.ZtfUrl)
		if isTimeout || status {
			return
		}

		time.Sleep(time.Second)
	}
}