Add test to check both -json and -t are not specified

This commit is contained in:
Kenjiro Nakayama 2016-08-06 21:38:41 +09:00
parent b5b6297cd3
commit 32d20c8453
8 changed files with 71 additions and 13 deletions

View File

@ -87,7 +87,10 @@ func (c *AllocStatusCommand) Run(args []string) int {
// If args not specified but output format is specified, format and output the allocations data list
if len(args) == 0 {
var format string
if json {
if json && len(tmpl) > 0 {
c.Ui.Error("Both -json and -t are not allowed")
return 1
} else if json {
format = "json"
} else if len(tmpl) > 0 {
format = "template"
@ -177,7 +180,10 @@ func (c *AllocStatusCommand) Run(args []string) int {
// If output format is specified, format and output the data
var format string
if json {
if json && len(tmpl) > 0 {
c.Ui.Error("Both -json and -t are not allowed")
return 1
} else if json {
format = "json"
} else if len(tmpl) > 0 {
format = "template"

View File

@ -61,5 +61,13 @@ func TestAllocStatusCommand_Fails(t *testing.T) {
if out := ui.ErrorWriter.String(); !strings.Contains(out, "No allocation(s) with prefix or id") {
t.Fatalf("expected not found error, got: %s", out)
}
ui.ErrorWriter.Reset()
// Failed on both -json and -t options are specified
if code := cmd.Run([]string{"-address=" + url, "-json", "-t", "{{.ID}}"}); code != 1 {
t.Fatalf("expected exit 1, got: %d", code)
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Both -json and -t are not allowed") {
t.Fatalf("expected getting formatter error, got: %s", out)
}
}

View File

@ -74,7 +74,10 @@ func (c *EvalStatusCommand) Run(args []string) int {
// If args not specified but output format is specified, format and output the evaluations data list
if len(args) == 0 {
var format string
if json {
if json && len(tmpl) > 0 {
c.Ui.Error("Both -json and -t are not allowed")
return 1
} else if json {
format = "json"
} else if len(tmpl) > 0 {
format = "template"
@ -143,7 +146,10 @@ func (c *EvalStatusCommand) Run(args []string) int {
if len(args) == 0 {
// If output format is specified, format and output the data
var format string
if json {
if json && len(tmpl) > 0 {
c.Ui.Error("Both -json and -t are not allowed")
return 1
} else if json {
format = "json"
} else if len(tmpl) > 0 {
format = "template"

View File

@ -43,4 +43,14 @@ func TestEvalStatusCommand_Fails(t *testing.T) {
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error querying evaluation") {
t.Fatalf("expected failed query error, got: %s", out)
}
ui.ErrorWriter.Reset()
// Failed on both -json and -t options are specified
if code := cmd.Run([]string{"-address=" + url, "-json", "-t", "{{.ID}}"}); code != 1 {
t.Fatalf("expected exit 1, got: %d", code)
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Both -json and -t are not allowed") {
t.Fatalf("expected getting formatter error, got: %s", out)
}
}

View File

@ -53,7 +53,10 @@ func (c *InspectCommand) Run(args []string) int {
// If args not specified but output format is specified, format and output the jobs data list
if len(args) == 0 {
var format string
if ojson {
if ojson && len(tmpl) > 0 {
c.Ui.Error("Both -json and -t are not allowed")
return 1
} else if ojson {
format = "json"
} else if len(tmpl) > 0 {
format = "template"

View File

@ -43,4 +43,13 @@ func TestInspectCommand_Fails(t *testing.T) {
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error inspecting job") {
t.Fatalf("expected failed query error, got: %s", out)
}
ui.ErrorWriter.Reset()
// Failed on both -json and -t options are specified
if code := cmd.Run([]string{"-address=" + url, "-json", "-t", "{{.ID}}"}); code != 1 {
t.Fatalf("expected exit 1, got: %d", code)
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Both -json and -t are not allowed") {
t.Fatalf("expected getting formatter error, got: %s", out)
}
}

View File

@ -120,6 +120,17 @@ func (c *NodeStatusCommand) Run(args []string) int {
// Use list mode if no node name was provided
if len(args) == 0 && !c.self {
// If output format is specified, format and output the node data list
var format string
if c.json && len(c.tmpl) > 0 {
c.Ui.Error("Both -json and -t are not allowed")
return 1
} else if c.json {
format = "json"
} else if len(c.tmpl) > 0 {
format = "template"
}
// Query the node info
nodes, _, err := client.Nodes().List(nil)
if err != nil {
@ -132,13 +143,6 @@ func (c *NodeStatusCommand) Run(args []string) int {
return 0
}
// If output format is specified, format and output the node data list
var format string
if c.json {
format = "json"
} else if len(c.tmpl) > 0 {
format = "template"
}
if len(format) > 0 {
f, err := DataFormat(format, c.tmpl)
if err != nil {
@ -252,7 +256,10 @@ func (c *NodeStatusCommand) Run(args []string) int {
// If output format is specified, format and output the data
var format string
if c.json {
if c.json && len(c.tmpl) > 0 {
c.Ui.Error("Both -json and -t are not allowed")
return 1
} else if c.json {
format = "json"
} else if len(c.tmpl) > 0 {
format = "template"

View File

@ -200,4 +200,13 @@ func TestNodeStatusCommand_Fails(t *testing.T) {
if out := ui.ErrorWriter.String(); !strings.Contains(out, "must contain at least two characters.") {
t.Fatalf("expected too few characters error, got: %s", out)
}
ui.ErrorWriter.Reset()
// Failed on both -json and -t options are specified
if code := cmd.Run([]string{"-address=" + url, "-json", "-t", "{{.ID}}"}); code != 1 {
t.Fatalf("expected exit 1, got: %d", code)
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Both -json and -t are not allowed") {
t.Fatalf("expected getting formatter error, got: %s", out)
}
}