github.com/paybyphone/terraform@v0.9.5-0.20170613192930-9706042ddd51/terraform/graph_builder_test.go (about) 1 package terraform 2 3 import ( 4 "reflect" 5 "strings" 6 "testing" 7 8 "github.com/hashicorp/terraform/dag" 9 ) 10 11 func TestBasicGraphBuilder_impl(t *testing.T) { 12 var _ GraphBuilder = new(BasicGraphBuilder) 13 } 14 15 func TestBasicGraphBuilder(t *testing.T) { 16 b := &BasicGraphBuilder{ 17 Steps: []GraphTransformer{ 18 &testBasicGraphBuilderTransform{1}, 19 }, 20 } 21 22 g, err := b.Build(RootModulePath) 23 if err != nil { 24 t.Fatalf("err: %s", err) 25 } 26 27 if !reflect.DeepEqual(g.Path, RootModulePath) { 28 t.Fatalf("bad: %#v", g.Path) 29 } 30 31 actual := strings.TrimSpace(g.String()) 32 expected := strings.TrimSpace(testBasicGraphBuilderStr) 33 if actual != expected { 34 t.Fatalf("bad: %s", actual) 35 } 36 } 37 38 func TestBasicGraphBuilder_validate(t *testing.T) { 39 b := &BasicGraphBuilder{ 40 Steps: []GraphTransformer{ 41 &testBasicGraphBuilderTransform{1}, 42 &testBasicGraphBuilderTransform{2}, 43 }, 44 Validate: true, 45 } 46 47 _, err := b.Build(RootModulePath) 48 if err == nil { 49 t.Fatal("should error") 50 } 51 } 52 53 func TestBasicGraphBuilder_validateOff(t *testing.T) { 54 b := &BasicGraphBuilder{ 55 Steps: []GraphTransformer{ 56 &testBasicGraphBuilderTransform{1}, 57 &testBasicGraphBuilderTransform{2}, 58 }, 59 Validate: false, 60 } 61 62 _, err := b.Build(RootModulePath) 63 if err != nil { 64 t.Fatalf("expected no error, got: %s", err) 65 } 66 } 67 68 type testBasicGraphBuilderTransform struct { 69 V dag.Vertex 70 } 71 72 func (t *testBasicGraphBuilderTransform) Transform(g *Graph) error { 73 g.Add(t.V) 74 return nil 75 } 76 77 const testBasicGraphBuilderStr = ` 78 1 79 `