-
Notifications
You must be signed in to change notification settings - Fork 10
/
xmlReport.go
118 lines (101 loc) · 3.14 KB
/
xmlReport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
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
/*----------------------------------------------------------------
* Copyright (c) ThoughtWorks, Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE in the project root for license information.
*----------------------------------------------------------------*/
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/getgauge/xml-report/logger"
"github.com/getgauge/common"
"github.com/getgauge/gauge-proto/go/gauge_messages"
"github.com/getgauge/xml-report/builder"
)
const (
defaultReportsDir = "reports"
gaugeReportsDirEnvName = "gauge_reports_dir" // directory where reports are generated by plugins
executionAction = "execution"
pluginActionEnv = "xml-report_action"
xmlReport = "xml-report"
overwriteReportsEnvProperty = "overwrite_reports"
resultFile = "result.xml"
timeFormat = "2006-01-02 15.04.05"
)
var projectRoot string
var pluginDir string
func createReport(suiteResult *gauge_messages.SuiteExecutionResult) {
dir := createReportsDirectory()
bytes, err := builder.NewXmlBuilder(0).GetXmlContent(suiteResult)
if err != nil {
logger.Fatal("Report generation failed: %s \n", err)
}
err = writeResultFile(dir, bytes)
if err != nil {
logger.Fatal("Report generation failed: %s \n", err)
}
logger.Info("Successfully generated xml-report to => %s\n", dir)
}
func writeResultFile(reportDir string, bytes []byte) error {
resultPath := filepath.Join(reportDir, resultFile)
err := os.WriteFile(resultPath, bytes, common.NewFilePermissions)
if err != nil {
return fmt.Errorf("failed to copy file: %s %s\n ", resultFile, err)
}
return nil
}
func findPluginAndProjectRoot() {
projectRoot = os.Getenv(common.GaugeProjectRootEnv)
if projectRoot == "" {
logger.Fatal("Environment variable '%s' is not set. \n", common.GaugeProjectRootEnv)
}
var err error
pluginDir, err = os.Getwd()
if err != nil {
logger.Fatal("Error finding current working directory: %s \n", err)
}
}
func createReportsDirectory() string {
reportsDir, err := filepath.Abs(os.Getenv(gaugeReportsDirEnvName))
if reportsDir == "" || err != nil {
reportsDir = defaultReportsDir
}
currentReportDir := filepath.Join(reportsDir, xmlReport, getNameGen().randomName())
createDirectory(currentReportDir)
return currentReportDir
}
func createDirectory(dir string) {
if common.DirExists(dir) {
return
}
if err := os.MkdirAll(dir, common.NewDirectoryPermissions); err != nil {
logger.Fatal("Failed to create directory %s: %s\n", defaultReportsDir, err)
}
}
func getNameGen() nameGenerator {
if shouldOverwriteReports() {
return emptyNameGenerator{}
}
return timeStampedNameGenerator{}
}
type nameGenerator interface {
randomName() string
}
type timeStampedNameGenerator struct{}
func (T timeStampedNameGenerator) randomName() string {
return time.Now().Format(timeFormat)
}
type emptyNameGenerator struct{}
func (T emptyNameGenerator) randomName() string {
return ""
}
func shouldOverwriteReports() bool {
envValue := os.Getenv(overwriteReportsEnvProperty)
if strings.ToLower(envValue) == "true" {
return true
}
return false
}