github.com/kjmkznr/terraform@v0.5.2-0.20180216194316-1d0f5fdac99e/tfdiags/rpc_friendly_test.go (about) 1 package tfdiags 2 3 import ( 4 "bytes" 5 "encoding/gob" 6 "fmt" 7 "reflect" 8 "testing" 9 10 "github.com/davecgh/go-spew/spew" 11 12 "github.com/hashicorp/hcl2/hcl" 13 ) 14 15 func TestDiagnosticsForRPC(t *testing.T) { 16 var diags Diagnostics 17 diags = diags.Append(fmt.Errorf("bad")) 18 diags = diags.Append(SimpleWarning("less bad")) 19 diags = diags.Append(&hcl.Diagnostic{ 20 Severity: hcl.DiagError, 21 Summary: "bad bad bad", 22 Detail: "badily bad bad", 23 Subject: &hcl.Range{ 24 Filename: "foo", 25 }, 26 Context: &hcl.Range{ 27 Filename: "bar", 28 }, 29 }) 30 31 buf := bytes.Buffer{} 32 enc := gob.NewEncoder(&buf) 33 dec := gob.NewDecoder(&buf) 34 35 rpcDiags := diags.ForRPC() 36 err := enc.Encode(rpcDiags) 37 if err != nil { 38 t.Fatalf("error on Encode: %s", err) 39 } 40 41 var got Diagnostics 42 err = dec.Decode(&got) 43 if err != nil { 44 t.Fatalf("error on Decode: %s", err) 45 } 46 47 want := Diagnostics{ 48 &rpcFriendlyDiag{ 49 Severity_: Error, 50 Summary_: "bad", 51 }, 52 &rpcFriendlyDiag{ 53 Severity_: Warning, 54 Summary_: "less bad", 55 }, 56 &rpcFriendlyDiag{ 57 Severity_: Error, 58 Summary_: "bad bad bad", 59 Detail_: "badily bad bad", 60 Subject_: &SourceRange{ 61 Filename: "foo", 62 }, 63 Context_: &SourceRange{ 64 Filename: "bar", 65 }, 66 }, 67 } 68 69 if !reflect.DeepEqual(got, want) { 70 t.Errorf("wrong result\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(want)) 71 } 72 }