opentofu/command/providers_schema_test.go
Kristin Laemmert 04be220f5f deprecate helper/copy
helper/copy CopyDir was used heavily in tests. It differes from
internal/copydir in a few ways, the main one being that it creates the
dst directory while the internal version expected the dst to exist
(there are other differences, which is why I did not just switch tests
to using internal's CopyDir).

I moved the CopyDir func from helper/copy into command_test.go; I could
also have moved it into internal/copy and named it something like
CreateDirAndCopy so if that seems like a better option please let me
know.

helper/copy/CopyFile was used in a couple of spots so I moved it into
internal, at which point I thought it made more sense to rename the
package copy (instead of copydir).

There's also a `go mod tidy` included.
2020-10-08 08:42:16 -04:00

112 lines
2.7 KiB
Go

package command
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/mitchellh/cli"
)
func TestProvidersSchema_error(t *testing.T) {
ui := new(cli.MockUi)
c := &ProvidersSchemaCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(testProvider()),
Ui: ui,
},
}
if code := c.Run(nil); code != 1 {
fmt.Println(ui.OutputWriter.String())
t.Fatalf("expected error: \n%s", ui.OutputWriter.String())
}
}
func TestProvidersSchema_output(t *testing.T) {
// there's only one test at this time. This can be refactored to have
// multiple test cases in individual directories as needed.
fixtureDir := "testdata/providers-schema"
testDirs, err := ioutil.ReadDir(fixtureDir)
if err != nil {
t.Fatal(err)
}
for _, entry := range testDirs {
if !entry.IsDir() {
continue
}
t.Run(entry.Name(), func(t *testing.T) {
td := tempDir(t)
inputDir := filepath.Join(fixtureDir, entry.Name())
testCopyDir(t, inputDir, td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
providerSource, close := newMockProviderSource(t, map[string][]string{
"test": []string{"1.2.3"},
})
defer close()
p := showFixtureProvider()
ui := new(cli.MockUi)
m := Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
ProviderSource: providerSource,
}
// `terrafrom init`
ic := &InitCommand{
Meta: m,
}
if code := ic.Run([]string{}); code != 0 {
t.Fatalf("init failed\n%s", ui.ErrorWriter)
}
// flush the init output from the mock ui
ui.OutputWriter.Reset()
// `terraform provider schemas` command
pc := &ProvidersSchemaCommand{Meta: m}
if code := pc.Run([]string{"-json"}); code != 0 {
t.Fatalf("wrong exit status %d; want 0\nstderr: %s", code, ui.ErrorWriter.String())
}
var got, want providerSchemas
gotString := ui.OutputWriter.String()
json.Unmarshal([]byte(gotString), &got)
wantFile, err := os.Open("output.json")
if err != nil {
t.Fatalf("err: %s", err)
}
defer wantFile.Close()
byteValue, err := ioutil.ReadAll(wantFile)
if err != nil {
t.Fatalf("err: %s", err)
}
json.Unmarshal([]byte(byteValue), &want)
if !cmp.Equal(got, want) {
t.Fatalf("wrong result:\n %v\n", cmp.Diff(got, want))
}
})
}
}
type providerSchemas struct {
FormatVersion string `json:"format_version"`
Schemas map[string]providerSchema `json:"provider_schemas"`
}
type providerSchema struct {
Provider interface{} `json:"provider,omitempty"`
ResourceSchemas map[string]interface{} `json:"resource_schemas,omitempty"`
DataSourceSchemas map[string]interface{} `json:"data_source_schemas,omitempty"`
}