github.com/google/osv-scalibr@v0.4.1/extractor/filesystem/list/list_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 list_test
    16  
    17  import (
    18  	"regexp"
    19  	"testing"
    20  
    21  	"github.com/google/go-cmp/cmp"
    22  	"github.com/google/go-cmp/cmp/cmpopts"
    23  	cpb "github.com/google/osv-scalibr/binary/proto/config_go_proto"
    24  	el "github.com/google/osv-scalibr/extractor/filesystem/list"
    25  )
    26  
    27  var (
    28  	reValidName = regexp.MustCompile(`^[a-z0-9/-]+$`)
    29  )
    30  
    31  func TestPluginNamesValid(t *testing.T) {
    32  	for _, initers := range el.All {
    33  		for _, initer := range initers {
    34  			name := initer(&cpb.PluginConfig{}).Name()
    35  			if !reValidName.MatchString(name) {
    36  				t.Errorf("Invalid plugin name %q", name)
    37  			}
    38  		}
    39  	}
    40  }
    41  
    42  func TestExtractorsFromName(t *testing.T) {
    43  	testCases := []struct {
    44  		desc     string
    45  		name     string
    46  		wantExts []string
    47  		wantErr  error
    48  	}{
    49  		{
    50  			desc:     "Find_all_extractors_of_a_type",
    51  			name:     "python",
    52  			wantExts: []string{"python/pdmlock", "python/pipfilelock", "python/poetrylock", "python/pylock", "python/condameta", "python/uvlock", "python/wheelegg", "python/requirements", "python/setup"},
    53  		},
    54  		{
    55  			desc:     "Nonexistent_plugin",
    56  			name:     "nonexistent",
    57  			wantErr:  cmpopts.AnyError,
    58  			wantExts: []string{},
    59  		},
    60  	}
    61  
    62  	for _, tc := range testCases {
    63  		t.Run(tc.desc, func(t *testing.T) {
    64  			got, err := el.ExtractorsFromName(tc.name, &cpb.PluginConfig{})
    65  			if diff := cmp.Diff(tc.wantErr, err, cmpopts.EquateErrors()); diff != "" {
    66  				t.Errorf("el.ExtractorsFromName(%v) error got diff (-want +got):\n%s", tc.name, diff)
    67  			}
    68  			gotNames := []string{}
    69  			for _, e := range got {
    70  				gotNames = append(gotNames, e.Name())
    71  			}
    72  			sort := func(e1, e2 string) bool { return e1 < e2 }
    73  			if diff := cmp.Diff(tc.wantExts, gotNames, cmpopts.SortSlices(sort)); diff != "" {
    74  				t.Errorf("el.ExtractorsFromName(%v): got diff (-want +got):\n%s", tc.name, diff)
    75  			}
    76  		})
    77  	}
    78  }