Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 8f72446

Browse files
committed
command/graph: takes config dir as arg
1 parent 10d17c4 commit 8f72446

4 files changed

Lines changed: 73 additions & 4 deletions

File tree

command/command_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
const fixtureDir = "./test-fixtures"
1414

1515
func testFixturePath(name string) string {
16-
return filepath.Join(fixtureDir, name, "main.tf")
16+
return filepath.Join(fixtureDir, name)
1717
}
1818

1919
func testCtxConfig(p terraform.ResourceProvider) *terraform.ContextOpts {

command/graph.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package command
22

33
import (
4+
"bytes"
45
"flag"
56
"fmt"
6-
"os"
77
"strings"
88

99
"github.com/hashicorp/terraform/config"
@@ -33,7 +33,9 @@ func (c *GraphCommand) Run(args []string) int {
3333
return 1
3434
}
3535

36-
conf, err := config.Load(args[0])
36+
path := args[0]
37+
38+
conf, err := config.LoadDir(path)
3739
if err != nil {
3840
c.Ui.Error(fmt.Sprintf("Error loading config: %s", err))
3941
return 1
@@ -48,11 +50,14 @@ func (c *GraphCommand) Run(args []string) int {
4850
return 1
4951
}
5052

53+
buf := new(bytes.Buffer)
5154
nodes := make([]digraph.Node, len(g.Nouns))
5255
for i, n := range g.Nouns {
5356
nodes[i] = n
5457
}
55-
digraph.GenerateDot(nodes, os.Stdout)
58+
digraph.GenerateDot(nodes, buf)
59+
60+
c.Ui.Output(buf.String())
5661

5762
return 0
5863
}
@@ -66,6 +71,10 @@ Usage: terraform graph [options] PATH
6671
shown. If the path is a plan file, then the dependency graph of the
6772
plan itself is shown.
6873
74+
The graph is outputted in DOT format. The typical program that can
75+
read this format is GraphViz, but many web services are also available
76+
to read this format.
77+
6978
`
7079
return strings.TrimSpace(helpText)
7180
}

command/graph_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package command
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/mitchellh/cli"
8+
)
9+
10+
func TestGraph(t *testing.T) {
11+
ui := new(cli.MockUi)
12+
c := &GraphCommand{
13+
ContextOpts: testCtxConfig(testProvider()),
14+
Ui: ui,
15+
}
16+
17+
args := []string{
18+
testFixturePath("graph"),
19+
}
20+
if code := c.Run(args); code != 0 {
21+
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
22+
}
23+
24+
output := ui.OutputWriter.String()
25+
if !strings.Contains(output, "digraph {") {
26+
t.Fatalf("doesn't look like digraph: %s", output)
27+
}
28+
}
29+
30+
func TestGraph_noArgs(t *testing.T) {
31+
ui := new(cli.MockUi)
32+
c := &ApplyCommand{
33+
ContextOpts: testCtxConfig(testProvider()),
34+
Ui: ui,
35+
}
36+
37+
args := []string{}
38+
if code := c.Run(args); code != 1 {
39+
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
40+
}
41+
}
42+
43+
func TestGraph_multipleArgs(t *testing.T) {
44+
ui := new(cli.MockUi)
45+
c := &ApplyCommand{
46+
ContextOpts: testCtxConfig(testProvider()),
47+
Ui: ui,
48+
}
49+
50+
args := []string{
51+
"bad",
52+
"bad",
53+
}
54+
if code := c.Run(args); code != 1 {
55+
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
56+
}
57+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
resource "test_instance" "foo" {
2+
ami = "bar"
3+
}

0 commit comments

Comments
 (0)