Skip to content

Commit

Permalink
feat: Add start node for DOT format
Browse files Browse the repository at this point in the history
Signed-off-by: Sergiy Kulanov <[email protected]>
  • Loading branch information
SergK committed Oct 8, 2023
1 parent 2039799 commit 74e7618
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
10 changes: 9 additions & 1 deletion pkg/taskgraph/taskgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func (g *TaskGraph) ToDOT() *DOT {
}

for _, node := range g.Nodes {
if !node.hasParent {
// "start" is the special node that represents the start of the pipeline
dot.Edges = append(dot.Edges, fmt.Sprintf(" \"start\" -> \"%s\"", node.Name))
}
if len(node.Dependencies) == 0 {
// "end" is the special node that represents the end of the pipeline
dot.Edges = append(dot.Edges, fmt.Sprintf(" \"%s\" -> \"end\"", node.Name))
Expand All @@ -94,6 +98,10 @@ func (g *TaskGraph) ToDOTWithTaskRef() *DOT {
}

for _, node := range g.Nodes {
if !node.hasParent {
// "start" is the special node that represents the start of the pipeline
dot.Edges = append(dot.Edges, fmt.Sprintf(" \"start\" -> \"%s\n(%s)\"", node.Name, node.TaskRefName))
}
if len(node.Dependencies) == 0 {
// "end" is the special node that represents the end of the pipeline
dot.Edges = append(dot.Edges, fmt.Sprintf(" \"%s\n(%s)\" -> \"end\"", node.Name, node.TaskRefName))
Expand All @@ -109,7 +117,7 @@ func (g *TaskGraph) ToDOTWithTaskRef() *DOT {
// String converts a DOT graph to a string
func (d *DOT) String() string {
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("%s {\n labelloc=\"t\"\n label=\"%s\"\n end [shape=\"point\" width=0.2]\n", d.Format, d.Name))
buf.WriteString(fmt.Sprintf("%s {\n labelloc=\"t\"\n label=\"%s\"\n end [shape=\"point\" width=0.2]\n start [shape=\"point\" width=0.2]\n", d.Format, d.Name))
for _, edge := range d.Edges {
buf.WriteString(fmt.Sprintf("%s\n", edge))
}
Expand Down
15 changes: 14 additions & 1 deletion pkg/taskgraph/taskgraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ func TestTaskGraphToDOT(t *testing.T) {
assert.Contains(t, dot.Edges, " \"task3\" -> \"task2\"")
assert.Contains(t, dot.Edges, " \"task1\" -> \"end\"")
assert.Contains(t, dot.Edges, " \"task4\" -> \"end\"")
assert.Contains(t, dot.Edges, " \"start\" -> \"task4\"")
assert.Contains(t, dot.Edges, " \"start\" -> \"task3\"")
}

func TestTaskGraphToDOTWithTaskRef(t *testing.T) {
Expand All @@ -96,6 +98,8 @@ func TestTaskGraphToDOTWithTaskRef(t *testing.T) {
assert.Contains(t, dot.Edges, " \"task3\n(taskRef3)\" -> \"task2\n(taskRef2)\"")
assert.Contains(t, dot.Edges, " \"task1\n(taskRef1)\" -> \"end\"")
assert.Contains(t, dot.Edges, " \"task4\n(taskRef4)\" -> \"end\"")
assert.Contains(t, dot.Edges, " \"start\" -> \"task3\n(taskRef3)\"")
assert.Contains(t, dot.Edges, " \"start\" -> \"task4\n(taskRef4)\"")
}

func TestDOTString(t *testing.T) {
Expand All @@ -114,7 +118,16 @@ func TestDOTString(t *testing.T) {
}

// Test the String method
expected := "digraph {\n labelloc=\"t\"\n label=\"test-pipeline\"\n end [shape=\"point\" width=0.2]\n \"task1\" -> \"task2\"\n \"task1\" -> \"task3\"\n \"task2\" -> \"task3\"\n}\n"
expected := `digraph {
labelloc="t"
label="test-pipeline"
end [shape="point" width=0.2]
start [shape="point" width=0.2]
"task1" -> "task2"
"task1" -> "task3"
"task2" -> "task3"
}
`
assert.Equal(t, expected, dot.String())
}

Expand Down

0 comments on commit 74e7618

Please sign in to comment.