Skip to content

Commit 1360a9e

Browse files
authored
Merge pull request #3037 from crazy-max/0.21_picks_0.21.2
[v0.21] cherry-picks for v0.21.2
2 parents 7c2359c + 6019a2b commit 1360a9e

File tree

6 files changed

+103
-9
lines changed

6 files changed

+103
-9
lines changed

bake/hcl_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ func TestHCLAttrsCapsuleType(t *testing.T) {
608608
target "app" {
609609
attest = [
610610
{ type = "provenance", mode = "max" },
611-
"type=sbom,disabled=true",
611+
"type=sbom,disabled=true,generator=foo,\"ENV1=bar,baz\",ENV2=hello",
612612
]
613613
614614
cache-from = [
@@ -641,7 +641,7 @@ func TestHCLAttrsCapsuleType(t *testing.T) {
641641
require.NoError(t, err)
642642

643643
require.Equal(t, 1, len(c.Targets))
644-
require.Equal(t, []string{"type=provenance,mode=max", "type=sbom,disabled=true"}, stringify(c.Targets[0].Attest))
644+
require.Equal(t, []string{"type=provenance,mode=max", "type=sbom,disabled=true,\"ENV1=bar,baz\",ENV2=hello,generator=foo"}, stringify(c.Targets[0].Attest))
645645
require.Equal(t, []string{"type=local,dest=../out", "type=oci,dest=../out.tar"}, stringify(c.Targets[0].Outputs))
646646
require.Equal(t, []string{"type=local,src=path/to/cache", "user/app:cache"}, stringify(c.Targets[0].CacheFrom))
647647
require.Equal(t, []string{"type=local,dest=path/to/cache"}, stringify(c.Targets[0].CacheTo))

tests/bake.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func bakeCmd(sb integration.Sandbox, opts ...cmdOpt) (string, error) {
3838
var bakeTests = []func(t *testing.T, sb integration.Sandbox){
3939
testBakePrint,
4040
testBakePrintSensitive,
41+
testBakePrintOverrideEmpty,
4142
testBakeLocal,
4243
testBakeLocalMulti,
4344
testBakeRemote,
@@ -286,6 +287,47 @@ RUN echo "Hello ${HELLO}"
286287
}
287288
}
288289

290+
func testBakePrintOverrideEmpty(t *testing.T, sb integration.Sandbox) {
291+
dockerfile := []byte(`
292+
FROM scratch
293+
COPY foo /foo
294+
`)
295+
bakefile := []byte(`
296+
target "default" {
297+
cache-to = ["type=gha,mode=min,scope=integration-tests"]
298+
}
299+
`)
300+
dir := tmpdir(
301+
t,
302+
fstest.CreateFile("docker-bake.hcl", bakefile, 0600),
303+
fstest.CreateFile("Dockerfile", dockerfile, 0600),
304+
fstest.CreateFile("foo", []byte("foo"), 0600),
305+
)
306+
307+
cmd := buildxCmd(sb, withDir(dir), withArgs("bake", "--print", "--set", "*.cache-to="))
308+
stdout := bytes.Buffer{}
309+
stderr := bytes.Buffer{}
310+
cmd.Stdout = &stdout
311+
cmd.Stderr = &stderr
312+
require.NoError(t, cmd.Run(), stdout.String(), stderr.String())
313+
314+
require.JSONEq(t, `{
315+
"group": {
316+
"default": {
317+
"targets": [
318+
"default"
319+
]
320+
}
321+
},
322+
"target": {
323+
"default": {
324+
"context": ".",
325+
"dockerfile": "Dockerfile"
326+
}
327+
}
328+
}`, stdout.String())
329+
}
330+
289331
func testBakeLocal(t *testing.T, sb integration.Sandbox) {
290332
dockerfile := []byte(`
291333
FROM scratch
@@ -871,6 +913,7 @@ target "default" {
871913
})
872914
}
873915
}
916+
874917
func testBakeSetNonExistingOutsideNoParallel(t *testing.T, sb integration.Sandbox) {
875918
for _, ent := range []bool{true, false} {
876919
t.Run(fmt.Sprintf("ent=%v", ent), func(t *testing.T) {

util/buildflags/attests.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,8 @@ func (a *Attest) UnmarshalText(text []byte) error {
148148
if !ok {
149149
return errors.Errorf("invalid value %s", field)
150150
}
151-
key = strings.TrimSpace(strings.ToLower(key))
152151

153-
switch key {
152+
switch strings.TrimSpace(strings.ToLower(key)) {
154153
case "type":
155154
a.Type = value
156155
case "disabled":

util/buildflags/attests_test.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,21 @@ func TestAttests(t *testing.T) {
1313
attests := Attests{
1414
{Type: "provenance", Attrs: map[string]string{"mode": "max"}},
1515
{Type: "sbom", Disabled: true},
16+
{Type: "sbom", Attrs: map[string]string{
17+
"generator": "scanner",
18+
"ENV1": `"foo,bar"`,
19+
"Env2": "hello",
20+
}},
1621
}
1722

18-
expected := `[{"type":"provenance","mode":"max"},{"type":"sbom","disabled":true}]`
23+
expected := `[{"type":"provenance","mode":"max"},{"type":"sbom","disabled":true},{"ENV1":"\"foo,bar\"","Env2":"hello","generator":"scanner","type":"sbom"}]`
1924
actual, err := json.Marshal(attests)
2025
require.NoError(t, err)
2126
require.JSONEq(t, expected, string(actual))
2227
})
2328

2429
t.Run("UnmarshalJSON", func(t *testing.T) {
25-
in := `[{"type":"provenance","mode":"max"},{"type":"sbom","disabled":true}]`
30+
in := `[{"type":"provenance","mode":"max"},{"type":"sbom","disabled":true},{"ENV1":"\"foo,bar\"","Env2":"hello","generator":"scanner","type":"sbom"}]`
2631

2732
var actual Attests
2833
err := json.Unmarshal([]byte(in), &actual)
@@ -31,6 +36,11 @@ func TestAttests(t *testing.T) {
3136
expected := Attests{
3237
{Type: "provenance", Attrs: map[string]string{"mode": "max"}},
3338
{Type: "sbom", Disabled: true, Attrs: map[string]string{}},
39+
{Type: "sbom", Disabled: false, Attrs: map[string]string{
40+
"generator": "scanner",
41+
"ENV1": `"foo,bar"`,
42+
"Env2": "hello",
43+
}},
3444
}
3545
require.Equal(t, expected, actual)
3646
})
@@ -41,7 +51,14 @@ func TestAttests(t *testing.T) {
4151
"type": cty.StringVal("provenance"),
4252
"mode": cty.StringVal("max"),
4353
}),
54+
cty.ObjectVal(map[string]cty.Value{
55+
"type": cty.StringVal("sbom"),
56+
"generator": cty.StringVal("scan"),
57+
"ENV1": cty.StringVal(`foo,bar`),
58+
"Env2": cty.StringVal(`hello`),
59+
}),
4460
cty.StringVal("type=sbom,disabled=true"),
61+
cty.StringVal(`type=sbom,generator=scan,"FOO=bar,baz",Hello=World`),
4562
})
4663

4764
var actual Attests
@@ -50,7 +67,17 @@ func TestAttests(t *testing.T) {
5067

5168
expected := Attests{
5269
{Type: "provenance", Attrs: map[string]string{"mode": "max"}},
70+
{Type: "sbom", Attrs: map[string]string{
71+
"generator": "scan",
72+
"ENV1": "foo,bar",
73+
"Env2": "hello",
74+
}},
5375
{Type: "sbom", Disabled: true, Attrs: map[string]string{}},
76+
{Type: "sbom", Attrs: map[string]string{
77+
"generator": "scan",
78+
"FOO": "bar,baz",
79+
"Hello": "World",
80+
}},
5481
}
5582
require.Equal(t, expected, actual)
5683
})
@@ -59,6 +86,11 @@ func TestAttests(t *testing.T) {
5986
attests := Attests{
6087
{Type: "provenance", Attrs: map[string]string{"mode": "max"}},
6188
{Type: "sbom", Disabled: true},
89+
{Type: "sbom", Attrs: map[string]string{
90+
"generator": "scan",
91+
"ENV1": `"foo,bar"`,
92+
"Env2": "hello",
93+
}},
6294
}
6395

6496
actual := attests.ToCtyValue()
@@ -71,6 +103,12 @@ func TestAttests(t *testing.T) {
71103
"type": cty.StringVal("sbom"),
72104
"disabled": cty.StringVal("true"),
73105
}),
106+
cty.MapVal(map[string]cty.Value{
107+
"type": cty.StringVal("sbom"),
108+
"generator": cty.StringVal("scan"),
109+
"ENV1": cty.StringVal(`"foo,bar"`),
110+
"Env2": cty.StringVal("hello"),
111+
}),
74112
})
75113

76114
result := actual.Equals(expected)

util/buildflags/cache.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ func ParseCacheEntry(in []string) (CacheOptions, error) {
175175

176176
opts := make(CacheOptions, 0, len(in))
177177
for _, in := range in {
178+
if in == "" {
179+
continue
180+
}
181+
178182
if !strings.Contains(in, "=") {
179183
// This is ref only format. Each field in the CSV is its own entry.
180184
fields, err := csvvalue.Fields(in, nil)

util/buildflags/export.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package buildflags
22

33
import (
4+
"encoding/csv"
45
"encoding/json"
56
"maps"
67
"regexp"
@@ -259,9 +260,18 @@ func (w *csvBuilder) Write(key, value string) {
259260
if w.sb.Len() > 0 {
260261
w.sb.WriteByte(',')
261262
}
262-
w.sb.WriteString(key)
263-
w.sb.WriteByte('=')
264-
w.sb.WriteString(value)
263+
264+
pair := key + "=" + value
265+
if strings.ContainsRune(pair, ',') || strings.ContainsRune(pair, '"') {
266+
var attr strings.Builder
267+
writer := csv.NewWriter(&attr)
268+
writer.Write([]string{pair})
269+
writer.Flush()
270+
// Strips the extra newline added by the csv writer
271+
pair = strings.TrimSpace(attr.String())
272+
}
273+
274+
w.sb.WriteString(pair)
265275
}
266276

267277
func (w *csvBuilder) WriteAttributes(attrs map[string]string) {

0 commit comments

Comments
 (0)