forked from tebeka/go2xunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (88 loc) · 2.17 KB
/
main.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
package main
import (
"fmt"
"io"
"log"
"os"
"time"
"github.com/tebeka/go2xunit/lib"
)
const (
// Version is the current version
Version = "1.4.4"
)
// getInput return input io.File from file name, if file name is - it will
// return os.Stdin
func getInput(filename string) (*os.File, error) {
if filename == "-" || filename == "" {
return os.Stdin, nil
}
return os.Open(filename)
}
// getInput return output io.File from file name, if file name is - it will
// return os.Stdout
func getOutput(filename string) (*os.File, error) {
if filename == "-" || filename == "" {
return os.Stdout, nil
}
return os.Create(filename)
}
// getIO returns input and output streams from file names
func getIO(inFile, outFile string) (*os.File, io.Writer, error) {
input, err := getInput(inFile)
if err != nil {
return nil, nil, fmt.Errorf("can't open %s for reading: %s", inFile, err)
}
output, err := getOutput(outFile)
if err != nil {
return nil, nil, fmt.Errorf("can't open %s for writing: %s", outFile, err)
}
return input, output, nil
}
func main() {
if args.showVersion {
fmt.Printf("go2xunit %s\n", Version)
os.Exit(0)
}
// No time ... prefix for error messages
log.SetFlags(0)
if err := validateArgs(); err != nil {
log.Fatalf("error: %s", err)
}
input, output, err := getIO(args.inFile, args.outFile)
if err != nil {
log.Fatalf("error: %s", err)
}
// We'd like the test time to be the time of the generated file
var testTime time.Time
stat, err := input.Stat()
if err != nil {
testTime = time.Now()
} else {
testTime = stat.ModTime()
}
var parse func(rd io.Reader, suiteName string) (lib.Suites, error)
if args.isGocheck {
parse = lib.ParseGocheck
} else {
parse = lib.ParseGotest
}
suites, err := parse(input, args.suitePrefix)
if err != nil {
log.Fatalf("error: %s", err)
}
if len(suites) == 0 {
log.Fatalf("error: no tests found")
os.Exit(1)
}
xmlTemplate := lib.XUnitTemplate
if args.xunitnetOut {
xmlTemplate = lib.XUnitNetTemplate
} else if args.bambooOut || (len(suites) > 1) {
xmlTemplate = lib.XMLMultiTemplate
}
lib.WriteXML(suites, output, xmlTemplate, testTime)
if args.fail && suites.HasFailures() {
os.Exit(1)
}
}