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