github.com/0xKiwi/rules_go@v0.24.3/go/tools/testwrapper/wrap.go (about)

     1  // Copyright 2020 The Bazel Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"io"
    21  	"io/ioutil"
    22  	"log"
    23  	"os"
    24  	"os/exec"
    25  	"strconv"
    26  )
    27  
    28  // testWrapperAbnormalExit is used by the testwrapper to indicate the child
    29  // process exitted without an exit code (for example being killed by a signal).
    30  // We use 6, in line with Bazel's RUN_FAILURE.
    31  const testWrapperAbnormalExit = 6
    32  
    33  func shouldWrap() bool {
    34  	if wrapEnv, ok := os.LookupEnv("GO_TEST_WRAP"); ok {
    35  		wrap, err := strconv.ParseBool(wrapEnv)
    36  		if err != nil {
    37  			log.Fatalf("invalid value for GO_TEST_WRAP: %q", wrapEnv)
    38  		}
    39  		return wrap
    40  	}
    41  	_, ok := os.LookupEnv("XML_OUTPUT_FILE")
    42  	return ok
    43  }
    44  
    45  // shouldAddTestV indicates if the test wrapper should prepend a -test.v flag to
    46  // the test args. This is required to get information about passing tests from
    47  // test2json for complete XML reports.
    48  func shouldAddTestV() bool {
    49  	if wrapEnv, ok := os.LookupEnv("GO_TEST_WRAP_TESTV"); ok {
    50  		wrap, err := strconv.ParseBool(wrapEnv)
    51  		if err != nil {
    52  			log.Fatalf("invalid value for GO_TEST_WRAP_TESTV: %q", wrapEnv)
    53  		}
    54  		return wrap
    55  	}
    56  	return false
    57  }
    58  
    59  func wrap(pkg string) error {
    60  	var jsonBuffer bytes.Buffer
    61  	jsonConverter := NewConverter(&jsonBuffer, pkg, Timestamp)
    62  
    63  	args := os.Args[1:]
    64  	if shouldAddTestV() {
    65  		args = append([]string{"-test.v"}, args...)
    66  	}
    67  	cmd := exec.Command(os.Args[0], args...)
    68  	cmd.Env = append(os.Environ(), "GO_TEST_WRAP=0")
    69  	cmd.Stderr = os.Stderr
    70  	cmd.Stdout = io.MultiWriter(os.Stdout, jsonConverter)
    71  	err := cmd.Run()
    72  	jsonConverter.Close()
    73  	if out, ok := os.LookupEnv("XML_OUTPUT_FILE"); ok {
    74  		werr := writeReport(jsonBuffer, pkg, out)
    75  		if werr != nil {
    76  			if err != nil {
    77  				return fmt.Errorf("error while generating testreport: %s, (error wrapping test execution: %s)", werr, err)
    78  			}
    79  			return fmt.Errorf("error while generating testreport: %s", werr)
    80  		}
    81  	}
    82  	return err
    83  }
    84  
    85  func writeReport(jsonBuffer bytes.Buffer, pkg string, path string) error {
    86  	xml, cerr := json2xml(&jsonBuffer, pkg)
    87  	if cerr != nil {
    88  		return fmt.Errorf("error converting test output to xml: %s", cerr)
    89  	}
    90  	if err := ioutil.WriteFile(path, xml, 0664); err != nil {
    91  		return fmt.Errorf("error writing test xml: %s", err)
    92  	}
    93  	return nil
    94  }