github.com/zntrio/harp/v2@v2.0.9/pkg/bundle/patch/reader_test.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package patch 19 20 import ( 21 "fmt" 22 "io" 23 "os" 24 "path/filepath" 25 "testing" 26 ) 27 28 func mustLoad(filePath string) io.Reader { 29 f, err := os.Open(filePath) 30 if err != nil { 31 panic(err) 32 } 33 return f 34 } 35 36 type readerTestCase struct { 37 name string 38 args io.Reader 39 wantErr bool 40 } 41 42 //nolint:unparam // rootPath has always the same value for the moment 43 func generateReaderTests(t *testing.T, rootPath, state string, wantErr bool) []readerTestCase { 44 tests := []readerTestCase{} 45 // Generate invalid test cases 46 if err := filepath.Walk(filepath.Join(rootPath, state), func(path string, info os.FileInfo, errWalk error) error { 47 if errWalk != nil { 48 return errWalk 49 } 50 if info.IsDir() { 51 return nil 52 } 53 if filepath.Ext(path) != ".yaml" { 54 return nil 55 } 56 57 tests = append(tests, readerTestCase{ 58 name: fmt.Sprintf("%s-%s", state, filepath.Base(info.Name())), 59 args: mustLoad(path), 60 wantErr: wantErr, 61 }) 62 return nil 63 }); err != nil { 64 t.Fatal(err) 65 } 66 67 return tests 68 } 69 70 func TestYAML(t *testing.T) { 71 tests := []readerTestCase{ 72 { 73 name: "nil", 74 wantErr: true, 75 }, 76 } 77 78 // Generate invalid test cases 79 tests = append(tests, generateReaderTests(t, "../../../test/fixtures/patch", "invalid", true)...) 80 81 // Generate valid test cases 82 tests = append(tests, generateReaderTests(t, "../../../test/fixtures/patch", "valid", false)...) 83 84 // Execute them 85 for _, tt := range tests { 86 t.Run(tt.name, func(t *testing.T) { 87 _, err := YAML(tt.args) 88 if (err != nil) != tt.wantErr { 89 t.Errorf("YAML() error = %v, wantErr %v", err, tt.wantErr) 90 return 91 } 92 }) 93 } 94 }