Skip to content

Commit

Permalink
Sync depsjsontest
Browse files Browse the repository at this point in the history
  • Loading branch information
Octaviusss committed Jan 13, 2025
1 parent 0ea17c0 commit 5f30a54
Showing 1 changed file with 50 additions and 33 deletions.
83 changes: 50 additions & 33 deletions extractor/filesystem/language/dotnet/depsjson/depsjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package depsjson_test
import (
"context"
"io/fs"
"os"
"path/filepath"
"testing"

Expand All @@ -27,9 +28,9 @@ import (
"github.com/google/osv-scalibr/extractor/filesystem/internal/units"
"github.com/google/osv-scalibr/extractor/filesystem/language/dotnet/depsjson"
"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 @@ -159,13 +160,19 @@ func TestFileRequired(t *testing.T) {
}

func TestExtract(t *testing.T) {
tests := []extracttest.TestTableEntry{
tests := []struct {
name string
path string
osrelease string
cfg depsjson.Config
wantInventory []*extractor.Inventory
wantErr error
wantResultMetric stats.FileExtractedResult
}{
{
Name: "valid application1.deps.json file",
InputConfig: extracttest.ScanInputMockConfig{
Path: "testdata/valid",
},
WantInventory: []*extractor.Inventory{
name: "valid application1.deps.json file",
path: "testdata/valid",
wantInventory: []*extractor.Inventory{
{
Name: "TestLibrary",
Version: "1.0.0",
Expand Down Expand Up @@ -197,27 +204,24 @@ func TestExtract(t *testing.T) {
Locations: []string{"testdata/valid"},
},
},
wantResultMetric: stats.FileExtractedResultSuccess,
},
{
Name: "application1.deps.json file not valid JSON",
InputConfig: extracttest.ScanInputMockConfig{
Path: "testdata/invalid",
},
WantErr: cmpopts.AnyError,
name: "application1.deps.json file not json",
path: "testdata/invalid",
wantErr: cmpopts.AnyError,
wantResultMetric: stats.FileExtractedResultErrorUnknown,
},
{
Name: "application1.deps.json file empty",
InputConfig: extracttest.ScanInputMockConfig{
Path: "testdata/empty",
},
WantErr: cmpopts.AnyError,
name: "application1.deps.json file empty",
path: "testdata/empty",
wantErr: cmpopts.AnyError,
wantResultMetric: stats.FileExtractedResultErrorUnknown,
},
{
Name: "valid application1.deps.json file with an invalid package",
InputConfig: extracttest.ScanInputMockConfig{
Path: "testdata/nopackagename",
},
WantInventory: []*extractor.Inventory{
name: "valid application1.deps.json file with an invalid package",
path: "testdata/nopackagename",
wantInventory: []*extractor.Inventory{
{
Name: "TestLibrary",
Version: "1.0.0",
Expand All @@ -239,31 +243,44 @@ func TestExtract(t *testing.T) {
Locations: []string{"testdata/nopackagename"},
},
},
wantResultMetric: stats.FileExtractedResultSuccess,
},
}

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 = depsjson.New(depsjson.Config{
Stats: collector,
MaxFileSizeBytes: 100,
})

scanInput := extracttest.GenerateScanInputMock(t, tt.InputConfig)
defer extracttest.CloseTestScanInput(t, scanInput)
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)
}

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

// Compare the expected error with the actual error
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
input := &filesystem.ScanInput{
FS: scalibrfs.DirFS(d), Path: tt.path, Reader: r, Root: d, Info: info,
}

// Compare the expected inventory with the actual inventory
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)
got, err := e.Extract(context.Background(), input)

if diff := cmp.Diff(tt.wantInventory, got, cmpopts.SortSlices(invLess)); diff != "" {
t.Errorf("Inventory mismatch (-want +got):\n%s", diff)
}
})
}
Expand Down

0 comments on commit 5f30a54

Please sign in to comment.