opentofu/internal/states/statefile/roundtrip_test.go
Martin Atkins f40800b3a4 Move states/ to internal/states/
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

77 lines
1.3 KiB
Go

package statefile
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"testing"
"github.com/go-test/deep"
)
func TestRoundtrip(t *testing.T) {
const dir = "testdata/roundtrip"
entries, err := ioutil.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
for _, info := range entries {
const inSuffix = ".in.tfstate"
const outSuffix = ".out.tfstate"
if info.IsDir() {
continue
}
inName := info.Name()
if !strings.HasSuffix(inName, inSuffix) {
continue
}
name := inName[:len(inName)-len(inSuffix)]
outName := name + outSuffix
t.Run(name, func(t *testing.T) {
oSrcWant, err := ioutil.ReadFile(filepath.Join(dir, outName))
if err != nil {
t.Fatal(err)
}
oWant, diags := readStateV4(oSrcWant)
if diags.HasErrors() {
t.Fatal(diags.Err())
}
ir, err := os.Open(filepath.Join(dir, inName))
if err != nil {
t.Fatal(err)
}
defer ir.Close()
f, err := Read(ir)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
var buf bytes.Buffer
err = Write(f, &buf)
if err != nil {
t.Fatal(err)
}
oSrcWritten := buf.Bytes()
oGot, diags := readStateV4(oSrcWritten)
if diags.HasErrors() {
t.Fatal(diags.Err())
}
problems := deep.Equal(oGot, oWant)
sort.Strings(problems)
for _, problem := range problems {
t.Error(problem)
}
})
}
}