Skip to content

Commit

Permalink
close task#339019
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronchen2k committed Jun 8, 2021
1 parent 363cb35 commit 55fd685
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 10 deletions.
2 changes: 1 addition & 1 deletion demo/lang/javascript/1_string_match.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
/**
/*
title=check string matches pattern
cid=0
pid=0
Expand Down
51 changes: 51 additions & 0 deletions demo/sample/8_extract_desc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
title=sync step from comments
cid=1
pid=0
group1
1.1 >> expect 1.1
1.2 >> expect 1.2
1.3 >> expect 1.3 ]]
group2
2.1 >> expect 2.1
2.2 >> expect 2.2
2.3 >> expect 2.3 ]]
multi line expect >>
expect 3.1
expect 3.2
>>
4 >> expect 4
5 >> expect 5
*/


/* group: group1 */
run() && out() && expect(); // Step: 1.1 >> expect 1.1
run() && out() && expect(); // Step: 1.2 >> expect 1.2
run() && out() && expect(); // Step: 1.3 >> expect 1.3 ]]

/* group: group2 */
run() && out() && expect(); // Step: 2.1 >> expect 2.1
run() && out() && expect(); // Step: 2.2 >> expect 2.2
run() && out() && expect(); // Step: 2.3 >> expect 2.3 ]]

/**
step: multi line expect >>
expect 3.1
expect 3.2
>>
*
*
*/
run() && out() && expect();
run() && out() && expect(); // step: 4 >> expect 4
run() && out() && expect(); // step: 5 >> expect 5
212 changes: 212 additions & 0 deletions src/action/extract.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package action

import (
"fmt"
"github.com/easysoft/zentaoatf/src/model"
assertUtils "github.com/easysoft/zentaoatf/src/utils/assert"
constant "github.com/easysoft/zentaoatf/src/utils/const"
fileUtils "github.com/easysoft/zentaoatf/src/utils/file"
i118Utils "github.com/easysoft/zentaoatf/src/utils/i118"
langUtils "github.com/easysoft/zentaoatf/src/utils/lang"
logUtils "github.com/easysoft/zentaoatf/src/utils/log"
zentaoUtils "github.com/easysoft/zentaoatf/src/utils/zentao"
"regexp"
"strings"
)

const (
groupTag = "group:"
stepTag = "step:"
)

func Extract(files []string) error {
logUtils.InitLogger()

cases := assertUtils.GetCaseByDirAndFile(files)

if len(cases) < 1 {
logUtils.PrintTo("\n" + i118Utils.I118Prt.Sprintf("no_cases"))
return nil
}

for _, file := range cases {
stepObjs := extractFromComments(file)
steps := prepareSteps(stepObjs)
desc := prepareDesc(steps, file)
replaceCaseDesc(desc, file)
}

return nil
}
func prepareSteps(stepObjs []*model.TestStep) (steps []string) {
for _, stepObj := range stepObjs {
line := stepObj.Desc

if len(stepObj.Children) == 0 && stepObj.Expect != "" {
if stepObj.MutiLine {
line += " >>\n" + stepObj.Expect + ">>\n"
} else {
line += " >> " + stepObj.Expect
}
}

steps = append(steps, line)

for _, childObj := range stepObj.Children {
lineChild := childObj.Desc + " >> " + childObj.Expect
steps = append(steps, lineChild)
}
if len(stepObj.Children) > 0 {
steps = append(steps, "")
}
}

return
}

func extractFromComments(file string) (stepObjs []*model.TestStep) {
lang := langUtils.GetLangByFile(file)
content := fileUtils.ReadFile(file)

findCaseTag := false
start := false
inGroup := false

lines := strings.Split(content, "\n")
for index := 0; index < len(lines); index++ {
line := lines[index]
lowerLine := strings.ToLower(line)

if strings.Index(lowerLine, "cid") > -1 {
findCaseTag = true
continue
}
if findCaseTag && !start {
if !isCommentEndTag(line, lang) {
continue
}

start = true
continue
}
if !start {
continue
}

if strings.Index(lowerLine, groupTag) > -1 { // is group
groupName, _ := getName(line, groupTag, lang)
stepObj := model.TestStep{Desc: groupName}
stepObjs = append(stepObjs, &stepObj)
inGroup = true

continue
}

if strings.Index(lowerLine, stepTag) > -1 {
isMuti := false
stepName, expect := getName(line, stepTag, lang)
if expect == "" {
moreExpect, increase := parseMutiStep(lang, lines[index+1:])
if increase > 0 {
expect = moreExpect
index += increase
isMuti = true
}
}

stepObj := model.TestStep{Desc: stepName, Expect: expect, MutiLine: isMuti}

if inGroup {
stepObjs[len(stepObjs)-1].Children = append(stepObjs[len(stepObjs)-1].Children, stepObj)

if strings.Index(line, "]]") > -1 {
inGroup = false
}
} else {
stepObjs = append(stepObjs, &stepObj)
}
}
}
return
}

func prepareDesc(steps []string, file string) (desc string) {
pass, caseId, productId, title := zentaoUtils.GetCaseInfo(file)
if !pass {
return
}

info := make([]string, 0)
info = append(info, fmt.Sprintf("title=%s", title))
info = append(info, fmt.Sprintf("cid=%d", caseId))
info = append(info, fmt.Sprintf("pid=%d", productId))
info = append(info, "\n"+strings.Join(steps, "\n"))

desc = strings.Join(info, "\n")
return
}

func replaceCaseDesc(desc, file string) {
content := fileUtils.ReadFile(file)
lang := langUtils.GetLangByFile(file)

regStr := fmt.Sprintf(`(?smU)%s((?U:.*pid.*))\n(.*)%s`,
constant.LangCommentsRegxMap[lang][0], constant.LangCommentsRegxMap[lang][1])

re, _ := regexp.Compile(regStr)
out := re.ReplaceAllString(content, "\n/**\n\n"+desc+"\n\n*/\n")

fileUtils.WriteFile(file, out)
}

func getName(line, str, lang string) (name, expect string) {
lowerLine := strings.ToLower(line)

idx := strings.Index(lowerLine, str)
name = line[idx+len(str):]

if strings.Index(str, "step:") > -1 {
arr := strings.Split(name, ">>")
if len(arr) > 1 {
name = strings.TrimSpace(arr[0])
expect = strings.TrimSpace(arr[1])
}
}

regx, _ := regexp.Compile(constant.LangCommentsTagMap[lang][1])
name = strings.TrimSpace(regx.ReplaceAllString(name, ""))
expect = strings.TrimSpace(regx.ReplaceAllString(expect, ""))

return
}

func parseMutiStep(lang string, nextLines []string) (ret string, increase int) {
for index, line := range nextLines {
if isCommentStartTag(line, lang) || isCommentEndTag(line, lang) {
break
}

if strings.Index(line, "<<") > -1 || strings.TrimSpace(line) == ">>" {
increase = index
break
}

ret += strings.TrimSpace(line) + "\n"
}

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

return
}

func isCommentStartTag(str, lang string) (pass bool) {
pass, _ = regexp.MatchString(constant.LangCommentsRegxMap[lang][0], str)
return
}

func isCommentEndTag(str, lang string) (pass bool) {
pass, _ = regexp.MatchString(constant.LangCommentsRegxMap[lang][1], str)
return
}
2 changes: 1 addition & 1 deletion src/service/script/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func GenerateTestCaseScript(cs model.TestCase, langType string, independentFile

if fileUtils.FileExist(scriptFile) { // update title and steps
regStr := fmt.Sprintf(`(?sm)%s((?U:.*pid.*))\n(.*)%s`,
constant.LangCommentsMap[langType][0], constant.LangCommentsMap[langType][1])
constant.LangCommentsRegxMap[langType][0], constant.LangCommentsRegxMap[langType][1])

// replace info
re, _ := regexp.Compile(regStr)
Expand Down
2 changes: 1 addition & 1 deletion src/service/script/viewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func Brief(file string, keywords string) (bool, []string) {
`cid=([^\n]*)\n+`+
`pid=([^\n]*)\n+`+
`([\S\s]*)\n*%s`,
constant.LangCommentsMap[lang][0], constant.LangCommentsMap[lang][1])
constant.LangCommentsRegxMap[lang][0], constant.LangCommentsRegxMap[lang][1])
}
myExp := regexp.MustCompile(regStr)
arr := myExp.FindStringSubmatch(content)
Expand Down
18 changes: 15 additions & 3 deletions src/utils/const/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,24 @@ var (
RunModeServer = "server"
RunModeRequest = "request"

LangCommentsMap = map[string][]string{
LangCommentsTagMap = map[string][]string{
"bat": {"goto start", ":start"},
"javascript": {"/\\*{1,}", "\\*{1,}/"},
"lua": {"--\\[\\[", "\\]\\]"},
"perl": {"=pod", "=cut"},
"php": {"/\\*{1,}", "\\*{1,}/"},
"python": {"'''", "'''"},
"ruby": {"=begin", "=end"},
"shell": {":<<!", "!"},
"tcl": {"set case {", "}"},
}

LangCommentsRegxMap = map[string][]string{
"bat": {"^\\s*goto start\\s*$", "^\\s*:start\\s*$"},
"javascript": {"^\\s*/\\*{2,}\\s*$", "^\\s*\\*{1,}/\\s*$"},
"javascript": {"^\\s*/\\*{1,}\\s*$", "^\\s*\\*{1,}/\\s*$"},
"lua": {"^\\s*--\\[\\[\\s*$", "^\\s*\\]\\]\\s*$"},
"perl": {"^\\s*=pod\\s*$", "^\\s*=cut\\s*$"},
"php": {"^\\s*/\\*{2,}\\s*$", "^\\s*\\*{1,}/\\s*$"},
"php": {"^\\s*/\\*{1,}\\s*$", "^\\s*\\*{1,}/\\s*$"},
"python": {"^\\s*'''\\s*$", "^\\s*'''\\s*$"},
"ruby": {"^\\s*=begin\\s*$", "^\\s*=end\\s*$"},
"shell": {"^\\s*:<<!\\s*$", "^\\s*!\\s*$"},
Expand Down
4 changes: 2 additions & 2 deletions src/utils/script/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ func SortFile(file string) {
from = `(?s)\[case\].*\[esac\]`
to = "[case]\n" + info + "\n" + stepsTxt + "\n\n[esac]"
} else {
from = fmt.Sprintf(`(?s)%s.*%s`, constant.LangCommentsMap[lang][0], constant.LangCommentsMap[lang][1])
from = fmt.Sprintf(`(?s)%s.*%s`, constant.LangCommentsRegxMap[lang][0], constant.LangCommentsRegxMap[lang][1])
to = fmt.Sprintf("%s\n"+info+"\n"+stepsTxt+"\n\n%s",
constant.LangCommentsMap[lang][0], constant.LangCommentsMap[lang][1])
constant.LangCommentsRegxMap[lang][0], constant.LangCommentsRegxMap[lang][1])
}
re, _ := regexp.Compile(from)
script := re.ReplaceAllString(txt, to)
Expand Down
4 changes: 2 additions & 2 deletions src/utils/zentao/zentao.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func GetCaseInfo(file string) (bool, int, int, string) {
regStr = `(?s)\[case\](.*)\[esac\]`
} else {
regStr = fmt.Sprintf(`(?sm)%s((?U:.*pid.*))\n(.*)%s`,
constant.LangCommentsMap[lang][0], constant.LangCommentsMap[lang][1])
constant.LangCommentsRegxMap[lang][0], constant.LangCommentsRegxMap[lang][1])
}
myExp := regexp.MustCompile(regStr)
arr := myExp.FindStringSubmatch(content)
Expand Down Expand Up @@ -335,7 +335,7 @@ func ReadCaseInfo(content, lang string, isOldFormat bool) (info, checkpoints str
regStr = `(?s)\[case\]((?U:.*pid.*))\n(.*)\[esac\]`
} else {
regStr = fmt.Sprintf(`(?sm)%s((?U:.*pid.*))\n(.*)%s`,
constant.LangCommentsMap[lang][0], constant.LangCommentsMap[lang][1])
constant.LangCommentsRegxMap[lang][0], constant.LangCommentsRegxMap[lang][1])
}
myExp := regexp.MustCompile(regStr)
arr := myExp.FindStringSubmatch(content)
Expand Down
4 changes: 4 additions & 0 deletions src/ztf.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ func main() {
//log.Println("===" + os.Getenv("debug"))
run(os.Args)

case "extract":
files := fileUtils.GetFilesFromParams(os.Args[2:])
action.Extract(files)

case "expect":
files := fileUtils.GetFilesFromParams(os.Args[2:])
action.GenExpectFiles(files)
Expand Down

0 comments on commit 55fd685

Please sign in to comment.