github.com/google/osv-scalibr@v0.4.1/testing/testcollector/test_collector_test.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_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/google/osv-scalibr/stats"
    21  	"github.com/google/osv-scalibr/testing/testcollector"
    22  )
    23  
    24  func TestCollector(t *testing.T) {
    25  	tests := []struct {
    26  		name               string
    27  		fileRequiredStats  *stats.FileRequiredStats
    28  		fileExtractedStats *stats.FileExtractedStats
    29  	}{
    30  		{
    31  			name: "file_required_stats",
    32  			fileRequiredStats: &stats.FileRequiredStats{
    33  				Path:          "testdata/required.txt",
    34  				Result:        stats.FileRequiredResultOK,
    35  				FileSizeBytes: 1000,
    36  			},
    37  		},
    38  		{
    39  			name: "file_extracted_stats",
    40  			fileExtractedStats: &stats.FileExtractedStats{
    41  				Path:              "testdata/extracted.txt",
    42  				Result:            stats.FileExtractedResultSuccess,
    43  				FileSizeBytes:     1000,
    44  				UncompressedBytes: 2000,
    45  			},
    46  		},
    47  		{
    48  			name: "both_file_required_and_extracted_stats",
    49  			fileRequiredStats: &stats.FileRequiredStats{
    50  				Path:          "testdata/required.txt",
    51  				Result:        stats.FileRequiredResultSizeLimitExceeded,
    52  				FileSizeBytes: 1000000,
    53  			},
    54  			fileExtractedStats: &stats.FileExtractedStats{
    55  				Path:              "testdata/extracted.txt",
    56  				Result:            stats.FileExtractedResultErrorMemoryLimitExceeded,
    57  				FileSizeBytes:     1000,
    58  				UncompressedBytes: 2000000,
    59  			},
    60  		},
    61  	}
    62  
    63  	for _, tt := range tests {
    64  		t.Run(tt.name, func(t *testing.T) {
    65  			collector := testcollector.New()
    66  			if tt.fileRequiredStats != nil {
    67  				collector.AfterFileRequired("test", tt.fileRequiredStats)
    68  			}
    69  			if tt.fileExtractedStats != nil {
    70  				collector.AfterFileExtracted("test", tt.fileExtractedStats)
    71  			}
    72  
    73  			if tt.fileRequiredStats != nil {
    74  				gotResult := collector.FileRequiredResult(tt.fileRequiredStats.Path)
    75  				if gotResult != tt.fileRequiredStats.Result {
    76  					t.Errorf("FileRequiredResult(%s) = %v, want %v", tt.fileRequiredStats.Path, gotResult, tt.fileRequiredStats.Result)
    77  				}
    78  			}
    79  
    80  			if tt.fileExtractedStats != nil {
    81  				gotResult := collector.FileExtractedResult(tt.fileExtractedStats.Path)
    82  				if gotResult != tt.fileExtractedStats.Result {
    83  					t.Errorf("FileExtractedResult(%s) = %v, want %v", tt.fileExtractedStats.Path, gotResult, tt.fileExtractedStats.Result)
    84  				}
    85  
    86  				gotFileSize := collector.FileExtractedFileSize(tt.fileExtractedStats.Path)
    87  				if gotFileSize != tt.fileExtractedStats.FileSizeBytes {
    88  					t.Errorf("FileExtractedFileSize(%s) = %v, want %v", tt.fileExtractedStats.Path, gotFileSize, tt.fileExtractedStats.FileSizeBytes)
    89  				}
    90  			}
    91  		})
    92  	}
    93  }