github.com/google/osv-scalibr@v0.4.1/testing/testcollector/test_collector.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 testcollector provides an implementation of stats.Collector that
    16  // stores recorded metrics for verification in tests.
    17  package testcollector
    18  
    19  import "github.com/google/osv-scalibr/stats"
    20  
    21  // Collector implements the stats.Collector interface and simply stores metrics
    22  // by path.
    23  type Collector struct {
    24  	stats.NoopCollector
    25  
    26  	fileRequiredStats  map[string]*stats.FileRequiredStats
    27  	fileExtractedStats map[string]*stats.FileExtractedStats
    28  }
    29  
    30  // New returns a new test Collector with maps initialized.
    31  func New() *Collector {
    32  	return &Collector{
    33  		fileRequiredStats:  make(map[string]*stats.FileRequiredStats),
    34  		fileExtractedStats: make(map[string]*stats.FileExtractedStats),
    35  	}
    36  }
    37  
    38  // AfterFileRequired stores the metrics for calls to `FileRequired`.
    39  func (c *Collector) AfterFileRequired(name string, filestats *stats.FileRequiredStats) {
    40  	c.fileRequiredStats[filestats.Path] = filestats
    41  }
    42  
    43  // AfterFileExtracted stores the metrics for calls to `Extract`.
    44  func (c *Collector) AfterFileExtracted(name string, filestats *stats.FileExtractedStats) {
    45  	c.fileExtractedStats[filestats.Path] = filestats
    46  }
    47  
    48  // FileRequiredResult returns the result metric for a given path, if found.
    49  // Otherwise, returns an empty string.
    50  func (c *Collector) FileRequiredResult(path string) stats.FileRequiredResult {
    51  	if filestats, ok := c.fileRequiredStats[path]; ok {
    52  		return filestats.Result
    53  	}
    54  	return ""
    55  }
    56  
    57  // FileExtractedResult returns the result metric for a given path, if found.
    58  // Otherwise, returns an empty string.
    59  func (c *Collector) FileExtractedResult(path string) stats.FileExtractedResult {
    60  	if filestats, ok := c.fileExtractedStats[path]; ok {
    61  		return filestats.Result
    62  	}
    63  	return ""
    64  }
    65  
    66  // FileExtractedFileSize returns the file size recorded for a given path, if
    67  // found. Otherwise, returns 0.
    68  func (c *Collector) FileExtractedFileSize(path string) int64 {
    69  	if filestats, ok := c.fileExtractedStats[path]; ok {
    70  		return filestats.FileSizeBytes
    71  	}
    72  	return 0
    73  }