Skip to content

Commit

Permalink
Merge pull request #391 from asteris-llc/fix/385-dependencies-in-cond…
Browse files Browse the repository at this point in the history
…itionals

Fix/385 dependencies in conditionals
  • Loading branch information
rebeccaskinner authored Oct 18, 2016
2 parents b223b10 + d538be4 commit 90e5869
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
11 changes: 8 additions & 3 deletions load/dependencyresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,23 @@ func ResolveDependencies(ctx context.Context, g *graph.Graph) (*graph.Graph, err
out.Connect(meta.ID, dep)
}
}

return nil
})
}

func getDepends(_ *graph.Graph, id string, node *parse.Node) ([]string, error) {
func getDepends(g *graph.Graph, id string, node *parse.Node) ([]string, error) {
deps, err := node.GetStringSlice("depends")
switch err {
case parse.ErrNotFound:
return []string{}, nil
case nil:
for idx := range deps {
deps[idx] = graph.SiblingID(id, deps[idx])
for idx, dep := range deps {
if ancestor, ok := getNearestAncestor(g, id, dep); ok {
deps[idx] = ancestor
} else {
return nil, fmt.Errorf("nonexistent vertices in edges: %s", dep)
}
}
return deps, nil
default:
Expand Down
20 changes: 19 additions & 1 deletion load/dependencyresolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/stretchr/testify/require"
)

// TestDependencyResolverResolvesDependencies tests dependency resolution
func TestDependencyResolverResolvesDependencies(t *testing.T) {
defer logging.HideLogs(t)()

Expand All @@ -40,6 +41,23 @@ func TestDependencyResolverResolvesDependencies(t *testing.T) {
)
}

// TestDependencyResolverResolvesExplicitDepsInBranch tests explicit
// dependencies inside of case branch nodes
func TestDependencyResolverResolvesExplicitDepsInBranch(t *testing.T) {
defer logging.HideLogs(t)()

nodes, err := load.Nodes(context.Background(), "../samples/conditionalDeps.hcl", false)
require.NoError(t, err)

resolved, err := load.ResolveDependencies(context.Background(), nodes)
assert.NoError(t, err)
assert.Contains(
t,
graph.Targets(resolved.DownEdges("root/macro.switch.sample/macro.case.true/file.content.foo-output")),
"root/task.query.foo",
)
}

func TestDependencyResolverBadDependency(t *testing.T) {
defer logging.HideLogs(t)()

Expand All @@ -48,7 +66,7 @@ func TestDependencyResolverBadDependency(t *testing.T) {

_, err = load.ResolveDependencies(context.Background(), nodes)
if assert.Error(t, err) {
assert.EqualError(t, err, "nonexistent vertices in edges: root/task.nonexistent")
assert.EqualError(t, err, "1 error(s) occurred:\n\n* root/task.bad_requirement: nonexistent vertices in edges: task.nonexistent")
}
}

Expand Down
26 changes: 26 additions & 0 deletions samples/conditionalDeps.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
task.query "foo" {
query = "echo foo"
}

task.query "bar" {
query = "echo bar"
}

switch "sample" {
case "true" "true" {
task.query "baz" {
query = "echo baz"
}

file.content "baz" {
destination = "baz-file.txt"
content = "{{lookup `task.query.baz.status.stdout`}}"
}

file.content "foo-output" {
destination = "foo-file.txt"
content = "{{lookup `task.query.foo.status.stdout`}}"
depends = ["task.query.bar", "file.content.baz"]
}
}
}
8 changes: 0 additions & 8 deletions transform/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ func getSwitchNode(id string, g *graph.Graph) (*control.SwitchTask, bool) {
return nil, false
}
elem := elemMeta.Value()
elem, canResolve := resource.ResolveTask(elem)
if !canResolve {
return nil, false
}
if asSwitch, ok := elem.(*control.SwitchTask); ok {
return asSwitch, true
}
Expand All @@ -90,10 +86,6 @@ func getCaseNode(id string, g *graph.Graph) (*control.CaseTask, bool) {
return nil, false
}
elem := elemMeta.Value()
elem, canResolve := resource.ResolveTask(elem)
if !canResolve {
return nil, false
}
if asCase, ok := elem.(*control.CaseTask); ok {
return asCase, true
}
Expand Down

0 comments on commit 90e5869

Please sign in to comment.