Skip to content

Commit

Permalink
tests: add unit tests for the node package
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickMenoti committed Jul 11, 2024
1 parent 9383c6c commit f9bbfe1
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 9 deletions.
3 changes: 2 additions & 1 deletion pkg/cmd/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ func (cmd *initCmd) Run(c *cobra.Command, _ []string) error {
logger.Debug("Running init command")

msgs := []string{}
err := node.NodeVersion()
nodeManager := node.NewNode()
err := nodeManager.NodeVer(nodeManager)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/link/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ func (cmd *LinkCmd) run(c *cobra.Command, info *LinkInfo) error {
logger.Debug("Running link command")

msgs := []string{}
err := node.NodeVersion()
nodeManager := node.NewNode()
err := nodeManager.NodeVer(nodeManager)
if err != nil {
return err
}
Expand Down
20 changes: 17 additions & 3 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,29 @@ import (
"strings"
)

func NodeVersion() error {
cmd := exec.Command("node", "--version")
type NodePkg struct {
NodeVer func(node *NodePkg) error
CheckNode func(str string) error
CmdBuilder func(name string, arg ...string) *exec.Cmd
}

func NewNode() *NodePkg {
return &NodePkg{
NodeVer: nodeVersion,
CheckNode: checkNode,
CmdBuilder: exec.Command,
}
}

func nodeVersion(node *NodePkg) error {
cmd := node.CmdBuilder("node", "--version")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return errors.New(NODE_NOT_INSTALLED)
}
return checkNode(out.String())
return node.CheckNode(out.String())
}

func checkNode(str string) error {
Expand Down
90 changes: 86 additions & 4 deletions pkg/node/node_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package node

import (
"errors"
"os/exec"
"testing"
)

Expand Down Expand Up @@ -34,18 +36,98 @@ func Test_checkNode(t *testing.T) {
wantErr: true,
err: NODE_OLDER_VERSION,
},
{
name: "case 05",
args: "v18",
wantErr: false,
},
{
name: "case 06",
args: "18.7.3",
wantErr: false,
},
{
name: "case 07",
args: "v18.0.0beta",
wantErr: false,
},
{
name: "case 08",
args: "vX.Y.Z",
wantErr: true,
err: NODE_OLDER_VERSION,
},
{
name: "case 09",
args: "v16",
wantErr: true,
err: NODE_OLDER_VERSION,
},
{
name: "case 10",
args: "v18.10",
wantErr: false,
},
{
name: "case 11",
args: "v18.0.0-alpha",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := checkNode(tt.args)
nodeManager := NewNode()
err := nodeManager.CheckNode(tt.args)
if (err != nil) != tt.wantErr {
t.Errorf("checkNode() error = %v, wantErr %v", err, tt.wantErr)
}
if err != nil {
if err.Error() != tt.err {
t.Errorf("checkNode() error = %v, wantErr %v", err, tt.wantErr)
if err != nil && err.Error() != tt.err {
t.Errorf("checkNode() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func Test_nodeVersion(t *testing.T) {
tests := []struct {
name string
args string
wantErr bool
err string
}{
{
name: "case 01",
args: "ls",
wantErr: false,
},
{
name: "case 02",
args: "ls",
wantErr: true,
err: NODE_NOT_INSTALLED,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
nodeManager := NewNode()
nodeManager.CmdBuilder = func(name string, arg ...string) *exec.Cmd {
cmd := exec.Command(tt.args)
return cmd
}
nodeManager.CheckNode = func(str string) error {
if tt.wantErr {
return errors.New(tt.err)
} else {
return nil
}
}
err := nodeManager.NodeVer(nodeManager)
if (err != nil) != tt.wantErr {
t.Errorf("checkNode() error = %v, wantErr %v", err, tt.wantErr)
}
if err != nil && err.Error() != tt.err {
t.Errorf("checkNode() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit f9bbfe1

Please sign in to comment.