Skip to content

Commit 9d2f533

Browse files
authored
Added template func to colorize text (#11838)
1 parent 8817cec commit 9d2f533

File tree

5 files changed

+68
-9
lines changed

5 files changed

+68
-9
lines changed

.ci/magician/cmd/templates.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func color(color, text string) string {
8+
if color == "" || text == "" {
9+
return text
10+
}
11+
return fmt.Sprintf("$\\textcolor{%s}{\\textsf{%s}}$", color, text)
12+
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{{- if gt (len .RecordingResult.PassedTests) 0 -}}
2-
$\textcolor{green}{\textsf{Tests passed during RECORDING mode:}}$
2+
{{color "green" "Tests passed during RECORDING mode:"}}
33
{{range .RecordingResult.PassedTests}}`{{.}}`[[Debug log](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-{{$.PRNumber}}/artifacts/{{$.BuildID}}/recording/{{.}}.log)]
44
{{end}}
55

66
{{- if gt (len .ReplayingAfterRecordingResult.FailedTests ) 0 -}}
7-
$\textcolor{red}{\textsf{Tests failed when rerunning REPLAYING mode:}}$
7+
{{color "red" "Tests failed when rerunning REPLAYING mode:"}}
88
{{range .ReplayingAfterRecordingResult.FailedTests}}`{{.}}`[[Error message](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-{{$.PRNumber}}/artifacts/{{$.BuildID}}/build-log/replaying_build_after_recording/{{.}}_replaying_test.log)] [[Debug log](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-{{$.PRNumber}}/artifacts/{{$.BuildID}}/replaying_after_recording/{{.}}.log)]
99
{{end}}
1010

@@ -13,19 +13,19 @@ Tests failed due to non-determinism or randomness when the VCR replayed the resp
1313
Please fix these to complete your PR. If you believe these test failures to be incorrect or unrelated to your change, or if you have any questions, please raise the concern with your reviewer.
1414

1515
{{else}}
16-
$\textcolor{green}{\textsf{No issues found for passed tests after REPLAYING rerun.}}$
16+
{{color "green" "No issues found for passed tests after REPLAYING rerun."}}
1717
{{end}}{{/* end of if gt (len .ReplayingAfterRecordingResult.FailedTests ) 0 */}}
1818
---
1919
{{end}}{{/* end of if gt (len .RecordingResult.PassedTests) 0 */}}
2020

2121
{{if gt (len .RecordingResult.FailedTests) 0 -}}
22-
$\textcolor{red}{\textsf{Tests failed during RECORDING mode:}}$
22+
{{color "red" "Tests failed during RECORDING mode:"}}
2323
{{range .RecordingResult.FailedTests}}`{{.}}`[[Error message](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-{{$.PRNumber}}/artifacts/{{$.BuildID}}/build-log/recording_build/{{.}}_recording_test.log)] [[Debug log](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-{{$.PRNumber}}/artifacts/{{$.BuildID}}/recording/{{.}}.log)]
2424
{{end}}
2525
{{end}} {{- /* end of if gt (len .RecordingResult.FailedTests) 0 */ -}}
2626

27-
{{if .HasTerminatedTests}}$\textcolor{red}{\textsf{Several tests got terminated during RECORDING mode.}}${{end}}
28-
{{if .RecordingErr}}$\textcolor{red}{\textsf{Errors occurred during RECORDING mode. Please fix them to complete your PR.}}${{end}}
29-
{{if .AllRecordingPassed}}$\textcolor{green}{\textsf{All tests passed!}}${{end}}
27+
{{if .HasTerminatedTests}}{{color "red" "Several tests got terminated during RECORDING mode."}}{{end}}
28+
{{if .RecordingErr}}{{color "red" "Errors occurred during RECORDING mode. Please fix them to complete your PR."}}{{end}}
29+
{{if .AllRecordingPassed}}{{color "green" "All tests passed!"}}{{end}}
3030

3131
View the [build log](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-{{.PRNumber}}/artifacts/{{.BuildID}}/build-log/recording_test.log) or the [debug log](https://console.cloud.google.com/storage/browser/ci-vcr-logs/beta/refs/heads/auto-pr-{{.PRNumber}}/artifacts/{{.BuildID}}/recording) for each test
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{- if .ReplayingErr -}}
2-
$\textcolor{red}{\textsf{Errors occurred during REPLAYING mode. Please fix them to complete your PR.}}$
2+
{{color "red" "Errors occurred during REPLAYING mode. Please fix them to complete your PR."}}
33
{{- else -}}
4-
$\textcolor{green}{\textsf{All tests passed!}}$
4+
{{color "green" "All tests passed!"}}
55
{{- end}}
66

77
View the [build log](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-{{.PRNumber}}/artifacts/{{.BuildID}}/build-log/replaying_test.log)

.ci/magician/cmd/templates_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package cmd
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestColor(t *testing.T) {
8+
cases := []struct {
9+
name string
10+
color string
11+
text string
12+
want string
13+
}{
14+
{
15+
name: "red",
16+
color: "red",
17+
text: "Test text",
18+
want: "$\\textcolor{red}{\\textsf{Test text}}$",
19+
},
20+
{
21+
name: "green",
22+
color: "green",
23+
text: "Test text",
24+
want: "$\\textcolor{green}{\\textsf{Test text}}$",
25+
},
26+
{
27+
name: "empty color",
28+
text: "Test text",
29+
want: "Test text",
30+
},
31+
{
32+
name: "empty text",
33+
color: "green",
34+
want: "",
35+
},
36+
}
37+
38+
for _, tc := range cases {
39+
t.Run(tc.name, func(t *testing.T) {
40+
got := color(tc.color, tc.text)
41+
if got != tc.want {
42+
t.Errorf("color(%s, %s) got %s; want %s", tc.color, tc.text, got, tc.want)
43+
}
44+
})
45+
}
46+
}

.ci/magician/cmd/test_terraform_vcr.go

+1
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ func formatComment(fileName string, tmplText string, data any) (string, error) {
477477
funcs := template.FuncMap{
478478
"join": strings.Join,
479479
"add": func(i, j int) int { return i + j },
480+
"color": color,
480481
}
481482
tmpl, err := template.New(fileName).Funcs(funcs).Parse(tmplText)
482483
if err != nil {

0 commit comments

Comments
 (0)