mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-26 00:41:27 -06:00
780e758f1e
Due to the use of interfaces, Diagnostics is not super-friendly to the gob encoding we currently use for plugin RPC. To mitigate this, we provide a helper that converts all of the wrapped objects into a predictable flat structure that we can pre-emptively register with gob. This means that the decoded Diagnostics still has the same meaning as the original, though the original wrapped errors (if any) are lost and thus our errwrap integration won't be effective any longer.
73 lines
1.3 KiB
Go
73 lines
1.3 KiB
Go
package tfdiags
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/gob"
|
|
"fmt"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/hashicorp/hcl2/hcl"
|
|
)
|
|
|
|
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))
|
|
}
|
|
}
|