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

import (
	"fmt"
5 6 7
	"io/ioutil"
	"path"
	"path/filepath"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
8 9 10
	"regexp"
	"strconv"
	"strings"
Z
zhaoke 已提交
11 12 13 14 15 16 17

	commConsts "github.com/easysoft/zentaoatf/internal/pkg/consts"
	commDomain "github.com/easysoft/zentaoatf/internal/pkg/domain"
	langHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/lang"
	"github.com/easysoft/zentaoatf/pkg/consts"
	commonUtils "github.com/easysoft/zentaoatf/pkg/lib/common"
	fileUtils "github.com/easysoft/zentaoatf/pkg/lib/file"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
18 19
)

aaronchen2k2k's avatar
aaronchen2k2k 已提交
20 21 22 23 24
func GetStepAndExpectMap(file string) (steps []commDomain.ZentaoCaseStep) {
	if !fileUtils.FileExist(file) {
		return
	}

25
	lang := langHelper.GetLangByFile(file)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
26
	content := fileUtils.ReadFile(file)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
27

aaronchen2k2k's avatar
aaronchen2k2k 已提交
28 29 30 31 32
	info, checkpoints := ReadCaseInfoInOldFormat(content, lang)
	if info != "" {
		steps = GetStepAndExpectMapInOldFormat(checkpoints, file)
		return
	}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
33

aaronchen2k2k's avatar
aaronchen2k2k 已提交
34
	_, _, steps = ReadTitleAndStepsInNewFormat(content, lang)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
35

aaronchen2k2k's avatar
aaronchen2k2k 已提交
36 37
	return
}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
38

aaronchen2k2k's avatar
aaronchen2k2k 已提交
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
func ReadTitleAndStepsInNewFormat(content, lang string) (caseId int, title string, steps []commDomain.ZentaoCaseStep) {
	//测试用例标题 #1
	//- 步骤1 @期待结果1
	//- 步骤2
	//- 子步骤2.1 @{
	//	期待结果2.1.1
	//	期待结果2.1.2
	//}
	//- 子步骤2.2 @期待结果2.2
	//- 步骤3 @期待结果3

	comments := strings.TrimSpace(getScriptComments(content, lang))
	index := 0
	titleLineStart := false
	lines := strings.Split(comments, "\n")
	for index < len(lines) {
		line := lines[index]

		if !titleLineStart {
			caseId, title = findTitle(line)
			if title != "" {
				titleLineStart = true
				index += 1
				continue
			}
		}

		isStepLine, descAndExpect, isChild := isStepLine(line)
		if !isStepLine {
			index += 1
			continue
		}

		isMultiExpect, desc2 := isMultiLineExpectStart(line)
		step := commDomain.ZentaoCaseStep{}

		if isMultiExpect { // more than one line
			index += 1
			step.Desc = desc2
			step.Expect = getMultiExpect(lines, &index)
		} else {
			step.Desc, step.Expect = getSingleExpect(descAndExpect)
		}

		step.Type = commConsts.Group
		if isChild {
			step.Type = commConsts.Item
		}

		steps = append(steps, step)

		index += 1
	}

	return
aaronchen2k2k's avatar
aaronchen2k2k 已提交
94
}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
95

aaronchen2k2k's avatar
aaronchen2k2k 已提交
96 97 98 99 100
func getSingleExpect(descAndExpect string) (desc, expect string) {
	arr := strings.Split(descAndExpect, "@")

	desc = strings.TrimSpace(arr[0])
	if len(arr) > 1 {
101 102 103 104
		expect = strings.TrimSpace(arr[1])
		if expect == "" {
			expect = "pass"
		}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
	}

	return
}

func findTitle(line string) (id int, title string) {
	reg := `(.*)#(\d*)`
	arr := regexp.MustCompile(reg).FindStringSubmatch(line)
	if len(arr) > 2 {
		var err error
		id, err = strconv.Atoi(arr[2])
		if err == nil {
			title = strings.TrimSpace(arr[1])
		}
	}

	return
}

func getMultiExpect(lines []string, index *int) (ret string) {
	var arr []string

	for *index < len(lines) {
		line := strings.TrimSpace(lines[*index])
		if isMultiLineExpectEnd(line) {
			break
		}

		arr = append(arr, line)

		*index += 1
	}

	ret = strings.Join(arr, "\r\n")
	return
}

func isStepLine(line string) (is bool, ret string, isChild bool) {
	reg := `^(\s*)-\s*(.+)$`
	arr := regexp.MustCompile(reg).FindStringSubmatch(line)
	if len(arr) > 2 {
		is = true
		ret = arr[2]

		if len(arr[1]) > 0 {
			isChild = true
		}
	}

	return
}
func isMultiLineExpectStart(line string) (is bool, step string) {
	reg := `^\s*-\s*(.+)@\s*\{\s*$`
	arr := regexp.MustCompile(reg).FindStringSubmatch(line)
	if len(arr) > 1 {
		is = true
		step = arr[1]
	}

	return
}
func isMultiLineExpectEnd(line string) (is bool) {
	is = strings.TrimSpace(line) == "}"
	return
}

func ReadCaseInfoInOldFormat(content, lang string) (info, checkpoints string) {
	regStr := fmt.Sprintf(`(?smU)%s((?U:.*pid.*))\n(.*)%s`,
		commConsts.LangCommentsRegxMap[lang][0], commConsts.LangCommentsRegxMap[lang][1])

	myExp := regexp.MustCompile(regStr)
	arr := myExp.FindStringSubmatch(content)

	if len(arr) > 2 {
		info = strings.TrimSpace(arr[1])
		checkpoints = strings.TrimSpace(arr[2])

aaronchen2k2k's avatar
aaronchen2k2k 已提交
182 183 184
		return
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
185 186
	return
}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
187

aaronchen2k2k's avatar
aaronchen2k2k 已提交
188
func GetStepAndExpectMapInOldFormat(checkpoints, file string) (steps []commDomain.ZentaoCaseStep) {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
189 190
	lines := strings.Split(checkpoints, "\n")

191
	groupArr := getStepNestedArrInOldFormat(lines)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
192
	_, steps = getSortedTextFromNestedSteps(groupArr)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
193

194 195
	isIndependent, expectIndependentContent := GetDependentExpect(file)
	if isIndependent {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
196
		GetExpectMapFromIndependentFile(&steps, expectIndependentContent, false)
197 198 199 200 201
	}

	return
}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
202 203 204
func ReadCaseId(content string) string {
	myExp := regexp.MustCompile(`(?s).*\ncid=((?U:.*))\n.*`)
	arr := myExp.FindStringSubmatch(content)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
205

aaronchen2k2k's avatar
aaronchen2k2k 已提交
206 207 208 209
	if len(arr) > 1 {
		id := strings.TrimSpace(arr[1])
		return id
	}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
210

aaronchen2k2k's avatar
aaronchen2k2k 已提交
211 212
	return ""
}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
213

aaronchen2k2k's avatar
aaronchen2k2k 已提交
214 215 216 217
func GetDependentExpect(file string) (bool, string) {
	dir := fileUtils.AddFilePathSepIfNeeded(filepath.Dir(file))
	name := strings.Replace(filepath.Base(file), path.Ext(file), ".exp", -1)
	expectIndependentFile := dir + name
aaronchen2k2k's avatar
aaronchen2k2k 已提交
218

aaronchen2k2k's avatar
aaronchen2k2k 已提交
219 220 221
	if !fileUtils.FileExist(expectIndependentFile) {
		expectIndependentFile = dir + "." + name
	}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
222

aaronchen2k2k's avatar
aaronchen2k2k 已提交
223 224 225
	if fileUtils.FileExist(expectIndependentFile) {
		expectIndependentContent := fileUtils.ReadFile(expectIndependentFile)
		return true, expectIndependentContent
aaronchen2k2k's avatar
aaronchen2k2k 已提交
226 227
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
228
	return false, ""
aaronchen2k2k's avatar
aaronchen2k2k 已提交
229 230
}

231
func getStepNestedArrInOldFormat(lines []string) (ret []commDomain.ZtfStep) {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
	parent := commDomain.ZtfStep{}
	increase := 0
	for index := 0; index < len(lines); index++ {
		line := lines[index]
		lineTrim := strings.TrimSpace(line)
		if lineTrim == "" || lineTrim == ">>" {
			continue
		}

		if strings.Index(line, " ") != 0 {
			parent, increase = parserNextLines(line, lines[index+1:])
			index += increase

			if strings.TrimSpace(parent.Expect) == "" && strings.Index(line, ">>") > -1 {
				parent.Expect = commConsts.ExpectResultPass
			}
			ret = append(ret, parent)
		} else { // 有缩进
			child := commDomain.ZtfStep{}
			child, increase = parserNextLines(line, lines[index+1:])
			index += increase

			if parent.Desc != "" {
				if strings.TrimSpace(child.Expect) == "" && strings.Index(line, ">>") > -1 {
					child.Expect = commConsts.ExpectResultPass
				}

				ret[len(ret)-1].Children = append(ret[len(ret)-1].Children, child)
			}
		}
	}

	return
}
func parserNextLines(str string, nextLines []string) (ret commDomain.ZtfStep, increase int) {
	arr := strings.Split(str, ">>")
	desc := strings.TrimSpace(arr[0])

	expect := ""
	if len(arr) > 1 {
		expect = strings.TrimSpace(arr[1])
	}

	if strings.Index(str, ">>") < 0 || expect != "" { // no >> or single line expect
		ret = commDomain.ZtfStep{Desc: desc, Expect: expect}
		return
	}

	if strings.Index(str, ">>") > -1 { // will test if it has multi-line expect
		for index, line := range nextLines {
			if strings.TrimSpace(line) == ">>" {
				increase = index
				break
			}

			if strings.Index(line, ">>") > -1 {
				expect = ""
				break
			}

			if len(expect) > 0 {
293
				expect += "\r\n"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306
			}
			expect += strings.TrimSpace(line)
		}

		if increase == 0 { // multi-line
			expect = ""
		}
	}

	ret = commDomain.ZtfStep{Desc: desc, Expect: expect}
	return
}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
307 308 309
func ReplaceCaseDesc(desc, file string) {
	content := fileUtils.ReadFile(file)
	lang := langHelper.GetLangByFile(file)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
310

aaronchen2k2k's avatar
aaronchen2k2k 已提交
311
	regStr := fmt.Sprintf(`(?smU)%s((?U:.*cid.*))\n(.*)%s`,
aaronchen2k2k's avatar
aaronchen2k2k 已提交
312 313
		commConsts.LangCommentsRegxMap[lang][0], commConsts.LangCommentsRegxMap[lang][1])
	re, _ := regexp.Compile(regStr)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
314

aaronchen2k2k's avatar
aaronchen2k2k 已提交
315 316 317
	newDesc := fmt.Sprintf("\n%s\n\n"+desc+"\n\n%s",
		commConsts.LangCommentsTagMap[lang][0],
		commConsts.LangCommentsTagMap[lang][1])
aaronchen2k2k's avatar
aaronchen2k2k 已提交
318

aaronchen2k2k's avatar
aaronchen2k2k 已提交
319
	out := re.ReplaceAllString(content, newDesc)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
320

aaronchen2k2k's avatar
aaronchen2k2k 已提交
321
	fileUtils.WriteFile(file, out)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
322 323
}

324
func getSortedTextFromNestedSteps(groups []commDomain.ZtfStep) (ret string, steps []commDomain.ZentaoCaseStep) {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
325 326
	arr := make([]string, 0)

327 328 329 330
	for _, group := range groups {
		step := commDomain.ZentaoCaseStep{}

		stepType := commConsts.Item
aaronchen2k2k's avatar
aaronchen2k2k 已提交
331
		if len(group.Children) > 0 {
332
			stepType = commConsts.Group
aaronchen2k2k's avatar
aaronchen2k2k 已提交
333
		}
334
		step.Type = stepType
aaronchen2k2k's avatar
aaronchen2k2k 已提交
335 336

		stepTxt := strings.TrimSpace(group.Desc)
337
		step.Desc = stepTxt
aaronchen2k2k's avatar
aaronchen2k2k 已提交
338 339 340 341 342

		expectTxt := strings.TrimSpace(group.Expect)
		expectTxt = strings.TrimRight(expectTxt, "]]")
		expectTxt = strings.TrimSpace(expectTxt)

343 344 345
		step.Expect = expectTxt

		steps = append(steps, step)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
346 347 348 349 350 351

		if expectTxt != "" {
			expectTxt = ">> " + expectTxt
		}
		arr = append(arr, fmt.Sprintf("  %s %s", stepTxt, expectTxt))

352 353 354 355
		for _, child := range group.Children {
			stepChild := commDomain.ZentaoCaseStep{}

			stepChild.Type = commConsts.Item
aaronchen2k2k's avatar
aaronchen2k2k 已提交
356 357

			stepTxt := strings.TrimSpace(child.Desc)
358
			stepChild.Desc = stepTxt
aaronchen2k2k's avatar
aaronchen2k2k 已提交
359 360

			expectTxt := strings.TrimSpace(child.Expect)
361 362
			stepChild.Expect = expectTxt

aaronchen2k2k's avatar
aaronchen2k2k 已提交
363
			steps = append(steps, stepChild)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376

			if expectTxt != "" {
				expectTxt = ">> " + expectTxt
			}

			arr = append(arr, fmt.Sprintf("  %s %s", stepTxt, expectTxt))
		}
	}

	ret = strings.Join(arr, "\n")
	return
}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
377
func GetExpectMapFromIndependentFile(steps *[]commDomain.ZentaoCaseStep, content string, withEmptyExpect bool) {
378
	expectArr := ReadExpectIndependentArr(content)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
379

aaronchen2k2k's avatar
aaronchen2k2k 已提交
380 381 382 383 384
	index := 0
	for idx, _ := range *steps {
		if len(expectArr) > index && (*steps)[idx].Expect == "pass" { // not set step that has no expect
			(*steps)[idx].Expect = strings.Join(expectArr[index], "\r\n")
			index++
aaronchen2k2k's avatar
aaronchen2k2k 已提交
385 386
		} else {
			if withEmptyExpect {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
387
				(*steps)[idx].Expect = ""
aaronchen2k2k's avatar
aaronchen2k2k 已提交
388 389 390 391
			}
		}
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
392
	return
aaronchen2k2k's avatar
aaronchen2k2k 已提交
393 394
}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
395 396 397
func ScriptToExpectName(file string) string {
	fileSuffix := path.Ext(file)
	expectName := strings.TrimSuffix(file, fileSuffix) + ".exp"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
398

aaronchen2k2k's avatar
aaronchen2k2k 已提交
399
	return expectName
aaronchen2k2k's avatar
aaronchen2k2k 已提交
400 401
}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
402 403 404 405 406
func getScriptComments(content, lang string) (ret string) {
	reg := fmt.Sprintf(`(?smU)%s((?U:.*))%s`, commConsts.LangCommentsRegxMap[lang][0], commConsts.LangCommentsRegxMap[lang][1])
	arr := regexp.MustCompile(reg).FindStringSubmatch(content)
	if len(arr) < 2 { // wrong format
		return
aaronchen2k2k's avatar
aaronchen2k2k 已提交
407 408
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
409 410
	ret = strings.TrimSpace(arr[1])

aaronchen2k2k's avatar
aaronchen2k2k 已提交
411 412 413
	return
}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
414 415 416
func GetCaseInfo(file string) (pass bool, caseId, productId int, title string, timeout int64) {
	content := fileUtils.ReadFile(file)
	lang := langHelper.GetLangByFile(file)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
417

aaronchen2k2k's avatar
aaronchen2k2k 已提交
418 419 420 421 422 423 424 425
	comments := strings.TrimSpace(getScriptComments(content, lang))
	index := 0
	lines := strings.Split(comments, "\n")
	for index < len(lines) {
		line := strings.TrimSpace(lines[index])
		caseId, title = findTitle(line)
		if title != "" {
			break
426 427
		}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
428
		index += 1
aaronchen2k2k's avatar
aaronchen2k2k 已提交
429 430
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
431 432 433
	pass = title != ""
	if pass {
		return
aaronchen2k2k's avatar
aaronchen2k2k 已提交
434 435
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
436
	// TODO: deal with old format, will removed
437
	isOldFormat := strings.Index(content, "[esac]") > -1
aaronchen2k2k's avatar
aaronchen2k2k 已提交
438
	pass = CheckFileContentIsScript(content)
439
	if !pass {
Z
zhaoke 已提交
440
		return false, caseId, productId, title, timeout
441 442 443 444 445 446 447 448
	}

	caseInfo := ""
	regStr := ""
	if isOldFormat {
		regStr = `(?s)\[case\](.*)\[esac\]`
	} else {
		regStr = fmt.Sprintf(`(?sm)%s((?U:.*pid.*))\n(.*)%s`,
449
			commConsts.LangCommentsRegxMap[lang][0], commConsts.LangCommentsRegxMap[lang][1])
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
	}
	myExp := regexp.MustCompile(regStr)
	arr := myExp.FindStringSubmatch(content)
	if len(arr) > 1 {
		caseInfo = arr[1]
	}

	caseInfo += "\n"

	myExp = regexp.MustCompile(`[\S\s]*cid=\s*([^\n]*?)\s*\n`)
	arr = myExp.FindStringSubmatch(caseInfo)
	if len(arr) > 1 {
		caseId, _ = strconv.Atoi(arr[1])
	}

Z
zhaoke 已提交
465 466 467 468 469 470
	myExp = regexp.MustCompile(`[\S\s]*timeout=\s*([^\n]*?)\s*\n`)
	arr = myExp.FindStringSubmatch(caseInfo)
	if len(arr) > 1 {
		timeout, _ = strconv.ParseInt(arr[1], 10, 64)
	}

471 472 473 474 475 476
	myExp = regexp.MustCompile(`[\S\s]*pid=\s*([^\n]*?)\s*\n`)
	arr = myExp.FindStringSubmatch(caseInfo)
	if len(arr) > 1 {
		productId, _ = strconv.Atoi(arr[1])
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
477
	myExp = regexp.MustCompile(`[\S\s]*title\s*=\s*([^\n]*?)\n`)
478 479
	arr = myExp.FindStringSubmatch(caseInfo)
	if len(arr) > 1 {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
480
		title = strings.TrimSpace(arr[1])
481 482
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
483 484 485 486 487
	if caseId <= 0 {
		pass = false
	}

	return
488 489 490
}

func ReadExpectIndependentArr(content string) [][]string {
491 492
	//正常显示6
	//E2.16
493
	//{
494 495
	//  E2.2 - 16
	//  E2.2 - 26
496 497
	//}
	//{
498 499
	//  E3 - 16
	//  E3 - 26
500
	//}
501

502 503 504 505 506
	lines := strings.Split(content, "\n")

	ret := make([][]string, 0)
	var cpArr []string

507 508 509 510
	currModel := ""
	idx := 0
	for idx < len(lines) {
		line := strings.TrimSpace(lines[idx])
511

512
		if line == "{" { // more than one line
513
			currModel = "multi"
514
			cpArr = make([]string, 0)
515
		} else if currModel == "multi" { // in { and } in multi line mode
516 517
			cpArr = append(cpArr, line)

518
			if idx == len(lines)-1 || strings.TrimSpace(lines[idx+1]) == "}" { // end multi line
519
				temp := make([]string, 0)
520
				temp = append(temp, strings.Join(cpArr, "\r\n"))
521 522 523

				ret = append(ret, temp)
				cpArr = make([]string, 0)
524 525 526
				currModel = ""

				idx += 1
527 528
			}
		} else {
529
			currModel = "single"
530 531 532 533 534 535 536

			line = strings.TrimSpace(line)

			cpArr = append(cpArr, line)
			ret = append(ret, cpArr)
			cpArr = make([]string, 0)
		}
537 538

		idx += 1
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
	}

	return ret
}

func ReadLogArr(content string) (isSkip bool, ret [][]string) {
	lines := strings.Split(content, "\n")

	ret = make([][]string, 0)
	var cpArr []string

	model := ""
	for idx := 0; idx < len(lines); idx++ {
		line := strings.TrimSpace(lines[idx])

		if line == "skip" {
			isSkip = true
			return
		}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
559
		if line == "@{" { // more than one line
560 561
			model = "multi"
			cpArr = make([]string, 0)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
562
		} else if model == "multi" { // between line @{ and } in multi line mode
563 564
			cpArr = append(cpArr, line)

aaronchen2k2k's avatar
aaronchen2k2k 已提交
565 566
			//if idx == len(lines)-1 || strings.Index(lines[idx+1], "}") > -1 {
			if idx == len(lines)-1 || strings.TrimSpace(lines[idx+1]) == "}" {
567
				temp := make([]string, 0)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
568
				temp = append(temp, cpArr...)
569 570 571 572 573 574 575 576 577 578 579

				ret = append(ret, temp)
				cpArr = make([]string, 0)

				idx = idx + 1
				model = ""
			}
		} else {
			model = "single"

			line = strings.TrimSpace(line)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
580 581 582 583 584
			if strings.Index(line, "@") != 0 { // ignore the line not started with @
				continue
			}

			line = line[1:]
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602

			cpArr = append(cpArr, line)
			ret = append(ret, cpArr)
			cpArr = make([]string, 0)
		}
	}

	return
}

func CheckFileIsScript(path string) bool {
	content := fileUtils.ReadFile(path)

	pass := CheckFileContentIsScript(content)
	return pass
}

func CheckFileContentIsScript(content string) bool {
Z
zhaoke 已提交
603
	pass, _ := regexp.MatchString(`cid\b\s*=`, content)
604

aaronchen2k2k's avatar
aaronchen2k2k 已提交
605 606
	if !pass {
		pass, _ = regexp.MatchString(`(?m:^(.+ +)#\d*$)`, content)
607 608
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
609
	return pass
610 611
}

612
func GetScriptByIdsInDir(dirPth string, idMap *map[int]string) error {
613 614
	dirPth = fileUtils.AbsolutePath(dirPth)

615
	sep := consts.FilePthSep
616

617
	if commonUtils.IgnoreZtfFile(dirPth) {
618 619 620 621 622 623 624 625 626 627 628
		return nil
	}

	dir, err := ioutil.ReadDir(dirPth)
	if err != nil {
		return err
	}

	for _, fi := range dir {
		name := fi.Name()
		if fi.IsDir() { // 目录, 递归遍历
629
			GetScriptByIdsInDir(dirPth+name+sep, idMap)
630
		} else {
631
			regx := langHelper.GetSupportLanguageExtRegx()
632 633 634 635 636 637 638
			pass, _ := regexp.MatchString("^*.\\."+regx+"$", name)

			if !pass {
				continue
			}

			path := dirPth + name
Z
zhaoke 已提交
639
			pass, id, _, _, _ := GetCaseInfo(path)
640
			if pass {
641
				(*idMap)[id] = path
aaronchen2k2k's avatar
aaronchen2k2k 已提交
642 643
			} else {
				pass, id, _, _, _ = GetCaseInfo(path)
644 645 646 647 648 649
			}
		}
	}

	return nil
}
m0_58228130's avatar
m0_58228130 已提交
650

651
func GetCaseIdsInSuiteFile(name string, ids *[]int) {
m0_58228130's avatar
m0_58228130 已提交
652 653 654 655 656 657 658 659 660 661
	content := fileUtils.ReadFile(name)

	for _, line := range strings.Split(content, "\n") {
		idStr := strings.TrimSpace(line)
		if idStr == "" {
			continue
		}

		id, err := strconv.Atoi(idStr)
		if err == nil {
662
			*ids = append(*ids, id)
m0_58228130's avatar
m0_58228130 已提交
663 664 665
		}
	}
}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875

//func getGroupBlockArr(lines []string) [][]string {
//	groupBlockArr := make([][]string, 0)
//
//	idx := 0
//	for true {
//		if idx >= len(lines) {
//			break
//		}
//
//		var groupContent []string
//		line := strings.TrimSpace(lines[idx])
//		if isGroup(line) { // must match a group
//			groupContent = make([]string, 0)
//			groupContent = append(groupContent, line)
//
//			idx++
//
//			for true {
//				if idx >= len(lines) {
//					groupBlockArr = append(groupBlockArr, groupContent)
//					break
//				}
//
//				line = strings.TrimSpace(lines[idx])
//				if isGroup(line) {
//					groupBlockArr = append(groupBlockArr, groupContent)
//
//					break
//				} else if line != "" && !isGroup(line) {
//					groupContent = append(groupContent, line)
//				}
//
//				idx++
//			}
//		} else {
//			idx++
//		}
//	}
//
//	return groupBlockArr
//}

//func loadMultiLineSteps(arr []string) []commDomain.ZtfStep {
//	childs := make([]commDomain.ZtfStep, 0)
//
//	child := commDomain.ZtfStep{}
//	idx := 0
//	for true {
//		if idx >= len(arr) {
//			if child.Desc != "" {
//				childs = append(childs, child)
//			}
//
//			break
//		}
//
//		line := arr[idx]
//		line = strings.TrimSpace(line)
//
//		if isStepsIdent(line) {
//			if idx > 0 {
//				childs = append(childs, child)
//			}
//
//			child = commDomain.ZtfStep{}
//			idx++
//
//			stp := ""
//			for true { // retrieve next lines
//				if idx >= len(arr) || hasBrackets(arr[idx]) {
//					child.Desc = stp
//					break
//				}
//
//				stp += arr[idx] + "\n"
//				idx++
//			}
//		}
//
//		if isExpectsIdent(line) {
//			idx++
//
//			exp := ""
//			for true { // retrieve next lines
//				if idx >= len(arr) || hasBrackets(arr[idx]) {
//					child.Expect = exp
//					break
//				}
//
//				temp := strings.TrimSpace(arr[idx])
//				if temp == ">>" {
//					temp = ""
//				}
//				exp += temp + "\n"
//				idx++
//			}
//		}
//
//	}
//
//	return childs
//}
//
//func loadSingleLineSteps(arr []string) []commDomain.ZtfStep {
//	children := make([]commDomain.ZtfStep, 0)
//
//	for _, line := range arr {
//		line = strings.TrimSpace(line)
//
//		sections := strings.Split(line, ">>")
//		expect := ""
//		if len(sections) > 1 { // has expect
//			expect = strings.TrimSpace(sections[1])
//		}
//
//		child := commDomain.ZtfStep{Desc: sections[0], Expect: expect}
//
//		children = append(children, child)
//	}
//
//	return children
//}
//
//func isGroupIdent(str string) bool {
//	pass, _ := regexp.MatchString(`(?i)\[\s*group\s*\]`, str)
//	return pass
//}

//func isGroup(str string) bool {
//	ret := strings.Index(str, ">>") < 0 && hasBrackets(str) && !isStepsIdent(str) && !isExpectsIdent(str)
//
//	return ret
//}

//func isStepsIdent(str string) bool {
//	pass, _ := regexp.MatchString(`(?i)\[.*steps\.*\]`, str)
//	return pass
//}
//
//func isExpectsIdent(str string) bool {
//	pass, _ := regexp.MatchString(`(?i)\[.*expects\.*\]`, str)
//	return pass
//}
//
//func hasBrackets(str string) bool {
//	pass, _ := regexp.MatchString(`(?i)()\[.*\]`, str)
//	return pass
//}

//func getGroupName(str string) string {
//	reg := `\[\d\.\s]*(.*)\]`
//	repl := "${1}"
//
//	regx, _ := regexp.Compile(reg)
//	str = regx.ReplaceAllString(str, repl)
//
//	return str
//}
//
//func printMultiStepOrExpect(str string) string {
//	str = strings.TrimSpace(str)
//
//	ret := make([]string, 0)
//
//	for _, line := range strings.Split(str, "\n") {
//		line = strings.TrimSpace(line)
//
//		ret = append(ret, fmt.Sprintf("%s%s", strings.Repeat(" ", 4), line))
//	}
//
//	return strings.Join(ret, "\r\n")
//}

//func replaceNumb(str string, groupNumb int, childNumb int, withBrackets bool) string {
//	numb := getNumbStr(groupNumb, childNumb)
//
//	reg := `[\d\.\s]*(.*)`
//	repl := numb + " ${1}"
//	if withBrackets {
//		reg = `\[` + reg + `\]`
//		repl = `[` + repl + `]`
//	}
//
//	regx, _ := regexp.Compile(reg)
//	str = regx.ReplaceAllString(str, repl)
//
//	return str
//}
//func getNumbStr(groupNumb int, childNumb int) string {
//	numb := strconv.Itoa(groupNumb) + "."
//	if childNumb != -1 {
//		numb += strconv.Itoa(childNumb) + "."
//	}
//
//	return numb
//}

//func IsMultiLine(step commDomain.ZtfStep) bool {
//	if strings.Index(step.Desc, "\n") > -1 || strings.Index(step.Expect, "\n") > -1 {
//		return true
//	}
//
//	return false
//}
//func RunDateFolder() string {
//	runName := dateUtils.DateTimeStrFmt(time.Now(), "2006-01-02T150405") + string(os.PathSeparator)
//
//	return runName
//}