parser.go 18.8 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 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 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
func getSingleExpect(descAndExpect string) (desc, expect string) {
	arr := strings.Split(descAndExpect, "@")

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

	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 已提交
179 180 181
		return
	}

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

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

aaronchen2k2k's avatar
aaronchen2k2k 已提交
188 189
	groupArr := getStepNestedArr(lines)
	_, steps = getSortedTextFromNestedSteps(groupArr)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
190

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

	return
}

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

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

aaronchen2k2k's avatar
aaronchen2k2k 已提交
208 209
	return ""
}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
210

aaronchen2k2k's avatar
aaronchen2k2k 已提交
211 212 213 214
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 已提交
215

aaronchen2k2k's avatar
aaronchen2k2k 已提交
216 217 218
	if !fileUtils.FileExist(expectIndependentFile) {
		expectIndependentFile = dir + "." + name
	}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
219

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

aaronchen2k2k's avatar
aaronchen2k2k 已提交
225
	return false, ""
aaronchen2k2k's avatar
aaronchen2k2k 已提交
226 227 228 229 230 231 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
}

func getStepNestedArr(lines []string) (ret []commDomain.ZtfStep) {
	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 {
290
				expect += "\r\n"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303
			}
			expect += strings.TrimSpace(line)
		}

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

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

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

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

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

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

aaronchen2k2k's avatar
aaronchen2k2k 已提交
318
	fileUtils.WriteFile(file, out)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
319 320
}

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

324 325 326 327
	for _, group := range groups {
		step := commDomain.ZentaoCaseStep{}

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

		stepTxt := strings.TrimSpace(group.Desc)
334
		step.Desc = stepTxt
aaronchen2k2k's avatar
aaronchen2k2k 已提交
335 336 337 338 339

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

340 341 342
		step.Expect = expectTxt

		steps = append(steps, step)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
343 344 345 346 347 348

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

349 350 351 352
		for _, child := range group.Children {
			stepChild := commDomain.ZentaoCaseStep{}

			stepChild.Type = commConsts.Item
aaronchen2k2k's avatar
aaronchen2k2k 已提交
353 354

			stepTxt := strings.TrimSpace(child.Desc)
355
			stepChild.Desc = stepTxt
aaronchen2k2k's avatar
aaronchen2k2k 已提交
356 357

			expectTxt := strings.TrimSpace(child.Expect)
358 359
			stepChild.Expect = expectTxt

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

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

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

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

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

aaronchen2k2k's avatar
aaronchen2k2k 已提交
377 378 379 380 381
	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 已提交
382 383
		} else {
			if withEmptyExpect {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
384
				(*steps)[idx].Expect = ""
aaronchen2k2k's avatar
aaronchen2k2k 已提交
385 386 387 388
			}
		}
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
389
	return
aaronchen2k2k's avatar
aaronchen2k2k 已提交
390 391
}

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

aaronchen2k2k's avatar
aaronchen2k2k 已提交
396
	return expectName
aaronchen2k2k's avatar
aaronchen2k2k 已提交
397 398
}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
399 400 401 402 403
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 已提交
404 405
	}

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

aaronchen2k2k's avatar
aaronchen2k2k 已提交
408 409 410
	return
}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
411 412 413
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 已提交
414

aaronchen2k2k's avatar
aaronchen2k2k 已提交
415 416 417 418 419 420 421 422
	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
423 424
		}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
425
		index += 1
aaronchen2k2k's avatar
aaronchen2k2k 已提交
426 427
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
428 429 430
	pass = title != ""
	if pass {
		return
aaronchen2k2k's avatar
aaronchen2k2k 已提交
431 432
	}

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

	caseInfo := ""
	regStr := ""
	if isOldFormat {
		regStr = `(?s)\[case\](.*)\[esac\]`
	} else {
		regStr = fmt.Sprintf(`(?sm)%s((?U:.*pid.*))\n(.*)%s`,
446
			commConsts.LangCommentsRegxMap[lang][0], commConsts.LangCommentsRegxMap[lang][1])
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
	}
	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 已提交
462 463 464 465 466 467
	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)
	}

468 469 470 471 472 473
	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 已提交
474
	myExp = regexp.MustCompile(`[\S\s]*title\s*=\s*([^\n]*?)\n`)
475 476
	arr = myExp.FindStringSubmatch(caseInfo)
	if len(arr) > 1 {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
477
		title = strings.TrimSpace(arr[1])
478 479
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
480 481 482 483 484
	if caseId <= 0 {
		pass = false
	}

	return
485 486 487
}

func ReadExpectIndependentArr(content string) [][]string {
488 489 490 491 492 493 494 495 496 497 498
	//正常显示6
	//E2.16
	//>>
	//  E2.2 - 16
	//  E2.2 - 26
	//>>
	//>>
	//  E3 - 16
	//  E3 - 26
	//>>

499 500 501 502 503
	lines := strings.Split(content, "\n")

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

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

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

515
			if idx == len(lines)-1 || strings.Index(lines[idx+1], ">>") > -1 { // end multi line
516
				temp := make([]string, 0)
517
				temp = append(temp, strings.Join(cpArr, "\r\n"))
518 519 520

				ret = append(ret, temp)
				cpArr = make([]string, 0)
521 522 523
				currModel = ""

				idx += 1
524 525
			}
		} else {
526
			currModel = "single"
527 528 529 530 531 532 533

			line = strings.TrimSpace(line)

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

		idx += 1
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
	}

	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 已提交
556
		if line == "@{" { // more than one line
557 558
			model = "multi"
			cpArr = make([]string, 0)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
559
		} else if model == "multi" { // between line @{ and } in multi line mode
560 561
			cpArr = append(cpArr, line)

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

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

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

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

			line = line[1:]
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599

			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 已提交
600
	pass, _ := regexp.MatchString(`cid\b\s*=`, content)
601

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

aaronchen2k2k's avatar
aaronchen2k2k 已提交
606
	return pass
607 608
}

609
func GetScriptByIdsInDir(dirPth string, idMap *map[int]string) error {
610 611
	dirPth = fileUtils.AbsolutePath(dirPth)

612
	sep := consts.FilePthSep
613

614
	if commonUtils.IgnoreZtfFile(dirPth) {
615 616 617 618 619 620 621 622 623 624 625
		return nil
	}

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

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

			if !pass {
				continue
			}

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

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

648
func GetCaseIdsInSuiteFile(name string, ids *[]int) {
m0_58228130's avatar
m0_58228130 已提交
649 650 651 652 653 654 655 656 657 658
	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 {
659
			*ids = append(*ids, id)
m0_58228130's avatar
m0_58228130 已提交
660 661 662
		}
	}
}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
663 664 665 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

//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
//}