Skip to content

Commit

Permalink
#58 Add test for the extra args for the command
Browse files Browse the repository at this point in the history
And enable the placeholder %s to put the extra args in the command definition.
  • Loading branch information
zshamrock committed May 10, 2018
1 parent 2c2ac0f commit 8efc1cd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
33 changes: 22 additions & 11 deletions command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"flag"
"testing"

"github.com/zshamrock/vmx/config"
"github.com/zshamrock/vmx/core"
"gopkg.in/urfave/cli.v1"
)

Expand All @@ -30,24 +32,33 @@ func TestGetCommand(t *testing.T) {
}

func TestGetCommandExtraArgs(t *testing.T) {
t.SkipNow()
config.Init("")
followFlags := []string{"", "-f", "--follow"}
for _, followFlag := range followFlags {
commandText := "logs-extra"
extraText := "rest.log"
flags := flag.FlagSet{}
flags.Bool("follow", false, "")
commandText := "tail -f -n 10 logs/rest.log"
flags.Parse([]string{"--", followFlag, "dev", commandText})
follow := false
arguments := []string{"dev", commandText, extraText}
if followFlag != "" {
follow = true
flags.Bool("follow", false, "")
arguments = append([]string{"--", followFlag}, arguments...)
}
flags.Parse(arguments)
app := cli.NewApp()
context := cli.NewContext(app, &flags, nil)
command, extraArgs := getCommand(context, true)
if !command.IsAdHoc() {
t.Errorf("Command name should be ad-hoc, but got %s", command.Name)
command, extraArgs := getCommand(context, follow)
expectedCommand := core.Command{
Name: "logs-extra",
Command: "tail -f -n 10 logs/%s",
WorkingDir: "",
}
if command.Command != commandText {
t.Errorf("Command should be %s, but got %s", commandText, command.Command)
if command != expectedCommand {
t.Errorf("Command should be %v, but got %v", expectedCommand, command)
}
if extraArgs != "" {
t.Errorf("Extra args should be empty, but got %s", extraArgs)
if extraArgs != extraText {
t.Errorf("Extra args should %s, but got %s", extraText, extraArgs)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion command/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func CmdRun(c *cli.Context) {
}
cmd := command.Command
if command.WorkingDir != "" {
cmd = strings.TrimSpace(fmt.Sprintf("cd %s && %s %s", command.WorkingDir, cmd, extraArgs))
cmd = strings.TrimSpace(fmt.Sprintf("cd %s && %s", command.WorkingDir, fmt.Sprintf(cmd, extraArgs)))
}
fmt.Printf("Running command: %s from %s on %v\n", command.Command, command.WorkingDir, hosts)
sshConfig := readSSHConfig(config.DefaultConfig)
Expand Down
5 changes: 5 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func TestReadConfig(t *testing.T) {
Command: "df -h",
WorkingDir: "",
},
"logs-extra": {
Name: "logs-extra",
Command: "tail -f -n 10 logs/%s",
WorkingDir: "",
},
}
if diff := deep.Equal(commands, expected); diff != nil {
t.Error(diff)
Expand Down
3 changes: 3 additions & 0 deletions test/config/commands
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ command=./redeploy.sh

[disk-space]
command=df -h

[logs-extra]
command=tail -f -n 10 logs/%s

0 comments on commit 8efc1cd

Please sign in to comment.