github.com/google/osv-scalibr@v0.4.1/testing/extracttest/mockscaninput.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 extracttest provides structures to help create tabular tests for extractors. 16 package extracttest 17 18 import ( 19 "io" 20 "os" 21 "path/filepath" 22 "testing" 23 24 "github.com/google/osv-scalibr/extractor" 25 "github.com/google/osv-scalibr/extractor/filesystem" 26 scalibrfs "github.com/google/osv-scalibr/fs" 27 "github.com/google/osv-scalibr/testing/fakefs" 28 ) 29 30 // ScanInputMockConfig is used to quickly configure building a mock ScanInput 31 type ScanInputMockConfig struct { 32 // Path of the file ScanInput will read, relative to the ScanRoot 33 Path string 34 // FakeScanRoot allows you to set a custom scanRoot, can be relative or absolute, 35 // and will be translated to an absolute path 36 FakeScanRoot string 37 FakeFileInfo *fakefs.FakeFileInfo 38 } 39 40 // TestTableEntry is a entry to pass to ExtractionTester 41 type TestTableEntry struct { 42 Name string 43 InputConfig ScanInputMockConfig 44 WantPackages []*extractor.Package 45 WantErr error 46 } 47 48 // CloseTestScanInput takes a scan input generated by GenerateScanInputMock 49 // and closes the associated file handle 50 func CloseTestScanInput(t *testing.T, si filesystem.ScanInput) { 51 t.Helper() 52 // If the Reader is not an io.Closer, then there is an implementation error and this should panic. 53 err := si.Reader.(io.Closer).Close() 54 if err != nil { 55 t.Errorf("Close(): %v", err) 56 } 57 } 58 59 // GenerateScanInputMock will try to open the file locally, and fail if the file doesn't exist 60 func GenerateScanInputMock(t *testing.T, config ScanInputMockConfig) filesystem.ScanInput { 61 t.Helper() 62 63 var scanRoot string 64 if filepath.IsAbs(config.FakeScanRoot) { 65 scanRoot = config.FakeScanRoot 66 } else { 67 workingDir, err := os.Getwd() 68 if err != nil { 69 t.Fatalf("Can't get working directory because '%s'", workingDir) 70 } 71 scanRoot = filepath.Join(workingDir, config.FakeScanRoot) 72 } 73 74 f, err := os.Open(filepath.Join(scanRoot, config.Path)) 75 if err != nil { 76 t.Fatalf("Can't open test fixture '%s' because '%s'", config.Path, err) 77 } 78 info, err := f.Stat() 79 if err != nil { 80 t.Fatalf("Can't stat test fixture '%s' because '%s'", config.Path, err) 81 } 82 83 return filesystem.ScanInput{ 84 FS: os.DirFS(scanRoot).(scalibrfs.FS), 85 Path: config.Path, 86 Root: scanRoot, 87 Reader: f, 88 Info: info, 89 } 90 }