refactor: split color func from runGoTest

main
hau 2020-12-18 17:21:21 +07:00
parent 4467751b1a
commit c72f25c2db
1 changed files with 47 additions and 44 deletions

91
main.go
View File

@ -33,50 +33,8 @@ func runGoTest() int {
cmd.Stdout = outWriter cmd.Stdout = outWriter
cmd.Stderr = errWriter cmd.Stderr = errWriter
go func() { go colorOutputReader(outReader)
defer outReader.Close() go colorErrorReader(errReader)
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)
}
}()
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
return 1 return 1
@ -84,3 +42,48 @@ func runGoTest() int {
return 0 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)
}
}