bake: fix skipped group when already visited by another one

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2022-03-23 11:11:15 +01:00
parent 1ca30a58c2
commit 0b8dde1071
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
2 changed files with 81 additions and 4 deletions

View File

@ -412,12 +412,12 @@ func (c Config) newOverrides(v []string) (map[string]map[string]Override, error)
}
func (c Config) ResolveGroup(name string) []string {
return c.group(name, map[string]struct{}{})
return dedupString(c.group(name, map[string][]string{}))
}
func (c Config) group(name string, visited map[string]struct{}) []string {
func (c Config) group(name string, visited map[string][]string) []string {
if _, ok := visited[name]; ok {
return nil
return visited[name]
}
var g *Group
for _, group := range c.Groups {
@ -429,7 +429,7 @@ func (c Config) group(name string, visited map[string]struct{}) []string {
if g == nil {
return []string{name}
}
visited[name] = struct{}{}
visited[name] = []string{}
targets := make([]string, 0, len(g.Targets))
for _, t := range g.Targets {
tgroup := c.group(t, visited)
@ -439,6 +439,7 @@ func (c Config) group(name string, visited map[string]struct{}) []string {
targets = append(targets, t)
}
}
visited[name] = targets
return targets
}

View File

@ -1045,3 +1045,79 @@ func TestTargetName(t *testing.T) {
})
}
}
func TestNestedGroupsWithSameTarget(t *testing.T) {
ctx := context.TODO()
f := File{
Name: "docker-bake.hcl",
Data: []byte(`
group "a" {
targets = ["b", "c"]
}
group "b" {
targets = ["d"]
}
group "c" {
targets = ["b"]
}
target "d" {
context = "."
dockerfile = "./testdockerfile"
}
group "e" {
targets = ["a", "f"]
}
target "f" {
context = "./foo"
}`)}
cases := []struct {
name string
targets []string
ntargets int
}{
{
name: "a",
targets: []string{"b", "c"},
ntargets: 1,
},
{
name: "b",
targets: []string{"d"},
ntargets: 1,
},
{
name: "c",
targets: []string{"b"},
ntargets: 1,
},
{
name: "d",
targets: []string{"d"},
ntargets: 1,
},
{
name: "e",
targets: []string{"a", "f"},
ntargets: 2,
},
}
for _, tt := range cases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
m, g, err := ReadTargets(ctx, []File{f}, []string{tt.name}, nil, nil)
require.NoError(t, err)
require.Equal(t, 1, len(g))
require.Equal(t, tt.targets, g[0].Targets)
require.Equal(t, tt.ntargets, len(m))
require.Equal(t, ".", *m["d"].Context)
require.Equal(t, "./testdockerfile", *m["d"].Dockerfile)
})
}
}