Skip to content

Commit

Permalink
added unit test for input commands. Now handles wrong sub command pro…
Browse files Browse the repository at this point in the history
…perly
  • Loading branch information
ctomkow committed Aug 15, 2019
1 parent 1668659 commit 227113a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
20 changes: 13 additions & 7 deletions configuration/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@

package configuration

import "os"
import (
"errors"
"os"
)

type Command struct {
Install bool
Remove bool
Start bool
Stop bool
Status bool
Install bool
Remove bool
Start bool
Stop bool
Status bool
}

func (cmd *Command) MakeCmd() {
func (cmd *Command) MakeCmd() error {

if len(os.Args) > 1 {
cmds := os.Args[1]
Expand All @@ -28,7 +31,10 @@ func (cmd *Command) MakeCmd() {
cmd.Stop = true
case "status":
cmd.Status = true
default:
return errors.New("invalid command: " + cmds)
}
}

return nil
}
58 changes: 58 additions & 0 deletions configuration/commands_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Craig Tomkow
// August 15, 2019

package configuration

import (
"os"
"testing"
)

var argTests = []struct {
input string
expected bool
}{
{"install", true},
{"remove", true},
{"start", true},
{"stop", true},
{"status", true},
}

func TestCommand_MakeCmd(t *testing.T) {

cmd := new(Command)

for _, argTest := range argTests {
os.Args = []string{"tto", argTest.input}
err := cmd.MakeCmd()

switch argTest.input {
case "install":
if argTest.expected != cmd.Install {
t.Errorf("Input arg test failed; found, expected: %t, %t", cmd.Install, argTest.expected)
}
case "remove":
if argTest.expected != cmd.Remove {
t.Errorf("Input arg test failed; found, expected: %t, %t", cmd.Remove, argTest.expected)
}
case "start":
if argTest.expected != cmd.Start {
t.Errorf("Input arg test failed; found, expected: %t, %t", cmd.Start, argTest.expected)
}
case "stop":
if argTest.expected != cmd.Stop {
t.Errorf("Input arg test failed; found, expected: %t, %t", cmd.Stop, argTest.expected)
}
case "status":
if argTest.expected != cmd.Status {
t.Errorf("Input arg test failed; found, expected: %t, %t", cmd.Status, argTest.expected)
}
default:
if err == nil {
t.Errorf("Input arg test failed; found, expected: %t, %t", cmd.Status, argTest.expected)
}
}

}
}
6 changes: 4 additions & 2 deletions tto.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ func main() {
configFile := configuration.CliFlags()

var cmd = new(configuration.Command)
cmd.MakeCmd()
if err := cmd.MakeCmd(); err != nil {
glog.Fatal(err)
}

if cmd.Install {
install()
Expand All @@ -50,7 +52,7 @@ func main() {

func (srv *Service) Manage(cmd *configuration.Command, configFile *string) (string, error) {

usage := "Usage: tto install | remove | start | stop | status"
usage := "usage: tto install | remove | start | stop | status"

if cmd.Install {
return srv.Install()
Expand Down

0 comments on commit 227113a

Please sign in to comment.