github.com/GoogleContainerTools/kpt@v1.0.0-beta.50.0.20240520170205-c25345ffcbee/internal/builtins/pkg_context_test.go (about) 1 // Copyright 2022 The kpt Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package builtins 16 17 import ( 18 "bytes" 19 "os" 20 "path/filepath" 21 "testing" 22 23 "github.com/google/go-cmp/cmp" 24 "github.com/stretchr/testify/assert" 25 ) 26 27 type test struct { 28 name string 29 dir string 30 expErr error 31 } 32 33 func TestPkgContextGenerator(t *testing.T) { 34 tests := []test{ 35 { 36 name: "pkg context should succeed on a non-nested package", 37 dir: "pkg-wo-nesting", 38 }, 39 { 40 name: "pkg context should generate on a non-nested package with existing package context", 41 dir: "pkg-with-existing-ctx", 42 }, 43 { 44 name: "pkg context should succeed on package with nested package", 45 dir: "pkg-with-nesting", 46 }, 47 } 48 49 for i := range tests { 50 test := tests[i] 51 t.Run(test.name, func(t *testing.T) { 52 pkgCtxGenerator := &PackageContextGenerator{} 53 out := &bytes.Buffer{} 54 55 in, err := os.ReadFile(filepath.Join("testdata", test.dir, "in.yaml")) 56 assert.NoError(t, err) 57 58 exp, err := os.ReadFile(filepath.Join("testdata", test.dir, "out.yaml")) 59 assert.NoError(t, err) 60 61 err = pkgCtxGenerator.Run(bytes.NewReader(in), out) 62 if err != test.expErr { 63 t.Errorf("exp: %v got: %v", test.expErr, err) 64 } 65 if diff := cmp.Diff(string(exp), out.String()); diff != "" { 66 t.Errorf("pkg context mistmach (-want +got):\n%s", diff) 67 } 68 }) 69 } 70 }