Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

project: ensure withServices doesn't blow stack on cycles #274

Merged
merged 1 commit into from Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions types/project.go
Expand Up @@ -142,26 +142,26 @@ func (p Project) WithServices(names []string, fn ServiceFunc) error {
return p.withServices(names, fn, map[string]bool{})
}

func (p Project) withServices(names []string, fn ServiceFunc, done map[string]bool) error {
func (p Project) withServices(names []string, fn ServiceFunc, seen map[string]bool) error {
services, err := p.GetServices(names...)
if err != nil {
return err
}
for _, service := range services {
if done[service.Name] {
if seen[service.Name] {
continue
}
seen[service.Name] = true
dependencies := service.GetDependencies()
if len(dependencies) > 0 {
err := p.withServices(dependencies, fn, done)
err := p.withServices(dependencies, fn, seen)
if err != nil {
return err
}
}
if err := fn(service); err != nil {
return err
}
done[service.Name] = true
}
return nil
}
Expand Down
7 changes: 7 additions & 0 deletions types/project_test.go
Expand Up @@ -83,6 +83,13 @@ func Test_ForServices(t *testing.T) {
assert.Equal(t, p.DisabledServices[0].Name, "service_3")
}

func Test_ForServicesCycle(t *testing.T) {
p := makeProject()
p.Services[0].Links = []string{"service_2"}
err := p.ForServices([]string{"service_2"})
assert.NilError(t, err)
}

func makeProject() Project {
return Project{
Services: append(Services{},
Expand Down