-
Notifications
You must be signed in to change notification settings - Fork 10
/
fake_extractor.go
113 lines (95 loc) · 3.52 KB
/
fake_extractor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package fakeextractor provides a Extractor implementation to be used in tests.
package fakeextractor
import (
"context"
"errors"
"io/fs"
scalibrextractor "github.com/google/osv-scalibr/extractor"
"github.com/google/osv-scalibr/purl"
)
// NamesErr is a list of Inventory names and an error.
type NamesErr struct {
Names []string
Err error
}
// extractor is an Extractor implementation to be used in tests.
type extractor struct {
name string
version int
requiredFiles map[string]bool
pathToNamesErr map[string]NamesErr
}
// New returns a fake extractor.
//
// The extractor returns FileRequired(path) = true for any path in requiredFiles.
// The extractor returns the inventory and error from pathToNamesErr given the same path to Extract(...).
func New(name string, version int, requiredFiles []string, pathToNamesErr map[string]NamesErr) scalibrextractor.InventoryExtractor {
rfs := map[string]bool{}
for _, path := range requiredFiles {
rfs[path] = true
}
// Maintain non-nil fields to avoid nil pointers on access such as FileRequired(...).
if len(pathToNamesErr) == 0 {
pathToNamesErr = map[string]NamesErr{}
}
return &extractor{
name: name,
version: version,
requiredFiles: rfs,
pathToNamesErr: pathToNamesErr,
}
}
// Name returns the extractor's name.
func (e *extractor) Name() string { return e.name }
// Version returns the extractor's version.
func (e *extractor) Version() int { return e.version }
// FileRequired should return true if the file described by path and mode is
// relevant for the extractor.
//
// FileRequired returns true if the path was in requiredFiles and its value is true during
// construction in New(..., requiredFiles, ...) and false otherwise.
func (e *extractor) FileRequired(path string, mode fs.FileMode) bool {
return e.requiredFiles[path]
}
// Extract extracts inventory data relevant for the extractor from a given file.
//
// Extract returns the inventory list and error associated with input.Path from the pathToInventoryErr map used
// during construction in NewExtractor(..., pathToInventoryErr, ...).
func (e *extractor) Extract(ctx context.Context, input *scalibrextractor.ScanInput) ([]*scalibrextractor.Inventory, error) {
namesErr, ok := e.pathToNamesErr[input.Path]
if !ok {
return nil, errors.New("unrecognized path")
}
invs := []*scalibrextractor.Inventory{}
for _, name := range namesErr.Names {
invs = append(invs, &scalibrextractor.Inventory{
Name: name,
Locations: []string{input.Path},
Extractor: e.Name(),
})
}
return invs, namesErr.Err
}
// ToPURL returns a fake PURL based on the inventory name+version.
func (e *extractor) ToPURL(i *scalibrextractor.Inventory) (*purl.PackageURL, error) {
return &purl.PackageURL{
Type: purl.TypePyPi,
Name: i.Name,
Version: i.Version,
}, nil
}
// ToCPEs always returns an empty array.
func (e *extractor) ToCPEs(i *scalibrextractor.Inventory) ([]string, error) { return []string{}, nil }