Skip to content

Commit 771e0de

Browse files
authored
Merge pull request #3732 from tonistiigi/imagetools-manifest-regex
imagetools: use regex for manifest template matching
2 parents 1e54ca2 + 78439e2 commit 771e0de

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

util/imagetools/printers.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"os"
9+
"regexp"
910
"strings"
1011
"text/tabwriter"
1112
"text/template"
@@ -162,7 +163,7 @@ func (p *Printer) Print(raw bool, out io.Writer) error {
162163
}
163164

164165
func isWholeManifestTemplate(format string) bool {
165-
return strings.TrimSpace(format) == "{{.Manifest}}"
166+
return regexp.MustCompile(`^\{\{\s*\.Manifest\s*\}\}$`).MatchString(strings.TrimSpace(format))
166167
}
167168

168169
func (p *Printer) printManifestList(out io.Writer) error {

util/imagetools/printers_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package imagetools
2+
3+
import "testing"
4+
5+
func TestIsWholeManifestTemplate(t *testing.T) {
6+
t.Parallel()
7+
8+
tests := []struct {
9+
name string
10+
format string
11+
match bool
12+
}{
13+
{
14+
name: "exact",
15+
format: "{{.Manifest}}",
16+
match: true,
17+
},
18+
{
19+
name: "trimmed-whitespace",
20+
format: " {{.Manifest}} ",
21+
match: true,
22+
},
23+
{
24+
name: "inner-whitespace",
25+
format: "{{ .Manifest }}",
26+
match: true,
27+
},
28+
{
29+
name: "not-whole-template",
30+
format: "{{.Manifest.Digest}}",
31+
match: false,
32+
},
33+
{
34+
name: "split-field-name-does-not-match",
35+
format: "{{.Mani fest }}",
36+
match: false,
37+
},
38+
{
39+
name: "extra-text",
40+
format: "{{.Manifest}}\n",
41+
match: true,
42+
},
43+
}
44+
45+
for _, tc := range tests {
46+
t.Run(tc.name, func(t *testing.T) {
47+
t.Parallel()
48+
if got := isWholeManifestTemplate(tc.format); got != tc.match {
49+
t.Fatalf("isWholeManifestTemplate(%q) = %v, want %v", tc.format, got, tc.match)
50+
}
51+
})
52+
}
53+
}

0 commit comments

Comments
 (0)