github.com/google/osv-scalibr@v0.4.1/clients/clienttest/mock_resolution_client.go (about) 1 // Copyright 2025 Google LLC 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 clienttest 16 17 import ( 18 "os" 19 "strings" 20 "testing" 21 22 "deps.dev/util/resolve" 23 "deps.dev/util/resolve/schema" 24 "gopkg.in/yaml.v3" 25 ) 26 27 // ResolutionUniverse defines a mock resolution universe. 28 type ResolutionUniverse struct { 29 System string `yaml:"system"` 30 Schema string `yaml:"schema"` 31 } 32 33 // NewMockResolutionClient creates a new mock resolution client from the given universe YAML. 34 func NewMockResolutionClient(t *testing.T, universeYAML string) resolve.Client { 35 t.Helper() 36 f, err := os.Open(universeYAML) 37 if err != nil { 38 t.Fatalf("failed opening mock universe: %v", err) 39 } 40 defer f.Close() 41 dec := yaml.NewDecoder(f) 42 43 var universe ResolutionUniverse 44 if err := dec.Decode(&universe); err != nil { 45 t.Fatalf("failed decoding mock universe: %v", err) 46 } 47 48 var sys resolve.System 49 switch strings.ToLower(universe.System) { 50 case "npm": 51 sys = resolve.NPM 52 case "maven": 53 sys = resolve.Maven 54 case "pypi": 55 sys = resolve.PyPI 56 default: 57 t.Fatalf("unknown ecosystem in universe: %s", universe.System) 58 } 59 60 // schema needs a strict tab indentation, which is awkward to do within the YAML. 61 // Replace double space from yaml with single tab 62 universe.Schema = strings.ReplaceAll(universe.Schema, " ", "\t") 63 sch, err := schema.New(universe.Schema, sys) 64 if err != nil { 65 t.Fatalf("failed parsing schema: %v", err) 66 } 67 68 return sch.NewClient() 69 }