Skip to content

Commit

Permalink
test: separate tree and node packages for the sake of testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanilves committed Apr 11, 2023
1 parent fabb70b commit 362d5c5
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 29 deletions.
11 changes: 11 additions & 0 deletions fixtures/directory/terragrunt/dev/region-1/rds/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
terraform {
source = "my-terraform-module"
}

include "root" {
path = find_in_parent_folders()
}

inputs = {
foo = "bar"
}
38 changes: 38 additions & 0 deletions pkg/directory/tree/node/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package node

// Node is an element Tree gets composed from (a path with hierarchical and connection metadata)
type Node struct {
name string
path string
parent string
children []string
levelID int
terminal bool
}

// NewNode creates a new Node instance
func NewNode(name, path, parent string, children []string, levelID int, terminal bool) Node {
return Node{
name: name,
path: path,
parent: parent,
children: children,
levelID: levelID,
terminal: terminal,
}
}

// IsAChildOf tells us if current node is a child of another node
func (n Node) IsAChildOf(pn Node) bool {
return n.parent == pn.path
}

// HasChildren tells us if that node has child nodes (i.e. if node is a parent itself)
func (n Node) HasChildren() bool {
return len(n.children) > 0
}

// IsTerminal tells us if that node has can be a Tree exit point
func (n Node) IsTerminal() bool {
return n.terminal
}
45 changes: 16 additions & 29 deletions pkg/directory/tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,17 @@ import (
"strings"

"golang.org/x/exp/slices"

"github.com/ivanilves/travelgrunt/pkg/directory/tree/node"
)

// Tree is a hierarchical representation of the directory contents
type Tree struct {
nodes map[string]node
nodes map[string]node.Node

levels []map[string]string
}

type node struct {
name string
path string
parent string
children []string
levelID int
terminal bool
}

func getLevels(paths []string) (levels []map[string]string) {
for _, path := range paths {
elements := strings.Split(path, "/")
Expand Down Expand Up @@ -76,21 +69,21 @@ func getNodeChildren(path string, levels []map[string]string, idx int) (children
return children
}

func getNodes(levels []map[string]string, paths []string) (nodes map[string]node) {
nodes = make(map[string]node, 0)
func getNodes(levels []map[string]string, paths []string) (nodes map[string]node.Node) {
nodes = make(map[string]node.Node, 0)

var prevLevel map[string]string

for idx, level := range levels {
for path, name := range level {
nodes[path] = node{
name: name,
path: path,
parent: getNodeParent(path, prevLevel),
children: getNodeChildren(path, levels, idx),
levelID: idx,
terminal: slices.Contains(paths, path),
}
nodes[path] = node.NewNode(
name,
path,
getNodeParent(path, prevLevel),
getNodeChildren(path, levels, idx),
idx,
slices.Contains(paths, path),
)
}

prevLevel = level
Expand Down Expand Up @@ -160,14 +153,13 @@ func (t Tree) ChildItems(idx int, parentPath string) (items map[string]string) {

for path, name := range t.levels[idx+1] {
currentNode := t.nodes[path]
parentNode := t.nodes[parentPath]

if currentNode.parent == parentPath {
if currentNode.IsAChildOf(parentNode) {
items[name] = path
}

parentNode := t.nodes[parentPath]

if parentNode.terminal && parentNode.hasChildren() {
if parentNode.IsTerminal() && parentNode.HasChildren() {
items["."] = parentPath
}
}
Expand All @@ -190,8 +182,3 @@ func (t Tree) nodeExists(path string) bool {

return defined
}

// hasChildren tells us if that node has child nodes (if node is a parent itself)
func (n node) hasChildren() bool {
return len(n.children) > 0
}
16 changes: 16 additions & 0 deletions pkg/directory/tree/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ func TestChildItems(t *testing.T) {
assert.Equal(expected, items)
}

func TestChildItemsTerminalAndWithChildren(t *testing.T) {
assert := assert.New(t)

items := mock.ChildItems(3, "terragrunt/dev/region-1/rds")
expected := map[string]string{
".": "terragrunt/dev/region-1/rds",
"bar": "terragrunt/dev/region-1/rds/bar",
"baz": "terragrunt/dev/region-1/rds/baz",
"foo": "terragrunt/dev/region-1/rds/foo",
}

assert.NotNil(items)
assert.Equal(4, len(items))
assert.Equal(expected, items)
}

func TestChildNames(t *testing.T) {
assert := assert.New(t)

Expand Down

0 comments on commit 362d5c5

Please sign in to comment.