refactor: split color func from runGoTest
parent
4467751b1a
commit
c72f25c2db
91
main.go
91
main.go
|
@ -33,50 +33,8 @@ func runGoTest() int {
|
|||
cmd.Stdout = outWriter
|
||||
cmd.Stderr = errWriter
|
||||
|
||||
go func() {
|
||||
defer outReader.Close()
|
||||
|
||||
scanner := bufio.NewScanner(outReader)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
trimmedLine := strings.TrimSpace(line)
|
||||
|
||||
if strings.HasPrefix(trimmedLine, "--- PASS") ||
|
||||
strings.HasPrefix(line, "PASS") ||
|
||||
strings.HasPrefix(line, "ok") {
|
||||
color.Green("%s\n", line)
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.HasPrefix(trimmedLine, "--- FAIL") ||
|
||||
strings.HasPrefix(line, "FAIL") {
|
||||
color.Red("%s\n", line)
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Println(line)
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
log.Printf("scanner error: %s", err)
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
defer errReader.Close()
|
||||
|
||||
scanner := bufio.NewScanner(errReader)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
color.Red("%s\n", line)
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
log.Printf("scanner error: %s", err)
|
||||
}
|
||||
}()
|
||||
go colorOutputReader(outReader)
|
||||
go colorErrorReader(errReader)
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
return 1
|
||||
|
@ -84,3 +42,48 @@ func runGoTest() int {
|
|||
|
||||
return 0
|
||||
}
|
||||
|
||||
func colorOutputReader(reader io.ReadCloser) {
|
||||
defer reader.Close()
|
||||
|
||||
scanner := bufio.NewScanner(reader)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
trimmedLine := strings.TrimSpace(line)
|
||||
|
||||
if strings.HasPrefix(trimmedLine, "--- PASS") ||
|
||||
strings.HasPrefix(line, "PASS") ||
|
||||
strings.HasPrefix(line, "ok") {
|
||||
color.Green("%s\n", line)
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.HasPrefix(trimmedLine, "--- FAIL") ||
|
||||
strings.HasPrefix(line, "FAIL") {
|
||||
color.Red("%s\n", line)
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Println(line)
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
log.Printf("scanner error: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func colorErrorReader(reader io.ReadCloser) {
|
||||
defer reader.Close()
|
||||
|
||||
scanner := bufio.NewScanner(reader)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
color.Red("%s\n", line)
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
log.Printf("scanner error: %s", err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue