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

import (
	"fmt"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
5 6 7 8 9 10
	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 已提交
11
	"html"
12 13 14
	"io/ioutil"
	"path"
	"path/filepath"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
15 16 17 18 19
	"regexp"
	"strconv"
	"strings"
)

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

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

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

	out := re.ReplaceAllString(content, newDesc)

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

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

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

	isOldFormat = strings.Index(txt, "[esac]") > -1
46
	_, checkpoints := ReadCaseInfo(txt, lang, isOldFormat)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
47 48 49 50 51
	lines := strings.Split(checkpoints, "\n")

	if isOldFormat {
		groupBlockArr := getGroupBlockArr(lines)
		groupArr := getStepNestedArrObsolete(groupBlockArr)
52
		_, steps = getSortedTextFromNestedStepsObsolete(groupArr)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
53 54
	} else {
		groupArr := getStepNestedArr(lines)
55
		_, steps = getSortedTextFromNestedSteps(groupArr)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
56 57
	}

58 59 60
	isIndependent, expectIndependentContent := GetDependentExpect(file)
	if isIndependent {
		if isOldFormat {
61
			GetExpectMapFromIndependentFileObsolete(&steps, expectIndependentContent, false)
62
		} else {
63
			GetExpectMapFromIndependentFile(&steps, expectIndependentContent, false)
64 65 66 67 68 69
		}
	}

	return
}

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
//func SortFile(file string) {
//	stepsTxt := ""
//
//	if fileUtils.FileExist(file) {
//		txt := fileUtils.ReadFile(file)
//		lang := langUtils.GetLangByFile(file)
//		isOldFormat := strings.Index(txt, "[esac]") > -1
//		info, content := ReadCaseInfo(txt, lang, isOldFormat)
//		lines := strings.Split(content, "\n")
//
//		groupBlockArr := getGroupBlockArr(lines)
//		groupArr := getStepNestedArrObsolete(groupBlockArr)
//		stepsTxt, _, _, _ = getSortedTextFromNestedStepsObsolete(groupArr)
//
//		// replace info
//		from := ""
//		to := ""
//		if isOldFormat {
//			from = `(?s)\[case\].*\[esac\]`
//			to = "[case]\n" + info + "\n" + stepsTxt + "\n\n[esac]"
//		} else {
//			from = fmt.Sprintf(`(?s)%s.*%s`, langUtils.LangCommentsRegxMap[lang][0], langUtils.LangCommentsRegxMap[lang][1])
//			to = fmt.Sprintf("%s\n"+info+"\n"+stepsTxt+"\n\n%s",
//				langUtils.LangCommentsRegxMap[lang][0], langUtils.LangCommentsRegxMap[lang][1])
//		}
//		re, _ := regexp.Compile(from)
//		script := re.ReplaceAllString(txt, to)
//
//		fileUtils.WriteFile(file, script)
//	}
//}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
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

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 getStepNestedArrObsolete(blocks [][]string) (ret []commDomain.ZtfStep) {
	for _, block := range blocks {
		name := block[0]
		group := commDomain.ZtfStep{Desc: name}

		if isStepsIdent(block[1]) { // muti line
			group.MultiLine = true
aaronchen2k2k's avatar
aaronchen2k2k 已提交
151
			childs := loadMultiLineSteps(block[1:])
aaronchen2k2k's avatar
aaronchen2k2k 已提交
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 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

			group.Children = append(group.Children, childs...)
		} else {
			childs := loadSingleLineSteps(block[1:])

			group.Children = append(group.Children, childs...)
		}

		ret = append(ret, group)
	}

	return ret
}

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 {
228
				expect += "\r\n"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241
			}
			expect += strings.TrimSpace(line)
		}

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

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

aaronchen2k2k's avatar
aaronchen2k2k 已提交
242
func loadMultiLineSteps(arr []string) []commDomain.ZtfStep {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
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 293 294 295 296 297 298 299 300 301 302 303
	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 已提交
304
	children := make([]commDomain.ZtfStep, 0)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
305 306 307 308 309 310 311 312 313 314 315 316

	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 已提交
317
		children = append(children, child)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
318 319
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
320
	return children
aaronchen2k2k's avatar
aaronchen2k2k 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
}

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
}

349
func getSortedTextFromNestedStepsObsolete(groups []commDomain.ZtfStep) (stepsText string, steps []commDomain.ZentaoCaseStep) {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
350 351 352 353 354 355 356 357 358 359
	ret := make([]string, 0)

	groupNumb := 1
	for _, group := range groups {
		desc := group.Desc

		if desc == "[group]" {
			ret = append(ret, "\n"+desc)

			for idx, child := range group.Children { // level 1 item
360
				step := commDomain.ZentaoCaseStep{}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
361 362 363 364 365 366 367 368

				if group.MultiLine {
					// steps
					tag := replaceNumb("[steps]", groupNumb, -1, true)
					ret = append(ret, "  "+tag)

					stepTxt := printMutiStepOrExpect(child.Desc)
					ret = append(ret, stepTxt)
369 370

					step.Desc = stepTxt
aaronchen2k2k's avatar
aaronchen2k2k 已提交
371 372 373 374 375 376 377 378 379 380

					// expects
					tag = replaceNumb("[expects]", groupNumb, -1, true)
					ret = append(ret, "  "+tag)

					expectTxt := printMutiStepOrExpect(child.Expect)
					ret = append(ret, expectTxt)
					if idx < len(group.Children)-1 {
						ret = append(ret, "")
					}
381 382

					step.Expect = expectTxt
aaronchen2k2k's avatar
aaronchen2k2k 已提交
383 384 385
				} else {
					stepTxt := strings.TrimSpace(child.Desc)
					stepTxtWithNumb := replaceNumb(stepTxt, groupNumb, -1, false)
386 387

					step.Desc = stepTxt
aaronchen2k2k's avatar
aaronchen2k2k 已提交
388 389 390

					expectTxt := child.Expect
					expectTxt = strings.TrimSpace(expectTxt)
391 392

					step.Expect = expectTxt
aaronchen2k2k's avatar
aaronchen2k2k 已提交
393 394 395 396 397 398 399 400

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

					ret = append(ret, fmt.Sprintf("  %s %s", stepTxtWithNumb, expectTxt))
				}

401 402
				steps = append(steps, step)

aaronchen2k2k's avatar
aaronchen2k2k 已提交
403 404 405
				groupNumb++
			}
		} else {
406 407
			groupStep := commDomain.ZentaoCaseStep{}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
408 409 410
			desc = replaceNumb(group.Desc, groupNumb, -1, true)
			ret = append(ret, "\n"+desc)

411 412 413 414 415
			groupStep.Type = commConsts.Group
			groupStep.Desc = getGroupName(group.Desc)
			groupStep.Expect = ""

			steps = append(steps, groupStep)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
416 417 418

			childNumb := 1
			for _, child := range group.Children {
419 420 421
				itemStep := commDomain.ZentaoCaseStep{}

				itemStep.Type = commConsts.Item
aaronchen2k2k's avatar
aaronchen2k2k 已提交
422 423 424 425 426 427 428 429

				if group.MultiLine {
					// steps
					tag := replaceNumb("[steps]", groupNumb, childNumb, true)
					ret = append(ret, "  "+tag)

					stepTxt := printMutiStepOrExpect(child.Desc)
					ret = append(ret, stepTxt)
430 431

					itemStep.Desc = stepTxt
aaronchen2k2k's avatar
aaronchen2k2k 已提交
432 433 434 435 436 437 438

					// expects
					tag = replaceNumb("[expects]", groupNumb, childNumb, true)
					ret = append(ret, "  "+tag)

					expectTxt := printMutiStepOrExpect(child.Expect)
					ret = append(ret, expectTxt)
439 440

					itemStep.Expect = expectTxt
aaronchen2k2k's avatar
aaronchen2k2k 已提交
441 442
				} else {
					stepTxt := strings.TrimSpace(child.Desc)
443
					itemStep.Desc = stepTxt
aaronchen2k2k's avatar
aaronchen2k2k 已提交
444 445 446

					expectTxt := child.Expect
					expectTxt = strings.TrimSpace(expectTxt)
447
					itemStep.Expect = expectTxt
aaronchen2k2k's avatar
aaronchen2k2k 已提交
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462

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

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

				childNumb++
			}

			groupNumb++
		}
	}

463 464 465
	stepsText = strings.Join(ret, "\n")

	return
aaronchen2k2k's avatar
aaronchen2k2k 已提交
466 467
}

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

471 472 473 474
	for _, group := range groups {
		step := commDomain.ZentaoCaseStep{}

		stepType := commConsts.Item
aaronchen2k2k's avatar
aaronchen2k2k 已提交
475
		if len(group.Children) > 0 {
476
			stepType = commConsts.Group
aaronchen2k2k's avatar
aaronchen2k2k 已提交
477
		}
478
		step.Type = stepType
aaronchen2k2k's avatar
aaronchen2k2k 已提交
479 480

		stepTxt := strings.TrimSpace(group.Desc)
481
		step.Desc = stepTxt
aaronchen2k2k's avatar
aaronchen2k2k 已提交
482 483 484 485 486

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

487 488 489
		step.Expect = expectTxt

		steps = append(steps, step)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
490 491 492 493 494 495

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

496 497 498 499
		for _, child := range group.Children {
			stepChild := commDomain.ZentaoCaseStep{}

			stepChild.Type = commConsts.Item
aaronchen2k2k's avatar
aaronchen2k2k 已提交
500 501

			stepTxt := strings.TrimSpace(child.Desc)
502
			stepChild.Desc = stepTxt
aaronchen2k2k's avatar
aaronchen2k2k 已提交
503 504

			expectTxt := strings.TrimSpace(child.Expect)
505 506
			stepChild.Expect = expectTxt

aaronchen2k2k's avatar
aaronchen2k2k 已提交
507
			steps = append(steps, stepChild)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567

			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")
}

568
func GetExpectMapFromIndependentFileObsolete(steps *[]commDomain.ZentaoCaseStep, content string, withEmptyExpect bool) {
569
	expectArr := ReadExpectIndependentArrObsolete(content)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
570

571 572 573
	for idx, step := range *steps {
		if len(expectArr) > idx {
			step.Expect = strings.Join(expectArr[idx], "\r\n")
aaronchen2k2k's avatar
aaronchen2k2k 已提交
574 575
		} else {
			if withEmptyExpect {
576
				step.Expect = ""
aaronchen2k2k's avatar
aaronchen2k2k 已提交
577 578 579 580 581
			}
		}
	}
}

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

aaronchen2k2k's avatar
aaronchen2k2k 已提交
585 586 587 588 589
	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 已提交
590 591
		} else {
			if withEmptyExpect {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
592
				(*steps)[idx].Expect = ""
aaronchen2k2k's avatar
aaronchen2k2k 已提交
593 594 595 596
			}
		}
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
597
	return
aaronchen2k2k's avatar
aaronchen2k2k 已提交
598 599
}

600 601
func GetCaseContent(stepObj commDomain.ZtfStep, seq string, independentFile bool, isChild bool) (
	stepContent, expectContent string) {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
602 603 604 605 606 607 608

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

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

aaronchen2k2k's avatar
aaronchen2k2k 已提交
609
	if !independentFile {
610
		stepContent = stepStr + expectStr
aaronchen2k2k's avatar
aaronchen2k2k 已提交
611
	} else {
612 613 614 615
		stepContent = stepStr
		if stepObj.Children == nil || len(stepObj.Children) == 0 {
			stepContent += " >>"
		}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
616
	}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
617

618 619
	expectContent = expectStr

aaronchen2k2k's avatar
aaronchen2k2k 已提交
620 621 622
	stepContent = html.UnescapeString(stepContent)
	expectContent = html.UnescapeString(expectContent)

623
	return
aaronchen2k2k's avatar
aaronchen2k2k 已提交
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
}

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
	}

646 647
	isSingleLine := strings.Count(str, "\r\n") == 0
	if isSingleLine {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
648 649 650 651 652
		if independentFile {
			ret = str
		} else {
			ret = " >> " + str
		}
653
	} else { // multi-line
aaronchen2k2k's avatar
aaronchen2k2k 已提交
654
		rpl := "\r\n"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
655

656 657 658 659 660 661 662 663
		space := "  "
		spaceBeforeTerminator := ""
		spaceBeforeText := space
		if isChild {
			spaceBeforeTerminator = space
			spaceBeforeText = strings.Repeat(space, 2)
		}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
664
		if independentFile {
665 666 667 668 669
			//>>
			//	expect 1.2 line 1
			//	expect 1.2 line 2
			//>>
			ret = ">>\n" + space + strings.ReplaceAll(str, rpl, rpl+space) + "\n>>"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
670
		} else {
671 672 673 674 675 676 677
			//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 已提交
678 679 680 681 682 683 684 685 686 687 688 689 690
		}
	}

	return
}

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

	return false
}
691 692 693 694 695 696 697 698 699 700 701 702 703 704

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

aaronchen2k2k's avatar
aaronchen2k2k 已提交
705
func GetCaseInfo(file string) (pass bool, caseId, productId int, title string) {
706 707
	content := fileUtils.ReadFile(file)
	isOldFormat := strings.Index(content, "[esac]") > -1
aaronchen2k2k's avatar
aaronchen2k2k 已提交
708
	pass = CheckFileContentIsScript(content)
709 710 711 712 713
	if !pass {
		return false, caseId, productId, title
	}

	caseInfo := ""
714
	lang := langHelper.GetLangByFile(file)
715 716 717 718 719
	regStr := ""
	if isOldFormat {
		regStr = `(?s)\[case\](.*)\[esac\]`
	} else {
		regStr = fmt.Sprintf(`(?sm)%s((?U:.*pid.*))\n(.*)%s`,
720
			commConsts.LangCommentsRegxMap[lang][0], commConsts.LangCommentsRegxMap[lang][1])
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
	}
	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])
	}

	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 已提交
742
	myExp = regexp.MustCompile(`[\S\s]*title=([^\n]*?)\n`)
743 744
	arr = myExp.FindStringSubmatch(caseInfo)
	if len(arr) > 1 {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
745
		title = strings.TrimSpace(arr[1])
746 747
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
748 749 750 751 752
	if caseId <= 0 {
		pass = false
	}

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

//func ReadScriptCheckpoints(file string) ([]string, [][]string) {
//	_, expectIndependentContent := GetDependentExpect(file)
//
//	content := fileUtils.ReadFile(file)
//	_, checkpoints := ReadCaseInfo(content)
//
//	cpStepArr, expectArr := getCheckpointStepArr(checkpoints, expectIndependentContent)
//
//	return cpStepArr, expectArr
//}
//func getCheckpointStepArr(content string, expectIndependentContent string) ([]string, [][]string) {
//	cpStepArr := make([]string, 0)
//	expectArr := make([][]string, 0)
//
//	independentExpect := expectIndependentContent != ""
//
//	lines := strings.Split(content, "\n")
//	i := 0
//	for i < len(lines) {
//		step := ""
//		expects := make([]string, 0)
//
//		line := strings.TrimSpace(lines[i])
//
//		regx := regexp.MustCompile(`(?U:[\d\.]*)(.+)>>(.*)`)
//		arr := regx.FindStringSubmatch(line)
//		if len(arr) > 2 {
//			step = arr[1]
//			if !independentExpect {
//				expects = append(expects, strings.TrimSpace(arr[2]))
//			}
//		} else {
//			regx = regexp.MustCompile(`\[([\d\.]*).*expects\]`)
//			arr = regx.FindStringSubmatch(line)
//			if len(arr) > 1 {
//				step = arr[1]
//
//				if !independentExpect {
//					for i+1 < len(lines) {
//						ln := strings.TrimSpace(lines[i+1])
//
//						if strings.Index(ln, "[") == 0 || strings.Index(ln, ">>") > 0 || ln == "" {
//							break
//						} else {
//							expects = append(expects, ln)
//							i++
//						}
//					}
//				}
//			}
//		}
//
//		if step != "" && len(expects) > 0 {
//			cpStepArr = append(cpStepArr, step)
//			if !independentExpect {
//				expectArr = append(expectArr, expects)
//			}
//		}
//		i++
//	}
//
//	if independentExpect {
//		expectArr = ReadExpectIndependentArrObsolete(expectIndependentContent)
//	}
//
//	return cpStepArr, expectArr
//}

func ReadExpectIndependentArrObsolete(content string) [][]string {
	lines := strings.Split(content, "\n")

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

	for idx, line := range lines {
		line = strings.TrimSpace(line)

		if line == ">>" { // more than one line
			cpArr = make([]string, 0)
		} else if strings.Index(line, ">>") == 0 { // single line
			line = strings.Replace(line, ">>", "", -1)
			line = strings.TrimSpace(line)

			cpArr = append(cpArr, line)
			ret = append(ret, cpArr)
			cpArr = make([]string, 0)
		} else { // under >>
			cpArr = append(cpArr, line)

			if idx == len(lines)-1 || strings.Index(lines[idx+1], ">>") > -1 {
				ret = append(ret, cpArr)
				cpArr = make([]string, 0)
			}
		}
	}

	return ret
}

func ReadExpectIndependentArr(content string) [][]string {
855 856 857 858 859 860 861 862 863 864 865
	//正常显示6
	//E2.16
	//>>
	//  E2.2 - 16
	//  E2.2 - 26
	//>>
	//>>
	//  E3 - 16
	//  E3 - 26
	//>>

866 867 868 869 870
	lines := strings.Split(content, "\n")

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

871 872 873 874
	currModel := ""
	idx := 0
	for idx < len(lines) {
		line := strings.TrimSpace(lines[idx])
875 876

		if line == ">>" { // more than one line
877
			currModel = "multi"
878
			cpArr = make([]string, 0)
879
		} else if currModel == "multi" { // in >> and >> in multi line mode
880 881
			cpArr = append(cpArr, line)

882
			if idx == len(lines)-1 || strings.Index(lines[idx+1], ">>") > -1 { // end multi line
883
				temp := make([]string, 0)
884
				temp = append(temp, strings.Join(cpArr, "\r\n"))
885 886 887

				ret = append(ret, temp)
				cpArr = make([]string, 0)
888 889 890
				currModel = ""

				idx += 1
891 892 893 894
			}
		} else if line == ">>" {
			continue
		} else {
895
			currModel = "single"
896 897 898 899 900 901 902

			line = strings.TrimSpace(line)

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

		idx += 1
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
	}

	return ret
}

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

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

	model := ""
	for idx, line := range lines {
		line = strings.TrimSpace(line)

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

		if line == ">>" { // more than one line
			model = "multi"
			cpArr = make([]string, 0)
		} else if strings.Index(line, ">>") == 0 { // single line
			model = "single"

			line = strings.Replace(line, ">>", "", -1)
			line = strings.TrimSpace(line)

			cpArr = append(cpArr, line)
			ret = append(ret, cpArr)
			cpArr = make([]string, 0)
		} else {
			if model == "" || model == "single" {
				continue
			}

			// under >>
			cpArr = append(cpArr, line)

			if idx == len(lines)-1 || strings.Index(lines[idx+1], ">>") > -1 {
				ret = append(ret, cpArr)
				cpArr = make([]string, 0)
			}
		}
	}

	return
}

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 已提交
978
				temp = append(temp, cpArr...)
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021

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

				idx = idx + 1
				model = ""
			}
		} else if line == ">>" {
			continue
		} 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 {
	pass, _ := regexp.MatchString(`cid\b*=`, content)

	return pass
}

func ReadCaseInfo(content, lang string, isOldFormat bool) (info, checkpoints string) {
	regStr := ""
	if isOldFormat {
		regStr = `(?s)\[case\]((?U:.*pid.*))\n(.*)\[esac\]`
	} else {
		regStr = fmt.Sprintf(`(?smU)%s((?U:.*pid.*))\n(.*)%s`,
1022
			commConsts.LangCommentsRegxMap[lang][0], commConsts.LangCommentsRegxMap[lang][1])
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
	}
	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) {
1049
	dir := fileUtils.AddFilePathSepIfNeeded(filepath.Dir(file))
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
	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, ""
}

1065
func GetScriptByIdsInDir(dirPth string, idMap *map[int]string) error {
1066 1067
	dirPth = fileUtils.AbsolutePath(dirPth)

1068
	sep := consts.FilePthSep
1069

1070
	if commonUtils.IgnoreZtfFile(dirPth) {
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
		return nil
	}

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

	for _, fi := range dir {
		name := fi.Name()
		if fi.IsDir() { // 目录, 递归遍历
1082
			GetScriptByIdsInDir(dirPth+name+sep, idMap)
1083
		} else {
1084
			regx := langHelper.GetSupportLanguageExtRegx()
1085 1086 1087 1088 1089 1090 1091 1092 1093
			pass, _ := regexp.MatchString("^*.\\."+regx+"$", name)

			if !pass {
				continue
			}

			path := dirPth + name
			pass, id, _, _ := GetCaseInfo(path)
			if pass {
1094
				(*idMap)[id] = path
1095 1096 1097 1098 1099 1100
			}
		}
	}

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

1102
func GetCaseIdsInSuiteFile(name string, ids *[]int) {
m0_58228130's avatar
m0_58228130 已提交
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
	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 {
1113
			*ids = append(*ids, id)
m0_58228130's avatar
m0_58228130 已提交
1114 1115 1116
		}
	}
}