forked from bazelbuild/rules_go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrap.go
77 lines (70 loc) · 2.24 KB
/
wrap.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
// Copyright 2020 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"strconv"
)
// testWrapperAbnormalExit is used by the testwrapper to indicate the child
// process exitted without an exit code (for example being killed by a signal).
// We use 6, in line with Bazel's RUN_FAILURE.
const testWrapperAbnormalExit = 6
func shouldWrap() bool {
if wrapEnv, ok := os.LookupEnv("GO_TEST_WRAP"); ok {
wrap, err := strconv.ParseBool(wrapEnv)
if err != nil {
log.Fatalf("invalid value for GO_TEST_WRAP: %q", wrapEnv)
}
return wrap
}
_, ok := os.LookupEnv("XML_OUTPUT_FILE")
return ok
}
func wrap(pkg string) error {
var jsonBuffer bytes.Buffer
jsonConverter := NewConverter(&jsonBuffer, pkg, Timestamp)
args := append([]string{"-test.v"}, os.Args[1:]...)
cmd := exec.Command(os.Args[0], args...)
cmd.Env = append(os.Environ(), "GO_TEST_WRAP=0")
cmd.Stderr = os.Stderr
cmd.Stdout = io.MultiWriter(os.Stdout, jsonConverter)
err := cmd.Run()
jsonConverter.Close()
if out, ok := os.LookupEnv("XML_OUTPUT_FILE"); ok {
werr := writeReport(jsonBuffer, pkg, out)
if werr != nil {
if err != nil {
return fmt.Errorf("error while generating testreport: %s, (error wrapping test execution: %s)", werr, err)
}
return fmt.Errorf("error while generating testreport: %s", werr)
}
}
return err
}
func writeReport(jsonBuffer bytes.Buffer, pkg string, path string) error {
xml, cerr := json2xml(&jsonBuffer, pkg)
if cerr != nil {
return fmt.Errorf("error converting test output to xml: %s", cerr)
}
if err := ioutil.WriteFile(path, xml, 0664); err != nil {
return fmt.Errorf("error writing test xml: %s", err)
}
return nil
}