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

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

	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 已提交
19 20
)

aaronchen2k2k's avatar
aaronchen2k2k 已提交
21 22
func ReplaceCaseDesc(desc, file string) {
	content := fileUtils.ReadFile(file)
23
	lang := langHelper.GetLangByFile(file)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
24 25

	regStr := fmt.Sprintf(`(?smU)%s((?U:.*pid.*))\n(.*)%s`,
26
		commConsts.LangCommentsRegxMap[lang][0], commConsts.LangCommentsRegxMap[lang][1])
aaronchen2k2k's avatar
aaronchen2k2k 已提交
27 28 29
	re, _ := regexp.Compile(regStr)

	newDesc := fmt.Sprintf("\n%s\n\n"+desc+"\n\n%s",
30 31
		commConsts.LangCommentsTagMap[lang][0],
		commConsts.LangCommentsTagMap[lang][1])
aaronchen2k2k's avatar
aaronchen2k2k 已提交
32 33 34 35 36

	out := re.ReplaceAllString(content, newDesc)

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

aaronchen2k2k's avatar
aaronchen2k2k 已提交
38
func GetStepAndExpectMap(file string) (steps []commDomain.ZentaoCaseStep) {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
39 40 41 42
	if !fileUtils.FileExist(file) {
		return
	}

43
	lang := langHelper.GetLangByFile(file)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
44 45
	txt := fileUtils.ReadFile(file)

aaronchen2k2k's avatar
aaronchen2k2k 已提交
46
	_, checkpoints := ReadCaseInfo(txt, lang)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
47 48
	lines := strings.Split(checkpoints, "\n")

aaronchen2k2k's avatar
aaronchen2k2k 已提交
49 50
	groupArr := getStepNestedArr(lines)
	_, steps = getSortedTextFromNestedSteps(groupArr)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
51

52 53
	isIndependent, expectIndependentContent := GetDependentExpect(file)
	if isIndependent {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
54
		GetExpectMapFromIndependentFile(&steps, expectIndependentContent, false)
55 56 57 58 59
	}

	return
}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
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 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
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 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 {
164
				expect += "\r\n"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177
			}
			expect += strings.TrimSpace(line)
		}

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

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

aaronchen2k2k's avatar
aaronchen2k2k 已提交
178
func loadMultiLineSteps(arr []string) []commDomain.ZtfStep {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	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 {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
240
	children := make([]commDomain.ZtfStep, 0)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
241 242 243 244 245 246 247 248 249 250 251 252

	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}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
253
		children = append(children, child)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
254 255
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
256
	return children
aaronchen2k2k's avatar
aaronchen2k2k 已提交
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
}

func isGroupIdent(str string) bool {
	pass, _ := regexp.MatchString(`(?i)\[\s*group\s*\]`, str)
	return pass
}

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 isGroup(str string) bool {
	ret := strings.Index(str, ">>") < 0 && hasBrackets(str) && !isStepsIdent(str) && !isExpectsIdent(str)

	return ret
}

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

288 289 290 291
	for _, group := range groups {
		step := commDomain.ZentaoCaseStep{}

		stepType := commConsts.Item
aaronchen2k2k's avatar
aaronchen2k2k 已提交
292
		if len(group.Children) > 0 {
293
			stepType = commConsts.Group
aaronchen2k2k's avatar
aaronchen2k2k 已提交
294
		}
295
		step.Type = stepType
aaronchen2k2k's avatar
aaronchen2k2k 已提交
296 297

		stepTxt := strings.TrimSpace(group.Desc)
298
		step.Desc = stepTxt
aaronchen2k2k's avatar
aaronchen2k2k 已提交
299 300 301 302 303

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

304 305 306
		step.Expect = expectTxt

		steps = append(steps, step)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
307 308 309 310 311 312

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

313 314 315 316
		for _, child := range group.Children {
			stepChild := commDomain.ZentaoCaseStep{}

			stepChild.Type = commConsts.Item
aaronchen2k2k's avatar
aaronchen2k2k 已提交
317 318

			stepTxt := strings.TrimSpace(child.Desc)
319
			stepChild.Desc = stepTxt
aaronchen2k2k's avatar
aaronchen2k2k 已提交
320 321

			expectTxt := strings.TrimSpace(child.Expect)
322 323
			stepChild.Expect = expectTxt

aaronchen2k2k's avatar
aaronchen2k2k 已提交
324
			steps = append(steps, stepChild)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

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

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

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

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 getGroupName(str string) string {
	reg := `\[\d\.\s]*(.*)\]`
	repl := "${1}"

	regx, _ := regexp.Compile(reg)
	str = regx.ReplaceAllString(str, repl)

	return str
}

func printMutiStepOrExpect(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")
}

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

aaronchen2k2k's avatar
aaronchen2k2k 已提交
388 389 390 391 392
	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 已提交
393 394
		} else {
			if withEmptyExpect {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
395
				(*steps)[idx].Expect = ""
aaronchen2k2k's avatar
aaronchen2k2k 已提交
396 397 398 399
			}
		}
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
400
	return
aaronchen2k2k's avatar
aaronchen2k2k 已提交
401 402
}

403 404
func GetCaseContent(stepObj commDomain.ZtfStep, seq string, independentFile bool, isChild bool) (
	stepContent, expectContent string) {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
405 406 407 408 409 410 411

	step := strings.TrimSpace(stepObj.Desc)
	expect := strings.TrimSpace(stepObj.Expect)

	stepStr := getStepContent(step, isChild)
	expectStr := getExpectContent(expect, isChild, independentFile)

aaronchen2k2k's avatar
aaronchen2k2k 已提交
412
	if !independentFile {
413
		stepContent = stepStr + expectStr
aaronchen2k2k's avatar
aaronchen2k2k 已提交
414
	} else {
415 416 417 418
		stepContent = stepStr
		if stepObj.Children == nil || len(stepObj.Children) == 0 {
			stepContent += " >>"
		}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
419
	}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
420

421 422
	expectContent = expectStr

aaronchen2k2k's avatar
aaronchen2k2k 已提交
423 424 425
	stepContent = html.UnescapeString(stepContent)
	expectContent = html.UnescapeString(expectContent)

426
	return
aaronchen2k2k's avatar
aaronchen2k2k 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
}

func getStepContent(str string, isChild bool) (ret string) {
	str = strings.TrimSpace(str)

	rpl := "\n"
	if isChild {
		rpl = "\n" + "  "
	}
	ret = strings.ReplaceAll(str, "\r\n", rpl)
	if isChild {
		ret = "  " + ret
	}

	return
}
func getExpectContent(str string, isChild bool, independentFile bool) (ret string) {
	str = strings.TrimSpace(str)
	if str == "" {
		return
	}

449 450
	isSingleLine := strings.Count(str, "\r\n") == 0
	if isSingleLine {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
451 452 453 454 455
		if independentFile {
			ret = str
		} else {
			ret = " >> " + str
		}
456
	} else { // multi-line
aaronchen2k2k's avatar
aaronchen2k2k 已提交
457
		rpl := "\r\n"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
458

459 460 461 462 463 464 465 466
		space := "  "
		spaceBeforeTerminator := ""
		spaceBeforeText := space
		if isChild {
			spaceBeforeTerminator = space
			spaceBeforeText = strings.Repeat(space, 2)
		}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
467
		if independentFile {
468 469 470 471 472
			//>>
			//	expect 1.2 line 1
			//	expect 1.2 line 2
			//>>
			ret = ">>\n" + space + strings.ReplaceAll(str, rpl, rpl+space) + "\n>>"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
473
		} else {
474 475 476 477 478 479 480
			//step 1.2 >>
			//	expect 1.2 line 1
			//  expect 1.2 line 2
			//>>
			ret = " >> \n" + spaceBeforeText +
				strings.ReplaceAll(str, rpl, rpl+spaceBeforeText) +
				"\n" + spaceBeforeTerminator + ">>"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
481 482 483 484 485 486 487 488 489 490 491 492 493
		}
	}

	return
}

func IsMultiLine(step commDomain.ZtfStep) bool {
	if strings.Index(step.Desc, "\n") > -1 || strings.Index(step.Expect, "\n") > -1 {
		return true
	}

	return false
}
494 495 496 497 498 499 500 501 502 503 504 505 506 507

func ScriptToExpectName(file string) string {
	fileSuffix := path.Ext(file)
	expectName := strings.TrimSuffix(file, fileSuffix) + ".exp"

	return expectName
}

//func RunDateFolder() string {
//	runName := dateUtils.DateTimeStrFmt(time.Now(), "2006-01-02T150405") + string(os.PathSeparator)
//
//	return runName
//}

Z
zhaoke 已提交
508
func GetCaseInfo(file string) (pass bool, caseId, productId int, title string, timeout int64) {
509 510
	content := fileUtils.ReadFile(file)
	isOldFormat := strings.Index(content, "[esac]") > -1
aaronchen2k2k's avatar
aaronchen2k2k 已提交
511
	pass = CheckFileContentIsScript(content)
512
	if !pass {
Z
zhaoke 已提交
513
		return false, caseId, productId, title, timeout
514 515 516
	}

	caseInfo := ""
517
	lang := langHelper.GetLangByFile(file)
518 519 520 521 522
	regStr := ""
	if isOldFormat {
		regStr = `(?s)\[case\](.*)\[esac\]`
	} else {
		regStr = fmt.Sprintf(`(?sm)%s((?U:.*pid.*))\n(.*)%s`,
523
			commConsts.LangCommentsRegxMap[lang][0], commConsts.LangCommentsRegxMap[lang][1])
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
	}
	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 已提交
539 540 541 542 543 544
	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)
	}

545 546 547 548 549 550
	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 已提交
551
	myExp = regexp.MustCompile(`[\S\s]*title=([^\n]*?)\n`)
552 553
	arr = myExp.FindStringSubmatch(caseInfo)
	if len(arr) > 1 {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
554
		title = strings.TrimSpace(arr[1])
555 556
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
557 558 559 560 561
	if caseId <= 0 {
		pass = false
	}

	return
562 563 564
}

func ReadExpectIndependentArr(content string) [][]string {
565 566 567 568 569 570 571 572 573 574 575
	//正常显示6
	//E2.16
	//>>
	//  E2.2 - 16
	//  E2.2 - 26
	//>>
	//>>
	//  E3 - 16
	//  E3 - 26
	//>>

576 577 578 579 580
	lines := strings.Split(content, "\n")

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

581 582 583 584
	currModel := ""
	idx := 0
	for idx < len(lines) {
		line := strings.TrimSpace(lines[idx])
585 586

		if line == ">>" { // more than one line
587
			currModel = "multi"
588
			cpArr = make([]string, 0)
589
		} else if currModel == "multi" { // in >> and >> in multi line mode
590 591
			cpArr = append(cpArr, line)

592
			if idx == len(lines)-1 || strings.Index(lines[idx+1], ">>") > -1 { // end multi line
593
				temp := make([]string, 0)
594
				temp = append(temp, strings.Join(cpArr, "\r\n"))
595 596 597

				ret = append(ret, temp)
				cpArr = make([]string, 0)
598 599 600
				currModel = ""

				idx += 1
601 602
			}
		} else {
603
			currModel = "single"
604 605 606 607 608 609 610

			line = strings.TrimSpace(line)

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

		idx += 1
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
	}

	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
		}

		if line == ">>" { // more than one line
			model = "multi"
			cpArr = make([]string, 0)
		} else if model == "multi" { // in >> and >> in multi line mode
			cpArr = append(cpArr, line)

			if idx == len(lines)-1 || strings.Index(lines[idx+1], ">>") > -1 {
				temp := make([]string, 0)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
641
				temp = append(temp, cpArr...)
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670

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

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

			line = strings.TrimSpace(line)

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

	return pass
}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
676 677 678 679
func ReadCaseInfo(content, lang string) (info, checkpoints string) {
	regStr := fmt.Sprintf(`(?smU)%s((?U:.*pid.*))\n(.*)%s`,
		commConsts.LangCommentsRegxMap[lang][0], commConsts.LangCommentsRegxMap[lang][1])

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
	myExp := regexp.MustCompile(regStr)
	arr := myExp.FindStringSubmatch(content)

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

		return
	}

	return
}
func ReadCaseId(content string) string {
	myExp := regexp.MustCompile(`(?s).*\ncid=((?U:.*))\n.*`)
	arr := myExp.FindStringSubmatch(content)

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

	return ""
}

func GetDependentExpect(file string) (bool, string) {
705
	dir := fileUtils.AddFilePathSepIfNeeded(filepath.Dir(file))
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
	name := strings.Replace(filepath.Base(file), path.Ext(file), ".exp", -1)
	expectIndependentFile := dir + name

	if !fileUtils.FileExist(expectIndependentFile) {
		expectIndependentFile = dir + "." + name
	}

	if fileUtils.FileExist(expectIndependentFile) {
		expectIndependentContent := fileUtils.ReadFile(expectIndependentFile)
		return true, expectIndependentContent
	}

	return false, ""
}

721
func GetScriptByIdsInDir(dirPth string, idMap *map[int]string) error {
722 723
	dirPth = fileUtils.AbsolutePath(dirPth)

724
	sep := consts.FilePthSep
725

726
	if commonUtils.IgnoreZtfFile(dirPth) {
727 728 729 730 731 732 733 734 735 736 737
		return nil
	}

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

	for _, fi := range dir {
		name := fi.Name()
		if fi.IsDir() { // 目录, 递归遍历
738
			GetScriptByIdsInDir(dirPth+name+sep, idMap)
739
		} else {
740
			regx := langHelper.GetSupportLanguageExtRegx()
741 742 743 744 745 746 747
			pass, _ := regexp.MatchString("^*.\\."+regx+"$", name)

			if !pass {
				continue
			}

			path := dirPth + name
Z
zhaoke 已提交
748
			pass, id, _, _, _ := GetCaseInfo(path)
749
			if pass {
750
				(*idMap)[id] = path
751 752 753 754 755 756
			}
		}
	}

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

758
func GetCaseIdsInSuiteFile(name string, ids *[]int) {
m0_58228130's avatar
m0_58228130 已提交
759 760 761 762 763 764 765 766 767 768
	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 {
769
			*ids = append(*ids, id)
m0_58228130's avatar
m0_58228130 已提交
770 771 772
		}
	}
}