diff --git a/extractor/filesystem/language/wordpress/plugins/plugins_test.go b/extractor/filesystem/language/wordpress/plugins/plugins_test.go index ca4a7f99..b84f0dba 100644 --- a/extractor/filesystem/language/wordpress/plugins/plugins_test.go +++ b/extractor/filesystem/language/wordpress/plugins/plugins_test.go @@ -17,9 +17,7 @@ package plugins_test import ( "context" "io/fs" - "os" "path/filepath" - "reflect" "testing" "github.com/google/go-cmp/cmp" @@ -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" ) @@ -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) } }) } @@ -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{