github.com/google/osv-scalibr@v0.4.1/testing/fakedetector/fake_detector.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 fakedetector provides a Detector implementation to be used in tests.
    16  //
    17  //nolint:plugger // This package contains test only mocks
    18  package fakedetector
    19  
    20  import (
    21  	"context"
    22  
    23  	"github.com/google/go-cpy/cpy"
    24  	scalibrfs "github.com/google/osv-scalibr/fs"
    25  	"github.com/google/osv-scalibr/inventory"
    26  	"github.com/google/osv-scalibr/packageindex"
    27  	"github.com/google/osv-scalibr/plugin"
    28  )
    29  
    30  var (
    31  	copier = cpy.New(
    32  		cpy.IgnoreAllUnexported(),
    33  	)
    34  )
    35  
    36  // fakeDetector is an Detector implementation to be used in tests.
    37  // It returns a predefined Finding or error.
    38  type fakeDetector struct {
    39  	DetName       string
    40  	DetVersion    int
    41  	ReqExtractors []string
    42  	Findings      inventory.Finding
    43  	Err           error
    44  }
    45  
    46  // New creates an empty new fake detector.
    47  func New() *fakeDetector {
    48  	return &fakeDetector{}
    49  }
    50  
    51  // Name returns the detector's name.
    52  func (fd *fakeDetector) Name() string { return fd.DetName }
    53  
    54  // Version returns the detector's version.
    55  func (fd *fakeDetector) Version() int { return fd.DetVersion }
    56  
    57  // Requirements returns the detector's requirements.
    58  func (fd *fakeDetector) Requirements() *plugin.Capabilities { return &plugin.Capabilities{} }
    59  
    60  // RequiredExtractors returns a list of Extractors that this Detector requires.
    61  func (fd *fakeDetector) RequiredExtractors() []string { return fd.ReqExtractors }
    62  
    63  // DetectedFinding returns generic vulnerability information about what is detected.
    64  func (fd *fakeDetector) DetectedFinding() inventory.Finding {
    65  	return inventory.Finding{}
    66  }
    67  
    68  // Scan always returns the same predefined finding or error.
    69  func (fd *fakeDetector) Scan(ctx context.Context, scanRoot *scalibrfs.ScanRoot, px *packageindex.PackageIndex) (inventory.Finding, error) {
    70  	return fd.Findings, fd.Err
    71  }
    72  
    73  // WithName sets the fake detector's name.
    74  func (fd *fakeDetector) WithName(name string) *fakeDetector {
    75  	newDet := copier.Copy(fd).(*fakeDetector)
    76  	newDet.DetName = name
    77  	return newDet
    78  }
    79  
    80  // WithVersion sets the fake detector's version.
    81  func (fd *fakeDetector) WithVersion(version int) *fakeDetector {
    82  	newDet := copier.Copy(fd).(*fakeDetector)
    83  	newDet.DetVersion = version
    84  	return newDet
    85  }
    86  
    87  // WithRequiredExtractors sets the fake detector's required extractors.
    88  func (fd *fakeDetector) WithRequiredExtractors(extractors ...string) *fakeDetector {
    89  	newDet := copier.Copy(fd).(*fakeDetector)
    90  	newDet.ReqExtractors = extractors
    91  	return newDet
    92  }
    93  
    94  // WithPackageVuln sets the fake detector's package vulnerability that is returned when Scan() is called.
    95  func (fd *fakeDetector) WithPackageVuln(vuln *inventory.PackageVuln) *fakeDetector {
    96  	newDet := copier.Copy(fd).(*fakeDetector)
    97  	newDet.Findings.PackageVulns = []*inventory.PackageVuln{vuln}
    98  	return newDet
    99  }
   100  
   101  // WithGenericFinding sets the fake detector's generic finding that is returned when Scan() is called.
   102  func (fd *fakeDetector) WithGenericFinding(finding *inventory.GenericFinding) *fakeDetector {
   103  	newDet := copier.Copy(fd).(*fakeDetector)
   104  	newDet.Findings.GenericFindings = []*inventory.GenericFinding{finding}
   105  	return newDet
   106  }
   107  
   108  // WithErr sets the fake detector's error that is returned when Scan() is called.
   109  func (fd *fakeDetector) WithErr(err error) *fakeDetector {
   110  	newDet := copier.Copy(fd).(*fakeDetector)
   111  	newDet.Err = err
   112  	return newDet
   113  }