github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/views/validate_test.go (about) 1 package views 2 3 import ( 4 "encoding/json" 5 "strings" 6 "testing" 7 8 "github.com/hashicorp/terraform/internal/command/arguments" 9 "github.com/hashicorp/terraform/internal/terminal" 10 "github.com/hashicorp/terraform/internal/tfdiags" 11 ) 12 13 func TestValidateHuman(t *testing.T) { 14 testCases := map[string]struct { 15 diag tfdiags.Diagnostic 16 wantSuccess bool 17 wantSubstring string 18 }{ 19 "success": { 20 nil, 21 true, 22 "The configuration is valid.", 23 }, 24 "warning": { 25 tfdiags.Sourceless( 26 tfdiags.Warning, 27 "Your shoelaces are untied", 28 "Watch out, or you'll trip!", 29 ), 30 true, 31 "The configuration is valid, but there were some validation warnings", 32 }, 33 "error": { 34 tfdiags.Sourceless( 35 tfdiags.Error, 36 "Configuration is missing random_pet", 37 "Every configuration should have a random_pet.", 38 ), 39 false, 40 "Error: Configuration is missing random_pet", 41 }, 42 } 43 for name, tc := range testCases { 44 t.Run(name, func(t *testing.T) { 45 streams, done := terminal.StreamsForTesting(t) 46 view := NewView(streams) 47 view.Configure(&arguments.View{NoColor: true}) 48 v := NewValidate(arguments.ViewHuman, view) 49 50 var diags tfdiags.Diagnostics 51 52 if tc.diag != nil { 53 diags = diags.Append(tc.diag) 54 } 55 56 ret := v.Results(diags) 57 58 if tc.wantSuccess && ret != 0 { 59 t.Errorf("expected 0 return code, got %d", ret) 60 } else if !tc.wantSuccess && ret != 1 { 61 t.Errorf("expected 1 return code, got %d", ret) 62 } 63 64 got := done(t).All() 65 if strings.Contains(got, "Success!") != tc.wantSuccess { 66 t.Errorf("unexpected output:\n%s", got) 67 } 68 if !strings.Contains(got, tc.wantSubstring) { 69 t.Errorf("expected output to include %q, but was:\n%s", tc.wantSubstring, got) 70 } 71 }) 72 } 73 } 74 75 func TestValidateJSON(t *testing.T) { 76 testCases := map[string]struct { 77 diag tfdiags.Diagnostic 78 wantSuccess bool 79 }{ 80 "success": { 81 nil, 82 true, 83 }, 84 "warning": { 85 tfdiags.Sourceless( 86 tfdiags.Warning, 87 "Your shoelaces are untied", 88 "Watch out, or you'll trip!", 89 ), 90 true, 91 }, 92 "error": { 93 tfdiags.Sourceless( 94 tfdiags.Error, 95 "Configuration is missing random_pet", 96 "Every configuration should have a random_pet.", 97 ), 98 false, 99 }, 100 } 101 for name, tc := range testCases { 102 t.Run(name, func(t *testing.T) { 103 streams, done := terminal.StreamsForTesting(t) 104 view := NewView(streams) 105 view.Configure(&arguments.View{NoColor: true}) 106 v := NewValidate(arguments.ViewJSON, view) 107 108 var diags tfdiags.Diagnostics 109 110 if tc.diag != nil { 111 diags = diags.Append(tc.diag) 112 } 113 114 ret := v.Results(diags) 115 116 if tc.wantSuccess && ret != 0 { 117 t.Errorf("expected 0 return code, got %d", ret) 118 } else if !tc.wantSuccess && ret != 1 { 119 t.Errorf("expected 1 return code, got %d", ret) 120 } 121 122 got := done(t).All() 123 124 // Make sure the result looks like JSON; we comprehensively test 125 // the structure of this output in the command package tests. 126 var result map[string]interface{} 127 128 if err := json.Unmarshal([]byte(got), &result); err != nil { 129 t.Fatal(err) 130 } 131 }) 132 } 133 }