github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/test/report.go (about)

     1  package test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"bytes"
     6  )
     7  
     8  var newLine = []byte("\n")
     9  var checkFailed = []byte("failed: ")
    10  
    11  func ExtractFailedLines(file string, line int) string {
    12  	fileContent, err := ioutil.ReadFile(file)
    13  	if err != nil {
    14  		return "check failed"
    15  	}
    16  	line = line - 1
    17  	lines := bytes.Split(fileContent, newLine)
    18  	if line < 0 || line >= len(lines) {
    19  		return "check failed"
    20  	}
    21  	report := checkFailed
    22  	openBraces := 0
    23  	for ;line < len(lines);line++ {
    24  		report = append(report, bytes.TrimSpace(lines[line])...)
    25  		openBraces += bytes.Count(lines[line], []byte{'('})
    26  		openBraces -= bytes.Count(lines[line], []byte{')'})
    27  		if openBraces == 0 {
    28  			break
    29  		}
    30  	}
    31  	return string(report)
    32  }