command/graph: no args means pwd

This commit is contained in:
Mitchell Hashimoto 2014-07-11 20:41:47 -07:00
parent 8f7244695f
commit 235a253848
2 changed files with 39 additions and 16 deletions

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"flag" "flag"
"fmt" "fmt"
"os"
"strings" "strings"
"github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/config"
@ -26,15 +27,22 @@ func (c *GraphCommand) Run(args []string) int {
return 1 return 1
} }
var path string
args = cmdFlags.Args() args = cmdFlags.Args()
if len(args) != 1 { if len(args) > 1 {
c.Ui.Error("The graph command expects one argument.\n") c.Ui.Error("The graph command expects one argument.\n")
cmdFlags.Usage() cmdFlags.Usage()
return 1 return 1
} else if len(args) == 1 {
path = args[0]
} else {
var err error
path, err = os.Getwd()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error getting pwd: %s", err))
}
} }
path := args[0]
conf, err := config.LoadDir(path) conf, err := config.LoadDir(path)
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf("Error loading config: %s", err)) c.Ui.Error(fmt.Sprintf("Error loading config: %s", err))

View File

@ -1,6 +1,7 @@
package command package command
import ( import (
"os"
"strings" "strings"
"testing" "testing"
@ -27,19 +28,6 @@ func TestGraph(t *testing.T) {
} }
} }
func TestGraph_noArgs(t *testing.T) {
ui := new(cli.MockUi)
c := &ApplyCommand{
ContextOpts: testCtxConfig(testProvider()),
Ui: ui,
}
args := []string{}
if code := c.Run(args); code != 1 {
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
}
}
func TestGraph_multipleArgs(t *testing.T) { func TestGraph_multipleArgs(t *testing.T) {
ui := new(cli.MockUi) ui := new(cli.MockUi)
c := &ApplyCommand{ c := &ApplyCommand{
@ -55,3 +43,30 @@ func TestGraph_multipleArgs(t *testing.T) {
t.Fatalf("bad: \n%s", ui.OutputWriter.String()) t.Fatalf("bad: \n%s", ui.OutputWriter.String())
} }
} }
func TestGraph_noArgs(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(testFixturePath("graph")); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
ui := new(cli.MockUi)
c := &GraphCommand{
ContextOpts: testCtxConfig(testProvider()),
Ui: ui,
}
args := []string{}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}
output := ui.OutputWriter.String()
if !strings.Contains(output, "digraph {") {
t.Fatalf("doesn't look like digraph: %s", output)
}
}