github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/metadata_functions_test.go (about) 1 package command 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/mitchellh/cli" 8 ) 9 10 func TestMetadataFunctions_error(t *testing.T) { 11 ui := new(cli.MockUi) 12 c := &MetadataFunctionsCommand{ 13 Meta: Meta{ 14 Ui: ui, 15 }, 16 } 17 18 // This test will always error because it's missing the -json flag 19 if code := c.Run(nil); code != 1 { 20 t.Fatalf("expected error, got:\n%s", ui.OutputWriter.String()) 21 } 22 } 23 24 func TestMetadataFunctions_output(t *testing.T) { 25 ui := new(cli.MockUi) 26 m := Meta{Ui: ui} 27 c := &MetadataFunctionsCommand{Meta: m} 28 29 if code := c.Run([]string{"-json"}); code != 0 { 30 t.Fatalf("wrong exit status %d; want 0\nstderr: %s", code, ui.ErrorWriter.String()) 31 } 32 33 var got functions 34 gotString := ui.OutputWriter.String() 35 err := json.Unmarshal([]byte(gotString), &got) 36 if err != nil { 37 t.Fatal(err) 38 } 39 40 if len(got.Signatures) < 100 { 41 t.Fatalf("expected at least 100 function signatures, got %d", len(got.Signatures)) 42 } 43 44 // check if one particular stable function is correct 45 gotMax, ok := got.Signatures["max"] 46 wantMax := "{\"description\":\"`max` takes one or more numbers and returns the greatest number from the set.\",\"return_type\":\"number\",\"variadic_parameter\":{\"name\":\"numbers\",\"type\":\"number\"}}" 47 if !ok { 48 t.Fatal(`missing function signature for "max"`) 49 } 50 if string(gotMax) != wantMax { 51 t.Fatalf("wrong function signature for \"max\":\ngot: %q\nwant: %q", gotMax, wantMax) 52 } 53 54 stderr := ui.ErrorWriter.String() 55 if stderr != "" { 56 t.Fatalf("expected empty stderr, got:\n%s", stderr) 57 } 58 59 // test that ignored functions are not part of the json 60 for _, v := range ignoredFunctions { 61 _, ok := got.Signatures[v] 62 if ok { 63 t.Fatalf("found ignored function %q inside output", v) 64 } 65 } 66 } 67 68 type functions struct { 69 FormatVersion string `json:"format_version"` 70 Signatures map[string]json.RawMessage `json:"function_signatures,omitempty"` 71 }