Skip to content

Commit

Permalink
Truncate text (#7)
Browse files Browse the repository at this point in the history
Co-authored-by: Marcin Owsiany <[email protected]>
  • Loading branch information
janisz and porridge authored Feb 9, 2023
1 parent 0256333 commit 4c90ddf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
20 changes: 15 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,22 +255,22 @@ const (
desc = `
{{- if .Message }}
{code:title=Message|borderStyle=solid}
{{ .Message }}
{{ .Message | truncate }}
{code}
{{- end }}
{{- if .Stderr }}
{code:title=STDERR|borderStyle=solid}
{{ .Stderr }}
{{ .Stderr | truncate }}
{code}
{{- end }}
{{- if .Stdout }}
{code:title=STDOUT|borderStyle=solid}
{{ .Stdout }}
{{ .Stdout | truncate }}
{code}
{{- end }}
{{- if .Error }}
{code:title=ERROR|borderStyle=solid}
{{ .Error }}
{{ .Error | truncate }}
{code}
{{- end }}
Expand Down Expand Up @@ -352,7 +352,7 @@ func (tc *testCase) addSubTest(subTest junit.Test) {
}

func render(tc testCase, text string) (string, error) {
tmpl, err := template.New("test").Parse(text)
tmpl, err := template.New("test").Funcs(map[string]any{"truncate": truncate}).Parse(text)
if err != nil {
return "", err
}
Expand All @@ -372,3 +372,13 @@ func clearString(str string) string {
return ' '
}, str)
}

var maxTextBlockLength = 10000

func truncate(s string) string {
runes := []rune(s)
if len(runes) > maxTextBlockLength {
return string(runes[:maxTextBlockLength]) + "\n … too long, truncated."
}
return s
}
25 changes: 25 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,29 @@ org.spockframework.runtime.ConditionNotSatisfiedError: Condition not satisfied:
s, err := tc.summary()
assert.NoError(t, err)
assert.Equal(t, `DefaultPoliciesTest / Verify policy Apache Struts CVE-2017-5638 is triggered FAILED`, s)

maxTextBlockLength = 100
actual, err = tc.description()
assert.NoError(t, err)
assert.Equal(t, `
{code:title=Message|borderStyle=solid}
Condition not satisfied:
waitForViolation(deploymentName, policyName, 60)
| |
… too long, truncated.
{code}
{code:title=STDOUT|borderStyle=solid}
?[1;30m21:35:15?[0;39m | ?[34mINFO ?[0;39m | DefaultPoliciesTest | Starting testcase
?[1;30m21
… too long, truncated.
{code}
|| ENV || Value ||
| BUILD ID | [1|https://prow.ci.openshift.org/view/gs/origin-ci-test/logs//1]|
| BUILD TAG | [|]|
| JOB NAME ||
| CLUSTER ||
| ORCHESTRATOR ||
`, actual)
}

0 comments on commit 4c90ddf

Please sign in to comment.