Skip to content

Commit e6cfc34

Browse files
committed
Use a new way to compare the dot object
1 parent ee92032 commit e6cfc34

8 files changed

+120
-11
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ require (
1010
github.com/spf13/cobra v1.0.0
1111
github.com/stretchr/testify v1.5.1
1212
google.golang.org/api v0.21.0
13-
gopkg.in/yaml.v2 v2.2.8 // indirect
13+
gopkg.in/yaml.v2 v2.2.8
1414
)

internal/utils/cloudbuild_test.go

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package util
33
import (
44
"bytes"
55
"fmt"
6+
"gopkg.in/yaml.v2"
67
"io/ioutil"
78
"os"
89
"path"
@@ -11,11 +12,20 @@ import (
1112
"testing"
1213

1314
graphviz "github.com/goccy/go-graphviz"
15+
"github.com/goccy/go-graphviz/cgraph"
1416
"github.com/stretchr/testify/assert"
1517
)
1618

1719
var supportedFormat = []string{".yaml", ".yml", ".json"}
1820

21+
type result struct {
22+
Nodes []string `yaml:"nodes"`
23+
Edges []struct {
24+
From string `yaml:"from"`
25+
To string `yaml:"to"`
26+
} `yaml:"edges"`
27+
}
28+
1929
func init() {
2030
// Change the current working dir to root dir
2131
os.Chdir("../../")
@@ -35,24 +45,51 @@ func TestYamlToDAG(t *testing.T) {
3545
}
3646
for _, file := range testFiles {
3747
t.Run(fmt.Sprintf("TestYamlToDAG:%s", file), func(t *testing.T) {
38-
g := graphviz.New()
3948
cloudBuild, err := ParseYaml(file)
4049
assert.Empty(t, err)
4150
graph := BuildStepsToDAG(cloudBuild.Steps)
42-
var buf bytes.Buffer
43-
err = g.Render(graph, "dot", &buf)
44-
assert.Empty(t, err)
45-
dotFilePath := getDotFilePath(file)
46-
expected, err := ioutil.ReadFile(dotFilePath)
4751
assert.Empty(t, err)
48-
assert.Equal(t, strings.ReplaceAll(string(expected), "\r\n", "\n"), buf.String())
52+
53+
expectedResult := getExpectedResult(file)
54+
isExpectedGraph(t, expectedResult, graph)
4955
})
5056
}
5157
}
5258

53-
func getDotFilePath(filePath string) string {
59+
func getExpectedResult(filePath string) *result {
5460
ext := path.Ext(filePath)
5561
_, filename := filepath.Split(filePath)
56-
dotFile := filename[0:len(filename)-len(ext)] + ".dot"
57-
return filepath.Join("./", "test", "fixtures", "dot", dotFile)
62+
yamlFile := filename[0:len(filename)-len(ext)] + ".graph.yaml"
63+
fullPath := filepath.Join("./", "test", "fixtures", "graph", yamlFile)
64+
resultObj := result{}
65+
buf, _ := ioutil.ReadFile(fullPath)
66+
yaml.Unmarshal(buf, &resultObj)
67+
return &resultObj
68+
}
69+
70+
func isExpectedGraph(t *testing.T, expected *result, actual *cgraph.Graph) {
71+
assert.Equalf(t, len(expected.Nodes), actual.NumberNodes(), "Should have the same number of nodes.")
72+
for _, node := range expected.Nodes {
73+
n, err := actual.Node(node)
74+
assert.Empty(t, err)
75+
assert.NotEmptyf(t, n, "Expected Node %s but not found in generated graph", node)
76+
}
77+
assert.Equalf(t, len(expected.Edges), actual.NumberEdges(), "Should have the same number of edges.")
78+
var buf bytes.Buffer
79+
g := graphviz.New()
80+
g.Render(actual, "dot", &buf)
81+
actualOutput := buf.String()
82+
for _, edge := range expected.Edges {
83+
edge.From = sanitizeDotString(edge.From)
84+
edge.To = sanitizeDotString(edge.To)
85+
edgeInDot := fmt.Sprintf("%s -> %s", edge.From, edge.To)
86+
assert.Truef(t, strings.Contains(actualOutput, edgeInDot), "Should contain the edge \"%s\"\n Actual: %s", edgeInDot, actualOutput)
87+
}
88+
}
89+
90+
func sanitizeDotString(s string) string {
91+
if strings.Contains(s, " ") {
92+
return fmt.Sprintf("\"%s\"", s)
93+
}
94+
return s
5895
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
nodes:
2+
- A
3+
- B
4+
- C
5+
- D
6+
edges:
7+
- from: A
8+
to: C
9+
- from: B
10+
to: C
11+
- from: C
12+
to: D
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
nodes:
2+
- Step 0
3+
- Step 1
4+
- Step 2
5+
- Step 3
6+
edges:
7+
- from: Step 0
8+
to: Step 1
9+
- from: Step 1
10+
to: Step 2
11+
- from: Step 2
12+
to: Step 3
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
nodes:
2+
- Step 0
3+
- Step 1
4+
- Step 2
5+
- Step 3
6+
edges:
7+
- from: Step 0
8+
to: Step 1
9+
- from: Step 1
10+
to: Step 2
11+
- from: Step 2
12+
to: Step 3
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
nodes:
2+
- A
3+
- Step 2
4+
- B
5+
- Step 3
6+
edges:
7+
- from: A
8+
to: Step 2
9+
- from : B
10+
to: Step 3
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
nodes:
2+
- A
3+
- C
4+
- B
5+
- D
6+
edges:
7+
- from: A
8+
to: C
9+
- from: B
10+
to: C
11+
- from: B
12+
to: D
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
nodes:
2+
- A
3+
- C
4+
- B
5+
- D
6+
edges:
7+
- from: A
8+
to: C
9+
- from: B
10+
to: C
11+
- from: B
12+
to: D
13+
- from: C
14+
to: D

0 commit comments

Comments
 (0)