generator.go 5.6 KB
Newer Older
aaronchen2k2k's avatar
aaronchen2k2k 已提交
1
package scriptHelper
aaronchen2k2k's avatar
aaronchen2k2k 已提交
2 3 4

import (
	"fmt"
雨爱无痕 已提交
5 6 7 8
	"path/filepath"
	"strconv"
	"strings"

雨爱无痕 已提交
9 10
	commConsts "github.com/easysoft/zentaoatf/internal/pkg/consts"

aaronchen2k2k's avatar
aaronchen2k2k 已提交
11 12 13 14 15 16
	commDomain "github.com/easysoft/zentaoatf/internal/pkg/domain"
	"github.com/easysoft/zentaoatf/pkg/consts"
	fileUtils "github.com/easysoft/zentaoatf/pkg/lib/file"
	i118Utils "github.com/easysoft/zentaoatf/pkg/lib/i118"
	resUtils "github.com/easysoft/zentaoatf/pkg/lib/res"
	stdinUtils "github.com/easysoft/zentaoatf/pkg/lib/stdin"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
17 18
)

aaronchen2k2k's avatar
aaronchen2k2k 已提交
19
func GenerateScripts(cases []commDomain.ZtfCase, langType string, independentFile bool,
雨爱无痕 已提交
20
	byModule bool, targetDir string) (pths []string, realPath string, err error) {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
21 22
	caseIds := make([]string, 0)

aaronchen2k2k's avatar
aaronchen2k2k 已提交
23
	if commConsts.ExecFrom == commConsts.FromCmd { // from cmd
aaronchen2k2k's avatar
aaronchen2k2k 已提交
24 25 26 27
		targetDir = stdinUtils.GetInput("", targetDir, "where_to_store_script", targetDir)
		stdinUtils.InputForBool(&byModule, byModule, "co_organize_by_module")
	}
	targetDir = fileUtils.AbsolutePath(targetDir)
雨爱无痕 已提交
28
	realPath = targetDir
aaronchen2k2k's avatar
aaronchen2k2k 已提交
29

aaronchen2k2k's avatar
aaronchen2k2k 已提交
30
	createNew := false
aaronchen2k2k's avatar
aaronchen2k2k 已提交
31
	for _, cs := range cases {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
32 33
		pth, _ := GenerateScript(cs, langType, independentFile, &caseIds, targetDir, byModule)
		pths = append(pths, pth)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
34 35 36 37

		if cs.ScriptPath == "" {
			createNew = true
		}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
38 39
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
40 41 42
	if createNew {
		GenSuite(caseIds, targetDir)
	}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
43

aaronchen2k2k's avatar
aaronchen2k2k 已提交
44
	return
aaronchen2k2k's avatar
aaronchen2k2k 已提交
45 46
}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
47
func GenerateScript(cs commDomain.ZtfCase, langType string, independentFile bool, caseIds *[]string,
aaronchen2k2k's avatar
aaronchen2k2k 已提交
48 49
	targetDir string, byModule bool) (scriptPath string, err error) {

aaronchen2k2k's avatar
aaronchen2k2k 已提交
50 51 52 53 54
	caseId := cs.Id
	productId := cs.Product
	moduleId := cs.Module
	caseTitle := cs.Title

aaronchen2k2k's avatar
aaronchen2k2k 已提交
55
	if byModule {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
56
		targetDir = filepath.Join(targetDir, strconv.Itoa(moduleId))
aaronchen2k2k's avatar
aaronchen2k2k 已提交
57 58
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
59
	scriptPath = cs.ScriptPath
aaronchen2k2k's avatar
aaronchen2k2k 已提交
60
	if scriptPath == "" {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
61
		fileUtils.MkDirIfNeeded(targetDir)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
62 63 64
		scriptPath = filepath.Join(targetDir, fmt.Sprintf("%d.%s", caseId, commConsts.LangMap[langType]["extName"]))
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
65
	*caseIds = append(*caseIds, strconv.Itoa(caseId))
aaronchen2k2k's avatar
aaronchen2k2k 已提交
66 67 68 69

	info := make([]string, 0)
	steps := make([]string, 0)
	independentExpects := make([]string, 0)
70
	srcCode := fmt.Sprintf("%s %s", commConsts.LangMap[langType]["commentsTag"],
71
		i118Utils.Sprintf("find_example", consts.FilePthSep, langType))
aaronchen2k2k's avatar
aaronchen2k2k 已提交
72 73

	info = append(info, fmt.Sprintf("title=%s", caseTitle))
雨爱无痕 已提交
74
	info = append(info, fmt.Sprintf("timeout=%d", 0))
aaronchen2k2k's avatar
aaronchen2k2k 已提交
75 76
	info = append(info, fmt.Sprintf("cid=%d", caseId))
	info = append(info, fmt.Sprintf("pid=%d", productId))
aaronchen2k2k's avatar
aaronchen2k2k 已提交
77 78 79

	StepWidth := 20
	stepDisplayMaxWidth := 0
aaronchen2k2k's avatar
aaronchen2k2k 已提交
80
	computerTestStepWidth(cs.Steps, &stepDisplayMaxWidth, StepWidth)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
81
	generateTestStepAndScript(cs.Steps, &steps, &independentExpects, independentFile)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
82 83 84 85

	info = append(info, strings.Join(steps, "\n"))

	if independentFile {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
86
		expectFile := ScriptToExpectName(scriptPath)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
87 88 89
		fileUtils.WriteFile(expectFile, strings.Join(independentExpects, "\n"))
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
90
	if fileUtils.FileExist(scriptPath) { // update title and steps
aaronchen2k2k's avatar
aaronchen2k2k 已提交
91
		newContent := strings.Join(info, "\n")
aaronchen2k2k's avatar
aaronchen2k2k 已提交
92
		ReplaceCaseDesc(newContent, scriptPath)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
93 94 95
		return
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
96 97
	templatePath := fmt.Sprintf("res%stemplate%s", consts.FilePthSep, consts.FilePthSep)
	template, _ := resUtils.ReadRes(templatePath + langType + ".tpl")
aaronchen2k2k's avatar
aaronchen2k2k 已提交
98 99

	out := fmt.Sprintf(string(template), strings.Join(info, "\n"), srcCode)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
100 101 102
	fileUtils.WriteFile(scriptPath, out)

	return
aaronchen2k2k's avatar
aaronchen2k2k 已提交
103 104
}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
105 106 107 108 109 110
func GenEmptyScript(name, lang, pth string, productId int) {
	srcCode := fmt.Sprintf("%s %s", commConsts.LangMap[lang]["commentsTag"],
		i118Utils.Sprintf("find_example", consts.FilePthSep, lang))

	info := make([]string, 0)
	info = append(info, fmt.Sprintf("title=%s", name))
雨爱无痕 已提交
111
	info = append(info, fmt.Sprintf("timeout=%d", 0))
aaronchen2k2k's avatar
aaronchen2k2k 已提交
112 113 114 115 116 117 118 119 120 121
	info = append(info, fmt.Sprintf("cid=%d", 0))
	info = append(info, fmt.Sprintf("pid=%d", productId))

	templatePath := fmt.Sprintf("res%stemplate%s", consts.FilePthSep, consts.FilePthSep)
	template, _ := resUtils.ReadRes(templatePath + lang + ".tpl")

	out := fmt.Sprintf(string(template), strings.Join(info, "\n"), srcCode)
	fileUtils.WriteFile(pth, out)
}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
func generateTestStepAndScript(testSteps []commDomain.ZtfStep, steps *[]string, independentExpects *[]string, independentFile bool) {
	nestedSteps := make([]commDomain.ZtfStep, 0)

	// convert steps to nested
	for index := 0; index < len(testSteps); index++ {
		ts := testSteps[index]
		item := commDomain.ZtfStep{Desc: ts.Desc, Expect: ts.Expect, Children: make([]commDomain.ZtfStep, 0)}

		if ts.Type == "group" {
			nestedSteps = append(nestedSteps, item)
		} else if ts.Type == "item" {
			nestedSteps[len(nestedSteps)-1].Children = append(nestedSteps[len(nestedSteps)-1].Children, item)
		} else if ts.Type == "step" {
			nestedSteps = append(nestedSteps, item)
		}
	}

	// print nested steps, only one level
	stepNumb := 1
	*steps = append(*steps, "")
	for _, item := range nestedSteps {
		numbStr := fmt.Sprintf("%d", stepNumb)
144 145
		stepLines1, expects1 := GetCaseContent(item, numbStr, independentFile, false)
		*steps = append(*steps, stepLines1)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
146 147 148 149

		if independentFile && strings.TrimSpace(item.Expect) != "" {
			*independentExpects = append(*independentExpects, expects1)
		}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
150 151 152

		for childNo, child := range item.Children {
			numbStr := fmt.Sprintf("%d.%d", stepNumb, childNo+1)
153 154
			stepLines2, expects2 := GetCaseContent(child, numbStr, independentFile, true)
			*steps = append(*steps, stepLines2)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
155 156

			if independentFile && strings.TrimSpace(child.Expect) != "" {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
157
				*independentExpects = append(*independentExpects, expects2)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
158 159 160 161 162 163 164 165 166
			}
		}

		stepNumb++
	}
}

func computerTestStepWidth(steps []commDomain.ZtfStep, stepSDisplayMaxWidth *int, stepWidth int) {
	for _, ts := range steps {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
167
		length := len(strconv.Itoa(ts.Id))
aaronchen2k2k's avatar
aaronchen2k2k 已提交
168 169 170 171 172 173 174 175 176 177 178 179
		if length > *stepSDisplayMaxWidth {
			*stepSDisplayMaxWidth = length
		}
	}
	*stepSDisplayMaxWidth += stepWidth // prefix space and @step
}

func GenSuite(cases []string, targetDir string) {
	str := strings.Join(cases, "\n")

	fileUtils.WriteFile(targetDir+"all."+commConsts.ExtNameSuite, str)
}