opentofu/internal/tfdiags/rpc_friendly_test.go
Martin Atkins 05caff2ca3 Move tfdiags/ to internal/tfdiags/
This is part of a general effort to move all of Terraform's non-library
package surface under internal in order to reinforce that these are for
internal use within Terraform only.

If you were previously importing packages under this prefix into an
external codebase, you could pin to an earlier release tag as an interim
solution until you've make a plan to achieve the same functionality some
other way.
2021-05-17 14:09:07 -07:00

73 lines
1.3 KiB
Go

package tfdiags
import (
"bytes"
"encoding/gob"
"fmt"
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/hashicorp/hcl/v2"
)
func TestDiagnosticsForRPC(t *testing.T) {
var diags Diagnostics
diags = diags.Append(fmt.Errorf("bad"))
diags = diags.Append(SimpleWarning("less bad"))
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "bad bad bad",
Detail: "badily bad bad",
Subject: &hcl.Range{
Filename: "foo",
},
Context: &hcl.Range{
Filename: "bar",
},
})
buf := bytes.Buffer{}
enc := gob.NewEncoder(&buf)
dec := gob.NewDecoder(&buf)
rpcDiags := diags.ForRPC()
err := enc.Encode(rpcDiags)
if err != nil {
t.Fatalf("error on Encode: %s", err)
}
var got Diagnostics
err = dec.Decode(&got)
if err != nil {
t.Fatalf("error on Decode: %s", err)
}
want := Diagnostics{
&rpcFriendlyDiag{
Severity_: Error,
Summary_: "bad",
},
&rpcFriendlyDiag{
Severity_: Warning,
Summary_: "less bad",
},
&rpcFriendlyDiag{
Severity_: Error,
Summary_: "bad bad bad",
Detail_: "badily bad bad",
Subject_: &SourceRange{
Filename: "foo",
},
Context_: &SourceRange{
Filename: "bar",
},
},
}
if !reflect.DeepEqual(got, want) {
t.Errorf("wrong result\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(want))
}
}