github.com/mineiros-io/terradoc@v0.0.9-0.20220711062319-018bd4ae81f5/internal/validators/varsvalidator/varsvalidator_test.go (about) 1 package varsvalidator_test 2 3 import ( 4 "testing" 5 6 "github.com/mineiros-io/terradoc/internal/entities" 7 "github.com/mineiros-io/terradoc/internal/types" 8 "github.com/mineiros-io/terradoc/internal/validators" 9 "github.com/mineiros-io/terradoc/internal/validators/varsvalidator" 10 "github.com/mineiros-io/terradoc/test" 11 ) 12 13 func TestValidate(t *testing.T) { 14 tests := []struct { 15 desc string 16 docVariables entities.VariableCollection 17 variablesFileVariables entities.VariableCollection 18 wantMissingDoc []string 19 wantMissingDef []string 20 wantTypeMismatch []validators.TypeMismatchResult 21 }{ 22 { 23 24 desc: "when a variable is missing from variables file", 25 variablesFileVariables: entities.VariableCollection{}, 26 docVariables: entities.VariableCollection{ 27 { 28 Name: "name", 29 Type: entities.Type{TFType: types.TerraformString}, 30 }, 31 }, 32 wantMissingDef: []string{"name"}, 33 }, 34 { 35 desc: "when a variable is missing from doc file", 36 variablesFileVariables: entities.VariableCollection{ 37 { 38 Name: "age", 39 Type: entities.Type{TFType: types.TerraformNumber}, 40 }, 41 }, 42 docVariables: entities.VariableCollection{}, 43 wantMissingDoc: []string{"age"}, 44 }, 45 { 46 desc: "when doc and variables file have the same variables", 47 variablesFileVariables: entities.VariableCollection{ 48 { 49 Name: "name", 50 Type: entities.Type{TFType: types.TerraformString}, 51 }, 52 { 53 Name: "age", 54 Type: entities.Type{TFType: types.TerraformNumber}, 55 }, 56 }, 57 docVariables: entities.VariableCollection{ 58 { 59 Name: "name", 60 Type: entities.Type{TFType: types.TerraformString}, 61 }, 62 { 63 Name: "age", 64 Type: entities.Type{TFType: types.TerraformNumber}, 65 }, 66 }, 67 }, 68 { 69 desc: "when a variable has different types on doc and variables file", 70 variablesFileVariables: entities.VariableCollection{ 71 { 72 Name: "age", 73 Type: entities.Type{TFType: types.TerraformNumber}, 74 }, 75 }, 76 docVariables: entities.VariableCollection{ 77 { 78 Name: "age", 79 Type: entities.Type{TFType: types.TerraformString}, 80 }, 81 }, 82 wantTypeMismatch: []validators.TypeMismatchResult{ 83 { 84 Name: "age", 85 DefinedType: "number", 86 DocumentedType: "string", 87 }, 88 }, 89 }, 90 { 91 desc: "when a variable is missing from variables file, another missing from doc and another with type mismatch", 92 docVariables: entities.VariableCollection{ 93 { 94 Name: "name", 95 Type: entities.Type{TFType: types.TerraformString}, 96 }, 97 { 98 Name: "birth", 99 Type: entities.Type{TFType: types.TerraformBool}, 100 }, 101 }, 102 variablesFileVariables: entities.VariableCollection{ 103 { 104 Name: "age", 105 Type: entities.Type{TFType: types.TerraformNumber}, 106 }, 107 { 108 Name: "birth", 109 Type: entities.Type{TFType: types.TerraformString}, 110 }, 111 }, 112 wantMissingDef: []string{"name"}, 113 wantMissingDoc: []string{"age"}, 114 wantTypeMismatch: []validators.TypeMismatchResult{ 115 { 116 Name: "birth", 117 DefinedType: "string", 118 DocumentedType: "bool", 119 }, 120 }, 121 }, 122 } 123 124 for _, tt := range tests { 125 t.Run(tt.desc, func(t *testing.T) { 126 def := definitionFromVariables(tt.docVariables) 127 of := variableFileFromVariables(tt.variablesFileVariables) 128 129 got := varsvalidator.Validate(def, of) 130 131 test.AssertHasStrings(t, tt.wantMissingDef, got.MissingDefinition) 132 test.AssertHasStrings(t, tt.wantMissingDoc, got.MissingDocumentation) 133 test.AssertHasTypeMismatches(t, tt.wantTypeMismatch, got.TypeMismatch) 134 }) 135 } 136 } 137 138 func definitionFromVariables(variables entities.VariableCollection) entities.Doc { 139 section := entities.Section{Variables: variables} 140 141 return entities.Doc{Sections: []entities.Section{section}} 142 } 143 144 func variableFileFromVariables(variables entities.VariableCollection) entities.ValidationContents { 145 return entities.ValidationContents{Variables: variables} 146 }