github.com/dhaiducek/policy-generator-plugin@v1.99.99/internal/typohelper_test.go (about) 1 package internal 2 3 import ( 4 "bytes" 5 "fmt" 6 "path" 7 "regexp" 8 "testing" 9 ) 10 11 func TestConfigTypos(t *testing.T) { 12 t.Parallel() 13 tmpDir := t.TempDir() 14 createConfigMap(t, tmpDir, "configmap.yaml") 15 16 suggestFmt := "line %v: field %v found but not defined in type %v - did you mean '%v'?" 17 18 tests := map[string]struct { 19 desiredErrs []string 20 generator []byte 21 }{ 22 "no typos": { 23 desiredErrs: []string{}, 24 generator: []byte(` 25 apiVersion: policy.open-cluster-management.io/v1 26 kind: PolicyGenerator 27 metadata: 28 name: policy-generator-minimal 29 policyDefaults: 30 namespace: minimal 31 policies: 32 - name: my-minimal 33 manifests: 34 - path: @@@ 35 `), 36 }, 37 "one typo with suggestion": { 38 desiredErrs: []string{ 39 fmt.Sprintf(suggestFmt, "6", "policyDefault", "PolicyGenerator", "policyDefaults"), 40 }, 41 generator: []byte(` 42 apiVersion: policy.open-cluster-management.io/v1 43 kind: PolicyGenerator 44 metadata: 45 name: policy-generator-minimal 46 policyDefault: 47 namespace: minimal 48 policies: 49 - name: my-minimal 50 manifests: 51 - path: @@@ 52 `), 53 }, 54 "two typos with suggestions": { 55 desiredErrs: []string{ 56 fmt.Sprintf(suggestFmt, "6", "policyDefault", "PolicyGenerator", "policyDefaults"), 57 fmt.Sprintf(suggestFmt, "8", "policie", "PolicyGenerator", "policies"), 58 }, 59 generator: []byte(` 60 apiVersion: policy.open-cluster-management.io/v1 61 kind: PolicyGenerator 62 metadata: 63 name: policy-generator-minimal 64 policyDefault: 65 namespace: minimal 66 policie: 67 - name: my-minimal 68 manifests: 69 - path: @@@ 70 `), 71 }, 72 "one deeper typo": { 73 desiredErrs: []string{ 74 fmt.Sprintf(suggestFmt, "7", "configPolicyAnnotations", "policyDefaults", 75 "configurationPolicyAnnotations"), 76 }, 77 generator: []byte(` 78 apiVersion: policy.open-cluster-management.io/v1 79 kind: PolicyGenerator 80 metadata: 81 name: policy-generator-minimal 82 policyDefaults: 83 configPolicyAnnotations: {} 84 namespace: minimal 85 policies: 86 - name: my-minimal 87 manifests: 88 - path: @@@ 89 `), 90 }, 91 "typo inside a list": { 92 desiredErrs: []string{ 93 fmt.Sprintf(suggestFmt, "11", "paths", "manifests", "path"), 94 }, 95 generator: []byte(` 96 apiVersion: policy.open-cluster-management.io/v1 97 kind: PolicyGenerator 98 metadata: 99 name: policy-generator-minimal 100 policyDefaults: 101 namespace: minimal 102 policies: 103 - name: my-minimal 104 manifests: 105 - paths: @@@ 106 `), 107 }, 108 "typo no suggestion": { 109 desiredErrs: []string{ 110 "line 12: field namespace found but not defined in type manifests$", 111 }, 112 generator: []byte(` 113 apiVersion: policy.open-cluster-management.io/v1 114 kind: PolicyGenerator 115 metadata: 116 name: policy-generator-minimal 117 policyDefaults: 118 namespace: minimal 119 policies: 120 - name: my-minimal 121 manifests: 122 - path: @@@ 123 namespace: foo 124 `), 125 }, 126 "non-typo error": { 127 desiredErrs: []string{ 128 "line 6: cannot unmarshal !!seq into string", 129 }, 130 generator: []byte(` 131 apiVersion: policy.open-cluster-management.io/v1 132 kind: PolicyGenerator 133 metadata: 134 name: 135 - policy-generator-minimal 136 policyDefaults: 137 namespace: minimal 138 policies: 139 - name: my-minimal 140 manifests: 141 - path: @@@ 142 `), 143 }, 144 } 145 146 for name, test := range tests { 147 test := test 148 149 t.Run(name, func(t *testing.T) { 150 t.Parallel() 151 152 p := Plugin{} 153 generatorYaml := bytes.ReplaceAll( 154 test.generator, 155 []byte(`@@@`), 156 []byte(path.Join(tmpDir, "configmap.yaml"))) 157 158 err := p.Config(generatorYaml, tmpDir) 159 if err == nil && len(test.desiredErrs) > 0 { 160 t.Fatal("Expected an error to be emitted, got nil") 161 } 162 163 for _, want := range test.desiredErrs { 164 if match, _ := regexp.MatchString(want, err.Error()); !match { 165 t.Errorf("Expected error to include '%v' - got '%v'", want, err.Error()) 166 } 167 } 168 }) 169 } 170 }