Skip to content

Commit

Permalink
refact(GenerateDAG): improved testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Gelloz committed Jun 3, 2024
1 parent 93b6492 commit 5a76192
Show file tree
Hide file tree
Showing 11 changed files with 416 additions and 115 deletions.
24 changes: 24 additions & 0 deletions pkg/dag/dag.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package dag

import (
"cmp"
"slices"
"strings"
"sync"

"github.com/radiofrance/dib/internal/logger"
"golang.org/x/sync/errgroup"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -203,3 +207,23 @@ func createUniqueVisitorErr(visitor NodeVisitorFuncErr) NodeVisitorFuncErr {

return uniqueVisitor
}

func sort(a, b *Node) int {
return cmp.Compare(strings.ToLower(a.Image.ShortName), strings.ToLower(b.Image.ShortName))
}

func (d *DAG) Sprint(name string) string {
d.WalkInDepth(func(node *Node) {
slices.SortFunc(node.Children(), sort)
})
slices.SortFunc(d.nodes, sort)
rootNode := &Node{
Image: &Image{Name: name},
children: d.nodes,
}
res, err := defaultPrinter.WithRoot(rootNode).Srender()
if err != nil {
logger.Fatalf(err.Error())
}
return res
}
97 changes: 97 additions & 0 deletions pkg/dag/printer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package dag

import (
"fmt"
"io"
"strings"

"github.com/pterm/pterm"
)

var defaultPrinter = GraphPrinter{
TopRightCornerString: "└",
TopRightDownString: "├",
HorizontalString: "─",
VerticalString: "│",
RightDownLeftString: "┬",
Indent: 3,
}

type GraphPrinter struct {
Root *Node
TreeStyle *pterm.Style
TextStyle *pterm.Style
TopRightCornerString string
TopRightDownString string
HorizontalString string
VerticalString string
RightDownLeftString string
Indent int
Writer io.Writer
}

// WithRoot returns a new GraphPrinter with a specific Root node.
func (p GraphPrinter) WithRoot(root *Node) *GraphPrinter {
p.Root = root
return &p
}

// Render prints the graph to the terminal.
func (p GraphPrinter) Render() error {
s, _ := p.Srender()
pterm.Fprintln(p.Writer, s)

return nil
}

// Srender renders the graph as a string.
func (p GraphPrinter) Srender() (string, error) {
if p.TreeStyle == nil {
p.TreeStyle = pterm.NewStyle()
}
if p.TextStyle == nil {
p.TextStyle = pterm.NewStyle()
}

var result string
if p.Root.Image.Name != "" {
result += p.TextStyle.Sprint(p.Root.Image.Name) + "\n"
}
result += walkOverTree(p.Root.Children(), p, "")
return result, nil
}

func walkOverTree(nodes []*Node, printer GraphPrinter, prefix string) string {
var res string
for nodeIndex, node := range nodes {
txt := fmt.Sprintf("%s [%s]\n", node.Image.ShortName, node.Image.Hash)
if len(nodes) > nodeIndex+1 { // if not last in nodes
if len(node.Children()) == 0 { // if there are no children
res += prefix + printer.TreeStyle.Sprint(printer.TopRightDownString) +
strings.Repeat(printer.TreeStyle.Sprint(printer.HorizontalString), printer.Indent) +
printer.TextStyle.Sprint(txt)
} else { // if there are children
res += prefix + printer.TreeStyle.Sprint(printer.TopRightDownString) +
strings.Repeat(printer.TreeStyle.Sprint(printer.HorizontalString), printer.Indent-1) +
printer.TreeStyle.Sprint(printer.RightDownLeftString) +
printer.TextStyle.Sprint(txt)
res += walkOverTree(node.Children(), printer,
prefix+printer.TreeStyle.Sprint(printer.VerticalString)+strings.Repeat(" ", printer.Indent-1))
}
} else if len(nodes) == nodeIndex+1 { // if last in nodes
if len(node.Children()) == 0 { // if there are no children
res += prefix + printer.TreeStyle.Sprint(printer.TopRightCornerString) +
strings.Repeat(printer.TreeStyle.Sprint(printer.HorizontalString), printer.Indent) +
printer.TextStyle.Sprint(txt)
} else { // if there are children
res += prefix + printer.TreeStyle.Sprint(printer.TopRightCornerString) +
strings.Repeat(printer.TreeStyle.Sprint(printer.HorizontalString), printer.Indent-1) +
printer.TreeStyle.Sprint(printer.RightDownLeftString) +
printer.TextStyle.Sprint(txt)
res += walkOverTree(node.Children(), printer,
prefix+strings.Repeat(" ", printer.Indent))
}
}
}
return res
}
4 changes: 2 additions & 2 deletions pkg/dib/generate_dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ func GenerateDAG(buildPath, registryPrefix, customHashListPath string, buildArgs
img.IgnorePatterns = ignorePatterns

if n, ok := cache[img.Name]; ok {
return fmt.Errorf("duplicate image name %q found while reading file %q: previous file was %q",
img.Name, filePath, path.Join(n.Image.Dockerfile.ContextPath, n.Image.Dockerfile.Filename))
return fmt.Errorf("duplicate image name %q: previous file was %q",
img.Name, path.Join(n.Image.Dockerfile.ContextPath, n.Image.Dockerfile.Filename))
}

allParents[img.Name] = dckfile.From
Expand Down
Loading

0 comments on commit 5a76192

Please sign in to comment.