Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Octaviusss committed Jan 20, 2025
1 parent 7345ddb commit b3f80fa
Showing 1 changed file with 27 additions and 49 deletions.
76 changes: 27 additions & 49 deletions extractor/filesystem/language/wordpress/plugins/plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ package plugins_test
import (
"context"
"io/fs"
"os"
"path/filepath"
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -29,9 +27,9 @@ import (
"github.com/google/osv-scalibr/extractor/filesystem/internal/units"
"github.com/google/osv-scalibr/extractor/filesystem/language/wordpress/plugins"
"github.com/google/osv-scalibr/extractor/filesystem/simplefileapi"
scalibrfs "github.com/google/osv-scalibr/fs"
"github.com/google/osv-scalibr/purl"
"github.com/google/osv-scalibr/stats"
"github.com/google/osv-scalibr/testing/extracttest"
"github.com/google/osv-scalibr/testing/fakefs"
"github.com/google/osv-scalibr/testing/testcollector"
)
Expand Down Expand Up @@ -63,8 +61,8 @@ func TestNew(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := plugins.New(tt.cfg)
if !reflect.DeepEqual(got.Config(), tt.wantCfg) {
t.Errorf("New(%+v).Config(): got %+v, want %+v", tt.cfg, got.Config(), tt.wantCfg)
if diff := cmp.Diff(tt.wantCfg, got.Config()); diff != "" {
t.Errorf("New(%+v).Config(): (-want +got):\n%s", tt.cfg, diff)
}
})
}
Expand Down Expand Up @@ -161,79 +159,59 @@ func TestFileRequired(t *testing.T) {
}

func TestExtract(t *testing.T) {
tests := []struct {
name string
path string
osrelease string
cfg plugins.Config
wantInventory []*extractor.Inventory
wantErr error
wantResultMetric stats.FileExtractedResult
}{
tests := []extracttest.TestTableEntry{
{
name: "valid plugin file",
path: "testdata/valid",
wantInventory: []*extractor.Inventory{
Name: "valid plugin file",
InputConfig: extracttest.ScanInputMockConfig{
Path: "testdata/valid",
},
WantInventory: []*extractor.Inventory{
{
Name: "Akismet Anti-spam: Spam Protection",
Version: "5.3",
Locations: []string{"testdata/valid"},
},
},
wantResultMetric: stats.FileExtractedResultSuccess,
},
{
name: "wordpress plugin file not valid",
path: "testdata/invalid",
wantErr: cmpopts.AnyError,
wantResultMetric: stats.FileExtractedResultErrorUnknown,
Name: "wordpress plugin file not valid",
InputConfig: extracttest.ScanInputMockConfig{
Path: "testdata/invalid",
},
},
{
name: "wordpress plugin file empty",
path: "testdata/empty",
wantErr: cmpopts.AnyError,
wantResultMetric: stats.FileExtractedResultErrorUnknown,
Name: "wordpress plugin file empty",
InputConfig: extracttest.ScanInputMockConfig{
Path: "testdata/empty",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Run(tt.Name, func(t *testing.T) {
collector := testcollector.New()
var e filesystem.Extractor = plugins.New(plugins.Config{
Stats: collector,
MaxFileSizeBytes: 100,
})

d := t.TempDir()

// Opening and Reading the Test File
r, err := os.Open(tt.path)
defer func() {
if err = r.Close(); err != nil {
t.Errorf("Close(): %v", err)
}
}()
if err != nil {
t.Fatal(err)
}
scanInput := extracttest.GenerateScanInputMock(t, tt.InputConfig)
defer extracttest.CloseTestScanInput(t, scanInput)

info, err := os.Stat(tt.path)
if err != nil {
t.Fatalf("Failed to stat test file: %v", err)
}
got, err := e.Extract(context.Background(), &scanInput)

input := &filesystem.ScanInput{
FS: scalibrfs.DirFS(d), Path: tt.path, Reader: r, Root: d, Info: info,
if diff := cmp.Diff(tt.WantErr, err, cmpopts.EquateErrors()); diff != "" {
t.Errorf("%s.Extract(%q) error diff (-want +got):\n%s", e.Name(), tt.InputConfig.Path, diff)
return
}

got, err := e.Extract(context.Background(), input)

if diff := cmp.Diff(tt.wantInventory, got); diff != "" {
t.Errorf("Inventory mismatch (-want +got):\n%s", diff)
if diff := cmp.Diff(tt.WantInventory, got, cmpopts.SortSlices(extracttest.InventoryCmpLess)); diff != "" {
t.Errorf("%s.Extract(%q) diff (-want +got):\n%s", e.Name(), tt.InputConfig.Path, diff)
}
})
}
}

func TestToPURL(t *testing.T) {
e := plugins.Extractor{}
i := &extractor.Inventory{
Expand Down

0 comments on commit b3f80fa

Please sign in to comment.