Skip to content

Commit c7d14cd

Browse files
committed
NO-JIRA: tests(buildinputs) implement a heredoc.Doc() helper to dedent backtick-strings (opendatahub-io#2522)
1 parent be6fa5d commit c7d14cd

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

scripts/buildinputs/buildinputs_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"os"
66
"path/filepath"
7+
"reflect"
78
"runtime"
89
"strings"
910
"testing"

scripts/buildinputs/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.24
44

55
require (
66
github.com/containerd/platforms v1.0.0-rc.1
7+
github.com/google/go-cmp v0.7.0
78
github.com/moby/buildkit v0.23.2
89
github.com/opencontainers/go-digest v1.0.0
910
github.com/opencontainers/image-spec v1.1.1

scripts/buildinputs/heredoc.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// llm-powered reimplementation of github.com/MakeNowJust/heredoc
2+
package main
3+
4+
import (
5+
"math"
6+
"strings"
7+
)
8+
9+
// Doc removes common leading whitespace from every line in a string.
10+
func Doc(s string) string {
11+
lines := strings.Split(s, "\n")
12+
minIndent := math.MaxInt32
13+
14+
// First, find the minimum indentation of non-empty lines.
15+
for _, line := range lines {
16+
if len(strings.TrimSpace(line)) == 0 {
17+
continue // Skip empty or whitespace-only lines
18+
}
19+
20+
indent := 0
21+
for _, r := range line {
22+
if r == ' ' || r == '\t' {
23+
indent++
24+
} else {
25+
break
26+
}
27+
}
28+
29+
if indent < minIndent {
30+
minIndent = indent
31+
}
32+
}
33+
34+
// If no common indentation is found, return the original string.
35+
if minIndent == math.MaxInt32 {
36+
return s
37+
}
38+
39+
// Create a builder to efficiently construct the new string.
40+
var builder strings.Builder
41+
for i, line := range lines {
42+
if i == 0 && line == "" {
43+
continue // Skip the first line if it's empty.
44+
}
45+
if len(strings.TrimSpace(line)) == 0 {
46+
if i != len(lines)-1 {
47+
// Unless this is the last line, in which case we drop trailing whitespace.
48+
builder.WriteString(line) // Keep empty lines as they are.
49+
}
50+
} else {
51+
// Trim the minimum common indentation from the start of the line.
52+
builder.WriteString(line[minIndent:])
53+
}
54+
55+
// Add the newline back, except for the very last line.
56+
if i < len(lines)-1 {
57+
builder.WriteString("\n")
58+
}
59+
}
60+
61+
return builder.String()
62+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/google/go-cmp/cmp"
7+
)
8+
9+
func TestDoc(t *testing.T) {
10+
input := `
11+
a
12+
b
13+
`
14+
diff(t, "a\nb\n", Doc(input))
15+
}
16+
17+
// diff errors with a diff between expected and actual if they are not equal.
18+
func diff(t *testing.T, expected, actual string) {
19+
t.Helper()
20+
if diff := cmp.Diff(expected, actual); diff != "" {
21+
t.Errorf("mismatch (-want +got):\n%s", diff)
22+
}
23+
}

0 commit comments

Comments
 (0)