github.com/google/osv-scalibr@v0.4.1/artifact/image/require/require_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 require_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/google/osv-scalibr/artifact/image/require"
    21  )
    22  
    23  func TestFileRequired(t *testing.T) {
    24  	tests := []struct {
    25  		name     string
    26  		requirer require.FileRequirer
    27  		path     string
    28  		want     bool
    29  	}{{
    30  		name:     "always require file",
    31  		requirer: &require.FileRequirerAll{},
    32  		path:     "some/file.txt",
    33  		want:     true,
    34  	}, {
    35  		name:     "never require file",
    36  		requirer: &require.FileRequirerNone{},
    37  		path:     "some/file.txt",
    38  		want:     false,
    39  	}, {
    40  		name:     "require specific file",
    41  		requirer: require.NewFileRequirerPaths([]string{"some/file.txt"}),
    42  		path:     "some/file.txt",
    43  		want:     true,
    44  	}, {
    45  		name:     "require specific file",
    46  		requirer: require.NewFileRequirerPaths([]string{"some/file.txt"}),
    47  		path:     "another/file.txt",
    48  		want:     false,
    49  	}}
    50  
    51  	for _, tc := range tests {
    52  		t.Run(tc.name, func(t *testing.T) {
    53  			got := tc.requirer.FileRequired(tc.path, nil)
    54  			if got != tc.want {
    55  				t.Errorf("FileRequired(%q, nil) = %v, want: %v", tc.path, got, tc.want)
    56  			}
    57  		})
    58  	}
    59  }