From 3bc9a91314929eb12c02fca136a1204a809f1b42 Mon Sep 17 00:00:00 2001 From: aaron <462826@qq.com> Date: Thu, 4 May 2023 13:37:06 +0800 Subject: [PATCH] support jacoco --- cmd/command/main.go | 5 ++ internal/pkg/consts/var.go | 1 + internal/pkg/domain/testing-jacoco.go | 66 +++++++++++++++++++++++ internal/pkg/helper/exec/exec.go | 1 + internal/pkg/helper/exec/report-jacoco.go | 47 ++++++++++++++++ internal/pkg/helper/exec/unit.go | 7 ++- res/server/en/messages.json | 4 ++ res/server/zh/messages.json | 4 ++ xdoc/jacoco/jacoco.xml | 66 +++++++++++++++++++++++ 9 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 internal/pkg/domain/testing-jacoco.go create mode 100644 internal/pkg/helper/exec/report-jacoco.go create mode 100644 xdoc/jacoco/jacoco.xml diff --git a/cmd/command/main.go b/cmd/command/main.go index ecd54f9d..bd16c6f1 100644 --- a/cmd/command/main.go +++ b/cmd/command/main.go @@ -82,6 +82,8 @@ func main() { flagSet.StringVar(&unitTestTool, "unitTestTool", "", "") flagSet.StringVar(&unitBuildTool, "unitBuildTool", "", "") + flagSet.StringVar(&commConsts.JacocoReport, "jacocoReport", "", "") + flagSet.StringVar(&keywords, "k", "", "") flagSet.StringVar(&keywords, "keywords", "", "") @@ -265,6 +267,9 @@ func runUnitTest(args []string) { if commConsts.AllureReportDir != "" { start = start + 2 } + if commConsts.JacocoReport != "" { + start = start + 2 + } if unitTestTool != "" { start = start + 2 } diff --git a/internal/pkg/consts/var.go b/internal/pkg/consts/var.go index f5719064..bf0c0d94 100644 --- a/internal/pkg/consts/var.go +++ b/internal/pkg/consts/var.go @@ -27,6 +27,7 @@ var ( UnitBuildTool BuildTool UnitTestTool TestTool AllureReportDir string + JacocoReport string ProductId string ZenTaoVersion string diff --git a/internal/pkg/domain/testing-jacoco.go b/internal/pkg/domain/testing-jacoco.go new file mode 100644 index 00000000..4def3936 --- /dev/null +++ b/internal/pkg/domain/testing-jacoco.go @@ -0,0 +1,66 @@ +package commDomain + +type JacocoReport struct { + Sessioninfo JacocoSessioninfo `xml:"sessioninfo"` + Package JacocoPackage `xml:"package"` + Counter []JacocoCounter `xml:"counter"` + Name string `xml:"name,attr"` +} + +type JacocoCounter struct { + Type Type `xml:"type,attr"` + Missed string `xml:"missed,attr"` + Covered string `xml:"covered,attr"` +} + +type JacocoPackage struct { + Class JacocoClassClass `xml:"class"` + Sourcefile JacocoSourcefile `xml:"sourcefile"` + Counter []JacocoCounter `xml:"counter"` + Name string `xml:"name,attr"` +} + +type JacocoClassClass struct { + Method []JacocoMethodElement `xml:"method"` + Counter []JacocoCounter `xml:"counter"` + Name string `xml:"name,attr"` + Sourcefilename string `xml:"sourcefilename,attr"` +} + +type JacocoMethodElement struct { + Counter []JacocoCounter `xml:"counter"` + Name string `xml:"name,attr"` + Desc string `xml:"desc,attr"` + Line string `xml:"line,attr"` +} + +type JacocoSourcefile struct { + Line []JacocoLineElement `xml:"line"` + Counter []JacocoCounter `xml:"counter"` + Name string `xml:"name,attr"` +} + +type JacocoLineElement struct { + Nr string `xml:"nr,attr"` + Mi string `xml:"mi,attr"` + Ci string `xml:"ci,attr"` + MB string `xml:"mb,attr"` + Cb string `xml:"cb,attr"` +} + +type JacocoSessioninfo struct { + ID string `xml:"id,attr"` + Start string `xml:"start,attr"` + Dump string `xml:"dump,attr"` +} + +type Type string + +const ( + Branch Type = "BRANCH" + Class Type = "CLASS" + Complexity Type = "COMPLEXITY" + Instruction Type = "INSTRUCTION" + Line Type = "LINE" + Method Type = "METHOD" +) diff --git a/internal/pkg/helper/exec/exec.go b/internal/pkg/helper/exec/exec.go index e7e8d1ac..45928d68 100644 --- a/internal/pkg/helper/exec/exec.go +++ b/internal/pkg/helper/exec/exec.go @@ -35,6 +35,7 @@ func Exec(ch chan int, req serverDomain.ExecReq, msg *websocket.Message) ( ExecUnit(ch, testSet, msg) } }() // for defer + } return diff --git a/internal/pkg/helper/exec/report-jacoco.go b/internal/pkg/helper/exec/report-jacoco.go new file mode 100644 index 00000000..c9735e90 --- /dev/null +++ b/internal/pkg/helper/exec/report-jacoco.go @@ -0,0 +1,47 @@ +package execHelper + +import ( + "encoding/xml" + commConsts "github.com/easysoft/zentaoatf/internal/pkg/consts" + commDomain "github.com/easysoft/zentaoatf/internal/pkg/domain" + dateUtils "github.com/easysoft/zentaoatf/pkg/lib/date" + fileUtils "github.com/easysoft/zentaoatf/pkg/lib/file" + i118Utils "github.com/easysoft/zentaoatf/pkg/lib/i118" + logUtils "github.com/easysoft/zentaoatf/pkg/lib/log" + "github.com/fatih/color" + "time" +) + +const ( + Tmpl = `Counter Missed Covered +------------------------------------------ +%11s %s %s +%11s %s %s +%11s %s %s +%11s %s %s +%11s %s %s +%11s %s %s +` +) + +func GenJacocoCovReport() (report commDomain.JacocoReport) { + content := fileUtils.ReadFileBuf(commConsts.JacocoReport) + + xml.Unmarshal(content, &report) + + var params []interface{} + for _, counter := range report.Counter { + params = append(params, string(counter.Type)) + params = append(params, counter.Missed) + params = append(params, counter.Covered) + } + + title := i118Utils.Sprintf("jacoco_report") + + msg := dateUtils.DateTimeStr(time.Now()) + " " + title + " \n" + i118Utils.Sprintf(Tmpl, params...) + + logUtils.ExecConsole(color.FgCyan, msg) + logUtils.ExecResult(msg) + + return +} diff --git a/internal/pkg/helper/exec/unit.go b/internal/pkg/helper/exec/unit.go index 42775cf9..c2580e1b 100644 --- a/internal/pkg/helper/exec/unit.go +++ b/internal/pkg/helper/exec/unit.go @@ -41,7 +41,7 @@ func ExecUnit(ch chan int, iris.Map{"key": key, "status": "start"}, wsMsg) } - //deal with -allureReportDir param + //deal with -allureReportDir param arr := strings.Split(req.Cmd, " ") if len(arr) > 1 && strings.TrimSpace(arr[0]) == "-allureReportDir" { commConsts.AllureReportDir = arr[1] @@ -74,6 +74,11 @@ func ExecUnit(ch chan int, // gen report report := GenUnitTestReport(req, startTime.Unix(), entTime.Unix(), ch, wsMsg) + // dealwith jacoco report + if commConsts.JacocoReport != "" { + GenJacocoCovReport() + } + // submit result if req.SubmitResult && (report.FuncResult != nil || report.UnitResult != nil) { configDir := req.WorkspacePath diff --git a/res/server/en/messages.json b/res/server/en/messages.json index 95a1590f..eaef53f6 100644 --- a/res/server/en/messages.json +++ b/res/server/en/messages.json @@ -726,6 +726,10 @@ { "id": "pls_check_zentao_url", "translation": "%s, Zentao version '%s'.\n Please use above OpenSource 17.0, Biz 7.0, Max 3.1 version, or you may install RestAPI plugin according to UserManual section 4.4." + }, + { + "id": "jacoco_report", + "translation": "Jacoco Coverage Report" } ] } diff --git a/res/server/zh/messages.json b/res/server/zh/messages.json index e5d05219..c410c00c 100644 --- a/res/server/zh/messages.json +++ b/res/server/zh/messages.json @@ -714,6 +714,10 @@ { "id": "pls_check_zentao_url", "translation": "%s,当前禅道版本'%s'。\n请确认您使用了禅道开源17.0、企业7.0、旗舰3.1以上版本,或已按照用户手册4.4小节成功安装RestAPI插件。" + }, + { + "id": "jacoco_report", + "translation": "Jacoco覆盖率报告" } ] } diff --git a/xdoc/jacoco/jacoco.xml b/xdoc/jacoco/jacoco.xml new file mode 100644 index 00000000..12779768 --- /dev/null +++ b/xdoc/jacoco/jacoco.xml @@ -0,0 +1,66 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- GitLab